Sie sind auf Seite 1von 106

Top-down parsers

Top-Down Parsing
Start with the root of the parse tree
Root of the tree: node labeled with the start symbol

Algorithm:
Repeat until the fringe of the parse tree matches
input string
At a node A, select a production for A
Add a child node for each symbol on rhs

If a terminal symbol is added that doesnt match,


backtrack
Find the next node to be expanded
(a non-terminal)

Done when:
Leaves of parse tree match input string
All productions exhausted in backtracking
(failure)

(success)

Top-Down Parsing
Consider the grammar:
ScAd
A ab | a
The input string is cad

Top-Down Parsing
Build parse tree:
step 1. From start symbol.
S
c

Top-Down Parsing
Step 2. We expand A using the first
alternative A ab to obtain the
following tree:
S
c

A
a

d
b
5

Top-Down Parsing
Now, we have a match for the second
input symbol a, so we advance the input
pointer to d, the third input symbol, and
compare d against the next leaf b.
Backtracking
Since b does not match d, we report failure and go
back to A to see whether there is another alternative
for A that has not been tried - that might produce a
match!
In going back to A, we must reset the input pointer to
a.
6

Top-Down Parsing
Step 3.
S
c

a
7

Example
Expression grammar

(with precedence)
# Production rule
1 expr expr + term
2
| expr - term
3
| term
4 term term * factor
5
| term / factor
6
| factor
7 factor number
8
| identifier

Input string x 2 * y
8

Example
Rule Sentential form
2
-3
6
8
-

expr + term
expr + term
term
factor + term
<id> + term
<id,x> + term

Current position in
the input stream

Input string
x - 2 *
x - 2 *
x 2 *
x 2 *
x 2 *
x 2 *

y
y
y
y
y
y

expr

expr +

term
fact

Problem:
Cant match next terminal
We guessed wrong at step 2
9

term

Backtracking
Rule Sentential form
2
-3
6
8
?

expr + term
expr + term
term
factor + term
<id> + term
<id,x> + term

Input string
x - 2 *
x - 2 *
x 2 *
x 2 *
x 2 *
x 2 *

y
y
y
y
y
y

Undo all these


productions

Rollback productions
Choose a different production for expr
Continue
10

Retrying
Rule Sentential form
2
-3
6
8
3
7

expr - term
expr - term
term
factor - term
<id> - term
<id,x> - term
<id,x> - factor
<id,x> - <num>

Input string
x - 2 *
x - 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *

expr
y
y
y
y
y
y
y
y

expr -

term

term

fact

fact

Problem:
More input to read
Another cause of backtracking
11

Successful Parse
Rule Sentential form
2
-3
6
8
4
6
7
8

expr - term
expr - term
term
factor - term
<id> - term
<id,x> - term
<id,x> - term * fact
<id,x> - fact * fact
<id,x> - <num> * fact
<id,x> - <num,2> * fact
<id,x> - <num,2> * <id>

Input string
x - 2 *
x - 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 *
x 2 * y

expr
y
y
y
y
y
y
y
y
y
y

expr -

term

term

fact

fact

All terminals match were finished


12

term

fact

Common Prefix
In Fig. 5.12, the common prefix:
if Expr then StmtList (R1,R2)
makes looking ahead to distinguish R1
from R2 hard.
Just use Fig. 5.13 to factor it and
var(R5,6)
The resulting grammar is in Fig. 5.14.
14

Left Recursion
A production is left recursive
if its LHS symbol is the first symbol
of its RHS.
In fig. 5.14, the production
StmtList StmtList ; Stmt
StmtList is left-recursion.
17

Left Recursion (Cont.)

18

Left Recursion (Cont.)


Grammars with left-recursive
productions can never be LL(1).
Some look-ahead symbol t predicts the
application of the left-recursive production
A A.
with recursive-descent parsing, the application
of this production will cause
procedure A to be invoked infinitely.
Thus, we must eliminate left-recursion.
19

