0% found this document useful (0 votes)
89 views

Syntax Analysis

linguistics
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Syntax Analysis

linguistics
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

Syntax Analysis - Part 1

Syntax Analysis
Outline
Context-Free Grammars (CFGs)
Parsing
Top-Down
Recursive Descent
Table-Driven
Bottom-Up
LR Parsing Algorithm
How to Build LR Tables
Parser Generators
Grammar Issues for Programming Languages

Harry H. Porter, 2005

Syntax Analysis - Part 1

Top-Down Parsing
!LL Grammars - A subclass of all CFGs
!Recursive-Descent Parsers - Programmed by hand
!Non-Recursive Predictive Parsers - Table Driven
!Simple, Easy to Build, Better Error Handling

Bottom-Up Parsing
!LR Grammars - A larger subclass of CFGs
!Complex Parsing Algorithms - Table Driven
!Table Construction Techniques
!Parser Generators use this technique
!Faster Parsing, Larger Set of Grammars
!Complex
!Error Reporting is Tricky

Harry H. Porter, 2005

Syntax Analysis - Part 1

Output of Parser?
Succeed is string is recognized
... and fail if syntax errors
Syntax Errors?
Good, descriptive, helpful message!
Recover and continue parsing!
*

Build a Parse Tree (also called derivation tree)


Build Abstract Syntax Tree (AST)
In memory (with objects, pointers)
Output to a file

Execute Semantic Actions


Build AST
Type Checking
Generate Code
Dont build a tree at all!

Harry H. Porter, 2005

Syntax Analysis - Part 1

Errors in Programs
Lexical
if x<1 thenn y = 5:

Typos
Syntactic
if ((x<1) & (y>5))) ...
{ ... { ...
... }

Semantic
if (x+5) then ...

Type Errors
Undefined IDs, etc.
Logical Errors
if (i<9) then ...

Should be <= not <


Bugs
Compiler cannot detect Logical Errors

Harry H. Porter, 2005

Syntax Analysis - Part 1

Compiler
Always halts
Any checks guaranteed to terminate
Decidable
Other Program Checking Techniques
Debugging
Testing
Correctness Proofs
Partially Decidable
Okay? ! The test terminates.
Not Okay? ! The test may not terminate!
You may need to run some programs to see if they are okay.

Harry H. Porter, 2005

Syntax Analysis - Part 1

Requirements
Detect All Errors (Except Logical!)
Messages should be helpful.
Difficult to produce clear messages!
Example:
Syntax Error

Example:
Line 23: Unmatched Paren
if ((x == 1) then
^

Compiler Should Recover


Keep going to find more errors
Example:
x := (a + 5)) * (b + 7));
This error missed
Were in the middle of a statement
Skip tokens until we see a ;
Error detected here
Resume Parsing
Misses a second error... Oh, well...
Checks most of the source
Harry H. Porter, 2005

Syntax Analysis - Part 1

Difficult to generate clear and accurate error messages.


Example
function foo () {
...
if (...) {
...
} else {
...
...
}
<eof>

Missing } here

Not detected until here

Example
var myVarr: Integer;
...
x := myVar;
Misspelled ID here
...
Detected here as
Undeclared ID
Harry H. Porter, 2005

Syntax Analysis - Part 1

For Mature Languages


Catalog common errors
Statistical studies
Tailor compiler to handle common errors well
Statement terminators versus separators
Terminators: C, Java, PCAT
{A;B;C;}
Separators: Pascal, Smalltalk, Haskell
Pascal Examples
begin
var t: Integer;
t := x;
x := y;
Tend to insert a ; here
y := t
end
if (...) then
x := 1
else
y := 2;
z := 3;

Tend to insert a ; here

function foo (x: Integer; y: Integer)...

Harry H. Porter, 2005

Tend to put a comma here

Syntax Analysis - Part 1

Error-Correcting Compilers
!Issue an error message
!Fix the problem
!Produce an executable
Example
Error on line 23: myVarr undefined.
myVar was used.

Is this a good idea???


Compiler guesses the programmers intent
A shifting notion of what constitutes a correct / legal / valid program
May encourage programmers to get sloppy
Declarations provide redundancy
! Increased reliability

Harry H. Porter, 2005

Syntax Analysis - Part 1

Error Avalanche
One error generates a cascade of messages
Example
x := 5 while ( a == b ) do
^
Expecting ;
^
Expecting ;
^
Expecting ;

The real messages may be buried under the avalanche.


Missing #include or import will also cause an avalanche.
Approaches:
Only print 1 message per token [ or per line of source ]
Only print a particular message once
Error: Variable myVarr is undeclared
All future notices for this ID have been suppressed

Abort the compiler after 50 errors.


Harry H. Porter, 2005

10

Syntax Analysis - Part 1

Error Recovery Approaches: Panic Mode


Discard tokens until we see a synchronizing token.

Example
Skip to next occurrence of
} end ;

Resume by parsing the next statement

!Simple to implement
!Commonly used
!The key...
Good set of synchronizing tokens
Knowing what to do then
!May skip over large sections of source

Harry H. Porter, 2005

11

Syntax Analysis - Part 1

Error Recovery Approaches: Phrase-Level Recovery


Compiler corrects the program
by deleting or inserting tokens
...so it can proceed to parse from where it was.

Example
while (x = 4)

y := a+b; ...

Insert do to fix the statement.

!The key...
Dont get into an infinite loop
...constantly inserting tokens
...and never scanning the actual source

Harry H. Porter, 2005

12

Syntax Analysis - Part 1

Error Recovery Approaches: Error Productions


Augment the CFG with Error Productions
Now the CFG accepts anything!
If error productions are used...
Their actions:
{ print (Error...) }

Used with...
LR (Bottom-up) parsing
!Parser Generators

Error Recovery Approaches: Global Correction


Theoretical Approach
Find the minimum change to the source to yield a valid program
(Insert tokens, delete tokens, swap adjacent tokens)
Impractical algorithms - too time consuming
Harry H. Porter, 2005

13

Syntax Analysis - Part 1

CFG: Context Free Grammars


Example Rule:
Stmt " if Expr then Stmt else Stmt
Terminals
Keywords
else else

Token Classes
ID INTEGER REAL
Punctuation
;

Non-terminals
Any symbol appearing on the lefthand side of any rule
Start Symbol
Usually the non-terminal on the lefthand side of the first rule
Rules (or Productions)
BNF: Backus-Naur Form / Backus-Normal Form
Stmt ::= if Expr then Stmt else Stmt
Harry H. Porter, 2005

14

Syntax Analysis - Part 1

Rule Alternatives
E
E
E
E

"E+E
"(E)
"-E
" ID

E "E+E
"(E)
"-E
" ID

All Notations are Equivalent

E "E+E
|(E)
|-E
| ID
E "E+E

| (E) | -E |

ID

Harry H. Porter, 2005

15

Syntax Analysis - Part 1

Notational Conventions
Terminals
a b c ...
Nonterminals
A B C ...
S
Expr
Grammar Symbols (Terminals or Nonterminals)
X Y Z U V W ...
Strings of Symbols
# $ % ...
Strings of Terminals
x y z u v w ...

A sequence of zero
Or more terminals
And nonterminals

Including &

Examples
A"#B
A rule whose righthand side ends with a nonterminal
A"x#
A rule whose righthand side begins with a string of terminals (call it x)
Harry H. Porter, 2005

16

Syntax Analysis - Part 1

Derivations

1.
2.
3.
4.
5.

E "E+E
"E*E
"(E)
"-E
" ID

