3) Removing Empty Strings
Ensure only top-level symbol can be nullable
program ::= stmtSeq� stmtSeq ::= stmt | stmt ; stmtSeq� stmt ::= “” | assignment | whileStmt | blockStmt� blockStmt ::= { stmtSeq }� assignment ::= expr = expr� whileStmt ::= while (expr) stmt� expr ::= identifier
How to do it in this example?
3) Removing Empty Strings - Result
program ::= “” | stmtSeq � stmtSeq ::= stmt| stmt ; stmtSeq | � | ; stmtSeq | stmt ; | ;� stmt ::= assignment | whileStmt | blockStmt� blockStmt ::= { stmtSeq } | { }� assignment ::= expr = expr� whileStmt ::= while (expr) stmt� whileStmt ::= while (expr)� expr ::= identifier
3) Removing Empty Strings - Algorithm
3) Removing Empty Strings
4) Eliminating unit productions
X ::=Y
where X,Y are non-terminals
program ::= stmtSeq� stmtSeq ::= stmt � | stmt ; stmtSeq� stmt ::= assignment | whileStmt� assignment ::= expr = expr� whileStmt ::= while (expr) stmt
4) Unit Production Elimination Algorithm
X ::=Y put an edge (X,Y) into graph
X ::= s1 s2 … sn
At the end, remove all unit productions.
4) Eliminate unit productions - Result
program ::= expr = expr | while (expr) stmt � | stmt ; stmtSeq� stmtSeq ::= expr = expr | while (expr) stmt � | stmt ; stmtSeq� stmt ::= expr = expr | while (expr) stmt � assignment ::= expr = expr� whileStmt ::= while (expr) stmt
5) Reducing Arity:�No more than 2 symbols on RHS�
stmt ::= while (expr) stmt
becomes
stmt ::= while stmt1� stmt1 ::= ( stmt2� stmt2 ::= expr stmt3� stmt3 ::= ) stmt
6) A non-terminal for each terminal
stmt ::= while (expr) stmt
becomes
stmt ::= Nwhile stmt1� stmt1 ::= N( stmt2� stmt2 ::= expr stmt3� stmt3 ::= N) stmt� Nwhile ::= while� N( ::= (� N) ::= )
Order of steps in conversion to CNF
Ordering of �Unreachable / Unproductive symbols
S := B C | “”
C := D
D := a
R := r
First Unreachable then Unproductive
S := “”
C := D
D := a
S := B C | “”
C := D
D := a
S := B C | “”
C := D
D := C
R := r
First Unproductive then Unreachable
S := “”
S := “”
C := D
D := a
R := r
Exercises on LL(1) grammars
Exercise 1:
Consider a grammar for expressions where the multiplication sign is optional.
ex ::= ex + ex | ex * ex | ex ex |ID
Exercise 1 – Solution
Exercise 1 – LL(1) parsing table
LL(1) parsing table
| ID | + | * | EOF |
ex | 1 | Error | Error | Error |
Z | Error | 2 | Error | 3 |
S | 4 | Error | Error | Error |
Z2 | 6 | 7 | 5 | 7 |
Exercise 2
Prove that every LL(1) grammar is unambiguous.
Intuition:
There is a unique way to derive / parse a string because, for any given non-terminal, at most one alternative is applicable on scanning an input character.
This means we have unique left most derivations / parse trees for every string
Formal proof is presented in the next slide
Solution to Exercise 2 [Cont.]
Solution to Exercise 2 [Cont.]
Corollary of the proof
Exercise 3
Solution to Exercise 3
Exercise 4
Show that the regular languages can be recognized with LL(1) parsers. Describe a process that, given a regular expression, constructs an LL(1) parser for it.
Solution for Exercise 4
Exercise 5