Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need...
Transcript of Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need...
![Page 1: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/1.jpg)
1
Topic 3: Parsing and Yaccing
COS 320
Compiling Techniques
Princeton University Spring 2016
Lennart Beringer
![Page 2: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/2.jpg)
2
The Compiler
![Page 3: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/3.jpg)
3
Role of parser
• verify that stream of tokens is valid according to the language definition
• report violations as (informative) syntax error and recover
• build abstract syntax tree (AST) for use in next compiler phase
Lexer partitioned document into stream of tokens.
But not all token lists represent programs.
>=IF ID(foo) NUM(42) THEN
>= THEN ID(foo) IF NUM(42)
![Page 4: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/4.jpg)
4
Syntactical Analysis
• each language definition has rules that describe the syntax of well-
formed programs.
• format of the rules: context-free grammars
• why not regular expressions/NFA’s/DFA’s?
![Page 5: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/5.jpg)
5
Syntactical Analysis
• each language definition has rules that describe the syntax of well-
formed programs.
• format of the rules: context-free grammars
• why not regular expressions/NFA’s/DFA’s?
• source program constructs have recursive structure:
digits = [0-9]+;
expr = digits | “(“ expr “+” expr “)”
![Page 6: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/6.jpg)
6
Syntactical Analysis
• each language definition has rules that describe the syntax of well-
formed programs.
• format of the rules: context-free grammars
• why not regular expressions/NFA’s/DFA’s?
• source program constructs have recursive structure:
digits = [0-9]+;
expr = digits | “(“ expr “+” expr “)”
• finite automata can’t recognize recursive constructs, so cannot ensure
expressions are well-bracketed: a machine with N states cannot
remember parenthesis—nesting depth greater than N
• CFG’s are more powerful, but also more costly to implement
![Page 7: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/7.jpg)
7
Context-Free Grammar
![Page 8: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/8.jpg)
8
Context-Free Grammars: definitions
• language: set of strings
• string: finite sequence of symbols taken from finite alphabet
Regular expressions and CFG’s both describe languages, but
over different alphabets
Lexical analysis Syntax analysis
symbols/alphabet ASCII token
strings lists of tokens lists of phrases
language set of (legal) token sequences
set of (legal) phrase sequences (“programs”)
![Page 9: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/9.jpg)
9
Context-free grammars versus regular expressions
Also known as Backus-Naur Form (BNF, Algol 60)
CFG’s strictly more expressive than RE’s:
Any language recognizable/generated by a RE can also be
recognized/generated by a CFG, but not vice versa.
CFG RE
matching parentheses
nested comments
![Page 10: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/10.jpg)
10
Context-Free Grammars: definition
A CFG consists of
• a finite set N = N1 , .., Nk of non-terminal symbols, one of which is
singled out as the start symbol
• a finite set T = T1, …, Tm of terminal symbols (representing token types)
• a finite set of productions LHS RHS where LHS is a single
non-terminal, and RHS is a list of terminals and non-terminals
Each production specifies one way in which terminals and non-
terminals may be combined to form a legal (sub)string.
Recursion (e.g. well-bracketing) can be modeled by referring to the LHS
inside the RHS: stmt IF exp THEN stmt ELSE stmt
The language recognized by the CFG is the set of
terminal-only strings derivable from the start symbol .
![Page 11: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/11.jpg)
11
CFG’s: derivations
1. Start with start symbol
2. While the current string contains a non-terminal N,
replace it by the RHS of one of the rules for N.
Non-determinism
• Choice of non-terminal N to be replaced in each step
• Choice of production/RHS once N has been chosen.
Acceptance of a string is independent of these choices (cf. NFA),
so a string may have multiple derivations.
Notable derivation strategies
• left-most derivation: in each step, replace the left-most non-terminal
• right-most derivation: …
(In both cases, use the chosen symbol’s first rule)
![Page 12: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/12.jpg)
12
Example (cf HW1)
Nonterminals
stmt /*statements/*
expr /*expressions*/
expr_list /*expression lists*/
Terminals : tokens
SEMI
ID
ASSIGN
LPAREN
RPAREN
NUM
PLUS
COMMA
i.e. stmt SEMI stmt
i.e. PRINT LPAREN expr_list RPAREN
Productions
stmt stmt; stmt
stmt ID := expr
stmt print ( expr_list )
expr_list expr
expr_list expr_list, expr
expr ID
expr NUM
expr expr + expr
expr ( stmt, expr )
![Page 13: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/13.jpg)
13
Example: leftmost Derivation for a:=12; print(23)
Show that the following token sequence
ID := NUM; PRINT(NUM) is a legal phraseID ASSIGN NUM SEMI PRINT LPAREN NUM RPAREN
![Page 14: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/14.jpg)
14
Example: leftmost Derivation for a:=12; print(23)
Show that the following token sequence
ID := NUM; PRINT(NUM) is a legal phrase
stmt
stmt SEMI stmt
ID ASSIGN expr SEMI stmt
ID ASSIGN NUM SEMI stmt
ID ASSIGN NUM SEMI PRINT LPAREN expr_list RPAREN
ID ASSIGN NUM SEMI PRINT LPAREN expr RPAREN
ID ASSIGN NUM SEMI PRINT LPAREN NUM RPAREN
ID ASSIGN NUM SEMI PRINT LPAREN NUM RPAREN
![Page 15: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/15.jpg)
15
Example: rightmost Derivation for a:=12; print(23)
Show that the following token sequence
ID := NUM; PRINT(NUM) is a legal phrase
stmt
stmt SEMI stmt
stmt SEMI PRINT LPAREN expr_list RPAREN
stmt SEMI PRINT LPAREN expr RPAREN
stmt SEMI PRINT LPAREN NUM RPAREN
ID ASSIGN expr SEMI PRINT LPAREN NUM RPAREN
ID ASSIGN NUM SEMI PRINT LPAREN NUM RPAREN
ID ASSIGN NUM SEMI PRINT LPAREN NUM RPAREN
![Page 16: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/16.jpg)
16
Parse tree: graphical representation of derivation results
• Root: start symbol
• Inner nodes labeled with non-terminals; #branches according to
chosen production
• Leaf nodes labeled with terminals
Example (parse tree of previous CFG): stmt
SEMI
expr
NUM
NUM
stmt stmt
ASSIGNID expr PRINT LPAREN expr_list RPAREN
Different derivations may or may not yield different parse trees.
![Page 17: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/17.jpg)
17
Ambiguous Grammars
A grammar is ambiguous is it can derive a string of tokens with two
or more different parse trees
Example?
![Page 18: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/18.jpg)
A grammar is ambiguous is it can derive a string of tokens with two
or more different parse trees
Example?
18
Ambiguous Grammars
![Page 19: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/19.jpg)
A grammar is ambiguous is it can derive a string of tokens with two
or more different parse trees
Example?
19
Ambiguous Grammars
![Page 20: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/20.jpg)
A grammar is ambiguous is it can derive a string of tokens with two
or more different parse trees
Example?
20
Ambiguous Grammars
![Page 21: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/21.jpg)
21
Ambiguous Grammars
exponentiation,…plus, minus, times,…
![Page 22: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/22.jpg)
22
Ambiguous Grammars
exponentiation,…plus, minus, times,…
![Page 23: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/23.jpg)
23
Ambiguous Grammars
Conflict:
Operators with same precedence should have same associativity Why?
Example: suppose plus right-assoc, minus left-assoc, equal precedence
a – b – c + d + e
(a – b) – c + (d + e)
((a – b) – c) + (d + e) (a – b) – (c + (d + e))
((10 – 4) – 3) + (20 + 15) =
(6 – 3) + 35 = 3 + 35 = 38
(10 - 4) – (3 + (20 + 15)) =
6 – (3 + 35) = 6 – 38 = -32
Next: how to rewrite an ambiguous grammar
vs.
![Page 24: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/24.jpg)
24
Ambiguous Grammars
![Page 25: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/25.jpg)
25
End-Of-File Marker
![Page 26: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/26.jpg)
26
Grammars and Lexical Analysis
CFG’s are sufficiently powerful to describe regular expressions.
Example: language (a | b)* abb is described by the following CFG
W aW
W bW
W aX
X bY
Y b
(W start symbol)
So can combine lexical and syntactic analysis (parsing) into one module
+ ok for small languages, with simple/few RE’s and syntactic grammar
- regular expression specification arguably more concise
- separating phases increases compiler modularity
![Page 27: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/27.jpg)
27
Context-Free Grammars versus REs (I)
Claim: context-free grammars are strictly more powerful that
RE’s (and hence NFA’s and DFA’s).
Part2: define a grammar G such that there is no finite automaton
F with L(G)=L( F ).
Part 1: any language that can be generated using RE’s can be
generated by a CFG.
Proof: give a translation that transforms an RE R into a CFG G(R)
such that L( R ) = L ( G(R) ).
![Page 28: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/28.jpg)
28
Context Free Grammars and REs (II)
Part 1: any language that can be generated using RE’s can be
generated by a CFG.
Proof: give a translation that transforms an RE R into a CFG G(R)
such that L( R ) = L ( G(R) ).
Construction of the translation by induction over structure of RE’s:
1. Introduce one non-terminal (R) for each subterm R of the RE
2. Rules (base cases):
i. Symbol (a): R a
ii. Epsilon(є): R є
3. Rules (inductive cases):
i. Alternation (M | N ): R M R N
ii. Concatenation (M N): R M N
iii. Kleene closure (M *): R M R R ε
![Page 29: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/29.jpg)
29
Context Free Grammars and REs (II)
Part 1: any language that can be generated using RE’s can be
generated by a CFG.
Proof: give a translation that transforms an RE R into a CFG G(R)
such that L( R ) = L ( G(R) ).
Construction of the translation by induction over structure of RE’s:
1. Introduce one non-terminal (R) for each subterm R of the RE
2. Rules (base cases):
i. Symbol (a): R a
ii. Epsilon(є): R є
3. Rules (inductive cases):
i. Alternation (M | N ): R M R N
ii. Concatenation (M N): R M N
iii. Kleene closure (M *): R M R R ε
![Page 30: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/30.jpg)
30
Context Free Grammars and REs (II)
Part 1: any language that can be generated using RE’s can be
generated by a CFG.
Proof: give a translation that transforms an RE R into a CFG G(R)
such that L( R ) = L ( G(R) ).
Construction of the translation by induction over structure of RE’s:
1. Introduce one non-terminal (R) for each subterm R of the RE
2. Rules (base cases):
i. Symbol (a): R a
ii. Epsilon(є): R є
3. Rules (inductive cases):
i. Alternation (M | N ): R M R N
ii. Concatenation (M N): R M N
iii. Kleene closure (M *): R M R R ε
![Page 31: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/31.jpg)
31
Context-Free Grammar with no RE/FA
NFA? DFA?
Suggestions?
![Page 32: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/32.jpg)
32
Context-Free Grammar with no RE/FA
NFA? DFA?
Either one is fine!
Well-bracketing!S ( S )
S ε
• FA must have finite number of states, say N
• FA must “remember” number of “(“ to generate equally many “)”
• At or before seeing the N+1st “(”, FA must revisit a state already
traversed, say s
• s represents two different counts of “)”, both of which must be accepted
• One count will be invalid, ie not match up the number of “(“’s seen.
Proof by contradiction:
assume there’s a NFA/DFA accepting the language
![Page 33: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/33.jpg)
33
Grammars versus automata models (for the curious)
Q: is there a class of grammars capturing exactly the power of RE’s?
A: yes, the “regular, right-linear, finite state grammars” for example
Q: is there an automaton model that precisely captures context-
free grammars?
A: yes, “push-down automata” (ie automata with a stack)
at most one non-terminal in each RHSnon-terminals occur only in
right-most position of RHS
Q: we use context-free grammars to specify parsing. Are there
grammars that are not context-free?
A: yes, context-sensitive grammars, for example (LHS of
productions are strings with > 0 nonterminals and >=0 terminals)
![Page 34: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/34.jpg)
34
Parsing
![Page 35: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/35.jpg)
35
Outline
• Recursive Descent Parsing
• Shift-Reduce Parsing
• ML-Yacc
• Recursive Descent Parser Generation
![Page 36: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/36.jpg)
36
Recursive Descent Parsing
Reminder: context-free grammars: symbols (terminals, non-terminals),
productions (rules), derivations, …
Many CFG’s can be parsed with an algorithm called recursive descent:
• one function for each non-terminal
• each production rule yields one clause in the function for the LHS symbol
• functions (possibly mutually) recursive
Other names for recursive descent:
• predictive parsing: rule selection is based on next symbol(s) seen
• top-down parsing (algorithm starts with initial symbol)
• LL(1) parsing (Left-to-right parsing, Leftmost derivation, 1 symbol
look-ahead)
![Page 37: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/37.jpg)
37
Recursive descent: example
![Page 38: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/38.jpg)
38
Recursive descent: another example
![Page 39: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/39.jpg)
39
The Problem
A S EOF
S id := E
S print ( L )
E id
E num
![Page 40: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/40.jpg)
40
The Problem
RPAREN is the (only) nonterminal that may follow a string derivable from
M (why?...). Seeing RPAREN thus indicates we should use rule M ε.
A S EOF
S id := E
S print ( L )
E id
E num
![Page 41: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/41.jpg)
41
Limitation of Recursive Descent Parsing
Need algorithm that
• decides if a grammar is suitable for recursive descent parsing, and
• if so, calculates the parsing table, ie a table indicating which production
to apply in each situation, given the next terminal in the token stream
Sometimes, we can modify a grammar so that is amenable to
predictive parsing without changing the language recognized, for
example by replacing left recursion by right recursion.
Key ingredient: analysis of first and follow.
![Page 42: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/42.jpg)
42
Formal Techniques for predictive parsing
For each γ that is a RHS of one of the rules, must determine the
set of all terminals that can begin a string derivable from γ: First(γ)
Examples: First (id := E) = { id } for grammar
on previous slide.
FIRST(γ)
Let γ range over strings of terminals and nonterminals from our grammar.
(i.e. RHS of grammar rules)
![Page 43: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/43.jpg)
43
Formal Techniques for predictive parsing
For each γ that is a RHS of one of the rules, must determine the
set of all terminals that can begin a string derivable from γ: First(γ)
Examples: First (id := E) = { id } for grammar
on previous slide.
First ( T * F ) = { id, num, LPAREN } for
FIRST(γ)
Let γ range over strings of terminals and nonterminals from our grammar.
T id
T num
T ( E )
F . . .
(i.e. RHS of grammar rules)
![Page 44: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/44.jpg)
44
Formal Techniques for predictive parsing
For each γ that is a RHS of one of the rules, must determine the
set of all terminals that can begin a string derivable from γ: First(γ)
Examples: First (id := E) = { id } for grammar
on previous slide.
First ( T * F ) = { id, num, LPAREN } for
FIRST(γ)
Let γ range over strings of terminals and nonterminals from our grammar.
Predictive parsing impossible whenever there are productions
X γ1 and X γ2 such that First (γ1) and First (γ2)
overlap: can’t decide which rule to use!
T id
T num
T ( E )
F . . .
(i.e. RHS of grammar rules)
![Page 45: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/45.jpg)
45
Formal Techniques for predictive parsing
Consider γ = X Y Z where
Then, First (S) should contain First (Z), because Y and X can derive ε.
Y …
Y ε
X Y
Z …S X Y Z
![Page 46: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/46.jpg)
46
Formal Techniques for predictive parsing
Consider γ = X Y Z where
Then, First (S) should contain First (Z), because Y and X can derive ε.
NULLABLE(N)
Hence, for calculating First sets we need to determine which
nonterminals N can derive the empty string ε: Nullable(N).
Here, Y and (hence) X are nullable.
Extension to strings: γ nullable if all symbols in γ are nullable.
Y …
Y ε
X Y
Z …S X Y Z
![Page 47: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/47.jpg)
47
Computation of First
![Page 48: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/48.jpg)
48
Computation of First
![Page 49: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/49.jpg)
49
Computation of First
![Page 50: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/50.jpg)
50
Computation of First
Similarly: first(γ) where γ is a string of terminals and nonterminals.
![Page 51: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/51.jpg)
51
Formal Techniques for predictive parsing
For each nonterminal N, we also must determine the set of all terminal
symbols t that can immediately follow N in a derivation: Follow(N).
In particular, must know which terminals may follow a nullable nonterminal.
FOLLOW(N)
![Page 52: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/52.jpg)
52
Formal Techniques for predictive parsing
For each nonterminal N, we also must determine the set of all terminal
symbols t that can immediately follow N in a derivation: Follow(N).
In particular, must know which terminals may follow a nullable nonterminal.
FOLLOW(N)
• Follow(X) contains t, due to S P X t
X Y
S P X t
S u
Example (S start symbol):Y …
Y ε
P …
![Page 53: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/53.jpg)
53
Formal Techniques for predictive parsing
For each nonterminal N, we also must determine the set of all terminal
symbols t that can immediately follow N in a derivation: Follow(N).
In particular, must know which terminals may follow a nullable nonterminal.
FOLLOW(N)
• Follow(X) contains t, due to S P X t
• hence Follow(P) contains t, since X is nullable
• in case P is also nullable, First (PXt) and hence First(S) contain t
X Y
S P X t
S u
Example (S start symbol):Y …
Y ε
P …
![Page 54: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/54.jpg)
54
Formal Techniques for predictive parsing
For each nonterminal N, we also must determine the set of all terminal
symbols t that can immediately follow N in a derivation: Follow(N).
In particular, must know which terminals may follow a nullable nonterminal.
FOLLOW(N)
X Y
S P X t
S u
• Follow(X) contains t, due to S P X t
• hence Follow(P) contains t, since X is nullable
• in case P is also nullable, First (PXt) and hence First(S) contain t
• hence Follow(Y) contains t, due to X Y
Example (S start symbol):Y …
Y ε
P …
![Page 55: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/55.jpg)
55
Computation of Follow
• whenever the grammar contains a production X γY:
Let X and Y be nonterminals, and γ, γ1 strings of terminals and nonterminals.
UIfollow (X) follow(Y)
![Page 56: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/56.jpg)
56
Computation of Follow
• whenever the grammar contains a production X γY:
Let X and Y be nonterminals, and γ, γ1 strings of terminals and nonterminals.
• whenever the grammar contains a production X γ Y δ:
UIfollow (X) follow(Y) whenever δ is nullable
UIfirst (δ) follow(Y)
UIfollow (X) follow(Y)
![Page 57: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/57.jpg)
57
Computation of Follow
• whenever the grammar contains a production X γY:
Let X and Y be nonterminals, and γ, γ1 strings of terminals and nonterminals.
• whenever the grammar contains a production X γ Y δ:
UIfollow (X) follow(Y) whenever δ is nullable
UIfirst (δ) follow(Y)
UIfollow (X) follow(Y)
Iteratively visit grammar productions to compute nullable, first, follow
for each nonterminal in grammar.
![Page 58: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/58.jpg)
58
Computation of Follow
• whenever the grammar contains a production X γY:
Let X and Y be nonterminals, and γ, γ1 strings of terminals and nonterminals.
• whenever the grammar contains a production X γ Y δ:
UIfollow (X) follow(Y) whenever δ is nullable
UIfirst (δ) follow(Y)
UIfollow (X) follow(Y)
Iteratively visit grammar productions to compute nullable, first, follow
for each nonterminal in grammar.• start the iteration with empty first and follow sets and nullable=No
for all nonterminals and only add elements if forced to do so.
want smallest sets!
![Page 59: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/59.jpg)
59
Computation of Follow
• whenever the grammar contains a production X γY:
Let X and Y be nonterminals, and γ, γ1 strings of terminals and nonterminals.
• whenever the grammar contains a production X γ Y δ:
UIfollow (X) follow(Y) whenever δ is nullable
UIfirst (δ) follow(Y)
UIfollow (X) follow(Y)
Iteratively visit grammar productions to compute nullable, first, follow
for each nonterminal in grammar.• start the iteration with empty first and follow sets and nullable=No
for all nonterminals and only add elements if forced to do so.
• may need to visit some rules repeatedly. Order in which rules are
visited affects the number of iterations needed, but not the final result.
want smallest sets!
![Page 60: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/60.jpg)
Nullable, First, Follow: example
60
nullable first follow
X No
Y No
Z No
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rules
1, 3, 4, 6
![Page 61: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/61.jpg)
Nullable, First, Follow: example
61
nullable first follow
X No
Y No
Z No
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
nullable first follow
X No a(6)
Y Yes (3) c(4)
Z No d(1)
Visit rules
1, 3, 4, 6
Visit rule 5
![Page 62: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/62.jpg)
Nullable, First, Follow: example
62
nullable first follow
X No
Y No
Z No
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
nullable first follow
X No a(6)
Y Yes (3) c(4)
Z No d(1)
Visit rules
1, 3, 4, 6
nullable first follow
X Yes(5) a, c(5)
Y Yes c
Z No d
Visit rule 5
![Page 63: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/63.jpg)
Nullable, First, Follow: example
63
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rule 2
nullable first follow
X Yes a, c
Y Yes c
Z No d
…..
![Page 64: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/64.jpg)
Nullable, First, Follow: example
64
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rule 2
nullable first follow
X Yes a, c
Y Yes c
Z No d
…..
nullable first follow
X Yes a, c ?
Y Yes c ?
Z No d, a(2), c(2) ?
![Page 65: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/65.jpg)
Nullable, First, Follow: example
65
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rule 2
nullable first follow
X Yes a, c
Y Yes c
Z No d
…..
nullable first follow
X Yes a, c c (2)
Y Yes c ?
Z No d, a, c ?
![Page 66: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/66.jpg)
Nullable, First, Follow: example
66
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rule 2
nullable first follow
X Yes a, c
Y Yes c
Z No d
…..
nullable first follow
X Yes a, c c
Y Yes c a, c, d (2)
Z No d, a, c ?
![Page 67: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/67.jpg)
Nullable, First, Follow: example
67
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rule 2
nullable first follow
X Yes a, c
Y Yes c
Z No d
…..
nullable first follow
X Yes a, c c
Y Yes c a, c, d
Z No d, a, c ?
nullable first follow
X Yes a, c c, a(2), d(2)
Y Yes c a, c, d
Z No d, a, c ?
![Page 68: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/68.jpg)
Nullable, First, Follow: example
68
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
next rule?
…..
nullable first follow
X Yes a, c c, a, d
Y Yes c a, c, d
Z No d, a, c
![Page 69: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/69.jpg)
Nullable, First, Follow: example
69
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Visit rules
1-6
…..
nullable first follow
X Yes a, c c, a, d
Y Yes c a, c, d
Z No d, a, c
nullable first follow
X Yes a, c c, a, d
Y Yes c a, c, d
Z No d, a, c
(no change)
![Page 70: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/70.jpg)
a c d
X
Y
Z
Parse table extraction
70
• contains “action” for each nonterminal * terminal pair (N,t)
• predictive parsing: “action” is “rule to be applied” when seeing token
type t during execution of function for nonterminal N
• lookup attempt in blank cells indicates syntax error (phrase rejected)
![Page 71: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/71.jpg)
a c d
X
Y
Z
Parse table extraction
71
1. Add rule N γ in row N, column t whenever t occurs in First(γ).
Filling the table:
Motivation: seeing t when expecting N selects rule N γ.
![Page 72: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/72.jpg)
a c d
X
Y
Z
Parse table extraction
72
null. first follow
X Yes ac acd
Y Yes c acd
Z No acd
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
Add rule N γ in row N, column t whenever t occurs in First(γ).
![Page 73: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/73.jpg)
a c d
X
Y
Z
Parse table extraction
73
null. first follow
X Yes ac acd
Y Yes c acd
Z No acd
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
X a
Y c
Z dZ X Y Z Z X Y ZZ X Y Z
Add rule N γ in row N, column t whenever t occurs in First(γ).
first (XYZ) = {a,c,d}
![Page 74: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/74.jpg)
a c d
X
Y
Z
Parse table extraction
74
null. first follow
X Yes ac acd
Y Yes c acd
Z No acd
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
X a
Y c
Z dZ X Y Z Z X Y ZZ X Y Z
Add rule N γ in row N, column t whenever t occurs in First(γ).
2. If γ is nullable, add rule N γ in row N, column t whenever t in Follow(N).
Motivation: seeing t when expecting N selects rule N γ.
X Y
![Page 75: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/75.jpg)
a c d
X
Y
Z
Parse table extraction
75
null. first follow
X Yes ac acd
Y Yes c acd
Z No acd
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
X a
Y c
Z dZ X Y Z Z X Y ZZ X Y Z
Add rule N γ in row N, column t whenever t occurs in First(γ).
If γ is nullable, add rule N γ in row N, column t whenever t in Follow(N).
Y εY εY ε
X YX YX Y
![Page 76: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/76.jpg)
a c d
X
Y
Z
Parse table extraction
76
null. first follow
X Yes ac acd
Y Yes c acd
Z No acd
X Y
X a
Y ε
Y c
Z d
Z X Y Z
1
2
3
4
5
6
X a
Y c
Z dZ X Y Z Z X Y ZZ X Y Z
Add rule N γ in row N, column t whenever t occurs in First(γ).
If γ is nullable, add rule N γ in row N, column t whenever t in Follow(N).
Y εY εY ε
X YX YX Y
Observation: some cells contain more than one rule –
which one should be used???
![Page 77: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/77.jpg)
77
Predictive Parsing Table
aa .. dd
X
Y
Z
(k=2)
![Page 78: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/78.jpg)
78
Another Example
![Page 79: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/79.jpg)
79
Another Example
![Page 80: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/80.jpg)
80
Predictive Parsing Table
![Page 81: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/81.jpg)
81
Problem 1
![Page 82: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/82.jpg)
82
Problem 2
![Page 83: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/83.jpg)
83
Example (try at home)
![Page 84: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/84.jpg)
84
Example
![Page 85: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/85.jpg)
85
Limitation of Recursive Descent Parsing
Reminder: predictive parsing selects rule based on the next input token(s)
• LL(1): single next token
• LL(k): next k tokens
Sometimes, there’s no k that does the job.
![Page 86: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/86.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
86
Shift-Reduce Parsing
![Page 87: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/87.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
87
Shift-Reduce Parsing
![Page 88: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/88.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
88
Shift-Reduce Parsing
![Page 89: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/89.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a full RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
89
Shift-Reduce Parsing
![Page 90: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/90.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a full RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
90
Shift-Reduce Parsing
![Page 91: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/91.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a full RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
91
Shift-Reduce Parsing
![Page 92: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/92.jpg)
Idea: delay decision until all tokens of an RHS have been seen
• use a stack to remember (“shift”) symbols
• based on stack content and next symbol, select one of two actions:
• shift: push input token onto stack
• reduce: chose a production (X A B C); pop its RHS (C B A);
push its LHS (X). So can only reduce if we see a full RHS on top of stack.
• initial state: empty stack, parsing pointer at beginning of token stream
• shifting of end marker $: input stream has been parsed successfully.
• exhaustion of input stream with nonempty stack: parse fails
92
Shift-Reduce Parsing
• a.k.a. bottom-up parsing
• a.k.a LR(k): left-to-right parse, rightmost derivation, k-token lookahead
• shift-reduce parsing can parse more grammars than predictive parsing
![Page 93: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/93.jpg)
93
Shift-Reduce Parsing
![Page 94: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/94.jpg)
94
Example
![Page 95: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/95.jpg)
95
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 96: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/96.jpg)
96
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 97: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/97.jpg)
97
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 98: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/98.jpg)
98
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 99: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/99.jpg)
99
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 100: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/100.jpg)
100
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 101: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/101.jpg)
101
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 102: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/102.jpg)
102
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 103: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/103.jpg)
103
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 104: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/104.jpg)
104
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 105: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/105.jpg)
105
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 106: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/106.jpg)
106
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 107: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/107.jpg)
107
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 108: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/108.jpg)
108
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 109: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/109.jpg)
109
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 110: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/110.jpg)
110
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 111: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/111.jpg)
111
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
![Page 112: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/112.jpg)
112
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
i.e. shift (implicit) EOF
![Page 113: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/113.jpg)
113
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
i.e. shift (implicit) EOF
Next lecture: how to build the shift-reduce DFA, ie parser table
![Page 114: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/114.jpg)
114
Example 1. A S EOF
2. S ( L )
3. S id = num
4. L L; S
5. L S
i.e. shift (implicit) EOF
Next lecture: how to build the shift-reduce DFA, ie parser table
Again, duplicate entries in cells denote parse conflicts:
shift-reduce, reduce-reduce
![Page 115: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/115.jpg)
115
The Dangling Else Problem
![Page 116: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/116.jpg)
116
The Dangling Else Problem
Potential problem?
![Page 117: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/117.jpg)
117
The Dangling Else Problem
![Page 118: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/118.jpg)
118
The Dangling Else Problem
![Page 119: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/119.jpg)
119
The Dangling Else Problem
![Page 120: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/120.jpg)
• Construction of recursive-descent parse tables
• Shift-reduce parsing overcomes requirement to make decision which rule to apply before entire RHS is seen
• “dangling-else” as typical shift-reduce conflict
Summary
120
=
ID
. . .
=
NUM
ID
. . .
=
NUM
ID
. . .
shift(NUM) reduce 3
FirstNullable
Follow
![Page 121: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/121.jpg)
121
ML-YACC (Yet Another Compiler-Compiler)
![Page 122: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/122.jpg)
122
CFG Specification
Specification of a parser has three parts:
User Declarations
%%
ML-YACC-Definitions
%%
Rules
User declarations: definitions of values to be used in rules
ML-YACC Definitions:
• definitions of terminals and non-terminals
• precedence rules that help resolve shift-reduce conflicts
Rules:
• production rules of grammar
• semantic actions associated with reductions
![Page 123: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/123.jpg)
123
ML-YACC Declarations
![Page 124: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/124.jpg)
• nonterminal and terminal symbols are associated with “attribute values”
• for rule A γ, synthesize attribute for A from attribute for the
symbols in γ
• can be used to do simple calculation inside parser:
• requires type-correctness of synthesized attributes w.r.t. ML operators like +, based on association of types to symbols
• ML-YACC’s result of successful parse: attribute synthesized for start
symbol (unit value if no attribute given)
• other use: abstract syntax tree (topic of future lecture)
124
Attribute Grammar
exp exp PLUS exp (exp1 + exp2)
terminal symbol of grammar ML’s infix operator int -> int -> int
Semantic action:
synthesize attribute value
![Page 125: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/125.jpg)
125
Rules
![Page 126: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/126.jpg)
126
ML-YACC and Ambiguous Grammars
Remember: grammar is ambiguous if there are strings with > 1 parse trees
Example: expr expr – expr
expr expr * expr …vs.
Shift-reduce conflicts:
when parsing 4 – 5 * 6: eventually,
• top element of stack is E – E
• TIMES is current symbol
• can reduce using rule E E – E,
or can shift TIMES.
• shift preferred!
4 – 5 * 6 4 – 5 * 6
![Page 127: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/127.jpg)
127
ML-YACC and Ambiguous Grammars
Remember: grammar is ambiguous if there are strings with > 1 parse trees
Example: expr expr – expr
expr expr * expr …vs.
Shift-reduce conflicts:
when parsing 4 – 5 * 6: eventually,
• top element of stack is E – E
• TIMES is current symbol
• can reduce using rule E E – E,
or can shift TIMES.
• shift preferred!
when parsing 4 – 5 – 6: eventually,
• top element of stack is E – E
• MINUS is current symbol
• can reduce using rule E E – E,
or can shift MINUS.
• reduce preferred!
Also: reduce-reduce conflicts if multiple rules can be reduced
ML-YACC warns about conflicts, and applies default choice (see below)
4 – 5 * 6 4 – 5 * 6
![Page 128: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/128.jpg)
128
Directives
![Page 129: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/129.jpg)
129
Directives
![Page 130: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/130.jpg)
130
Precedence Examples
![Page 131: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/131.jpg)
131
Default Behavior
![Page 132: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/132.jpg)
132
Direct Rule Precedence Specification
![Page 133: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/133.jpg)
133
Syntax vs. Semantics
![Page 134: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/134.jpg)
134
Constructing LR parsing tables
![Page 135: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/135.jpg)
135
Shift-Reduce, Bottom Up, LR(1) Parsing
![Page 136: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/136.jpg)
136
LR(k)
![Page 137: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/137.jpg)
137
Shift Reduce Parsing DFA
![Page 138: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/138.jpg)
138
Parsing Algorithm
![Page 139: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/139.jpg)
139
LR(0) Parsing
Grammar 3.20 (book):
represents the initial parser state. Develop
other states step by step, by considering all
possible transitions:
1
. . .1. move cursor past x
2. move cursor past (
3. move cursor past S
![Page 140: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/140.jpg)
Moving the dot past a terminal t represents “shift t”.
140
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
x
![Page 141: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/141.jpg)
141
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
3
x
(
Moving the dot past a terminal t represents “shift t”.
![Page 142: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/142.jpg)
142
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
L . S
L . L , S
3
x
(
“shift (“. Don’t forget the closure!
![Page 143: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/143.jpg)
143
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
x
(
“shift (“. Don’t forget the closure! Yes, CLOSURE!
![Page 144: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/144.jpg)
144
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4S
x
(
Moving the dot past a nonterminal N represents “goto” after
a reduction for a rule with LHS N has been performed.
![Page 145: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/145.jpg)
145
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4S
x
(
(
S
x
L
In 3 , we have four transitions to consider. Let’s start with the terminals…
![Page 146: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/146.jpg)
146
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4S
x
(
(
S
x
L
Next, nonterminals S and L.
![Page 147: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/147.jpg)
147
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
S
x
(
(
S
x
L
![Page 148: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/148.jpg)
148
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
S
x
(
(
S
x
L
,
)
![Page 149: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/149.jpg)
149
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S ( L ) .
6
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
L L , . S
S . x
S . ( L )
8
S
x
(
(
S
x
L
,
)
Closure! In 8 , we have three transitions to consider. Terminals x and (…
![Page 150: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/150.jpg)
150
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S ( L ) .
6
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
L L , . S
S . x
S . ( L )
8
S
x
(
(
S
x
x
(
L
,
)
and nonterminal S…
![Page 151: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/151.jpg)
151
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S ( L ) .
6
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
L L , . S
S . x
S . ( L )
8
L L , S .
9
S
x
(
(
S
x
x
(
L
,
)
S
![Page 152: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/152.jpg)
152
LR(0) States S’ S $
S ( L )
S x
L S
L L , S
S ( L ) .
6
S’ . S $
S . ( L )
S . x
1
S x .
2
S ( L . )
L L . , S
5
S ( . L )
L . S
L . L , S
S . ( L )
S . x
3
S’ S . $
4
L S .
7
L L , . S
S . x
S . ( L )
8
L L , S .
9
S
x
(
(
S
x
x
(
L
,
)
S
Done. How can we extract the parsing table?
![Page 153: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/153.jpg)
153
LR(0) parsing table
( ) x , $ S L
1
2
3
4
5
6
7
8
9
![Page 154: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/154.jpg)
154
LR(0) parsing table
( ) x , $ S L
1
2
3
4
5
6
7
8
9
For each transition X Y
where t is a terminal, add
“shift-to Y” in cell (X, t).
t
![Page 155: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/155.jpg)
155
LR(0) parsing table
( ) x , $ S L
1 s3 s2
2
3 s3 s2
4
5 s6 s8
6
7
8 s3 s2
9
For each transition X Y
where t is a terminal, add
“shift-to Y” in cell (X, t).
t
![Page 156: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/156.jpg)
156
LR(0) parsing table
( ) x , $ S L
1 s3 s2
2
3 s3 s2
4
5 s6 s8
6
7
8 s3 s2
9
For each transition X Y
where N is a nonterminal, add
“goto Y” in cell (X, N).
N
![Page 157: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/157.jpg)
157
LR(0) parsing table
( ) x , $ S L
1 s3 s2 g4
2
3 s3 s2 g7 g5
4
5 s6 s8
6
7
8 s3 s2 g9
9
For each transition X Y
where N is a nonterminal, add
“goto Y” in cell (X, N).
N
![Page 158: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/158.jpg)
158
LR(0) parsing table
( ) x , $ S L
1 s3 s2 g4
2
3 s3 s2 g7 g5
4
5 s6 s8
6
7
8 s3 s2 g9
9
For each state X containing an
item S’ S . $ put “accept”
in cell (X, $).
![Page 159: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/159.jpg)
159
LR(0) parsing table
For each state X containing an
item S’ S . $ put “accept”
in cell (X, $).
( ) x , $ S L
1 s3 s2 g4
2
3 s3 s2 g7 g5
4 accept
5 s6 s8
6
7
8 s3 s2 g9
9
![Page 160: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/160.jpg)
160
LR(0) parsing table
( ) x , $ S L
1 s3 s2 g4
2
3 s3 s2 g7 g5
4 accept
5 s6 s8
6
7
8 s3 s2 g9
9
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
For each state X containing an
item N γ . (dot at end of
item for rule n) put “reduce n” in
all cells (X, t).
n
![Page 161: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/161.jpg)
161
LR(0) parsing table
( ) x , $ S L
1 s3 s2 g4
2 r2 r2 r2 r2 r2
3 s3 s2 g7 g5
4 accept
5 s6 s8
6 r1 r1 r1 r1 r1
7 r3 r3 r3 r3 r3
8 s3 s2 g9
9 r4 r4 r4 r4 r4
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
For each state X containing an
item N γ . (dot at end of
item for rule n) put “reduce n” in
all cells (X, t).
n
![Page 162: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/162.jpg)
162
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $
![Page 163: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/163.jpg)
163
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $
![Page 164: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/164.jpg)
164
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $
![Page 165: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/165.jpg)
165
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S? , x) $
![Page 166: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/166.jpg)
166
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $
![Page 167: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/167.jpg)
167
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L? , x) $
![Page 168: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/168.jpg)
168
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $
![Page 169: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/169.jpg)
169
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $
![Page 170: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/170.jpg)
170
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $
![Page 171: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/171.jpg)
171
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S? ) $
![Page 172: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/172.jpg)
172
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $
![Page 173: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/173.jpg)
173
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L? ) $
![Page 174: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/174.jpg)
174
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L5 ) $
![Page 175: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/175.jpg)
175
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L5 ) $ Shift 6
1 (3 L5 )6 $
![Page 176: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/176.jpg)
176
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L5 ) $ Shift 6
1 (3 L5 )6 $ Reduce 1
1 S? $
![Page 177: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/177.jpg)
177
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L5 ) $ Shift 6
1 (3 L5 )6 $ Reduce 1
1 S4 $
![Page 178: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/178.jpg)
178
LR(0) parsing table
S’ S $S ( L )
S xL S
L L , S0
3
2 4
1
No cell with >1 action => LR(0)
How to use the table: parse (x, x)
Stack Input Action
1 (x, x) $ Shift 3
1 (3 x, x) $ Shift 2
1 (3 x2 , x) $ Reduce 2
1 (3 S7 , x) $ Reduce 3
1 (3 L5 , x) $ Shift 8
1 (3 L5 ,8 x) $ Shift 2
1 (3 L5 ,8 x2 ) $ Reduce 2
1 (3 L5 ,8 S9 ) $ Reduce 4
1 (3 L5 ) $ Shift 6
1 (3 L5 )6 $ Reduce 1
1 S4 $ Accept
![Page 179: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/179.jpg)
179
Another Example (3.23 in book)
S E $E T + E
E TT x
0
3
2
1Do on blackboard
![Page 180: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/180.jpg)
180
Another Example (3.23 in book)
S E $E T + E
E TT x
0
3
2
1
T x .
5
E T . + E
E T .
3
S E . $
2
E T + E .
6
x
S . E $
E . T + E
E . T
T . x
1
E T + . E
E . T + E
E . T
T . x
4
x
E
E
T+
T x + $ E T
1 s5 g2 g3
2 accept
3 r2 s4, r2 r2
4 s5 g6 g3
5 r3 r3 r3
6 r1 r1 r1
Duplicate entry – grammar is not LR(0)!
![Page 181: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/181.jpg)
181
Another Example - SLR
Compare to LR(0) rule for filling parse table:
![Page 182: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/182.jpg)
S E $E T + E
E TT x
0
3
2
1
182
Another Example – SLR
nullable first follow
S ? ? ?
E ? ? ?
T ? ? ?
![Page 183: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/183.jpg)
S E $E T + E
E TT x
0
3
2
1
183
Another Example – SLR
x + $ E T
1 s5 g2 g3
2 accept
3 s4 r2
4 s5 g6 g3
5 r3 r3
6 r1
nullable first follow
S No x
E No x $
T No x + $
No conflicts – grammar is SLR!
![Page 184: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/184.jpg)
184
Yet Another Example
Convince yourself
at home!
![Page 185: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/185.jpg)
185
LR(1) Parsing
![Page 186: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/186.jpg)
186
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?
1
Initial item as in LR(0); lookahead ? arbitrary
since first($z) = { $ } for all z.
![Page 187: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/187.jpg)
187
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $V . * E $
1
Closure similar to LR(0), but with lookahead $.
![Page 188: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/188.jpg)
188
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S
*
V
E
x
Repeated closure yields two new items, with new lookaheads.
![Page 189: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/189.jpg)
189
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S E . $
5
V x . $,=
8
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
S
*
V
E
x
Transition shared by items from different rules.
![Page 190: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/190.jpg)
190
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S E . $
5
V x . $,=
8
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
S
*
V
E
x
![Page 191: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/191.jpg)
191
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S E . $
5
V x . $,=
8
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
S
*
V
E
x
![Page 192: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/192.jpg)
192
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S E . $
5
V x . $,=
8
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
S
*
V
E
x
Closure yields second item for same
rule, but different cursor position.
![Page 193: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/193.jpg)
193
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S V = . E $E . V $V . x $
V . * E $
4
S E . $
5
V x . $,=
8
V * E . $,=
10
E V . $,=
12
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
S
*
*
V
E
x
x
V
E
=
![Page 194: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/194.jpg)
194
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S V = . E $E . V $V . x $
V . * E $
4
S E . $
5
E V . $
7
V x . $,=
8
S V = E . $
9
V * E . $,=
10
V x . $
11
E V . $,=
12
E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
E . V $
V . x $V . * E $
13
V * . E $
S
*
*
V
E
x
x
V
E
=
V
xE
*
![Page 195: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/195.jpg)
195
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S V = . E $E . V $V . x $
V . * E $
4
S E . $
5
E V . $
7
V x . $,=
8
S V = E . $
9
V * E . $,=
10
V x . $
11
E V . $,=
12
V * E . $
14E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
E . V $
V . x $V . * E $
13
V * . E $
S
*
*
V
E
x
x
V
E
=
V
x
V
x
E
*
*
E
![Page 196: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/196.jpg)
196
LR(1) example S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
S’ . S $ ?S . V = E $S . E $E . V $V . x $,=V . * E $,=
1
S’ S . $ ?
2
S V . = E $
E V . $
3
S V = . E $E . V $V . x $
V . * E $
4
S E . $
5
E V . $
7
V x . $,=
8
S V = E . $
9
V * E . $,=
10
V x . $
11
E V . $,=
12
V * E . $
14E . V $,=V . x $,=V . * E $,=
6
V * . E $,=
E . V $
V . x $V . * E $
13
V * . E $
S
*
*
V
E
x
x
V
E
=
V
x
V
x
E
*
*
E
![Page 197: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/197.jpg)
197
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6
2
3 s4
4 s11 s13
5
6 s8 s6
7
8
9
10
11
12
13 s11 s13
14
shift as before: when dot is
before terminal, ie transition
marked by terminal.
![Page 198: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/198.jpg)
198
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6 g2 g5 g3
2
3 s4
4 s11 s13 g9 g7
5
6 s8 s6 g10 g12
7
8
9
10
11
12
13 s11 s13 g14 g7
14
goto as before: when dot is
before nonterminal, ie transition
marked by nonterminal.
![Page 199: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/199.jpg)
199
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6 g2 g5 g3
2 acc
3 s4
4 s11 s13 g9 g7
5
6 s8 s6 g10 g12
7
8
9
10
11
12
13 s11 s13 g14 g7
14
accept as before: when dot is
before $.
![Page 200: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/200.jpg)
200
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6 g2 g5 g3
2 acc
3 s4 r3
4 s11 s13 g9 g7
5
6 s8 s6 g10 g12
7
8
9
10
11
12
13 s11 s13 g14 g7
14
reduce n: when dot is at end of
item for rule n with lookahead t
in state X, add reduce n in cell
(X, t).
![Page 201: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/201.jpg)
201
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6 g2 g5 g3
2 acc
3 s4 r3
4 s11 s13 g9 g7
5 r2
6 s8 s6 g10 g12
7 r3
8 r4 r4
9 r1
10 r5 r5
11 r4
12 r3 r3
13 s11 s13 g14 g7
14 r5
reduce n: when dot is at end of
item for rule n with lookahead t
in state X, and add reduce n in
cell (X, t).
![Page 202: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/202.jpg)
202
LR(1) parsing table S’ S $S V = E
S EE V
V x5
V * E
0
3
2 4
1
x * = $ S E V
1 s8 s6 g2 g5 g3
2 acc
3 s4 r3
4 s11 s13 g9 g7
5 r2
6 s8 s6 g10 g12
7 r3
8 r4 r4
9 r1
10 r5 r5
11 r4
12 r3 r3
13 s11 s13 g14 g7
14 r5
No duplicate entries Grammar is LR(1)!
![Page 203: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/203.jpg)
203
LALR(1)
s11=s8 s13=s6s12=s7 s14=s10
x * = $ S E V
1 s8 s6 g2 g5 g3
2 acc
3 s4 r3
4 s8 s6 g9 g7
5 r2
6 s8 s6 g10
g7
7 r3 r3
8 r4 r4
9 r1
10 r5 r5
![Page 204: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/204.jpg)
204
Parsing Power
![Page 205: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/205.jpg)
205
Parsing Error Recovery
![Page 206: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/206.jpg)
206
Error Recovery
![Page 207: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/207.jpg)
207
LL Local Error Recovery
![Page 208: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/208.jpg)
208
LL Local Error Recovery
![Page 209: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/209.jpg)
209
LR Local Error Recovery
E IDE E + E
E ( ES )ES E
ES ES ; EExample:
• match a sequence of erroneous input tokens using the error token – a fresh terminal
• preferable: have error followed with synchronizing lookahead token, like
RPAREN and SEMI here, by adding rules like this:
(alternative: add but that does not allow us to skip ahead to RPAREN, SEMI)
E (error) ES error ; E
![Page 210: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/210.jpg)
210
LR Local Error Recovery
E IDE E + E
E ( ES )ES E
ES ES ; EExample:
• match a sequence of erroneous input tokens using the error token – a fresh terminal
• preferable: have error followed with synchronizing lookahead token, like
RPAREN and SEMI here, by adding rules like this:
(alternative: add but that does not allow us to skip ahead to RPAREN, SEMI)
• build parse table for the extended grammar
E (error) ES error ; E
![Page 211: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/211.jpg)
211
LR Local Error Recovery
E IDE E + E
E ( ES )ES E
ES ES ; EExample:
• match a sequence of erroneous input tokens using the error token – a fresh terminal
• preferable: have error followed with synchronizing lookahead token, like
RPAREN and SEMI here, by adding rules like this:
(alternative: add but that does not allow us to skip ahead to RPAREN, SEMI)
• build parse table for the extended grammar
• LR parse engine: when trying to read from empty cell,
1. pop the stack until a state is reached in which the action for error is shift
2. do the shift action
E (error) ES error ; E
![Page 212: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/212.jpg)
212
LR Local Error Recovery
E IDE E + E
E ( ES )ES E
ES ES ; EExample:
• match a sequence of erroneous input tokens using the error token – a fresh terminal
• preferable: have error followed with synchronizing lookahead token, like
RPAREN and SEMI here, by adding rules like this:
(alternative: add but that does not allow us to skip ahead to RPAREN, SEMI)
• build parse table for the extended grammar
• LR parse engine: when trying to read from empty cell,
1. pop the stack until a state is reached in which the action for error is shift
2. do the shift action
3. discard the input symbols (if necessary) until a state is reached that has a proper
shift/goto/reduce/accept action in the current state (in case we have indeed
synchronizing lookahead, this will be a shift action for one of the lookaheads)
4. resume normal parsing
E (error) ES error ; E
![Page 213: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/213.jpg)
213
Global Error Recovery
![Page 214: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/214.jpg)
214
Burke-Fisher
![Page 215: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/215.jpg)
215
Burke-Fisher
![Page 216: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/216.jpg)
216
Burke-Fisher Example
![Page 217: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/217.jpg)
217
Burke-Fisher
![Page 218: Topic 3: Parsing and Yaccing - cs.princeton.eduLimitation of Recursive Descent Parsing Need algorithm that • decides if a grammar is suitable for recursive descent parsing, and •](https://reader034.fdocuments.net/reader034/viewer/2022052014/602b40f49e5fe157290b3ef3/html5/thumbnails/218.jpg)
218
Burke-Fisher in ML-YACC