Left Recursion (Cont.)


Consider the following left-recursive rules.
1. A A
2.
|
the rules produce strings like
we can change the grammar to:
1. A A'
2. A' A'
3.
|
the rules also produce strings like
The EliminateLeftRecursion algorithm is shown in fig.
5.15.
Applying it to the grammar in fig. 5.14 results in fig.
5.16.

20

Left Recursion (Cont.)


1

2
3
4
5

21

Left Recursion (Cont.)


Now, we trace the algorithm with the grammar below:
(4) StmtList StmtList ; Stmt
(5)
| Stmt
first, the input is (4) StmtList StmtList ; Stmt
because
RHS(4) = StmtList it is left-recursive(marker 1)
create two non-terminals X, and Y for rule (4)
(marker 2)
create StmtList XY
(marker 3)
for rule (5) as StmtList = Stmt (marker 2)
create X Stmt
(marker 4)
finally,
create Y ; Stmt and Y
(marker 5)

Left Recursion (Cont.)

23

Left Recursion (Cont.)


It does not eliminate left recursion involving
derivations of two or more steps.
Eg:
S-->Aa|b
A-->Ac|Sd|
The non terminal S is left recursive because
S==>Aa==>Sda
But it is not immediately left recursive.

Left Recursion (Cont.)

Left Recursion (Cont.)

Substituting S productions in A
A-->Ac|Sd| ==> A-->Ac|Aad|bd|
Then Eliminate immediate left recursion of A
S-->Aa|b
A-->bdA'|A'
A'-->cA'|adA'|
26

Recursive Descent Parsing


A top-down parsing program consists of a
set of procedures, one for each nonterminal.
Execution begins with the procedure for
the start symbol, which halts and
announces success if its procedure body
scans the entire input string.

27

Recursive Descent Parsing


A typical procedure for non-terminal A in a top-down
parser:
boolean A() {
choose an A-production, A X1 X2 Xk;
for (i= 1 to k) {
if (Xi is a non-terminal)
call procedure Xi();
else if (Xi matches the current input token a)
advance the input to the next token;
else /* an error has occurred */;
}
}
28

Recursive Descent Parsing


Given a grammar:
input
expression
expression term rest_expression
term ID | parenthesized_expression
parenthesized_expression ( expression )
rest_expression + expression |

29

Recursive Descent Parsing


For example:
input:

ID + (ID + ID)

30

Recursive Descent Parsing


Build parse tree:
start from start symbol to invoke:
int input (void)
input
expression

Next, invoke expression()


31

input
expression

term
ID

rest_expression
+

expression

term

ID + ( ID+ ID )

rest_expression

parenthesized_expression
(

expression

)
32

Recursive Descent Parsing


# Production rule
1 expr expr + term
2
| expr - term
3
| term
4 term term * factor
5
| term / factor
6
| factor
7 factor number
8
| identifier
9
| (expr)

Creating a top-down parser


(Cont.)
Given the grammar :

E TE
E +TE |-TE|
T FT
T *FT | /FT|
F id|digit|(E)

The input: id + id * id
35

37

Procedure
Input
Grammar without left E()
i+i*i-i/i$
recursion
T()
i+i*i-i/i$
E->TE'
F()
i+i*i-i/i$
E'->+TE'|-TE'|e
T'()
+i*i-i/i$
T->FT'
E'()
+i*i-i/i$
T'->*FT'|/FT'|e
T()
i*i-i/i$
F->(E)|i
F()
i*i-i/i$
T'()
*i-i/i$
Enter Expression
F()
i-i/i$
end with $: i+i*iT'()
-i/i$
i/i$
E'()
-i/i$
T()
i/i$
F()
i/i$
T'()
/i$
F()
i$
T'()
$
E'()
$

Enter Expression end with


$i*i+ii-/i$
Procedure
Input
_____________________
E()
T()
F()
T'()
F()
T'()
E'()
T()
F()
T'()
E'()

