Sei sulla pagina 1di 83

LL(1) Parser Quiz

Which of the following characteristics are true for an LL(1)


Parser?
They perform backtracking
They perform leftmost derivation
They perform one token look-ahead
Not every context free language can be
parsed by LL(1) parsers
LL(1) Parsing
Push start symbol onto stack

Replace non-terminal symbol on stack using grammar


rules
If top of stack matches input token, both are to be
discarded, mismatch is a syntax error
If, eventually, both stack and input string are empty
then it is a successful parse
LL(1) Parsing
Stack Match!!!

Terminal (token) symbol


...
Non-terminal symbol Input Token

Start symbol
LL(1) Parsing
Stack Mismatch!!!
(Syntax Error)

Terminal (token) symbol


Non-terminal symbol Input Token

Start symbol
LL(1) Parsing
Stack

Input Token Stack: Empty


Stream: Empty
Simple Example
The Grammar The Input
S ( S ) S | e ()$

$
Simple Example
The Grammar The Input
S ( S ) S | e ()$

S
$
Simple Example
The Grammar The Input
S ( S ) S | e ()$

(
S
Replace it with a rule from the grammar:
)
S ( S ) S
S
$
Simple Example
The Grammar The Input
S ( S ) S | e ()$

Match the top of the stack with the next S


input character )
S
$
Simple Example
The Grammar The Input
S ( S ) S | e )$

S
)
S
$
Simple Example
The Grammar The Input
S ( S ) S | e )$

S
Now we use the rule: S e
)
S
$
Simple Example
The Grammar The Input
S ( S ) S | e )$

)
S
$
Simple Example
The Grammar The Input
S ( S ) S | e $

S
$
Simple Example
The Grammar The Input
S ( S ) S | e $

One more application of the rule: S e

$
Simple Example
The Grammar The Input
S ( S ) S | e $

SUCCESS!!!
$
LL(1) Parser
The Grammar The Input
S ( S ) S | e ()$
LL(1) Parser
The Grammar The Input
S ( S ) S | e ()$

The top of the stack may contain


tokens or non-terminals
S
$
LL(1) Parser
The Grammar The Input
S ( S ) S | e ()$

Parsing Table
M[N][T] ( ) $

S S (S) S S e S e S
$
NOTE: LL(1) parser is deterministic: rule for expansion
selected by 1 token lookahead
Parse Table
Parsing Table
M[N][T] ( ) $

S S (S) S S e S e

LL(1) parse table consists of a column for each token


and a row for each non-terminal symbol
Parse Table
Parsing Table
M[N][T] ( ) $

S S (S) S S e S e

Note that the input stream will only contain tokens


Parse Table
Parsing Table
M[N][T] ( ) $

S S (S) S S e S e

The top of the stack may contain tokens or non-terminals


Parse Table
Parsing Table
M[N][T] ( ) $

S S (S) S S e S e

Each entry in the table is the grammar rule to apply in


that situation
Parse Table
A grammar is LL(1) grammar if the associated LL(1)
parsing table has at most one production rule in each
table entry.

LL(1) grammar is a proper subset of context-free


grammar
Table Construction
How to construct the parsing table if grammar is complex?
Example:
exp term exp'
exp' addop term exp' | e
addop + | -
term factor term'
term' mulop factor term' | e
mulop *
factor ( exp ) | num
Grammar Rules

The grammar for LL(1)


The grammar must not
parsing must not be left
be ambiguous
recursive
First Sets
FIRST SET:
X X1X2X3X4...Xn

First set for a symbol


First Sets
FIRST SET:
X X1X2X3X4...Xn
}

LHS
First set for a symbol (on the left hand side of a rule)
First Sets
FIRST SET:
X X1X2X3X4...Xn
}

