SlideShare uma empresa Scribd logo
1 de 118
Baixar para ler offline
1
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Syntax Analysis
2
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
3
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Don’t build a tree at all!
*
y 1
x +
4
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
5
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
6
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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));
We’re in the middle of a statement
Skip tokens until we see a “;”
Resume Parsing
Misses a second error... Oh, well...
Checks most of the source
Error detected here
This error missed
7
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Difficult to generate clear and accurate error messages.
Example
function foo () {
...
if (...) {
...
} else {
...
...
}
<eof>
Example
var myVarr: Integer;
...
x := myVar;
...
Missing } here
Not detected until here
Misspelled ID here
Detected here as
“Undeclared ID”
8
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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;
y := t
end
if (...) then
x := 1
else
y := 2;
z := 3;
function foo (x: Integer; y: Integer)...
Tend to put a comma here
Tend to insert a ; here
Tend to insert a ; here
9
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 programmer’s intent
A shifting notion of what constitutes a correct / legal / valid program
May encourage programmers to get sloppy
Declarations provide redundancy
! Increased reliability
10
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
11
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Error Recovery Approaches: Panic Mode
Discard tokens until we see a “synchronizing” token.
•!Simple to implement
•!Commonly used
•!The key...
Good set of synchronizing tokens
Knowing what to do then
•!May skip over large sections of source
Example
Skip to next occurrence of
} end ;
Resume by parsing the next statement
12
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
•!The key...
Don’t get into an infinite loop
...constantly inserting tokens
...and never scanning the actual source
Example
while (x = 4) y := a+b; ...
Insert do to “fix” the statement.
13
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
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
Error Recovery Approaches: Global Correction
14
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
15
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Rule Alternatives
E " E + E
E " ( E )
E " - E
E " ID
E " E + E
" ( E )
" - E
" ID
E " E + E
| ( E )
| - E
| ID
E " E + E | ( E ) | - E | ID
All Notations are Equivalent
16
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 ...
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”)
A sequence of zero
Or more terminals
And nonterminals
Including &
17
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Derivations
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)
1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
18
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 !* (id*id)
If # !* $ and $ ! %, then # !* %
Derives in one-or-more steps !+
19
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Given
G A grammar
S The Start Symbol
Define
L(G) The language generated
L(G) = { w | S !+ w}
“Equivalence” of CFG’s
If two CFG’s 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
20
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
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* # then we say # is a “left-sentential form.”
1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
21
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Rightmost Derivations
In a derivation... always expand the rightmost nonterminal.
E
! E+E
! E+id
! (E)+id
! (E*E)+id
! (E*id)+id
! (id*id)+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.”
1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
22
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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)
23
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
E
E
EE
EE
id id
id
+
*
)(1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
24
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
E
E
EE
EE
id id
id
+
*
)(
Rightmost Derivation:
E
! E+E
! E+id
! (E)+id
! (E*E)+id
! (E*id)+id
! (id*id)+id
1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
25
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Rightmost Derivation:
E
! E+E
! E+id
! (E)+id
! (E*E)+id
! (E*id)+id
! (id*id)+id
E
E
EE
EE
id id
id
+
*
)(1. E " E + E
2. " E * E
3. " ( E )
4. " - E
5. " ID
26
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Given a leftmost derivation, we can build a parse tree.
Given a rightmost derivation, we can build a 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
Rightmost Derivation of
(id*id)+id
Lefttmost Derivation of
(id*id)+id
Same Parse Tree
27
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Ambuiguous Grammars
Leftmost Derivation #1
E
! E+E
! id+E
! id+E*E
! id+id*E
! id+id*id
Leftmost Derivation #2
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
EE
EE
id id
id
*
+
E
EE
E E
id id
id
+
*
Input: id+id*id
28
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
29
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " &
30
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Recursive Transition Networks
Regular Expressions ( NFA ( DFA
Context-Free Grammar ( Recursive Transition Networks
Exactly as expressive a CFGs... But clearer for humans!
;
elseIf
if then
else
end
then
Expr
Expr
Stmt
Stmt
Stmt
IfStmt
Stmt
Expr
...
...
...
...
Terminal Symbols: Nonterminal Symbols:
31
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Interpretation #2: if E1 then (if E2 then S1 else S2)
if then S2elseE1 S
if thenE2 S1
S
if then S2elseE2
S
if thenE1
S1
S
32
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Goal: “Match else-clause to the closest if without an else-clause already.”
Solution:
Stmt " if Expr then Stmt
" if Expr then WithElse else Stmt
" ...Other Stmt Forms...
WithElse " 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)
if then WithElseelseE2
Stmt
if thenE1
WithElse
Stmt
S1 S2
The Dangling “Else” Problem
33
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Goal: “Match else-clause to the closest if without an else-clause already.”
Solution:
Stmt " if Expr then Stmt
" if Expr then WithElse else Stmt
" ...Other Stmt Forms...
WithElse " 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 thenE1 Stmt
The Dangling “Else” Problem
elseWithElse
34
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Goal: “Match else-clause to the closest if without an else-clause already.”
Solution:
Stmt " if Expr then Stmt
" if Expr then WithElse else Stmt
" ...Other Stmt Forms...
WithElse " 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
if then WithElseelseE2
Stmt
if thenE1
WithElse
Stmt
The Dangling “Else” Problem
elseWithElse
35
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Goal: “Match else-clause to the closest if without an else-clause already.”
Solution:
Stmt " if Expr then Stmt
" if Expr then WithElse else Stmt
" ...Other Stmt Forms...
WithElse " 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
if then WithElseelseE2
Stmt
if thenE1
WithElse
Stmt
The Dangling “Else” Problem
elseWithElse
Oops, No Match!
36
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
37
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
38
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
39
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
Ba a
40
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
Ba a
41
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
Ba a
42
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
B
b
a a
bb
43
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
B
b
a a
bb
44
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
B
b
a a
bb
45
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
B
b
a a
bb
Failure Occurs Here!!!
46
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
Ba a
We need an ability to
back up in the input!!!
47
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
48
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
a a b a
49
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
a a b a
50
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
a a b a
51
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
a a b a
52
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
a a b a
Failure Occurs Here!!!
53
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
A a
54
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
55
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
C e
56
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
C e
a a D
57
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
C e
a
b
a D
b d
58
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Backtracking
Input: aabbde
1. S " Aa
2. " Ce
3. A " aaB
4. " aaba
5. B " bbb
6. C " aaD
7. D " bbd
S
C e
a
b
a D
b d
59
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 ...
60
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Predictive Parsing
LL(1) Grammars
Can do predictive parsing
Can select the right rule
Looking at only the next 1 input symbol
61
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
62
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
63
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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!
64
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
65
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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)
66
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 | &
67
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 | *1 | *2 | *3 | ...
C " $1 | $2 | $3 | ...
68
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 | *1 | *2 | *3 | ...
C " $1 | $2 | $3 | ...
#
#
$1
$2
*1
A
69
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 | *1 | *2 | *3 | ...
C " $1 | $2 | $3 | ...
C
*1
$1
#
#
#
$1
$2
$2
*1
A
A
C
70
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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' | ... | &
71
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Left Recursion in More Than One Step
Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
72
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Left Recursion in More Than One Step
Example:
S " Af | b
A " Ac | Sd | e
Is A left recursive? Yes.
Is S left recursive?
73
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
74
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
75
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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...
76
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 A’s rules start with S? Yes.
A " Sd
77
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 A’s rules start with S? Yes.
A " Sd
Get rid of the S. Substitute in the righthand sides of S.
A " Afd | bd
78
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 A’s 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
79
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 A’s 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' | &
80
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Left Recursion in More Than One Step
The Original Grammar:
S " Af | b
A " Ac | Sd | e
81
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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...
82
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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' | &
83
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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”?
84
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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...
85
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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”?
86
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
87
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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'...
88
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
89
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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'...
90
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Finally, eliminate any immediate
Left recursion involving “B”
91
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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”
92
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Left Recursion in More Than One Step
The Original Grammar:
S " Af | b
A " Ac | Sd | Be | C
B " Ag | Sh | k
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' | &
If there is another nonterminal,
then do it next.
93
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
Ai " Aj#
then replace it by
Ai " $1# | $2# | $3# | ... | $N#
endIf
endFor
Eliminate immediate left recursion
among the Ai rules
endFor
A1
A2
A3
...
Aj
...
A7
Ai
Inner Loop
Outer Loop
94
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
Step 1: From grammar, construct table.
Step 2: Use table to parse strings.
Example
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Term ...
+Term +Term + ... +Term
* Factor * Factor * ... * Factor
Factor ...
95
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Table-Driven Predictive Parsing Algorithm
Input String
( a * 4 ) 3 $
Algorithm Output
+
Y
Z
$
X
Stack of
grammar symbols
Pre-Computed Table:
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
96
Syntax Analysis - Part 1
© Harry H. Porter, 2005
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
Nonterminals
Input Symbols, plus $
Input String
( a * 4 ) 3 $
Algorithm Output
+
Y
Z
$
X
Stack of
grammar symbols
Pre-Computed Table:
Table-Driven Predictive Parsing Algorithm
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
97
Syntax Analysis - Part 1
© Harry H. Porter, 2005
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
Nonterminals
Input Symbols, plus $
Input String
( a * 4 ) 3 $
Algorithm Output
+
Y
Z
$
X
Stack of
grammar symbols
Blank entries
indicate ERRORPre-Computed Table:
Table-Driven Predictive Parsing Algorithm
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
98
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
...
Push Y2
Push Y1
Print (“X " Y1 Y2 ... YK”)
else // Table[X,a] is blank
Syntax Error
endIf
until X == $
X
A
$
YK
A
$
...
Y2
Y1
99
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id+
100
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id+
Add $ to end of input
Push $
Push E
101
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Add $ to end of input
Push $
Push E
E
102
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Look at Table [ E, ‘(’ ]
Use rule E"TE'
Pop E
Push E'
Push T
Print E"TE'
E
103
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T
Look at Table [ E, ‘(’ ]
Use rule E"TE'
Pop E
Push E'
Push T
Print E"TE'
104
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Table [ T, ‘(’ ] = T"FT'
Pop T
Push T’
Push F
Print T"FT'
E’
T
105
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
F Table [ T, ‘(’ ] = T"FT'
Pop T
Push T’
Push F
Print T"FT'
106
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Table [ F, ‘(’ ] = F"(E)
Pop F
Push (
Push E
Push )
Print F"(E)
E’
T’
F
107
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E
(
Table [ F, ‘(’ ] = F"(E)
Pop F
Push )
Push E
Push (
Print F"(E)
108
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E
(
Top of Stack matches next input
Pop and Scan
109
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E Top of Stack matches next input
Pop and Scan
110
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E Table [ E, id ] = E"TE'
Pop E
Push E'
Push T
Print E"TE'
111
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T
Table [ E, id ] = E"TE'
Pop E
Push E'
Push T
Print E"TE'
112
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T
Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'
113
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
F
Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'
114
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
F
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
115
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
id
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
116
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
id
Top of Stack matches next input
Pop and Scan
117
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
Top of Stack matches next input
Pop and Scan
118
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
Table [ T', ‘*’ ] = T'"*FT'
Pop T'
Push T'
Push F
Push ‘*’
Print T'"*FT'
119
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
Table [ T', ‘*’ ] = T'"*FT'
Pop T'
Push T'
Push F
Push ‘*’
Print T'"*FT'
F
*
120
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
F
*
Top of Stack matches next input
Pop and Scan
121
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
F
Top of Stack matches next input
Pop and Scan
122
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
F
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
123
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
id
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
124
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
id
Top of Stack matches next input
Pop and Scan
125
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
Top of Stack matches next input
Pop and Scan
126
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’
T’
Table [ T', ‘)’ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
127
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’ Table [ T', ‘)’ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
128
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
E’ Table [ E', ‘)’ ] = E'"&
Pop E'
Push <nothing>
Print E'"&
129
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
Table [ E', ‘)’ ] = E'"&
Pop E'
Push <nothing>
Print E'"&
130
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
)
Top of Stack matches next input
Pop and Scan
131
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Top of Stack matches next input
Pop and Scan
132
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Table [ T', ‘+’ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
133
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
Table [ T', ‘+’ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
134
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
Table [ E', ‘+’ ] = E'"+TE'
Pop E'
Push E'
Push T
Push ‘+’
Print E'"+TE'
135
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
Table [ E', ‘+’ ] = E'"+TE'
Pop E'
Push E'
Push T
Push ‘+’
Print E'"+TE'
T
+
136
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T
+
Top of Stack matches next input
Pop and Scan
137
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T
Top of Stack matches next input
Pop and Scan
138
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T
Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'
139
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Table [ T, id ] = T"FT'
Pop T
Push T'
Push F
Print T"FT'
F
140
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
F
141
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Table [ F, id ] = F"id
Pop F
Push id
Print F"id
id
142
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
id
Top of Stack matches next input
Pop and Scan
143
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Top of Stack matches next input
Pop and Scan
144
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
T’
Table [ T', $ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
145
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
Table [ T', $ ] = T'"&
Pop T'
Push <nothing>
Print T'"&
146
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
E’
Table [ E', $ ] = E'"&
Pop E'
Push <nothing>
Print E'"&
147
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Table [ E', $ ] = E'"&
Pop E'
Push <nothing>
Print E'"&
148
Syntax Analysis - Part 1
© Harry H. Porter, 2005
ExampleInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T"FT'T"FT'T
T'"&T'"&T'"*FT'T'"&T'
*
F"(E)
E"TE'
(
E'"&
)
F"idF
E'"&
$+id
E'"+TE'E'
E"TE'E
( id * id ) id $+
$
Input symbol == $
Top of stack == $
Loop terminates with success
149
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
E
E'
150
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
E
E'
T'F
151
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
E
E
E'
T'F
)(
152
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'F
)(
153
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
F
F
)(
154
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
F
F
id
)(
155
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
T'
F
F
Fid *
)(
156
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
T'
F
F
Fid
id
*
)(
157
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
T'
F
F
Fid
id
*
)(
&
158
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
T'
F
F
Fid
id
*
)(
&
&
159
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
E
E
E'
E'
T'
T'
T'
F
F
Fid
id
*
)( &
&
&
160
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
T
E
E
E'
E'
E'T'
T'
T'
F
F
Fid
id
+
*
)( &
&
&
161
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
T
E
E
E'
E'
E'T'
T'
T'
T'
F
F
F
F
id
id
+
*
)( &
&
&
162
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
T
E
E
E'
E'
E'T'
T'
T'
T'
F
F
F
F
id
id
id
+
*
)( &
&
&
163
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
T
E
E
E'
E'
E'T'
T'
T'
T'
F
F
F
F
id
id
id
+
*
)( &
&
&
&
164
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
T
T
T
E
E
E'
E'
E'T'
T'
T'
T'
F
F
F
F
id
id
id
+
*
)( &&
&
&
&
165
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Reconstructing the Parse TreeInput:
(id*id)+id
Output:
E " T E'
T " F T'
F " ( E )
E " T E'
T " F T'
F " id
T' " * F T'
F " id
T' " &
E' " &
T' " &
E' " + T E'
T " F T'
F " id
T' " &
E' " &
E " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | idLeftmost 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
166
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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 # !* & }
167
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = ?
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
168
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = { (, id }
FIRST (T') = ?
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
169
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = { (, id }
FIRST (T') = { *, &}
FIRST (T) = ?
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
170
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = { (, id }
FIRST (T') = { *, &}
FIRST (T) = { (, id }
FIRST (E') = ?
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
171
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = { (, id }
FIRST (T') = { *, &}
FIRST (T) = { (, id }
FIRST (E') = { +, &}
FIRST (E) = ?
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
172
Syntax Analysis - Part 1
© Harry H. Porter, 2005
“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:
FIRST (F) = { (, id }
FIRST (T') = { *, &}
FIRST (T) = { (, id }
FIRST (E') = { +, &}
FIRST (E) = { (, id }
E " T E'
E'" + T E' | &
T " F T'
T'" * F T' | &
F " ( E ) | id
173
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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.
174
Syntax Analysis - Part 1
© Harry H. Porter, 2005
To Compute the FIRST(X1X2X3...XN)
Result = {}
Add everything in FIRST(X1), except &, to result
175
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Result = {}
Add everything in FIRST(X1), except &, to result
if & + FIRST(X1) then
Add everything in FIRST(X2), except &, to result
endIf
To Compute the FIRST(X1X2X3...XN)
176
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
To Compute the FIRST(X1X2X3...XN)
177
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
To Compute the FIRST(X1X2X3...XN)
178
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
To Compute the FIRST(X1X2X3...XN)
179
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
To Compute the FIRST(X1X2X3...XN)
180
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
To Compute FOLLOW(Ai) for all Nonterminals in the Grammar
181
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
182
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Add $ to FOLLOW(S)
183
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Add $ to FOLLOW(S)
184
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
F " ( E ) | id
What can follow E?
185
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
F " ( E ) | id
What can follow E?
186
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
E " T E'
Whatever can follow E
can also follow E'
187
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
E " T E'
Whatever can follow E
can also follow E'
188
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
E'0 " + T E'1
Whatever is in FIRST(E'1)
can follow T
189
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
E'0 " + T E'1
Whatever is in FIRST(E'1)
can follow T
190
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
T'0 " * F T'1
Whatever is in FIRST(T'1)
can follow F
191
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
T'0 " * F T'1
Whatever is in FIRST(T'1)
can follow F
192
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( 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
193
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( 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
194
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
T " F T'
Whatever can follow T
can also follow T'
195
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Look at rule
T " F T'
Whatever can follow T
can also follow T'
196
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( 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
197
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( 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
198
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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 " T E'
E' " + T E' | &
T " F T'
T' " * F T' | &
F " ( E ) | id
Nothing more can be added.
199
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Building the Predictive Parsing Table
The Main Idea:
Assume we’re looking for an A
i.e., A is on the stack top.
Assume b is the current input symbol.
200
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Building the Predictive Parsing Table
The Main Idea:
Assume we’re 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!
201
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Building the Predictive Parsing Table
The Main Idea:
Assume we’re 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!
202
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Building the Predictive Parsing Table
The Main Idea:
Assume we’re 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!
203
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
“if b then if b then otherStmt else otherStmt”
1. S " if E then S S'
2. S " otherStmt
3. S' " else S
4. S' " &
5. E " boolExpr
204
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o , “if b then if b then otherStmt else otherStmt”
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
1. S " if E then S S'
2. S " otherStmt
3. S' " else S
4. S' " &
5. E " boolExpr
205
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
206
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
207
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E
e i t $bo
S'
S
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
208
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E
e
S " iEtSS'
i t $bo
S'
S
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
209
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E
e
S " iEtSS'
i t $bo
S'
S
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
210
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E
e
S " iEtSS'
i t $bo
S'
S " oS
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
211
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E
e
S " iEtSS'
i t $bo
S'
S " oS
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
212
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E " bE
e
S " iEtSS'
i t $bo
S'
S " oS
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
213
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E " bE
e
S " iEtSS'
i t $bo
S'
S " oS
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
214
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
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
E " bE
S' " e S
e
S " iEtSS'
i t $bo
S'
S " oS
215
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
Look at Rule 4: S' " &
If we are looking for an S'
and & + FIRST(rhs)...
Then if $ + FOLLOW(S')...
Add that rule under $
E " bE
S' " e S
e
S " iEtSS'
i t $bo
S'
S " oS
216
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
Look at Rule 4: S' " &
If we are looking for an S'
and & + FIRST(rhs)...
Then if $ + FOLLOW(S')...
Add that rule under $
E " bE
S' " e S
e
S " iEtSS'
i t
S' " &
$bo
S'
S " oS
217
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " 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
E " bE
S' " e S
e
S " iEtSS'
i t
S' " &
$bo
S'
S " oS
218
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E " bE
S' " e S
S' " &
e
S " iEtSS'
i t
S' " &
$bo
S'
S " oS
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
219
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E " bE
S' " e S
S' " &
e
S " iEtSS'
i t
S' " &
$bo
S'
S " oS
CONFLICT!
Two rules in one table entry.
220
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Example: The “Dangling Else” Grammar
i b t i b t o e o
FIRST(S) = { i, o } FOLLOW(S) = { e, $ }
FIRST(S') = { e, & } FOLLOW(S') = { e, $ }
FIRST(E) = { b } FOLLOW(E) = { t }
1. S " i E t S S'
2. S " o
3. S' " e S
4. S' " &
5. E " b
E " bE
S' " e S
S' " &
e
S " iEtSS'
i t
S' " &
$bo
S'
S " oS
CONFLICT!
Two rules in one table entry.
The grammar is not LL(1)!
221
Syntax Analysis - Part 1
© Harry H. Porter, 2005
Algorithm to Build the Table
Input: Grammar G
Output: Parsing Table, such that TABLE[A,b]= Rule to use or “ERROR/Blank”
222
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
223
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
224
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
225
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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
226
Syntax Analysis - Part 1
© Harry H. Porter, 2005
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”
Syntax part1
Syntax part1
Syntax part1
Syntax part1
Syntax part1

Mais conteúdo relacionado

Mais procurados

Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzerahmed51236
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptApoorv Diwan
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manualNitesh Dubey
 
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015Mozaic Works
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzersArchana Gopinath
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designSadia Akter
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IVManoj Patil
 
Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyserArchana Gopinath
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
 

Mais procurados (20)

Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzer
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Module 11
Module 11Module 11
Module 11
 
Lexical
LexicalLexical
Lexical
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Parsing
ParsingParsing
Parsing
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyser
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Introduction
IntroductionIntroduction
Introduction
 

Semelhante a Syntax part1

Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxTameneTamire
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docxvenkatapranaykumarGa
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of Cjeremyrand
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
LVEE 2014: Text parsing with Python and PLY
LVEE 2014: Text parsing with Python and PLYLVEE 2014: Text parsing with Python and PLY
LVEE 2014: Text parsing with Python and PLYdmbaturin
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdfmovamag594
 

Semelhante a Syntax part1 (20)

Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptx
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Syntax
SyntaxSyntax
Syntax
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Control structure
Control structureControl structure
Control structure
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Theperlreview
TheperlreviewTheperlreview
Theperlreview
 
LVEE 2014: Text parsing with Python and PLY
LVEE 2014: Text parsing with Python and PLYLVEE 2014: Text parsing with Python and PLY
LVEE 2014: Text parsing with Python and PLY
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdf
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
intro2fortran.pptx
intro2fortran.pptxintro2fortran.pptx
intro2fortran.pptx
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Syntax part1

  • 1. 1 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Syntax Analysis 2 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 2. 3 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Don’t build a tree at all! * y 1 x + 4 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 3. 5 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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. 6 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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)); We’re in the middle of a statement Skip tokens until we see a “;” Resume Parsing Misses a second error... Oh, well... Checks most of the source Error detected here This error missed
  • 4. 7 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Difficult to generate clear and accurate error messages. Example function foo () { ... if (...) { ... } else { ... ... } <eof> Example var myVarr: Integer; ... x := myVar; ... Missing } here Not detected until here Misspelled ID here Detected here as “Undeclared ID” 8 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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; y := t end if (...) then x := 1 else y := 2; z := 3; function foo (x: Integer; y: Integer)... Tend to put a comma here Tend to insert a ; here Tend to insert a ; here
  • 5. 9 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 programmer’s intent A shifting notion of what constitutes a correct / legal / valid program May encourage programmers to get sloppy Declarations provide redundancy ! Increased reliability 10 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 6. 11 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Error Recovery Approaches: Panic Mode Discard tokens until we see a “synchronizing” token. •!Simple to implement •!Commonly used •!The key... Good set of synchronizing tokens Knowing what to do then •!May skip over large sections of source Example Skip to next occurrence of } end ; Resume by parsing the next statement 12 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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. •!The key... Don’t get into an infinite loop ...constantly inserting tokens ...and never scanning the actual source Example while (x = 4) y := a+b; ... Insert do to “fix” the statement.
  • 7. 13 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 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 Error Recovery Approaches: Global Correction 14 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 8. 15 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Rule Alternatives E " E + E E " ( E ) E " - E E " ID E " E + E " ( E ) " - E " ID E " E + E | ( E ) | - E | ID E " E + E | ( E ) | - E | ID All Notations are Equivalent 16 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 ... 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”) A sequence of zero Or more terminals And nonterminals Including &
  • 9. 17 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Derivations 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) 1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID 18 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 !* (id*id) If # !* $ and $ ! %, then # !* % Derives in one-or-more steps !+
  • 10. 19 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Given G A grammar S The Start Symbol Define L(G) The language generated L(G) = { w | S !+ w} “Equivalence” of CFG’s If two CFG’s 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 20 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 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* # then we say # is a “left-sentential form.” 1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID
  • 11. 21 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Rightmost Derivations In a derivation... always expand the rightmost nonterminal. E ! E+E ! E+id ! (E)+id ! (E*E)+id ! (E*id)+id ! (id*id)+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.” 1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID 22 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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)
  • 12. 23 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 E E EE EE id id id + * )(1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID 24 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 E E EE EE id id id + * )( Rightmost Derivation: E ! E+E ! E+id ! (E)+id ! (E*E)+id ! (E*id)+id ! (id*id)+id 1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID
  • 13. 25 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Rightmost Derivation: E ! E+E ! E+id ! (E)+id ! (E*E)+id ! (E*id)+id ! (id*id)+id E E EE EE id id id + * )(1. E " E + E 2. " E * E 3. " ( E ) 4. " - E 5. " ID 26 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Given a leftmost derivation, we can build a parse tree. Given a rightmost derivation, we can build a 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 Rightmost Derivation of (id*id)+id Lefttmost Derivation of (id*id)+id Same Parse Tree
  • 14. 27 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Ambuiguous Grammars Leftmost Derivation #1 E ! E+E ! id+E ! id+E*E ! id+id*E ! id+id*id Leftmost Derivation #2 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 EE EE id id id * + E EE E E id id id + * Input: id+id*id 28 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 15. 29 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " & 30 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Recursive Transition Networks Regular Expressions ( NFA ( DFA Context-Free Grammar ( Recursive Transition Networks Exactly as expressive a CFGs... But clearer for humans! ; elseIf if then else end then Expr Expr Stmt Stmt Stmt IfStmt Stmt Expr ... ... ... ... Terminal Symbols: Nonterminal Symbols:
  • 16. 31 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Interpretation #2: if E1 then (if E2 then S1 else S2) if then S2elseE1 S if thenE2 S1 S if then S2elseE2 S if thenE1 S1 S 32 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Goal: “Match else-clause to the closest if without an else-clause already.” Solution: Stmt " if Expr then Stmt " if Expr then WithElse else Stmt " ...Other Stmt Forms... WithElse " 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) if then WithElseelseE2 Stmt if thenE1 WithElse Stmt S1 S2 The Dangling “Else” Problem
  • 17. 33 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Goal: “Match else-clause to the closest if without an else-clause already.” Solution: Stmt " if Expr then Stmt " if Expr then WithElse else Stmt " ...Other Stmt Forms... WithElse " 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 thenE1 Stmt The Dangling “Else” Problem elseWithElse 34 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Goal: “Match else-clause to the closest if without an else-clause already.” Solution: Stmt " if Expr then Stmt " if Expr then WithElse else Stmt " ...Other Stmt Forms... WithElse " 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 if then WithElseelseE2 Stmt if thenE1 WithElse Stmt The Dangling “Else” Problem elseWithElse
  • 18. 35 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Goal: “Match else-clause to the closest if without an else-clause already.” Solution: Stmt " if Expr then Stmt " if Expr then WithElse else Stmt " ...Other Stmt Forms... WithElse " 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 if then WithElseelseE2 Stmt if thenE1 WithElse Stmt The Dangling “Else” Problem elseWithElse Oops, No Match! 36 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 19. 37 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S 38 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a
  • 20. 39 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a Ba a 40 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a Ba a
  • 21. 41 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a Ba a 42 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a B b a a bb
  • 22. 43 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a B b a a bb 44 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a B b a a bb
  • 23. 45 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a B b a a bb Failure Occurs Here!!! 46 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a Ba a We need an ability to back up in the input!!!
  • 24. 47 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a 48 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a a a b a
  • 25. 49 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a a a b a 50 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a a a b a
  • 26. 51 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a a a b a 52 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a a a b a Failure Occurs Here!!!
  • 27. 53 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S A a 54 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S
  • 28. 55 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S C e 56 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S C e a a D
  • 29. 57 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S C e a b a D b d 58 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Backtracking Input: aabbde 1. S " Aa 2. " Ce 3. A " aaB 4. " aaba 5. B " bbb 6. C " aaD 7. D " bbd S C e a b a D b d
  • 30. 59 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 ... 60 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol
  • 31. 61 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 62 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 32. 63 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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! 64 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 33. 65 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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) 66 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 | &
  • 34. 67 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 | *1 | *2 | *3 | ... C " $1 | $2 | $3 | ... 68 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 | *1 | *2 | *3 | ... C " $1 | $2 | $3 | ... # # $1 $2 *1 A
  • 35. 69 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 | *1 | *2 | *3 | ... C " $1 | $2 | $3 | ... C *1 $1 # # # $1 $2 $2 *1 A A C 70 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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' | ... | &
  • 36. 71 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Left Recursion in More Than One Step Example: S " Af | b A " Ac | Sd | e Is A left recursive? Yes. 72 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Left Recursion in More Than One Step Example: S " Af | b A " Ac | Sd | e Is A left recursive? Yes. Is S left recursive?
  • 37. 73 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 74 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 38. 75 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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... 76 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 A’s rules start with S? Yes. A " Sd
  • 39. 77 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 A’s rules start with S? Yes. A " Sd Get rid of the S. Substitute in the righthand sides of S. A " Afd | bd 78 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 A’s 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
  • 40. 79 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 A’s 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' | & 80 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Left Recursion in More Than One Step The Original Grammar: S " Af | b A " Ac | Sd | e
  • 41. 81 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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... 82 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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' | &
  • 42. 83 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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”? 84 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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...
  • 43. 85 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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”? 86 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 44. 87 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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'... 88 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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.
  • 45. 89 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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'... 90 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Finally, eliminate any immediate Left recursion involving “B”
  • 46. 91 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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” 92 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Left Recursion in More Than One Step The Original Grammar: S " Af | b A " Ac | Sd | Be | C B " Ag | Sh | k 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' | & If there is another nonterminal, then do it next.
  • 47. 93 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 Ai " Aj# then replace it by Ai " $1# | $2# | $3# | ... | $N# endIf endFor Eliminate immediate left recursion among the Ai rules endFor A1 A2 A3 ... Aj ... A7 Ai Inner Loop Outer Loop 94 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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. Step 1: From grammar, construct table. Step 2: Use table to parse strings. Example E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Term ... +Term +Term + ... +Term * Factor * Factor * ... * Factor Factor ...
  • 48. 95 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Table-Driven Predictive Parsing Algorithm Input String ( a * 4 ) 3 $ Algorithm Output + Y Z $ X Stack of grammar symbols Pre-Computed Table: E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id 96 Syntax Analysis - Part 1 © Harry H. Porter, 2005 T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E Nonterminals Input Symbols, plus $ Input String ( a * 4 ) 3 $ Algorithm Output + Y Z $ X Stack of grammar symbols Pre-Computed Table: Table-Driven Predictive Parsing Algorithm E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id
  • 49. 97 Syntax Analysis - Part 1 © Harry H. Porter, 2005 T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E Nonterminals Input Symbols, plus $ Input String ( a * 4 ) 3 $ Algorithm Output + Y Z $ X Stack of grammar symbols Blank entries indicate ERRORPre-Computed Table: Table-Driven Predictive Parsing Algorithm E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id 98 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 ... Push Y2 Push Y1 Print (“X " Y1 Y2 ... YK”) else // Table[X,a] is blank Syntax Error endIf until X == $ X A $ YK A $ ... Y2 Y1
  • 50. 99 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id+ 100 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id+ Add $ to end of input Push $ Push E
  • 51. 101 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Add $ to end of input Push $ Push E E 102 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Look at Table [ E, ‘(’ ] Use rule E"TE' Pop E Push E' Push T Print E"TE' E
  • 52. 103 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T Look at Table [ E, ‘(’ ] Use rule E"TE' Pop E Push E' Push T Print E"TE' 104 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Table [ T, ‘(’ ] = T"FT' Pop T Push T’ Push F Print T"FT' E’ T
  • 53. 105 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ F Table [ T, ‘(’ ] = T"FT' Pop T Push T’ Push F Print T"FT' 106 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Table [ F, ‘(’ ] = F"(E) Pop F Push ( Push E Push ) Print F"(E) E’ T’ F
  • 54. 107 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E ( Table [ F, ‘(’ ] = F"(E) Pop F Push ) Push E Push ( Print F"(E) 108 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E ( Top of Stack matches next input Pop and Scan
  • 55. 109 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E Top of Stack matches next input Pop and Scan 110 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E Table [ E, id ] = E"TE' Pop E Push E' Push T Print E"TE'
  • 56. 111 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T Table [ E, id ] = E"TE' Pop E Push E' Push T Print E"TE' 112 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T Table [ T, id ] = T"FT' Pop T Push T' Push F Print T"FT'
  • 57. 113 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ F Table [ T, id ] = T"FT' Pop T Push T' Push F Print T"FT' 114 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ F Table [ F, id ] = F"id Pop F Push id Print F"id
  • 58. 115 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ id Table [ F, id ] = F"id Pop F Push id Print F"id 116 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ id Top of Stack matches next input Pop and Scan
  • 59. 117 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ Top of Stack matches next input Pop and Scan 118 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ Table [ T', ‘*’ ] = T'"*FT' Pop T' Push T' Push F Push ‘*’ Print T'"*FT'
  • 60. 119 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ Table [ T', ‘*’ ] = T'"*FT' Pop T' Push T' Push F Push ‘*’ Print T'"*FT' F * 120 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ F * Top of Stack matches next input Pop and Scan
  • 61. 121 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ F Top of Stack matches next input Pop and Scan 122 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ F Table [ F, id ] = F"id Pop F Push id Print F"id
  • 62. 123 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ id Table [ F, id ] = F"id Pop F Push id Print F"id 124 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ id Top of Stack matches next input Pop and Scan
  • 63. 125 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ Top of Stack matches next input Pop and Scan 126 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ T’ Table [ T', ‘)’ ] = T'"& Pop T' Push <nothing> Print T'"&
  • 64. 127 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ Table [ T', ‘)’ ] = T'"& Pop T' Push <nothing> Print T'"& 128 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) E’ Table [ E', ‘)’ ] = E'"& Pop E' Push <nothing> Print E'"&
  • 65. 129 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) Table [ E', ‘)’ ] = E'"& Pop E' Push <nothing> Print E'"& 130 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ ) Top of Stack matches next input Pop and Scan
  • 66. 131 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Top of Stack matches next input Pop and Scan 132 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Table [ T', ‘+’ ] = T'"& Pop T' Push <nothing> Print T'"&
  • 67. 133 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ Table [ T', ‘+’ ] = T'"& Pop T' Push <nothing> Print T'"& 134 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ Table [ E', ‘+’ ] = E'"+TE' Pop E' Push E' Push T Push ‘+’ Print E'"+TE'
  • 68. 135 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ Table [ E', ‘+’ ] = E'"+TE' Pop E' Push E' Push T Push ‘+’ Print E'"+TE' T + 136 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T + Top of Stack matches next input Pop and Scan
  • 69. 137 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T Top of Stack matches next input Pop and Scan 138 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T Table [ T, id ] = T"FT' Pop T Push T' Push F Print T"FT'
  • 70. 139 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Table [ T, id ] = T"FT' Pop T Push T' Push F Print T"FT' F 140 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Table [ F, id ] = F"id Pop F Push id Print F"id F
  • 71. 141 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Table [ F, id ] = F"id Pop F Push id Print F"id id 142 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ id Top of Stack matches next input Pop and Scan
  • 72. 143 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Top of Stack matches next input Pop and Scan 144 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ T’ Table [ T', $ ] = T'"& Pop T' Push <nothing> Print T'"&
  • 73. 145 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ Table [ T', $ ] = T'"& Pop T' Push <nothing> Print T'"& 146 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ E’ Table [ E', $ ] = E'"& Pop E' Push <nothing> Print E'"&
  • 74. 147 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Table [ E', $ ] = E'"& Pop E' Push <nothing> Print E'"& 148 Syntax Analysis - Part 1 © Harry H. Porter, 2005 ExampleInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T"FT'T"FT'T T'"&T'"&T'"*FT'T'"&T' * F"(E) E"TE' ( E'"& ) F"idF E'"& $+id E'"+TE'E' E"TE'E ( id * id ) id $+ $ Input symbol == $ Top of stack == $ Loop terminates with success
  • 75. 149 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T E E' 150 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T E E' T'F
  • 76. 151 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T E E E' T'F )( 152 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T'F )(
  • 77. 153 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' F F )( 154 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' F F id )(
  • 78. 155 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' T' F F Fid * )( 156 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' T' F F Fid id * )(
  • 79. 157 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' T' F F Fid id * )( & 158 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' T' F F Fid id * )( & &
  • 80. 159 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T E E E' E' T' T' T' F F Fid id * )( & & & 160 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T T E E E' E' E'T' T' T' F F Fid id + * )( & & &
  • 81. 161 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T T E E E' E' E'T' T' T' T' F F F F id id + * )( & & & 162 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T T E E E' E' E'T' T' T' T' F F F F id id id + * )( & & &
  • 82. 163 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T T E E E' E' E'T' T' T' T' F F F F id id id + * )( & & & & 164 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id T T T E E E' E' E'T' T' T' T' F F F F id id id + * )( && & & &
  • 83. 165 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Reconstructing the Parse TreeInput: (id*id)+id Output: E " T E' T " F T' F " ( E ) E " T E' T " F T' F " id T' " * F T' F " id T' " & E' " & T' " & E' " + T E' T " F T' F " id T' " & E' " & E " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | idLeftmost 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 166 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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 # !* & }
  • 84. 167 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = ? E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id 168 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = { (, id } FIRST (T') = ? E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id
  • 85. 169 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = { (, id } FIRST (T') = { *, &} FIRST (T) = ? E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id 170 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = { (, id } FIRST (T') = { *, &} FIRST (T) = { (, id } FIRST (E') = ? E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id
  • 86. 171 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = { (, id } FIRST (T') = { *, &} FIRST (T) = { (, id } FIRST (E') = { +, &} FIRST (E) = ? E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id 172 Syntax Analysis - Part 1 © Harry H. Porter, 2005 “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: FIRST (F) = { (, id } FIRST (T') = { *, &} FIRST (T) = { (, id } FIRST (E') = { +, &} FIRST (E) = { (, id } E " T E' E'" + T E' | & T " F T' T'" * F T' | & F " ( E ) | id
  • 87. 173 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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. 174 Syntax Analysis - Part 1 © Harry H. Porter, 2005 To Compute the FIRST(X1X2X3...XN) Result = {} Add everything in FIRST(X1), except &, to result
  • 88. 175 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Result = {} Add everything in FIRST(X1), except &, to result if & + FIRST(X1) then Add everything in FIRST(X2), except &, to result endIf To Compute the FIRST(X1X2X3...XN) 176 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 To Compute the FIRST(X1X2X3...XN)
  • 89. 177 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 To Compute the FIRST(X1X2X3...XN) 178 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 To Compute the FIRST(X1X2X3...XN)
  • 90. 179 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 To Compute the FIRST(X1X2X3...XN) 180 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 To Compute FOLLOW(Ai) for all Nonterminals in the Grammar
  • 91. 181 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id 182 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Add $ to FOLLOW(S)
  • 92. 183 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Add $ to FOLLOW(S) 184 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule F " ( E ) | id What can follow E?
  • 93. 185 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule F " ( E ) | id What can follow E? 186 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule E " T E' Whatever can follow E can also follow E'
  • 94. 187 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule E " T E' Whatever can follow E can also follow E' 188 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule E'0 " + T E'1 Whatever is in FIRST(E'1) can follow T
  • 95. 189 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule E'0 " + T E'1 Whatever is in FIRST(E'1) can follow T 190 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule T'0 " * F T'1 Whatever is in FIRST(T'1) can follow F
  • 96. 191 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule T'0 " * F T'1 Whatever is in FIRST(T'1) can follow F 192 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( 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
  • 97. 193 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( 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 194 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule T " F T' Whatever can follow T can also follow T'
  • 98. 195 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Look at rule T " F T' Whatever can follow T can also follow T' 196 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( 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
  • 99. 197 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( 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 198 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 " T E' E' " + T E' | & T " F T' T' " * F T' | & F " ( E ) | id Nothing more can be added.
  • 100. 199 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Building the Predictive Parsing Table The Main Idea: Assume we’re looking for an A i.e., A is on the stack top. Assume b is the current input symbol. 200 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Building the Predictive Parsing Table The Main Idea: Assume we’re 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!
  • 101. 201 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Building the Predictive Parsing Table The Main Idea: Assume we’re 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! 202 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Building the Predictive Parsing Table The Main Idea: Assume we’re 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!
  • 102. 203 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar “if b then if b then otherStmt else otherStmt” 1. S " if E then S S' 2. S " otherStmt 3. S' " else S 4. S' " & 5. E " boolExpr 204 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o , “if b then if b then otherStmt else otherStmt” 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b 1. S " if E then S S' 2. S " otherStmt 3. S' " else S 4. S' " & 5. E " boolExpr
  • 103. 205 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b 206 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b
  • 104. 207 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E e i t $bo S' S 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 208 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E e S " iEtSS' i t $bo S' S 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
  • 105. 209 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E e S " iEtSS' i t $bo S' S 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 210 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E e S " iEtSS' i t $bo S' S " oS 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
  • 106. 211 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E e S " iEtSS' i t $bo S' S " oS 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 212 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E " bE e S " iEtSS' i t $bo S' S " oS 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
  • 107. 213 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E " bE e S " iEtSS' i t $bo S' S " oS 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 214 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b 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 E " bE S' " e S e S " iEtSS' i t $bo S' S " oS
  • 108. 215 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b Look at Rule 4: S' " & If we are looking for an S' and & + FIRST(rhs)... Then if $ + FOLLOW(S')... Add that rule under $ E " bE S' " e S e S " iEtSS' i t $bo S' S " oS 216 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b Look at Rule 4: S' " & If we are looking for an S' and & + FIRST(rhs)... Then if $ + FOLLOW(S')... Add that rule under $ E " bE S' " e S e S " iEtSS' i t S' " & $bo S' S " oS
  • 109. 217 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " 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 E " bE S' " e S e S " iEtSS' i t S' " & $bo S' S " oS 218 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E " bE S' " e S S' " & e S " iEtSS' i t S' " & $bo S' S " oS 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
  • 110. 219 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E " bE S' " e S S' " & e S " iEtSS' i t S' " & $bo S' S " oS CONFLICT! Two rules in one table entry. 220 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Example: The “Dangling Else” Grammar i b t i b t o e o FIRST(S) = { i, o } FOLLOW(S) = { e, $ } FIRST(S') = { e, & } FOLLOW(S') = { e, $ } FIRST(E) = { b } FOLLOW(E) = { t } 1. S " i E t S S' 2. S " o 3. S' " e S 4. S' " & 5. E " b E " bE S' " e S S' " & e S " iEtSS' i t S' " & $bo S' S " oS CONFLICT! Two rules in one table entry. The grammar is not LL(1)!
  • 111. 221 Syntax Analysis - Part 1 © Harry H. Porter, 2005 Algorithm to Build the Table Input: Grammar G Output: Parsing Table, such that TABLE[A,b]= Rule to use or “ERROR/Blank” 222 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 112. 223 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 224 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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
  • 113. 225 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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 226 Syntax Analysis - Part 1 © Harry H. Porter, 2005 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”