i*i+ii-/i$
i*i+ii-/i$
i*i+ii-/i$
*i+ii-/i$
i+ii-/i$
+ii-/i$
+ii-/i$
ii-/i$
ii-/i$
i-/i$
i-/i$

Enter Expression end with $i*(i+i$


Procedure
Input
_____________________
E()
T()
F()
T'()
F()
E()
T()
F()
T'()
E'()
T()
F()
T'()
E'()

i*(i+i$
i*(i+i$
i*(i+i$
*(i+i$
(i+i$
i+i$
i+i$
i+i$
+i$
+i$
i$
i$
$
$ERROR 1: Paranthesis

Mismatch

!!!!Not Accepted!!!!

T'()
$
E'()
$
!!!!Not Accepted!!!!

Predictive Parser
We apply rule X ::= s when
a is the first symbol that can be generated by
string s, OR
s reduces to the empty string (is nullable) and
a is the first symbol in any string that can
follow X

Computing Nullable Sets


Non-terminal X is Nullable only if the
following constraints are satisfied
(computed using iterative analysis)
base case:
if (X := ) then X is Nullable

inductive case:
if (X := ABC...) and A, B, C, ... are all Nullable
then X is Nullable

Computing First Sets

Computing Follow Sets


Follow(X) is computed iteratively

initially, we assume nothing in particular follows X


(Follow (X) is initially { })

base case:
Place $ in FOLLOW(S), where S is the start symbol, and $ is the
input
right endmarker.

inductive case:
If there is a production A ::=B, then everything in FIRST()
except is in FOLLOW(B).
If there is a production A ::= B , or a production A ::=B
,where FIRST() contains , then everything in FOLLOW (A) is
in FOLLOW (B) .

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

nullable
Z
Y

X ::= a
X ::= b Y e

first

follow

building a predictive parser


Y ::= c
Y ::=

Z ::= X Y Z
Z ::= d

nullable
Z

no

yes

base case

X ::= a
X ::= b Y e

first

follow

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

nullable
Z

no

yes

X ::= a
X ::= b Y e

first

follow

after one round of induction, we realize we have reached a fixed point

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

X ::= a
X ::= b Y e

nullable

first

no

yes

c,

base case

follow

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

X ::= a
X ::= b Y e

nullable

first

no

d,a,b

yes

c,

after one round of induction, no fixed point

follow

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

X ::= a
X ::= b Y e

nullable

first

no

d,a,b

yes

c,

follow

after two rounds of induction, no more changes ==> fixed point

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

X ::= a
X ::= b Y e

nullable

first

follow

no

d,a,b

{}

yes

c,

{}

base case

building a predictive parser


Z ::= X Y Z
Z ::= d

Y ::= c
Y ::=

X ::= a
X ::= b Y e

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b
}

Build parsing table

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

a
Z
Y
X

X ::= a
X ::= b Y e

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

Z
Y
X

X ::= a
X ::= b Y e

Z ::= XYZ

Z ::= XYZ

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

Z
Y
X

X ::= a
X ::= b Y e

Z ::= XYZ

Z ::= XYZ

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

d
Z ::= d

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

Z
Y

X ::= a
X ::= b Y e

Z ::= XYZ

Z ::= XYZ

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

d
Z ::= d

Y ::= c

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

X ::= a
X ::= b Y e

Z ::= XYZ

Z ::= XYZ

Y ::=

Y ::=

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

Z ::= d
Y ::= c

Y ::=

Y ::=

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

X ::= a
X ::= b Y e

Z ::= XYZ

Z ::= XYZ

Y ::=

Y ::=

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

Z ::= d
Y ::= c

Y ::=

Y ::=

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

X ::= a
X ::= b Y e

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

What are the blanks?

Z ::= XYZ

Z ::= XYZ

Y ::=

Y ::=

Z ::= d
Y ::= c

Y ::=

Y ::=

Grammar:

Computed Sets:

Z ::= X Y Z Y ::= c
Z ::= d
Y ::=

X ::= a
X ::= b Y e

nullable

first

follow

no

d,a,b

{ $}

yes

c,

{e,d,a,b}

no

a,b

{c,d,a,b}

What are the blanks? --> syntax errors

Z ::= XYZ

Z ::= XYZ

Y ::=

Y ::=

Z ::= d
Y ::= c

Y ::=

Y ::=

LL(1) Grammars
Predictive parsers, that is, recursive-descent
parsers needing no backtracking, can be
constructed for a class of grammars called
LL(1).
The first "L" in LL(1) stands for scanning the
input from left to right, the second "L" for
producing a leftmost derivation, and the "1"
for using one input symbol of lookahead at
each step to make parsing action decisions.
A Grammar whose parsing table has multiply
defined entries is not a LL(1) grammar

LL(1) Grammars

A grammar G is LL(1) if and only if whenever


A::=| are two distinct productions of G,
the following conditions hold:
1. For no terminal a do both and derive
strings beginning with a.
2. At most one of and can derive the
empty string.
3. If , -->* , then does not derive any
string beginning with a terminal in
FOLLOW(A). Likewise, if * , then
does not derive any string beginning with
a terminal in FOLLOW(A).

LL(1) Grammars
To tranform a grammar G to LL(1)
Eliminate all left recursion
Left factoring whenever is possible
Unfortunately, there are some
grammars for which no amount of
alteration will yield an LL(1)
grammar.

Table-driven predictive
parsing.
INPUT: A string w and a parsing table M
for grammar G.
OUTPUT: If w is in L(G), a leftmost
derivation of w; otherwise, an error
indication.
METHOD: Initially, the parser is in a
configuration with w$ in the input
buffer and the start symbol S of G on
top of the stack, above $.

Model of a table-driven
predictive parser

Input String: becd

Match

b
b
be
be

Stack
Z$
X Y Z$

Input
becd$
becd$

Action

bYeYZ$

becd$

X ::= b Y
e

YeYZ$
eYZ$
YZ$
cZ$

ecd$
ecd$
cd$
cd$

Y ::=

Z ::= X Y
Z

Y ::= c

Input String: ac

Match

a
a
ac
ac

Stack
Z$
X Y Z$

Input
ac$
ac$

Action

aYZ$
YZ$
cZ$
Z$
Z$
Error

ac$
c$
c$
$
$

X ::= a

Z ::= X Y
Z

Y ::= c

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

Nullable
E
E
T
T

T ::= &FT
T ::=

first

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

Nullable
E

No

Yes

No

Yes

T ::= &FT
T ::=

first

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

Nullable

first

No

{!,1,0,(}

Yes

No

Yes

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

Nullable

first

No

{!,1,0,(}

Yes

{|, }

No

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

Nullable

first

No

{!,1,0,(}

Yes

{|, }

No

{!,1,0,(}

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

Nullable

first

No

{!,1,0,(}

Yes

{|, }

No

{!,1,0,(}

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

follow

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{}

Yes

{|, }

{}

No

{!,1,0,(}

{}

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{}

No

{!,1,0,(}

{}

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{}

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

building a predictive parser


E ::= TE

E ::= |TE
E ::=

T ::= FT

T ::= &FT
T ::=

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

E
E
T
T
F

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

&

F
F
F
F

::= !F
::= 1
::= 0
::=(E)

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E
T
T
F

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

&

!
E ::=
T
E

F
F
F
F

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

::= !F
::= 1
::= 0
::=(E)

(
E ::= TE

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

|
E
E E ::= |
TE
T
T
F

&

!
E ::=
T
E

F
F
F
F

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

::= !F
::= 1
::= 0
::=(E)

(
E ::= TE

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

|
E
E E ::= |
TE
T
T
F

&

!
E ::=
T
E

F
F
F
F

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

::= !F
::= 1
::= 0
::=(E)

E ::= TE

E ::=

E ::=

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

|
E

&

!
E ::=
T
E

F
F
F
F

T
F

E ::= TE

E E ::= |
TE
T

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

::= !F
::= 1
::= 0
::=(E)

E ::=
T ::= FT

T ::= FT

E ::=
T ::= FT

T ::= FT

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

&

!
E ::=
T
E

F
F
F
F

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

E ::=

E ::= TE

E E ::= |
TE

Nullable

::= !F
::= 1
::= 0
::=(E)

T ::= FT
T ::= &FT

T ::= FT

E ::=
T ::= FT

T ::= FT

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

&

!
E ::=
T
E

F
F
F
F

T T ::= T ::= &FT


F

E ::= TE

E E ::= |
TE
T

Nullable

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

::= !F
::= 1
::= 0
::=(E)

E ::=
T ::= FT

T ::= FT

E ::=
T ::= FT

T ::=

T ::= FT
T ::=

E ::= TE

T ::= FT

E ::= |TE
E ::=

T ::= &FT
T ::=

&

!
E ::=
T
E

F
F
F
F

first

follow

No

{!,1,0,(}

{$,)}

Yes

{|, }

{$,)}

No

{!,1,0,(}

{|,$,)}

Yes

{&, }

{|,$,)}

No

{!,1,0,(} {&, |,$,)}

E ::= TE

E ::= TE

E ::=
T ::= FT

T ::= FT

T T ::= T ::= &FT


F

E ::= TE

E E ::= |
TE
T

Nullable

::= !F
::= 1
::= 0
::=(E)

E ::=
T ::= FT

T ::= FT

T ::=
F ::= !F

F ::=(E)

T ::=
F ::= 1

F ::= 0

input string: 1|(1&0)$


Stack
Input
$E
1|(1&0)$
$E'T
1|(1&0)$
$E'T'F
1|(1&0)$
$E'T'1
1|(1&0)$
$E'T'
|(1&0)$
$E'
|(1&0)$
$E'T|
|(1&0)$
$E'T
(1&0)$
$E'T'F
(1&0)$
$E'T')E(
(1&0)$
$E'T')E
1&0)$
$E'T')E'T
1&0)$
$E'T')E'T'F 1&0)$
$E'T')E'T'1 1&0)$

Output
E Te
T FT'
F1
T'
E' |TE'
T FT'
F (E)
E TE'
T FT'
F1

Stack
Output
$E'T')E'T'
$E'T')E'T'F&
&FT'
$E'T')E'T'F
$E'T')E'T'0
0
$E'T')E'T'
$E'T')E'
T'
$E'T')
E'
$E'T'
$E'
T'

