Theme
Theme
1 2
1
CFG: Parsing CFG: Parsing
S → SS | a | b
Parse Tree: Example 2 ab ∈ L( S )
Parse Tree: Example 2 Rightmost
derivation S ⇒ SS ⇒ Sb ⇒ ab
S S S S S
S S S
Derivation S S S S Derivation S S S S
Trees S S Trees
a a b S S b a b
S S S
S ⇒ SS ⇒ aS ⇒ ab
Leftmost S
derivation Rightmost
Derivation
a b S S
in Reverse
7 8
CFG: Parsing
CFG: Parsing
Practical Parsers
Example 3 Consider the CFG grammar G • Language/Grammar designed to enable deterministic (directed
S → A and backtrack-free) searches.
A → T | A +T
T → b | (A) Show that (b)+b ∈ L(
L(G
G)? – Top-down parsers : LL(k) languages
S S • E.g., Pascal, Ada, etc.
S S S S S S • Better error diagnosis and recovery.
A A – Bottom-up parsers : LALR(1), LR(k) languages
A A A A A
A • E.g., C/C++, Java, etc.
A T T
A TA T A T A T • Handles left recursion in the grammar.
T T – Backtracking parsers
T T T
A A • E.g., Prolog interpreter.
A A T T
T ( b )+ ( b )+ b 9 10
+ + ( )+ ( )+
11 12
2
Grammar Ambiguity Grammar Ambiguity
Definition
A string w∈L(G) is derived ambiguously if it has
more than one derivation tree (or equivalently: if it has
more than one leftmost derivation (or rightmost)).
Definition: a string is derived ambiguously A grammar is ambiguous if some strings are derived
in a context-free grammar if it has two or ambiguously.
more different parse trees
Typical example: rule S → 0 | 1 | S+S | S×S
Definition: a grammar is ambiguous if it
S ⇒ S+S ⇒ S×S+S ⇒ 0×S+S ⇒ 0×1+S ⇒ 0×1+1
generates some string ambiguously versus
S ⇒ S×S ⇒ 0×S ⇒ 0×S+S ⇒ 0×1+S ⇒ 0×1+1
13 14
The ambiguity of 0×1+1 is shown by the two Note that the two different derivations:
different parse trees: S ⇒ S+S ⇒ 0+S ⇒ 0+1 S
S
S and
S ⇒ S+S ⇒ S+1 ⇒ 0+1
do not constitute an ambiguous string
0 + 1
S
S × S 0+1 as have the same parse tree:
+ S
0 Ambiguity causes troubles when trying to interpret strings
S 1 S + S like: “She likes men who love women who don't smoke.”
× S
Solutions: Use parentheses, or use precedence rules
1 such as a+(b×c) = a+b×c ? (a+b)×c.
0 1 1 15 16
Grammar Ambiguity
Grammar Ambiguity
Example
<EXPR> ? <EXPR> + <EXPR> Inherently Ambiguous
<EXPR> ? <EXPR> * <EXPR>
<EXPR> ? ( <EXPR> ) n Languagesthat can only be generated by
<EXPR> ? a ambiguous grammars are inherently ambiguous.
ambiguous .
Build a parse tree for a + a * a n Example anbncm} ∪ {anbmcm}.
5.13: L = {{a
<EXPR> <EXPR> L = { a ib j c k | i = j ∨ j = k}
n The way to make a CFG for this L somehow has to
<EXPR> <EXPR> involve the step S ? S1|S2 where S1 produces the
<EXPR> <EXPR> strings anbncm and S2 the strings anbmcm.
n This will be ambiguous on strings an bn cn .
<EXPR> <EXPR> <EXPR> <EXPR>
17 18
a + a * a a + a * a
3
Grammar Ambiguity Grammar Ambiguity
Example Example
E → E + E | E ∗ E | ( E ) | - E | id E → E + E | E ∗ E | ( E ) | - E | id
Find a derivation for the expression: id + id ∗ id Find a derivation for the expression: id + id ∗ id
E E E E E
According to the grammar, both are correct.
E + E E + E E + E E + E
E ∗ E id E ∗ E id E ∗ E
E + E E + E id E ∗ E id
Example
One way to resolve ambiguity is to associate
precedence to the operators.
stm → if expr then stm
Example Grammar:
| if expr then stm
• * has precedence over + else stm
1 + 2 * 3 = 1 + (2 * 3)
1 + 2 * 3 ? (1 + 2)*3 if B1 then if B2 then S1 else S2
Ambiguity:
• Associativity and precedence information is typically vs
used to disambiguate non-fully parenthesized if B1 then if B2 then S1 else S2
expressions containing unary prefix/postfix operators
or binary infix operators.
21 22
Quiz 1 Quiz 2
P → aPb | λ
Yes: consider the string abc C → cC | λ Yes: consider ab
Q → bQc | λ
A → aA | λ 23 24
4
Grammar Ambiguity Simple Grammar
Definition
Quiz
A CFG (V,T,S,P) is a simple grammar
Is the following grammar ambiguous? S → SS | λ (s-grammar) if and only if all its productions are of the form
A ? ax with
A∈V, a∈T, x∈V* and any pair ( A,a) occurs at most once.
27 28
5
Chomsky Normal Form CNF Chomsky Normal Form CNF
Significance of CNF
A CFG is said to be in Chomsky Normal Form if every rule in the
• Length of derivation of a string of length grammar has one of the following forms:
n in CNF = (2n-1)
(Cf. Number of nodes of a strictly binary tree with n-leaves) A → BC (dyadic variable productions)
31 32
6
Chomsky Normal Form CNF Chomsky Normal Form CNF
CFGè CNF: Example 1 CFGè CNF: Example 1
Ensure that start variable doesn't appear Remove all epsilon productions, except
on the right hand side of any rule. from start variable.
S’àS S’àS | ?
Sà? | a | b | aSa | bSb Sà? | a | b | aSa | bSb | aa | bb
37 38
7
CFGè CNF: Example 2 CFGè CNF: Example 2
8
Chomsky Normal Form CNF Chomsky Normal Form CNF
Answer Answer
Answer(4): To make the CFG in Chomsky Answer(5): Finally, we have to ‘chain’ the
normal form, we have to introduce terminal variables in
producing variables for S ? XaA|a
S ? aA|a A ? XaXaA|XaXa
A ? aaA|aa, Xa ? a,
•which gives
•which gives
S ? XaA|a S ? XaA|a
A ? XaXaA|XaXa A ? XaA2 |XaXa
Xa ? a. A2 ? XaA
49 50
Xa ? a.
9
Context Sensitive Grammar Context Sensitive Grammar (CSG)
Example
An even more general form of grammars exists. Find the language generated by the CSG:
In general, a non-context free grammar is one
in which whole mixed variable/terminal S à ? | ASBC
substrings are replaced at a time. For Aàa
example with Σ = {a,b,c} consider: CB à BC
S à ? | ASBC aB à ab aB à ab
Aàa bB à bb bB à bb
CB à BC bC à bc bC à bc
cC à cc cC à cc
For technical reasons, when length of LHS
always ≤ length of RHS, these general
55 56
grammars are called context sensitive.
57 58
10
Applications of CFG
Parsing is where we use the theory of CFGs.
11