A Derivation of (id*id)
E ! (E) ! (E*E) ! (id*E) ! (id*id)
Sentential Forms
A sequence of terminals and nonterminals in a derivation
(id*E)
Harry H. Porter, 2005

17

Syntax Analysis - Part 1

Derives in one step !


If A " $ is a rule, then we can write

#A% ! #$%
Any sentential form containing a nonterminal (call it A)
... such that A matches the nonterminal in some rule.
Derives in zero-or-more steps !*

E !*
If #

(id*id)

!* $ and $ ! %, then # !* %

Derives in one-or-more steps !+

Harry H. Porter, 2005

18

Syntax Analysis - Part 1

Given
G
S

A grammar
The Start Symbol

Define
L(G) The language generated
L(G) = { w | S !+ w}
Equivalence of CFGs
If two CFGs generate the same language, we say they are equivalent.
G1 ' G2 whenever L(G1) = L(G2)
In making a derivation...
Choose which nonterminal to expand
Choose which rule to apply

Harry H. Porter, 2005

19

Syntax Analysis - Part 1

Leftmost Derivations
In a derivation... always expand the leftmost nonterminal.
E
!
E+E
!
(E)+E
!
(E*E)+E
!
(id*E)+E
!
(id*id)+E
!
(id*id)+id

1.
2.
3.
4.
5.

E "E+E
"E*E
"(E)
"-E
" ID

Let !LM denote a step in a leftmost derivation (!LM* means zero-or-more steps )

At each step in a leftmost derivation, we have


wA% !LM w$%
where A " $ is a rule
(Recall that w is a string of terminals.)

Each sentential form in a leftmost derivation is called a left-sentential form.


If S

!LM* #

Harry H. Porter, 2005

then we say # is a left-sentential form.


20

Syntax Analysis - Part 1

Rightmost Derivations
In a derivation... always expand the rightmost nonterminal.
E
1.
!
E+E
2.
!
E+id
3.
!
(E)+id
4.
!
(E*E)+id
5.
!
(E*id)+id
!
(id*id)+id

E "E+E
"E*E
"(E)
"-E
" ID

Let !RM denote a step in a rightmost derivation (!RM* means zero-or-more steps )

At each step in a rightmost derivation, we have


#Aw !RM #$w
where A " $ is a rule
(Recall that w is a string of terminals.)

Each sentential form in a rightmost derivation is called a right-sentential form.


If S

!RM* #

then we say # is a right-sentential form.

Harry H. Porter, 2005

21

Syntax Analysis - Part 1

Bottom-Up Parsing
Bottom-up parsers discover rightmost derivations!
Parser moves from input string back to S.
Follow

S !RM* w

in reverse.

At each step in a rightmost derivation, we have


#Aw !RM #$w
where A " $ is a rule
String of terminals (i.e., the rest of the input,
which we have not yet seen)

Harry H. Porter, 2005

22

Syntax Analysis - Part 1

Parse Trees
Two choices at each step in a derivation...
!Which non-terminal to expand
!Which rule to use in replacing it

The parse tree remembers only this

Leftmost Derivation:
!
!
!
!
!
!

E
E+E
(E)+E
(E*E)+E
(id*E)+E
(id*id)+E
(id*id)+id

1. E " E + E
2.
"E*E
3.
"(E)
4.
"-E
5.
" ID

E
E

id

id

id

Harry H. Porter, 2005

23

Syntax Analysis - Part 1

Parse Trees
Two choices at each step in a derivation...
!Which non-terminal to expand
!Which rule to use in replacing it

The parse tree remembers only this

Rightmost Derivation:
!
!
!
!
!
!

1. E " E + E
2.
"E*E
3.
"(E)
4.
"-E
5.
" ID

id

id
Harry H. Porter, 2005

E
E+E
E+id
(E)+id
(E*E)+id
(E*id)+id
(id*id)+id

id
24

Syntax Analysis - Part 1

Parse Trees
Two choices at each step in a derivation...
!Which non-terminal to expand
!Which rule to use in replacing it

The parse tree remembers only this

Leftmost Derivation:
!
!
!
!
!
!

Rightmost Derivation:

E
E+E
(E)+E
(E*E)+E
(id*E)+E
(id*id)+E
(id*id)+id

1. E " E + E
2.
"E*E
3.
"(E)
4.
"-E
5.
" ID

!
!
!
!
!
!

E
E

id

id

E
E+E
E+id
(E)+id
(E*E)+id
(E*id)+id
(id*id)+id

E
id

Harry H. Porter, 2005

25

Syntax Analysis - Part 1

Given a leftmost derivation, we can build a parse tree.


Given a rightmost derivation, we can build a parse tree.
Lefttmost Derivation of

Rightmost Derivation of

(id*id)+id

(id*id)+id

Same Parse Tree


Every parse tree corresponds to...
A single, unique leftmost derivation
A single, unique rightmost derivation

Ambiguity:
However, one input string may have several parse trees!!!
Therefore:
!Several leftmost derivations
!Several rightmost derivations

Harry H. Porter, 2005

26

Syntax Analysis - Part 1

Ambuiguous Grammars
E

Leftmost Derivation #1
!
!
!
!
!

E
E+E
id+E
id+E*E
id+id*E
id+id*id

id

Input: id+id*id
E

id

id

Leftmost Derivation #2
!
!
!
!
!

1. E " E + E
2.
"E*E
3.
"(E)
4.
"-E
5.
" ID

E
E*E
E+E*E
id+E*E
id+id*E
id+id*id

E
id

id

id

Harry H. Porter, 2005

27

Syntax Analysis - Part 1

Ambiguous Grammar
More than one Parse Tree for some sentence.
The grammar for a programming language may be ambiguous
Need to modify it for parsing.

Also: Grammar may be left recursive.


Need to modify it for parsing.

Harry H. Porter, 2005

28

Syntax Analysis - Part 1

Translating a Regular Expression into a CFG


First build the NFA.
For every state in the NFA...
Make a nonterminal in the grammar
For every edge labeled c from A to B...
Add the rule
A " cB
For every edge labeled & from A to B...
Add the rule
A" B
For every final state B...
Add the rule
B" &
Harry H. Porter, 2005

29

Syntax Analysis - Part 1

Recursive Transition Networks