Input
&0)$
&0)$

T'

0)$
0)$
)$
)$
)$
$
$

Read input string:(End with $):1&(!


0&11$
Stack
Input
Output
Stack
Input
$E
1&(!0&11$
Output
$E'T
1&(!0&11$
E TE'
$E'T')E'T'0
0&11$
$E'T'F
1&(!0&11$
T FT'
$E'T'1
1&(!0&11$
F1
$E'T')E'T'
&11$
$E'T'
&(!0&11$
$E'T'F&
&(!0&11$
T' &FT'$E'T')E'T'F& &11$
&FT'
$E'T'F
(!0&11$
$E'T')E'T'F
11$
$E'T')E(
(!0&11$
F (E)
$E'T')E'T'1
11$
$E'T')E
!0&11$
$E'T')E'T
!0&11$
E TE'
$E'T')E'T'
1$
$E'T')E'T'F

!0&11$

$E'T')E'T'F! !0&11$
$E'T')E'T'F
0&11$

T FT'
F !F ERROR 2:Syntax Error
!!!!Not Accepted!!!!

F0

T'

F1

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

nullable
E
E
T
T

T ::= *FT
T ::= /FT
T ::=

first

F ::= id
F ::= digit
F ::=(E)

follow

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

nullable
E

No

Yes

