Context-Free Grammars Example Grammar: Arithmetic Expressions
Context-Free Grammars Example Grammar: Arithmetic Expressions
Context-Free Grammars Example Grammar: Arithmetic Expressions
Context-free Grammars
Def: A Context-free Grammar (CFG) is a 4-tuple G=(N, !, P, S) where: 1. N is a finite, nonempty set of symbols (non-terminals) 2. ! is a finite set of symbols (terminals) 3. N * ! =+ 4. V, N$ ! (vocabulary) 5. S & N (Goal symbol or start symbol) 6. P is a finite subset of N - V* (Production rules). Sometimes written as G=(V, !, P,S), N = V \ !.
Page 1
Page 2
Derivations of a Grammar
Directly Derives or ': If % and ( are strings in V* (vocabulary), then % directly derives ( (written % ' () iff there is a production A") s.t. A is a symbol in % Substituting string ) for A in % produces the string ( Canonical Derivation Step: The above derivation step is called rightmost if A is the rightmost nonterminal in %. (Similarly, leftmost.) A rightmost derivation step is also called canonical.
Page 3
Page 5
Page 6
Uniqueness of Derivations
Derivations and Parse Trees Every parse tree has a unique derivation: Yes? No? Every parse tree has a unique rightmost derivation: Yes? No? Every parse tree has a unique leftmost derivation: Yes? No? Derivations and Strings of the Language Every u & L(G) has a unique derivation: Yes? No? Every u & L(G) has a unique rightmost derivation: Yes? No? Every u & L(G) has a unique leftmost derivation: Yes? No?
'
F * id
E T T F F
'
id * id
E T T F F
* id
id * id
Page 7
Page 8
Ambiguity
Def. A grammar, G, is said to be unambiguous if / u & L(G), 0 exactly one canonical derivation S '* u. Otherwise, G is said to be ambiguous. E.g., Grammar: E " E + E | E * E | ( E ) | id Two parse trees for u = id + id * id
E E
E E id +
E E E id
E E + id
id * id
Detecting Ambiguity
Caution: There is no mechanical algorithm to decide whether an arbitrary CFG is ambiguous. But one common kind of ambiguity can be detected: If a symbol, A & N is both left-recursive (I.e., A '+ A%, |%| 1 0) and rightrecursive (i.e., A '+ (2, |(| 1 0), then G is ambiguous, provided that G is reduced (i.e., has no redundant symbols).
A A
(
A
%
A
Page 11
Page 12