Regular Expressions ( NFA ( DFA
Context-Free Grammar ( Recursive Transition Networks
Exactly as expressive a CFGs... But clearer for humans!

IfStmt

Expr
Expr

if

...

then

Stmt

Stmt
Expr

elseIf

then

Stmt

...
...

Stmt

else

end

...

Terminal Symbols:
Harry H. Porter, 2005

Nonterminal Symbols:
30

Syntax Analysis - Part 1

The Dangling Else Problem


This grammar is ambiguous!
Stmt " if Expr then Stmt
" if Expr then Stmt else Stmt
" ...Other Stmt Forms...
Example String: if E1 then if E2 then S1 else S2
Interpretation #1: if E1 then (if E2 then S1) else S2
S

if

E1

then
if

S
E2

else
then

S2
S1

Interpretation #2: if E1 then (if E2 then S1 else S2)


S

if

E1

if

E2

then
then

S
S1

else

S2

Harry H. Porter, 2005

31

Syntax Analysis - Part 1

The Dangling Else Problem


Goal: Match else-clause to the closest if without an else-clause already.
Solution:
Stmt

WithElse

"
"
"
"
"

if Expr then Stmt


if Expr then WithElse else Stmt

...Other Stmt Forms...


if Expr then WithElse else WithElse
...Other Stmt Forms...

Any Stmt occurring between then and else must have an else.
i.e., the Stmt must not end with then Stmt.
Interpretation #2: if E1 then (if E2 then S1 else S2)
Stmt
if
if

E1
E2

then

Stmt

then WithElse else WithElse


S1

Harry H. Porter, 2005

S2
32

Syntax Analysis - Part 1

The Dangling Else Problem


Goal: Match else-clause to the closest if without an else-clause already.
Solution:
Stmt

WithElse

"
"
"
"
"

if Expr then Stmt


if Expr then WithElse else Stmt

...Other Stmt Forms...


if Expr then WithElse else WithElse
...Other Stmt Forms...

Any Stmt occurring between then and else must have an else.
i.e., the Stmt must not end with then Stmt.
Interpretation #1: if E1 then (if E2 then S1) else S2
Stmt
if

E1

then

WithElse

else

Stmt

Harry H. Porter, 2005

33

Syntax Analysis - Part 1

The Dangling Else Problem


Goal: Match else-clause to the closest if without an else-clause already.
Solution:
Stmt

WithElse

"
"
"
"
"

if Expr then Stmt


if Expr then WithElse else Stmt

...Other Stmt Forms...


if Expr then WithElse else WithElse
...Other Stmt Forms...

Any Stmt occurring between then and else must have an else.
i.e., the Stmt must not end with then Stmt.
Interpretation #1: if E1 then (if E2 then S1) else S2
Stmt
if
if

E2

Harry H. Porter, 2005

E1

then

WithElse

else

Stmt

then WithElse else WithElse

34

Syntax Analysis - Part 1

The Dangling Else Problem


Goal: Match else-clause to the closest if without an else-clause already.
Solution:
Stmt

WithElse

"
"
"
"
"

if Expr then Stmt


if Expr then WithElse else Stmt

...Other Stmt Forms...


if Expr then WithElse else WithElse
...Other Stmt Forms...

Any Stmt occurring between then and else must have an else.
i.e., the Stmt must not end with then Stmt.
Interpretation #1: if E1 then (if E2 then S1) else S2
Stmt
if
if

E2

E1

then

WithElse

else

Stmt

then WithElse else WithElse

Oops, No Match!
Harry H. Porter, 2005

35

Syntax Analysis - Part 1

Top-Down Parsing
Find a left-most derivation
Find (build) a parse tree
Start building from the root and work down...
As we search for a derivation...
Must make choices:
!Which rule to use
!Where to use it
May run into problems!
Option 1:
Backtracking
Made a bad decision
Back up and try another choice
Option 2:
Always make the right choice.
Never have to backtrack: Predictive Parser
Possible for some grammars (LL Grammars)
May be able to fix some grammars (but not others)
!Eliminate Left Recursion
!Left-Factor the Rules
Harry H. Porter, 2005

36

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

37

Syntax Analysis - Part 1

Backtracking
Input: aabbde

S
A

Harry H. Porter, 2005

1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

38

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

39

Syntax Analysis - Part 1

Backtracking
Input: aabbde

S
A
a

Harry H. Porter, 2005

1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

40

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

41

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

Harry H. Porter, 2005

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

42

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

43

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

Harry H. Porter, 2005

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

44

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Failure Occurs Here!!!

Harry H. Porter, 2005

45

Syntax Analysis - Part 1

Backtracking
Input: aabbde

S
A
a

1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

We need an ability to
back up in the input!!!

Harry H. Porter, 2005

46

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

47

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

Harry H. Porter, 2005

a
b

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

48

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

a
b

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

49

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

Harry H. Porter, 2005

a
b

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

50

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

a
b

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

51

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A
a

a
b

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Failure Occurs Here!!!

Harry H. Porter, 2005

52

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
A

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

53

Syntax Analysis - Part 1

Backtracking
Input: aabbde

Harry H. Porter, 2005

1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

54

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
C

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

55

Syntax Analysis - Part 1

Backtracking
Input: aabbde

S
C
a

Harry H. Porter, 2005

1.
2.
3.
4.
5.
6.
7.

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

56

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
C
a

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

Harry H. Porter, 2005

57

Syntax Analysis - Part 1

Backtracking
Input: aabbde
1.
2.
3.
4.
5.
6.
7.

S
C
a

Harry H. Porter, 2005

S " Aa
" Ce
A " aaB
" aaba
B " bbb
C " aaD
D " bbd

58

Syntax Analysis - Part 1

Predictive Parsing
Will never backtrack!
Requirement:
For every rule:
A " #1 | #2 | #3 | ... | #N
We must be able to choose the correct alternative
by looking only at the next symbol
May peek ahead to the next symbol (token).
Example
A
" aB
" cD
"E
Assuming a,c ) FIRST (E)
Example
Stmt " if Expr ...
" for LValue ...
" while Expr ...
" return Expr ...
" ID ...
Harry H. Porter, 2005

59

Syntax Analysis - Part 1

Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol

Harry H. Porter, 2005

60

Syntax Analysis - Part 1

Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol
LL(k) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next k input symbols

Harry H. Porter, 2005

61

Syntax Analysis - Part 1

Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol
LL(k) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next k input symbols
Techniques to modify the grammar:
!Left Factoring
!Removal of Left Recursion

Harry H. Porter, 2005

62

Syntax Analysis - Part 1

Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol
LL(k) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next k input symbols
Techniques to modify the grammar:
!Left Factoring
!Removal of Left Recursion
But these may not be enough!

Harry H. Porter, 2005

63

Syntax Analysis - Part 1

Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol
LL(k) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next k input symbols
Techniques to modify the grammar:
!Left Factoring
!Removal of Left Recursion
But these may not be enough!
LL(k) Language
Can be described with an LL(k) grammar.

Harry H. Porter, 2005

64

Syntax Analysis - Part 1

Left-Factoring
Problem:
Stmt

" if Expr then Stmt else Stmt


" if Expr then Stmt
" OtherStmt

With predictive parsing, we need to know which rule to use!


(While looking at just the next token)

Harry H. Porter, 2005

65

Syntax Analysis - Part 1

Left-Factoring
Problem:
Stmt

" if Expr then Stmt else Stmt


" if Expr then Stmt
" OtherStmt

With predictive parsing, we need to know which rule to use!


(While looking at just the next token)
Solution:
Stmt
" if Expr then Stmt ElsePart
" OtherStmt
ElsePart " else Stmt | &

Harry H. Porter, 2005

66

Syntax Analysis - Part 1

Left-Factoring
Problem:
Stmt

" if Expr then Stmt else Stmt


" if Expr then Stmt
" OtherStmt

With predictive parsing, we need to know which rule to use!


(While looking at just the next token)
Solution:
Stmt
" if Expr then Stmt ElsePart
" OtherStmt
ElsePart " else Stmt | &
General Approach:
Before: A
" #$1 | #$2 | #$3 | ... | *1 | *2 | *3 | ...
After:

A
C

" #C | *1 | *2 | *3 | ...
" $1 | $2 | $3 | ...

Harry H. Porter, 2005

67

Syntax Analysis - Part 1

Left-Factoring
Problem:
Stmt
A

" if Expr then Stmt else Stmt


#

& $1

$2

" if Expr then Stmt


" OtherStmt
*

1
With predictive parsing,
we need to know which rule to use!
(While looking at just the next token)
Solution:
Stmt
" if Expr then Stmt ElsePart

" OtherStmt
ElsePart " else Stmt | &
General Approach:
Before: A
" #$1 | #$2 | #$3 | ... | *1 | *2 | *3 | ...
After:
Harry H. Porter, 2005

A
C

" #C | *1 | *2 | *3 | ...
" $1 | $2 | $3 | ...
68

Syntax Analysis - Part 1

Left-Factoring
Problem:
Stmt
A

" if Expr then Stmt else Stmt


#

& $1

$2

" if Expr then Stmt


" OtherStmt
*

1
With predictive parsing,
we need to know which rule to use!
(While looking at just the next token)
Solution:
Stmt
" if Expr then Stmt ElsePart

" OtherStmt
*1

ElsePart " else Stmt | &


C

2
General Approach: 1
Before: A
" #$1 | #$2 | #$3 | ... | *1 | *2 | *3 | ...

After:

A
C

" #C | *1 | *2 | *3 | ...
" $1 | $2 | $3 | ...

Harry H. Porter, 2005

69

Syntax Analysis - Part 1

Left Recursion
Whenever
A !+ A#
Simplest Case: Immediate Left Recursion
Given:
A " A# | $
Transform into:
A " $A'
A' " #A' | &

where A' is a new nonterminal

More General (but still immediate):


A " A#1 | A#2 | A#3 | ... | $1 | $2 | $3 | ...
Transform into:
A " $1A' | $2A' | $3A' | ...
A' " #1A' | #2A' | #3A' | ... | &

Harry H. Porter, 2005

70

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.

Harry H. Porter, 2005

71

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive?

Harry H. Porter, 2005

72

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Harry H. Porter, 2005

73

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.

Harry H. Porter, 2005

74

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.
Look at the rules for A...

Harry H. Porter, 2005

75

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.
Look at the rules for A...
Do any of As rules start with S? Yes.
A " Sd

Harry H. Porter, 2005

76

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.
Look at the rules for A...
Do any of As rules start with S? Yes.
A " Sd
Get rid of the S. Substitute in the righthand sides of S.
A " Afd | bd

Harry H. Porter, 2005

77

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.
Look at the rules for A...
Do any of As rules start with S? Yes.
A " Sd
Get rid of the S. Substitute in the righthand sides of S.
A " Afd | bd
The modified grammar:
S " Af | b
A " Ac | Afd | bd | e

Harry H. Porter, 2005

78

Syntax Analysis - Part 1

Left Recursion in More Than One Step


Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive? Yes, but not immediate left recursion.

S ! Af ! Sdf

Approach:
Look at the rules for S only (ignoring other rules)... No left recursion.
Look at the rules for A...
Do any of As rules start with S? Yes.
A " Sd
Get rid of the S. Substitute in the righthand sides of S.
A " Afd | bd
The modified grammar:
S " Af | b
A " Ac | Afd | bd | e
Now eliminate immediate left recursion involving A.
S " Af | b
A " bdA' | eA'
A' " cA' | fdA' | &
Harry H. Porter, 2005

79

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | e

Harry H. Porter, 2005

80

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k

Assume there are still more nonterminals;


Look at the next one...

Harry H. Porter, 2005

81

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &

Harry H. Porter, 2005

82

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " Ag | Sh | k

Look at the B rules next;


Does any righthand side
start with S?

Harry H. Porter, 2005

83

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " Ag | Afh | bh | k

Substitute, using the rules for S


Af... | b...

Harry H. Porter, 2005

84

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " Ag | Afh | bh | k

Does any righthand side


start with A?

Harry H. Porter, 2005

85

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " Ag | Afh | bh | k

Do this one first.

Harry H. Porter, 2005

86

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " bdA'g | BeA'g | Afh | bh | k

Substitute, using the rules for A


bdA'... | BeA'...

Harry H. Porter, 2005

87

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " bdA'g | BeA'g | Afh | bh | k

Do this one next.

Harry H. Porter, 2005

88

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " bdA'g | BeA'g | bdA'fh | BeA'fh | bh | k

Substitute, using the rules for A


bdA'... | BeA'...

Harry H. Porter, 2005

89

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " bdA'g | BeA'g | bdA'fh | BeA'fh | bh | k

Harry H. Porter, 2005

Finally, eliminate any immediate


Left recursion involving B

90

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be
B " Ag | Sh | k
So Far:
S " Af | b
A " bdA' | BeA'
A' " cA' | fdA' | &
B " bdA'gB' | bdA'fhB' | bhB' | kB'
B' " eA'gB' | eA'fhB' | &

Finally, eliminate any immediate


Left recursion involving B

Harry H. Porter, 2005

91

Syntax Analysis - Part 1

Left Recursion in More Than One Step


The Original Grammar:
S " Af | b
A " Ac | Sd | Be | C
If there is another nonterminal,
B " Ag | Sh | k
then do it next.
C " BkmA | AS | j
So Far:
S " Af | b
A " bdA' | BeA' | CA'
A' " cA' | fdA' | &
B " bdA'gB' | bdA'fhB' | bhB' | kB' | CA'gB' | CA'fhB'
B' " eA'gB' | eA'fhB' | &

Harry H. Porter, 2005

92

Syntax Analysis - Part 1

Algorithm to Eliminate Left Recursion


Assume the nonterminals are ordered A1, A2, A3,...
(In the example: S, A, B)
for each nonterminal Ai (for i = 1 to N) do
for each nonterminal Aj (for j = 1 to i-1) do
Let Aj " $1 | $2 | $3 | ... | $N be all the rules for Aj
if there is a rule of the form
A i " Aj #
then replace it by
Ai " $1# | $2# | $3# | ... | $N#
endIf
endFor
A1
Eliminate immediate left recursion
A2
among the Ai rules
A3
Inner Loop
endFor
...
Aj
...

A7
Ai

Outer Loop

Harry H. Porter, 2005

93

Syntax Analysis - Part 1

Table-Driven Predictive Parsing Algorithm


Assume that the grammar is LL(1)
i.e., Backtracking will never be needed
Always know which righthand side to choose (with one look-ahead)
!No Left Recursion
! Grammar is Left-Factored.

Example
E "
E' "
T "
T' "
F "
Step 1:
Step 2:

Term ...

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

+Term +Term + ... +Term


Factor ...
* Factor * Factor * ... * Factor

From grammar, construct table.


Use table to parse strings.

Harry H. Porter, 2005

94

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Table-Driven Predictive Parsing Algorithm


(

X
Y
Z
$
Stack of
grammar symbols

* 4 )
Input String

Algorithm

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Output

Pre-Computed Table:

Harry H. Porter, 2005

95

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Table-Driven Predictive Parsing Algorithm


(

X
Y
Z
$
Stack of
grammar symbols

* 4 )
Input String

id
E"TE'

Harry H. Porter, 2005

Output

Input Symbols, plus $


+

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

E'"+TE'

E'
Nonterminals

Algorithm

Pre-Computed Table:
E

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

96

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Table-Driven Predictive Parsing Algorithm


(

X
Y
Z
$
Stack of
grammar symbols

* 4 )
Input String

id
E"TE'

Output

Input Symbols, plus $


+

T"FT'

F"id

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

T'

Blank entries
indicate ERROR

E"TE'
E'"+TE'

E'
Nonterminals

Algorithm

Pre-Computed Table:
E

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

Harry H. Porter, 2005

97

Syntax Analysis - Part 1

Predictive Parsing Algorithm


Set input ptr to first symbol; Place $ after last input symbol
Push $
Push S
repeat
X = stack top
a = current input symbol
if X is a terminal or X = $ then
if X == a then
Pop stack
Advance input ptr
else
Error
endIf
elseIf Table[X,a] contains a rule then
// call it X " Y1 Y2 ... YK
Pop stack
Push YK
Y1
...
Push Y2
Y2
Push Y1
...
Print (X " Y1 Y2 ... YK)
else
// Table[X,a] is blank
YK
X
Syntax Error
A
A
endIf
$
$
until X == $
Harry H. Porter, 2005

98

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:

( id * id )

id
E"TE'

+ id

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

99

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:

( id * id )

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

+ id

Add $ to end of input


Push $
Push E
E

id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

E'"+TE'

E'
T

T'"*FT'
F"(E)

100

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:

( id * id )

E
T

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Add $ to end of input


Push $
Push E

E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

101

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:

( id * id )

E
$

id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

+ id $

E"TE'

T"FT'

T'
F

E'"+TE'

E'
T

Look at Table [ E, ( ]
Use rule E"TE'
Pop E
Push E'
Push T
Print E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

102

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

" T E'
( id * id )

T
E
$

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

Look at Table [ E, ( ]
Use rule E"TE'
Pop E
Push E'
Push T
Print E"TE'

E'"+TE'

E'
T

+ id $

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

103

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

" T E'
( id * id )

Table [ T, ( ] = T"FT'
Pop T
Push T
Push F
Print T"FT'

T
E
$

id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

T'"*FT'
F"(E)

104

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

" T E'
" F T'
( id * id )

Table [ T, ( ] = T"FT'
Pop T
Push T
Push F
Print T"FT'

F
T
E
$

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

105

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

" T E'
" F T'
( id * id )

F
T
E
$

id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

E'"+TE'

E'
T

Table [ F, ( ] = F"(E)
Pop F
Push (
Push E
Push )
Print F"(E)

+ id $

T'"*FT'
F"(E)

106

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F

" T E'
" F T'
" ( E )

( id * id )
(
E
)
T
E
$

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ F, ( ] = F"(E)
Pop F
Push )
Push E
Push (
Print F"(E)

E'"+TE'

E'
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

107

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F

" T E'
" F T'
" ( E )

( id * id )
(
E
)
T
E
$

id
E"TE'

T
F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

108

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F

" T E'
" F T'
" ( E )

( id * id )
E
)
T
E
$

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

109

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F

" T E'
" F T'
" ( E )

( id * id )
E
)
T
E
$

id
E"TE'

T
F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ E, id ] = E"TE'
Pop E
Push E'
Push T
Print E"TE'

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

110

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E

"
"
"
"

T
F
(
T

E'
T'
E )
E'

( id * id )
T
E
)
T
E
$

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ E, id ] = E"TE'
Pop E
Push E'
Push T
Print E"TE'

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

111

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E

"
"
"
"

T
F
(
T

E'
T'
E )
E'

( id * id )
T
E
)
T
E
$

id
E"TE'

T
F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

112

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T

"
"
"
"
"

T
F
(
T
F

E'
T'
E )
E'
T'

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'

E'"+TE'

E'
T

( id * id )

F
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

113

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T

"
"
"
"
"

T
F
(
T
F

E'
T'
E )
E'
T'

id
E"TE'

F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

E'"+TE'

E'
T

( id * id )

F
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

114

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F

"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

E'"+TE'

E'
T

( id * id )

id
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

115

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F

"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id

id
E"TE'

F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'
T

( id * id )

id
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

116

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F

"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id

( id * id )
T
E
)
T
E
$
id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

117

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F

"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id

( id * id )
T
E
)
T
E
$
id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+ id $