No

Yes

T ::= *FT
T ::= /FT
T ::=

first

F ::= id
F ::= digit
F ::=(E)

follow

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

T ::= *FT
T ::= /FT
T ::=

nullable

first

No

id,digit,(

Yes

+,-,

No

id,digit,(

Yes

*,/,

F ::= id
F ::= digit
F ::=(E)

follow

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

T ::= *FT
T ::= /FT
T ::=

F ::= id
F ::= digit
F ::=(E)

nullable

first

follow

No

id,digit,(

{}

Yes

+,-,

{}

No

id,digit,(

{}

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

T ::= *FT
T ::= /FT
T ::=

F ::= id
F ::= digit
F ::=(E)

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{}

No

id,digit,(

{}

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

T ::= *FT
T ::= /FT
T ::=

F ::= id
F ::= digit
F ::=(E)

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{}

No

id,digit,(

{+,-}

building a predictive parser


E ::= TE

E ::= +TE
E ::= -TE
E ::=

T ::= FT

T ::= *FT
T ::= /FT
T ::=

F ::= id
F ::= digit
F ::=(E)

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

E
E
T
T
F

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E
T
T
F

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

(
E ::=
T
E

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E
T
T
F

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

nullable

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E ::= +TE

E ::= -TE

E ::=
T
E

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

T
F

T ::= FT

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E ::= +TE

E ::= -TE

E ::=
T
E

E
T T ::= FT

nullable

T ::= FT

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

T
F

T ::= FT

first

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E ::= +TE

E ::= -TE

T ::= *FT

T ::= /FT

E ::=
T
E

E
T T ::= FT

nullable

T ::= FT

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

T ::= FT

F ::= id

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E ::= +TE

E ::= -TE

T ::= *FT

T ::= /FT

T ::= FT

T
F ::=
d
i
g
it

first

E ::=
T
E

E
T T ::= FT

nullable

F ::=(E)

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

T ::= FT

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E::=

E ::= +TE

E ::= -TE

F ::= id

E::=
T ::= *FT

F ::=
d
i
g
it

T ::= FT

first

E ::=
T
E

E
T T ::= FT

nullable

F ::=(E)

T ::= /FT

E ::= TE

T ::= FT

F ::= id
E ::= +TE T ::= *FT F ::= digit
E ::= -TE T ::= /FT F ::=(E)
E ::=
T ::=

id

digit
E ::

=
T
E

E ::=
T
E

T ::= FT

F ::= id

follow

No

id,digit,(

{ ),$}

Yes

+,-,

{ ),$}

No

id,digit,(

{+,-,),$}

Yes

*,/,

{+,-,),$}

No

id,digit,(

{*,/,+,-,),$}

E::=

E ::= +TE

E ::= -TE

T::=

T::=

T::=

E::=

T ::= FT

T
F ::=
d
i
g
it

first

E ::=
T
E

E
T T ::= FT

nullable

F ::=(E)

T ::= *FT

T ::= /FT

T::=

Read input string:(End with $):i*(i+)$


Stack
Input
Output
$E
i*(i+)$
$E'T
i*(i+)$
E TE'
$E'T'F
i*(i+)$
T FT'
$E'T'i
i*(i+)$
Fi
$E'T'
*(i+)$
$E'T'F*
*(i+)$
T' *FT'
$E'T'F
(i+)$
$E'T')E(
(i+)$
F (E)
$E'T')E
i+)$
$E'T')E'T
i+)$
E TE'
$E'T')E'T'F
i+)$
T FT'
$E'T')E'T'i
i+)$
Fi
$E'T')E'T'
+)$
$E'T')E'
+)$
T' NULL
$E'T')E'T+
+)$
E' +TE'
$E'T')E'T
)$
ERROR 2:Syntax Error
!!!!Not Accepted!!!

Das könnte Ihnen auch gefallen