LHS
RHS
First set for a symbol (on the left hand side of a rule)
is the set of tokens that we find beginning the right
hand side of the rule
Let X be a grammar symbol (a
First Sets terminal or nonterminal) or e. Then
the set First(X) is defined as follows:
X First Set
If X is a terminal or e First(X)={X}.
Let X be a grammar symbol (a
First Sets terminal or nonterminal) or e. Then
the set First(X) is defined as follows:
X First Set
If X is a terminal or e First(X)={X}.
If X is nonterminal, then for First(X) contains First(X1)-
each production rule X {e}.
X1X2...Xn
Let X be a grammar symbol (a
First Sets terminal or nonterminal) or e. Then
the set First(X) is defined as follows:
X First Set
If X is a terminal or e First(X)={X}.
If X is nonterminal, then for First(X) contains First(X1)-
each production rule X {e}.
X1X2...Xn
If for some i<n, First(X) contains First(Xi+1)-
First(X1),...First(Xi) all {e}
contain e
Let X be a grammar symbol (a
First Sets terminal or nonterminal) or e. Then
the set First(X) is defined as follows:
X First Set
If X is a terminal or e First(X)={X}.
If X is nonterminal, then for First(X) contains First(X1)-
each production rule X {e}.
X1X2...Xn
If for some i<n, First(X) contains First(Xi+1)-
First(X1),...First(Xi) all {e}
contain e
If First(X1),...First(Xn) all First(X) contains e
contain e
First(a) for any string a= X1X2...Xn is
First Sets defined using rows 2-4.
X First Set
If X is a terminal or e First(X)={X}.
If X is nonterminal, then for First(X) contains First(X1)-
each production rule X {e}.
X1X2...Xn
If for some i<n, First(X) contains First(Xi+1)-
First(X1),...First(Xi) all {e}
contain e
If First(X1),...First(Xn) all First(X) contains e
contain e
Given the following grammar:
First Set Quiz S
A