Table [ T', * ] = T'"*FT'


Pop T'
Push T'
Push F
Push *
Print T'"*FT'

E'"+TE'

E'
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

118

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'

"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'

*
F
T
E
)
T
E
$
id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ T', * ] = T'"*FT'


Pop T'
Push T'
Push F
Push *
Print T'"*FT'

E'"+TE'

E'
T

( id * id )

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

119

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'

"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'

*
F
T
E
)
T
E
$
id
E"TE'

F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'
T

( id * id )

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

120

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'

"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'
T

( id * id )

F
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

121

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'

"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'

id
E"TE'

F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

E'"+TE'

E'
T

( id * id )

F
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

122

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F

"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id

id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

E'"+TE'

E'
T

( id * id )

id
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

123

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F

"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id

id
E"TE'

F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'
T

( id * id )

id
T
E
)
T
E
$

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

124

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F

"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id

( id * id )
T
E
)
T
E
$
id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Top of Stack matches next input


Pop and Scan

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

125

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F

"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id

( id * id )
T
E
)
T
E
$
id
E"TE'

T
F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ T', ) ] = T'"&


Pop T'
Push <nothing>
Print T'"&

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

126

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'

"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&

( id * id )
E
)
T
E
$
id
E"TE'

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ T', ) ] = T'"&


