Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Limits of R.E
Context-Free Grammars
1 2
1
3/8/2019
2
3/8/2019
3
3/8/2019
4
3/8/2019
Conversion of RE to CFG
The syntax for regular expressions does
not carry over to CFGs.
(a | b)*abb
Cannot use *, +, or parentheses.
A0 aA 0 | bA0 | aA1
R.E-- a*b
• S → Ab A1 bA 2
• A → Aa | ε A2 bA 3
R.E -- a(b|c*) A3 ∈
• S → aX
• X → (b|c*)
– X → b | c*
– X→b|C
17 18
– C → Cc | ε
19 20
5
3/8/2019
21 22
6
3/8/2019
7
3/8/2019
Sentential Forms
For eg. E ∗ (I + E) is a sentential form since
Any string of variables and/or terminals E ⇒ E∗E ⇒ E∗(E) ⇒ E∗(E+E) ⇒ E∗(I +E)
derived from the start symbol is called a This derivation is neither leftmost, nor
sentential form. rightmost
Formally, α is a sentential form iff Example: a ∗ E is a left-sentential form, since
S =>* α. E ⇒ lm E ∗ E ⇒ lm I ∗ E ⇒ lm a ∗ E
If S ⇒ lm α we say that α is a left- Example: E∗(E+E) is a right-sentential form,
sentential form, and if S ⇒ rm α we say Since E ⇒ rm E ∗ E ⇒ rm E ∗ (E) ⇒rm E ∗ (E + E)
that α is a right-sentential form
29 30
8
3/8/2019
9
3/8/2019
37 38
Ambiguous Grammar
E-> E + E / E* E/id
Ambiguity V={E} T= { +,*,id}
Derive id + id *id
Thus, equivalent definitions of
“ambiguous grammar’’ are: 2 approaches Left or Right
1. There is a string in the language that has Left most Right most
two different leftmost derivations. E E+E E E+E
E + E*E
2. There is a string in the language that has id + E
id + E*E E + E* id
two different rightmost derivations. E + id * id
id + id * E
id + id * id id + id * id
39 40
10
3/8/2019
E E*E E E*E
E+E*E E * id Problems for Parsers:-
id + E* E E+ E* id Which one is the correct derivation ??
id + id * E E+ id * id Example
id + id * id id + id * id 2 + 3*4 2 + (3*4) ( first tree)
2 + 3 *4 (2+3) *4
41 42
11
3/8/2019
12
3/8/2019
More issues?
For Precedence Add new operator ↑(raise to power)
New Grammar may be ↑ is right associative (2 ↑ (3 ↑2) )
E-> E+ T How to add ↑ to Grammar
T -> T * F Make Tree grow towards Right Hand Side
Points to note:- Add productions like
- in order to derive * we have to go through + F G↑F/G
-Once we have generated * no way to generate + again ie all Complete Grammar becomes
+ in expressions will be generated first
Problem in Grammar :- What if G does not contain + E-> E+T/T
-Therefore add new productions to generate T from E T ->T*F/F
- E->T and T->F F->G ↑ F / G
G ->id
E-> E+T/T
T ->T*F/F 49 What will be the precedence of +, *, | 50
13
3/8/2019
Ambiguity is a Property of
Example: Unambiguous Grammar
Grammars, not Languages
B -> (RB | ε R -> ) | (RR
For the balanced-parentheses Construct a unique leftmost derivation for
language, here is another CFG, which is a given balanced string of parentheses by
unambiguous. B, the start symbol, scanning the string from left to right.
B -> (RB | ε derives balanced strings. If we need to expand B, then use B -> (RB if
R -> ) | (RR the next symbol is “(” and ε if at the end.
R generates strings that If we need to expand R, use R -> ) if the next
have one more right paren symbol is “)” and (RR if it is “(”.
than left. 55 56
14
3/8/2019
15
3/8/2019
16
3/8/2019
LL(1) Grammars
As an aside, a grammar such B -> (RB | ε
R -> ) | (RR, where you can always figure
out the production to use in a leftmost
derivation by scanning the given string
left-to-right and looking only at the next
one symbol is called LL(1).
“Leftmost derivation, left-to-right scan, one
symbol of lookahead.”
65
17