ABCDE
a/e
Apply the following rules and find first B b/e
sets for each of the non-terminals : S, C c
A, B, C, D and E. Show rules applied D d/e
to find the sets for each non-terminal. E e/e
1.If X is a terminal or e, then First(X)={X}.
2.If X is nonterminal, then for each production
rule X X1X2...Xn, First(X) contains First(X1)-{e}.
3.If for some i<n, First(X1),...First(Xi) all
contain e, then First(X) contains First(Xi+1)-{e}
4.If First(X1),...First(Xn) all contain e, then
First(X) contains e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE
A a/e
B b/e
C c
D d/e
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e
B b/e
C c
D d/e
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e 2,4 {a,e}
B b/e
C c
D d/e
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e 2,4 {a,e}
B b/e 2,4 {b,e}
C c
D d/e
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e 2,4 {a,e}
B b/e 2,4 {b,e}
C c 2 {c}
D d/e
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e 2,4 {a,e}
B b/e 2,4 {b,e}
C c 2 {c}
D d/e 2,4 {d,e}
E e/e
See instructor notes for
First Set Quiz reference to grammar and rules.
Production Rule Applied First Set
S ABCDE 3 {a,b,c}
A a/e 2,4 {a,e}
B b/e 2,4 {b,e}
C c 2 {c}
D d/e 2,4 {d,e}
E e/e 2,4 {e,e}
First Set Algorithm
for each nonterminals A do First(A):={}
while there are changes to any First(A) do
for each production rule X X1X2...Xn do
k:=1;
while k<=n do
First(A) = First(A) (First(Xk)-{e})
if e is not in First(Xk) then break;
k := k+1;
if ( k>n) then First(A) = First(A) {e}
First Set Algorithm
Real world example:
stmt if-stmt| other
if-stmt if (exp) stmt else-part
else-part else stmt | e
exp 0 | 1
First Set Algorithm
First(stmt)= {other} U First(if-stmt) =
{other, if}
First(if-stmt) = {if}
First(else-part) = {else, e}
First(exp) = {0, 1}
First Set Example Quiz
Symbol First Set
Given the following (
grammar, determine the )
first sets.
+
E T X
X + E *
X int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the )
first sets.
+
E T X
X + E *
X int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+
E T X
X + E *
X int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E *
X int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int int
T int Y Y
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int int
T int Y Y ,*
T ( E ) X
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int int
T int Y Y ,*
T ( E ) X ,+
Y * T
T
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int int
T int Y Y ,*
T ( E ) X ,+
Y * T
T int,(
Y
E
First Set Example Quiz
Symbol First Set
Given the following ( (
grammar, determine the ) )
first sets.
+ +
E T X
X + E * *
X int int
T int Y Y ,*
T ( E ) X ,+
Y * T
T int,(
Y
E int,(
First Set Quiz 2
Construct the First Sets for the following grammar:
E TE First Set for:
E +TE E {a,b}
E
T FT E {,+}
T *FT T {a,b}
T
F a T {,*}
F b {a,b}
F
Follow Sets

Follow set of A is those symbols which will follow


after A and is used to determine if a rule such as
A e should be invoked to remove the A to
expose the tokens that follow A for matching them.
Follow Sets
Given a nonterminal A, the set Follow(A) is defined as:
If A is start symbol, then $ is in Follow(A)
If there is a production rule B A , then
Follow(A) contains First()-{}
If there is a production rule B A and beta is
nulleable, then Follow(A) contains Follow(B)

Notes:
$ is needed to indicate end of string
is never member of Follow set
Follow Sets
Construction:
for each nonterminals A do
Follow(A):={$} for start symbol or {} for others

while there are changes to any Follow(A) do


for each production rule X X1X2...Xn do
for each Xi that is a nonterminal do
Follow(Xi) = Follow(Xi) First(Xi+1...Xn)-{e}
if e is in First(Xi+1...Xn) then
Follow(Xi) = Follow(Xi) Follow(X)
Follow Sets
Example:
stmt if-stmt| other
if-stmt if (exp) stmt else-part
else-part else stmt | e
exp 0 | 1
Follow Sets
Example:
Follow(exp) = {)}
Follow(else-part) = Follow(if-stmt) =
Follow(stmt)
Follow (stmt) = {$} U First(else-
part)-{e} U Follow(if-stmt) = {$,
else}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE
determine the
follow sets: A a/ {a,}
S ABCDE B b/ {b,}
A a/
B b/ C c {c}
C c D d/ {d,}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,}
S ABCDE B b/ {b,}
A a/
B b/ C c {c}
C c D d/ {d,}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,} {b,c}
S ABCDE B b/ {b,}
A a/
B b/ C c {c}
C c D d/ {d,}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,} {b,c}
S ABCDE B b/ {b,} {c}
A a/
B b/ C c {c}
C c D d/ {d,}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,} {b,c}
S ABCDE B b/ {b,} {c}
A a/
B b/ C c {c} {d,e,$}
C c D d/ {d,}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,} {b,c}
S ABCDE B b/ {b,} {c}
A a/
B b/ C c {c} {d,e,$}
C c D d/ {d,} {e,$}
D d/
E e/ E e/ {e,}
Follow Set Quiz
Given the following Rule First Set Follow Set
grammar, {a,b,c}
S ABCDE {$}
determine the
follow sets: A a/ {a,} {b,c}
S ABCDE B b/ {b,} {c}
A a/
B b/ C c {c} {d,e,$}
C c D d/ {d,} {e,$}
D d/
E e/ E e/ {e,} {$}
Follow Set Symbol Follow Sets
Example Quiz (
)
Given the following
grammar, determine +
the follow sets: *
E T X int
X + E Y
X
X
T int Y
T ( E ) T
Y * T E
Y
Follow Set Symbol Follow Sets
Example Quiz ( N/A
) N/A
Given the following
grammar, determine + N/A
the follow sets: * N/A
E T X int N/A
X + E Y
X
X
T int Y
T ( E ) T
Y * T E
Y A detailed solution to this answer is in the recommended
reading for this lesson
Follow Set Symbol Follow Sets
Example Quiz ( N/A
) N/A
Given the following
grammar, determine + N/A
the follow sets: * N/A
E T X int N/A
X + E Y ),$,+
X
X
T int Y
T ( E ) T
Y * T E
Y A detailed solution to this answer is in the recommended
reading for this lesson
Follow Set Symbol Follow Sets
Example Quiz ( N/A
) N/A
Given the following
grammar, determine + N/A
the follow sets: * N/A
E T X int N/A
X + E Y ),$,+
X
X ),$
T int Y
T ( E ) T
Y * T E
Y A detailed solution to this answer is in the recommended
reading for this lesson
Follow Set Symbol Follow Sets
Example Quiz ( N/A
) N/A
Given the following
grammar, determine + N/A
the follow sets: * N/A
E T X int N/A
X + E Y ),$,+
X
X ),$
T int Y
T ( E ) T ),$,+
Y * T E
Y A detailed solution to this answer is in the recommended
reading for this lesson
Follow Set Symbol Follow Sets
Example Quiz ( N/A
) N/A
Given the following
grammar, determine + N/A
the follow sets: * N/A
E T X int N/A
X + E Y ),$,+
X
X ),$
T int Y
T ( E ) T ),$,+
Y * T E ),$
Y A detailed solution to this answer is in the recommended
reading for this lesson
Parsing Tables
Repeat the following two steps for each
nonterminal A and production choice A a