Pop T'
Push <nothing>
Print T'"&

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

127

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'

"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&

( id * id )
E
)
T
E
$
id
E"TE'

T
F
Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'

+ id $

Table [ E', ) ] = E'"&


Pop E'
Push <nothing>
Print E'"&

E'"+TE'

E'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

128

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'

"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Table [ E', ) ] = E'"&


Pop E'
Push <nothing>
Print E'"&

)
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

129

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'

"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&

( id * id )

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

Top of Stack matches next input


Pop and Scan

)
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

130

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'

"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Top of Stack matches next input


Pop and Scan

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

131

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'

"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&

( id * id )

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

Table [ T', + ] = T'"&


Pop T'
Push <nothing>
Print T'"&

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

132

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'

"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
E

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Table [ T', + ] = T'"&


Pop T'
Push <nothing>
Print T'"&

E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

133

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'

"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
E

( id * id )

E
$
id
E"TE'

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+ id $

Table [ E', + ] = E'"+TE'


Pop E'
Push E'
Push T
Push +
Print E'"+TE'

E'"+TE'

E'
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

134

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
E

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'
T

+ id $

Table [ E', + ] = E'"+TE'


Pop E'
Push E'
Push T
Push +
Print E'"+TE'

+
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

135

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
E

( id * id )

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

Top of Stack matches next input


Pop and Scan

+
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

136

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
E

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Top of Stack matches next input


Pop and Scan

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

137

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
E

( id * id )

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

138

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T

"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
E

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'

T"FT'

T"FT'
T'"&

T'
F

E'"+TE'

E'

+ id $

Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'

F
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

139

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T

"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
E

( id * id )

Harry H. Porter, 2005

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'

T"FT'

T'
F

+
E'"+TE'

E'

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

F
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

140

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F

"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
E'
T

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'
E'"+TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ F, id ] = F"id
Pop F
Push id
Print F"id

id
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

141

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F

"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
E'
T

( id * id )

F
Harry H. Porter, 2005

T"FT'

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'
E'"+TE'

T'

+ id $

Top of Stack matches next input


Pop and Scan

id
T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

142

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F

"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
E'
T

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'
E'"+TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Top of Stack matches next input


Pop and Scan

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

143

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F

"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
E'
T

( id * id )

F
Harry H. Porter, 2005

T"FT'

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'
E'"+TE'

T'

+ id $

Table [ T', $ ] = T'"&


Pop T'
Push <nothing>
Print T'"&

T
E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

144

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
&
E'
T

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'
E'"+TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ T', $ ] = T'"&


Pop T'
Push <nothing>
Print T'"&

E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

145

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
&
E'
T

( id * id )

F
Harry H. Porter, 2005

T"FT'

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'
E'"+TE'

T'

+ id $

Table [ E', $ ] = E'"&


Pop E'
Push <nothing>
Print E'"&

E
$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

146

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
&
E'
&
T

( id * id )

E'"&

E'"&

T'"&

T'"&

E"TE'
E'"+TE'

T"FT'

T"FT'
T'"&

T'
F

+ id $

Table [ E', $ ] = E'"&


Pop E'
Push <nothing>
Print E'"&

$
id
E"TE'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'

F"id

F"(E)

Harry H. Porter, 2005

147

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Example

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id E
&
E'
&
T

( id * id )

Harry H. Porter, 2005

+ id $

Input symbol == $
Top of stack == $
Loop terminates with success
$
id
E"TE'

T"FT'

E'"&

E'"&

T'"&

T'"&

T"FT'
T'"&

F"id

E"TE'
E'"+TE'

T'
F

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'"*FT'
F"(E)

148

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E

" T E'

E'

Harry H. Porter, 2005

149

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T

" T E'
" F T'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

T
F

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

150

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F

" T E'
" F T'
" ( E )

E'

T
F
(

T'
)

Harry H. Porter, 2005

151

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E

"
"
"
"

T
F
(
T

E'
T'
E )
E'

(
T

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

T
F

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'
)
E'

152

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T

"
"
"
"
"

T
F
(
T
F

E'
T'
E )
E'
T'

E'

T
F
(

T'
)

E'
T'

Harry H. Porter, 2005

153

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F

"
"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T E'
F T'
( E )
T E'
F T'
id

E'

T
F
(

T
F

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'
)
E'

T'

id

Harry H. Porter, 2005

154

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'

"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'

E'

T
F
(

T'
)

E'
T'

F
id

T'

Harry H. Porter, 2005

155

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F

"
"
"
"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T E'
F T'
( E )
T E'
F T'
id
* F T'
id

E'

T
F
(

T'
)
E'

T'

F
id

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

id
Harry H. Porter, 2005

156

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'

"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&

E'

T
F
(

T'
)

E'
T'

F
id

T'

id

&

Harry H. Porter, 2005

157

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'

"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&

F
(

T'
)

id

E'

&

T'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

id

&
158

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'

"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&

E'

T
F
(

T'
)

&
E'

&

T'

F
id

T'

id

&

Harry H. Porter, 2005

159

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'

F
(

T'
)

id

E'

&
E'

&

T'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

id

&
160

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T

"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'

E'

T
F
(

&

T
F

E'
T'

E'

&

T'

F
id

T'

T'

id

&

Harry H. Porter, 2005

161

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F

"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id

F
(

T'
)

&
E'

T
F

E'
T'

id

&

T'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

id

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

id

&
162

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id
&

E'

T
F
(

&
E'

E'

T'

id

&

&

T'

F
id

T'

T'

id

&

Harry H. Porter, 2005

163

Syntax Analysis - Part 1

E "
E' "
T "
T' "
F "

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id
&
&

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

E'

T
F
(

T'
)

id

&
E'

E'

T'

id

&

&

&

T'

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

T'

id

&
164

Syntax Analysis - Part 1

Reconstructing the Parse Tree

Input:
(id*id)+id

Output:
E
T
F
E
T
F
T'
F
T'
E'
T'
E'
T
F
T'
E'

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

T E'
F T'
( E )
T E'
F T'
id
* F T'
id
&
&
&
+ T E'
F T'
id
&
&

Leftmost Derivation:
E
T E'
F T' E'
( E ) T' E'
( T E' ) T' E'
( F T' E' ) T' E'
( id T' E' ) T' E'
( id * F T' E' ) T' E'
( id * id T' E' ) T' E'
( id * id E' ) T' E'
( id * id ) T' E'
( id * id ) E'
( id * id ) + T E'
( id * id ) + F T' E'
( id * id ) + id T' E'
( id * id ) + id E'
( id * id ) + id

Harry H. Porter, 2005

E "
E' "
T "
T' "
F "

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

165

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }

Harry H. Porter, 2005

166

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

FIRST (F) = ?

Harry H. Porter, 2005

167

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

FIRST (F) = { (, id }
FIRST (T') = ?

Harry H. Porter, 2005

168

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

FIRST (F) = { (, id }
FIRST (T') = { *, &}
FIRST (T) = ?

Harry H. Porter, 2005

169

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

FIRST (F)
FIRST (T')
FIRST (T)
FIRST (E')

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

= { (, id }
= { *, &}
= { (, id }
=?

170

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

FIRST (F)
FIRST (T')
FIRST (T)
FIRST (E')
FIRST (E)

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

= { (, id }
= { *, &}
= { (, id }
= { +, &}
=?

Harry H. Porter, 2005

171

Syntax Analysis - Part 1

FIRST Function
Let # be a string of symbols (terminals and nonterminals)
Define:
FIRST (#) = The set of terminals that could occur first
in any string derivable from #
= { a | # !* aw, plus & if # !* & }
Example:

E"
E'"
T"
T'"
F "

FIRST (F)
FIRST (T')
FIRST (T)
FIRST (E')
FIRST (E)

Harry H. Porter, 2005

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

= { (, id }
= { *, &}
= { (, id }
= { +, &}
= { (, id }

172

Syntax Analysis - Part 1

To Compute the FIRST Function


For all symbols X in the grammar...
if X is a terminal then
FIRST(X) = { X }
if X " & is a rule then
add & to FIRST(X)
if X " Y1 Y2 Y3 ... YK is a rule then
if a + FIRST(Y1) then
add a to FIRST(X)
if & + FIRST(Y1) and a + FIRST(Y2) then
add a to FIRST(X)
if & + FIRST(Y1) and & + FIRST(Y2) and a + FIRST(Y3) then
add a to FIRST(X)
...
if & + FIRST(Yi) for all Yi then
add & to FIRST(X)

Repeat until nothing more can be added to any sets.


Harry H. Porter, 2005

173

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result

Harry H. Porter, 2005

174

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result

endIf
Harry H. Porter, 2005

175

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result
if & + FIRST(X2) then
Add everything in FIRST(X3), except &, to result

endIf
endIf
Harry H. Porter, 2005

176

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result
if & + FIRST(X2) then
Add everything in FIRST(X3), except &, to result
if & + FIRST(X3) then
Add everything in FIRST(X4), except &, to result

endIf
endIf
endIf
Harry H. Porter, 2005

177

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result
if & + FIRST(X2) then
Add everything in FIRST(X3), except &, to result
if & + FIRST(X3) then
Add everything in FIRST(X4), except &, to result
...
if & + FIRST(XN-1) then
Add everything in FIRST(XN), except &, to result

endIf
...
endIf
endIf
endIf
Harry H. Porter, 2005

178

Syntax Analysis - Part 1

To Compute the FIRST(X1X2X3...XN)


Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result
if & + FIRST(X2) then
Add everything in FIRST(X3), except &, to result
if & + FIRST(X3) then
Add everything in FIRST(X4), except &, to result
...
if & + FIRST(XN-1) then
Add everything in FIRST(XN), except &, to result
if & + FIRST(XN) then
// Then X1 !* &, X2 !* &, X3 !* &, ... XN !* &
Add & to result
endIf
endIf
...
endIf
endIf
endIf
Harry H. Porter, 2005

179

Syntax Analysis - Part 1

To Compute FOLLOW(Ai) for all Nonterminals in the Grammar


add $ to FOLLOW(S)
repeat
if A"#B$ is a rule then
add every terminal in FIRST($) except & to FOLLOW(B)
if FIRST($) contains & then
add everything in FOLLOW(A) to FOLLOW(B)
endIf
endIf
if A"#B is a rule then
add everything in FOLLOW(A) to FOLLOW(B)
endIf
until We cannot add anything more

Harry H. Porter, 2005

180

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = {
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

?
?
?
?
?

Harry H. Porter, 2005

181

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = {
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Add $ to FOLLOW(S)

182

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $,
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Add $ to FOLLOW(S)

Harry H. Porter, 2005

183

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $,
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
F " ( E ) | id
What can follow E?

184

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
F " ( E ) | id
What can follow E?

Harry H. Porter, 2005

185

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = {
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E " T E'
Whatever can follow E
can also follow E'

186

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E " T E'
Whatever can follow E
can also follow E'

Harry H. Porter, 2005

187

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = {
FOLLOW (T') = {
FOLLOW (F) = {

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E'0 " + T E'1
Whatever is in FIRST(E'1)
can follow T

188

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +,
FOLLOW (T') = {
FOLLOW (F) = {

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E'0 " + T E'1
Whatever is in FIRST(E'1)
can follow T

Harry H. Porter, 2005

189

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +,
FOLLOW (T') = {
FOLLOW (F) = {

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T'0 " * F T'1
Whatever is in FIRST(T'1)
can follow F

190

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +,
FOLLOW (T') = {
FOLLOW (F) = { *,

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T'0 " * F T'1
Whatever is in FIRST(T'1)
can follow F

Harry H. Porter, 2005

191

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +,
FOLLOW (T') = {
FOLLOW (F) = { *,

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E'0 " + T E'1
Since E'1 can go to &
i.e., & + FIRST(E')
Everything in FOLLOW(E'0)
can follow T

192

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +, $, )
FOLLOW (T') = {
FOLLOW (F) = { *,

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
E'0 " + T E'1
Since E'1 can go to &
i.e., & + FIRST(E')
Everything in FOLLOW(E'0)
can follow T

Harry H. Porter, 2005

193

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +, $, )
FOLLOW (T') = {
FOLLOW (F) = { *,

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T " F T'
Whatever can follow T
can also follow T'

194

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +, $, )
FOLLOW (T') = { +, $, )
FOLLOW (F) = { *,

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T " F T'
Whatever can follow T
can also follow T'

Harry H. Porter, 2005

195

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +, $, )
FOLLOW (T') = { +, $, )
FOLLOW (F) = { *,

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T'0 " * F T'1
Since T'1 can go to &
i.e., & + FIRST(T')
Everything in FOLLOW(T'0)
can follow F
196

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, )
FOLLOW (E') = { $, )
FOLLOW (T) = { +, $, )
FOLLOW (T') = { +, $, )
FOLLOW (F) = { *, +, $, )

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Look at rule
T'0 " * F T'1
Since T'1 can go to &
i.e., & + FIRST(T')
Everything in FOLLOW(T'0)
can follow F

Harry H. Porter, 2005

197

Syntax Analysis - Part 1

Example of FOLLOW Computation


Previously computed FIRST sets...
FIRST (F)
= { (, id }
FIRST (T')
= { *, &}
FIRST (T)
= { (, id }
FIRST (E')
= { +, &}
FIRST (E)
= { (, id }
The FOLLOW sets...
FOLLOW (E) = { $, ) }
FOLLOW (E') = { $, ) }
FOLLOW (T) = { +, $, ) }
FOLLOW (T') = { +, $, ) }
FOLLOW (F) = { *, +, $, ) }

Harry H. Porter, 2005

E
E'
T
T'
F

"
"
"
"
"

T E'
+ T E' | &
F T'
* F T' | &
( E ) | id

Nothing more can be added.

198

Syntax Analysis - Part 1

Building the Predictive Parsing Table


The Main Idea:
Assume were looking for an A
i.e., A is on the stack top.
Assume b is the current input symbol.

Harry H. Porter, 2005

199

Syntax Analysis - Part 1

Building the Predictive Parsing Table


The Main Idea:
Assume were looking for an A
i.e., A is on the stack top.
Assume b is the current input symbol.
If A"# is a rule and b is in FIRST(#)
then expand A using the A"# rule!

Harry H. Porter, 2005

200

Syntax Analysis - Part 1

Building the Predictive Parsing Table


The Main Idea:
Assume were looking for an A
i.e., A is on the stack top.
Assume b is the current input symbol.
If A"# is a rule and b is in FIRST(#)
then expand A using the A"# rule!
What if & is in FIRST(#)? [i.e., # !* & ]
If b is in FOLLOW(A)
then expand A using the A"# rule!

Harry H. Porter, 2005

201

Syntax Analysis - Part 1

Building the Predictive Parsing Table


The Main Idea:
Assume were looking for an A
i.e., A is on the stack top.
Assume b is the current input symbol.
If A"# is a rule and b is in FIRST(#)
then expand A using the A"# rule!
What if & is in FIRST(#)? [i.e., # !* & ]
If b is in FOLLOW(A)
then expand A using the A"# rule!
If & is in FIRST(#) and $ is the current input symbol
then if $ is in FOLLOW(A)
then expand A using the A"# rule!

Harry H. Porter, 2005

202

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" if E then S S'


" otherStmt
" else S
"&
" boolExpr

if b then if b then otherStmt else otherStmt

Harry H. Porter, 2005

203

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

ibtibtoeo

Harry H. Porter, 2005

1.
2.
3.
4.
5.

S
S
S'
S'
E

" if E then S S'


" otherStmt
" else S
"&
" boolExpr

, if b then if b then otherStmt else otherStmt

204

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

ibtibtoeo

Harry H. Porter, 2005

205

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

Harry H. Porter, 2005

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

206

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 1: S " i E t S S'


If we are looking for an S
and the next symbol is in FIRST(i E t S S' )...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

S
S'
E
Harry H. Porter, 2005

207

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 1: S " i E t S S'


If we are looking for an S
and the next symbol is in FIRST(i E t S S' )...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

i
S " iEtSS'

S'
E
Harry H. Porter, 2005

208

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 2: S " o


If we are looking for an S
and the next symbol is in FIRST(o)...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

i
S " iEtSS'

S'
E
Harry H. Porter, 2005

209

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 2: S " o


If we are looking for an S
and the next symbol is in FIRST(o)...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

i
S " iEtSS'

S'
E
Harry H. Porter, 2005

210

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 5: E " b


If we are looking for an E
and the next symbol is in FIRST(b)...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

o
S S"o

i
S " iEtSS'

S'
E
Harry H. Porter, 2005

211

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 5: E " b


If we are looking for an E
and the next symbol is in FIRST(b)...
Add that rule to the table

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

i
S " iEtSS'

S'
E
Harry H. Porter, 2005

E"b
212

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

Look at Rule 3: S' " e S


If we are looking for an S'
and the next symbol is in FIRST(e S )...
Add that rule to the table

" i E t S S'
"o
"eS
"&
"b

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

o
S S"o

i
S " iEtSS'

S'
E"b

E
Harry H. Porter, 2005

213

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

Look at Rule 3: S' " e S


If we are looking for an S'
and the next symbol is in FIRST(e S )...
Add that rule to the table

" i E t S S'
"o
"eS
"&
"b

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

Harry H. Porter, 2005

i
S " iEtSS'

S' " e S

S'
E

E"b
214

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 4: S' " &


If we are looking for an S'
and & + FIRST(rhs)...
Then if $ + FOLLOW(S')...
Add that rule under $

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

o
S S"o

i
S " iEtSS'

S' " e S

S'
E"b

E
Harry H. Porter, 2005

215

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 4: S' " &


If we are looking for an S'
and & + FIRST(rhs)...
Then if $ + FOLLOW(S')...
Add that rule under $

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

S' " e S

S'
E
Harry H. Porter, 2005

i
S " iEtSS'

$
S' " &

E"b
216

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 4: S' " &


If we are looking for an S'
and & + FIRST(rhs)...
Then if e + FOLLOW(S')...
Add that rule under e

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

o
S S"o

i
S " iEtSS'

S' " e S

S'

$
S' " &

E"b

E
Harry H. Porter, 2005

217

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

Look at Rule 4: S' " &


If we are looking for an S'
and & + FIRST(rhs)...
Then if e + FOLLOW(S')...
Add that rule under e

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

S' " e S
S' " &

S'
E
Harry H. Porter, 2005

i
S " iEtSS'

$
S' " &

E"b
218

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

CONFLICT!
Two rules in one table entry.

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

o
S S"o

i
S " iEtSS'

S' " e S
S' " &

S'

$
S' " &

E"b

E
Harry H. Porter, 2005

219

Syntax Analysis - Part 1

Example: The Dangling Else Grammar


1.
2.
3.
4.
5.

S
S
S'
S'
E

" i E t S S'
"o
"eS
"&
"b

CONFLICT!
Two rules in one table entry.
The grammar is not LL(1)!

ibtibtoeo
FIRST(S) = { i, o }
FIRST(S') = { e, & }
FIRST(E) = { b }

o
S S"o

FOLLOW(S) = { e, $ }
FOLLOW(S') = { e, $ }
FOLLOW(E) = { t }

S' " e S
S' " &

S'
E
Harry H. Porter, 2005

i
S " iEtSS'

$
S' " &

E"b
220

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank

Harry H. Porter, 2005

221

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets

Harry H. Porter, 2005

222

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets
for each rule A"# do
for each terminal b in FIRST(#) do
add A"# to TABLE[A,b]
endFor

endFor

Harry H. Porter, 2005

223

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets
for each rule A"# do
for each terminal b in FIRST(#) do
add A"# to TABLE[A,b]
endFor
if & is in FIRST(#) then
for each terminal b in FOLLOW(A) do
add A"# to TABLE[A,b]
endFor

endIf
endFor

Harry H. Porter, 2005

224

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets
for each rule A"# do
for each terminal b in FIRST(#) do
add A"# to TABLE[A,b]
endFor
if & is in FIRST(#) then
for each terminal b in FOLLOW(A) do
add A"# to TABLE[A,b]
endFor
if $ is in FOLLOW(A) then
add A"# to TABLE[A,$]
endIf
endIf
endFor

Harry H. Porter, 2005

225

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets
for each rule A"# do
for each terminal b in FIRST(#) do
add A"# to TABLE[A,b]
endFor
if & is in FIRST(#) then
for each terminal b in FOLLOW(A) do
add A"# to TABLE[A,b]
endFor
if $ is in FOLLOW(A) then
add A"# to TABLE[A,$]
endIf
endIf
endFor
TABLE[A,b] is undefined? Then set TABLE[A,b] to error

Harry H. Porter, 2005

226

Syntax Analysis - Part 1

Algorithm to Build the Table


Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or ERROR/Blank
Compute FIRST and FOLLOW sets
for each rule A"# do
for each terminal b in FIRST(#) do
add A"# to TABLE[A,b]
endFor
if & is in FIRST(#) then
for each terminal b in FOLLOW(A) do
add A"# to TABLE[A,b]
endFor
if $ is in FOLLOW(A) then
add A"# to TABLE[A,$]
endIf
endIf
endFor
TABLE[A,b] is undefined? Then set TABLE[A,b] to error
TABLE[A,b] is multiply defined?

The algorithm fails!!! Grammar G is not LL(1)!!!


Harry H. Porter, 2005

227

Syntax Analysis - Part 1

LL(1) Grammars
LL(1) grammars
Are never ambiguous.
! Will never have left recursion.

Using only one symbol of look-ahead


Find Leftmost derivation

Furthermore...

Scanning input left-to-right


If we are looking for an A and the next symbol is b,
Then only one production must be possible.

More Precisely...
If A"# and A"$ are two rules
If # !* a... and $ !* b...
then we require a - b
(i.e., FIRST(#) and FIRST($) must not intersect)
If # !* &
then $ !* & must not be possible.
(i.e., only one alternative can derive &.)
If # !* & and $ !* b...
then b must not be in FOLLOW(A)
Harry H. Porter, 2005

228

Syntax Analysis - Part 1

Error Recovery
We have an error whenever...
!Stacktop is a terminal, but stacktop - input symbol
!Stacktop is a nonterminal but TABLE[A,b] is empty
Options
1. Skip over input symbols, until we can resume parsing
Corresponds to ignoring tokens
2. Pop stack, until we can resume parsing
Corresponds to inserting missing material
3. Some combination of 1 and 2
4. Panic Mode - Use Synchronizing tokens
!Identify a set of synchronizing tokens.
!Skip over tokens until we are positioned on a synchronizing token.
!Pop stack until we can resume parsing.

Harry H. Porter, 2005

229

Syntax Analysis - Part 1

Option 1: Skip Input Symbols


Example:
Decided to use rule
S " IF E THEN S ELSE S END
Stack tells us what we are expecting next in the input.
Weve already gotten IF and E
Assume there are extra tokens in the input.
if (x<5))) then y = 7; ...

A syntax error occurs here.

THEN
S
ELSE
S
END
...
$
Harry H. Porter, 2005

...

<

) TH y

...

We want to skip tokens until


we can resume parsing.
230

Syntax Analysis - Part 1

Option 2: Pop The Stack


Example:
Decided to use rules
S " IF E THEN S ELSE S END
E" (E)
Weve already gotten if ( E
Assume there are missing tokens.
if (x < 5 then y = 7;...

A syntax error occurs here.

)
THEN
S
ELSE
S
END
...
$

...

IF (

<

5 TH y

...

We want to pop the stack until


we can resume parsing.

Harry H. Porter, 2005

231

Syntax Analysis - Part 1

Panic Mode Recovery


The Synchronizing Set of tokens
... is determined by the compiler writer beforehand
Example: { SEMI-COLON, RIGHT-BRACE }
Skip input symbols until we find something in the synchronizing set.
Idea:
Look at the non-terminal on the stack top.
Choose the synchronizing set based on this non-terminal.
Assume A is on the stack top
Let SynchSet = FOLLOW(A)
Skip tokens until we see something in FOLLOW(A)
Pop A from the stack.
Should be able to keep going.
Idea:
Look at the non-terminals in the stack (e.g., A, B, C, ...)
Include FIRST(A), FIRST(B), FIRST(C), ... in the SynchSet.
Skip tokens until we see something in FIRST(A), FIRST(B), FIRST(C), ...
Pop stack until NextToken + FIRST(NonTerminalOnStackTop)
Harry H. Porter, 2005

232

Syntax Analysis - Part 1

Error Recovery - Table Entries


Each blank entry in the table indicates an error.
Tailor the error recovery for each possible error.
Fill the blank entry with an error routine.
The error routine will tell what to do.

Harry H. Porter, 2005

233

Syntax Analysis - Part 1

Error Recovery - Table Entries


Each blank entry in the table indicates an error.
Tailor the error recovery for each possible error.
Fill the blank entry with an error routine.
The error routine will tell what to do.
Example:
id

SEMI

RPAREN
E4

E'

E5

LPAREN

...

...

Harry H. Porter, 2005

234

Syntax Analysis - Part 1

Error Recovery - Table Entries


Each blank entry in the table indicates an error.
Tailor the error recovery for each possible error.
Fill the blank entry with an error routine.
The error routine will tell what to do.
Example:
E

id

SEMI

RPAREN
E4

E'

E5

LPAREN

...
...
E4:

Error-Handling Code
E5:
...
Harry H. Porter, 2005

...

Choose the SynchSet


based on the
particular error
SynchSet = { SEMI, IF, THEN }
SkipTokensTo (SynchSet)
Print (Unexpected right paren)
Pop stack
break
...

235

You might also like