Context free grammars
-
Upload
ronak-thakkar -
Category
Technology
-
view
1.431 -
download
0
Transcript of Context free grammars
![Page 1: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/1.jpg)
Ronak ThakkarRoll no 32
M.Sc. Computer Science
Context Free Grammars
![Page 2: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/2.jpg)
What are Context Free Grammars?In Formal Language Theory , a Context free
Grammar(CFG) is a formal grammar in which every production rule is of the form
V w Where V is a single nonterminal symbol and w
is a string of terminals and/or nonterminals (w can be empty)
The languages generated by context free grammars are knows as the context free languages
![Page 3: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/3.jpg)
What does CFG do?A CFG provides a simple and mathematically
precise mechanism for describing the methods by which phrases in some natural language are built from smaller blocks, capturing the “block structure” of sentences in a natural way.
Important features of natural language syntax such as agreement and reference is are not the part of context free grammar , but the basic recursive structure of sentences , the way in which clauses nest inside other clauses, and the way in which list of adjectives and adverbs are swallowed by nouns and verbs is described exactly.
![Page 4: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/4.jpg)
Formal Definition of CFGA context-free grammar G is a 4-tuple (V, , R, S), where: V is a finite set; each element v V is called a
non-terminal character or a variable. is a finite set of terminals, disjoint from , which
make up the actual content of the sentence.R is a finite relation from V to (V U )* , where the asterisk
represents the Kleene star operation.
If (,) R, we write production
is called a sentential form
• S, the start symbol, used to represent the whole sentence (or program). It must be an element of V.
![Page 5: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/5.jpg)
Production rule notation
A production rule in R is formalized mathematically as a pair (,) , where is a non-terminal and is a string of variables and nonterminals; rather than using ordered pair notation, production rules are usually written using an arrow operator with as its left hand side and as its right hand side: .
It is allowed for to be the empty string, and in this case it is customary to denote it by ε. The form ε is called an ε-production.
![Page 6: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/6.jpg)
Context-Free Languages•Given a context-free grammar G = (V,,R, S), the language generated or derived from G is the setL(G) = {w :S * w}
A language L is context-free if there is a context-free grammar G = (V,, R, S), such that L is generated from G.
![Page 7: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/7.jpg)
Example :Well-formed parenthesesThe canonical example of a context free
grammar is parenthesis matching, which is representative of the general case. There are two terminal symbols "(" and ")" and one nonterminal symbol S. The production rules are
S → SSS → (S)S → ()The first rule allows Ss to multiply; the second
rule allows Ss to become enclosed by matching parentheses; and the third rule terminates the recursion.
![Page 8: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/8.jpg)
Parse TreeA parse tree of a derivation is a tree in which:
• Each internal node is labeled with a nonterminal
• If a rule A A1A2…An occurs in the derivation then A
is a parent node of nodes labeled A1, A2, …, An
S
a S
a S
S
e
b
![Page 9: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/9.jpg)
Leftmost, Rightmost DerivationsA left-most derivation of a sentential form is one in
which rules transforming the left-most nonterminal are always applied
A right-most derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied
![Page 10: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/10.jpg)
Ambiguous Grammar. A grammar G is ambiguous if there is a word w
L(G) having are least two different parse trees
Notice that a has at least two left-most derivations
S AS BS ABA aAB bBA eB e
![Page 11: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/11.jpg)
Ambiguity & DisambiguationGiven an ambiguous grammar, would like
an equivalent unambiguous grammar.Allows you to know more about structure of a
given derivation.Simplifies inductive proofs on derivations.Can lead to more efficient parsing algorithms.In programming languages, want to impose a
canonical structure on derivations. E.g., for 1+23.
Strategy: Force an ordering on all derivations.
![Page 12: Context free grammars](https://reader035.fdocuments.in/reader035/viewer/2022062405/555ddca5d8b42a192c8b499d/html5/thumbnails/12.jpg)
CFG SimplificationCan’t always eliminate ambiguity.
But, CFG simplification & restriction still useful theoretically & pragmatically.Simpler grammars are easier to understand.Simpler grammars can lead to faster parsing.Restricted forms useful for some parsing
algorithms.Restricted forms can give you more
knowledge about derivations.