For each token a in First(a), add A


a to the entry M[A,a]

If e is in First(a), for each element a in


Follow(A) (a token or $), add A a to the
entry M[A,a]
Complete Example
exp term exp'
exp' addop term exp' | e
addop + | -
term factor term'
term' mulop factor term' | e
mulop *
factor ( exp ) | number
Complete Example
exp term exp'
exp' addop term exp'
exp' e
addop +
Expanded Grammar: addop -
term factor term'
term' mulop factor term'
term' e
mulop *
factor ( exp )
factor number
Complete Example
First and Follow Sets:
First(exp) = { ( number } Follow(exp) = { $ ) }
First(exp') = { + - e } Follow(exp') = { $ ) }
First(addop) = { + - } Follow(addop) = { ( number }
First(term) = { ( number } Follow(term) = { + - $ ) }
First(term') = { * e } Follow(term') = {+ - $ ) }
First(mulop) = { * } Follow(mulop) = { ( number }
First(factor) { ( number } Follow(factor) = { * + - $ ) }
Complete Example
First and Follow Sets:

Predict ( A alpha) = First (alpha) if first


(alpha) does not contain epsilon
= First (alpha) -{epsilon} U
Follow(A) otherwise
Complete Example
First and Follow Sets:
Some sample predict sets:
Predict (exp term exp) = First ( term exp) =
{ ( number}
Predict (exp epsilon) = First(epsilon) -
{epsilon} U Follow(exp) = {$ )}
Predict (term epsilon) = First(epsilon) -
{epsilon} U Follow(term) = {+ - $ )}
Complete Example
First and Follow Sets:

How to generate parsing table from predict


sets?
If a token t appears in Predict (A alpha)
put rule A alpha in entry M[A] [t]
Complete Example
Parsing Table
M[N][T] ( number ) + - * $
exp exp exp
term term
exp' exp'
exp' exp' exp' exp' exp'
e addop addop e
term term
exp' exp'
addop addop addop

+ -
Complete Example
Parsing Table (continued)
M[N][T] ( number ) + - * $
term term term
factor factor
term' term'
term' term' term' term' term' term'
e e e e
mulop
factor
term'
Complete Example
Parsing Table (continued)
M[N][T] ( number ) + - * $
mulop mulop
*
factor factor factor

( exp ) number

Potrebbero piacerti anche