top-down parsing - stanford university...our first top-down algorithm breadth-first search maintain...

359
Top-Down Parsing

Upload: others

Post on 11-Mar-2020

13 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Top-DownParsing

Page 2: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Announcements

● Office hours schedule posted on Piazza.● Keith:

– Monday/Tuesday, 2PM – 4PM in Gates 178.● Jinchao:

– Wednesday/Thursday, 6PM – 8PM in Gates B26.

● Feel free to email us with questions!● Sign up for Piazza (www.piazza.com).

Page 3: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Where We Are

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 4: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Review from Last Time

● Goal of syntax analysis: recover the intended structure of the program.

● Idea: Use a context-free grammar to describe the programming language.

● Given a sequence of tokens, look for a parse tree that generates those tokens.

● Recovering this syntax tree is called parsing and is the topic of this week (and part of next!)

Page 5: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Different Types of Parsing

● Top-Down Parsing (Today / Friday)● Beginning with the start symbol, try to guess

the productions to apply to end up at the user's program.

● Bottom-Up Parsing (Friday / Monday)● Beginning with the user's program, try to

apply productions in reverse to convert the program back into the start symbol.

Page 6: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Top-Down Parsing

E → TE → T + ET → intT → (E)

int + ( int + int )

T

E

E

T

T

T

E

E

Page 7: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Challenges in Top-Down Parsing

● Top-down parsing begins with virtually no information.● Begins with just the start symbol, which matches

every program.

● How can we know which productions to apply?● In general, we can't.

● There are some grammars for which the best we can do is guess and backtrack if we're wrong.

● If we have to guess, how do we do it?

Page 8: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Parsing as a Search

● An idea: treat parsing as a graph search.

● Each node is a sentential form (a string of terminals and nonterminals derivable from the start symbol).

● There is an edge from node α to node β iff α ⇒ β.

Page 9: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Parsing as a SearchE → TE → T + ET → intT → (E)

E

T

T + E

int

int + E

T + T

T + T + E

(E)

int + T

T + (E)

int + (E)

(T)

(T + E)

…int + T + E

int + int

……

Page 10: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Parsing as a SearchE → TE → T + ET → intT → (E)

E

T

T + E

int

int + E

T + T

T + T + E

(E)

int + T

T + (E)

int + (E)

(T)

(T + E)

…int + T + E

int + int

……

Page 11: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Our First Top-Down Algorithm

● Breadth-First Search● Maintain a worklist of sentential forms, initially

just the start symbol S.● While the worklist isn't empty:

● Remove an element from the worklist.● If it matches the target string, you're done.● Otherwise, for each possible string that can be

derived in one step, add that string to the worklist.

● Can recover a parse tree by tracking what productions we applied at each step.

Page 12: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

Page 13: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

E

Page 14: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

E

Page 15: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

E

T T + E

Page 16: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

E

T T + E

T T + E

Page 17: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T T + E

Page 18: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

T

Page 19: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

T

int (E)

Page 20: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

T

int (E)

int (E)

Page 21: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E int (E)

Page 22: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int (E)

T + E

Page 23: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int (E)

T + E

int + E (E) + E T + T T + T + E

Page 24: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int (E)

T + E

int + E (E) + E T + T T + T + E

int + E (E) + E T + T

T + T + E

Page 25: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int (E) int + E (E) + E T + T

T + T + E

Page 26: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) int + E (E) + E T + T

T + T + E

int

Page 27: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) int + E (E) + E T + T

T + T + E

Page 28: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E (E) + E T + T T + T + E

(E)

Page 29: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E (E) + E T + T T + T + E

(E)

(T) (T + E)

Page 30: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E (E) + E T + T T + T + E

(E)

(T) (T + E)

(T)(T + E)

Page 31: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E (E) + E T + T T + T + E

(T)(T + E)

Page 32: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) + E T + T T + T + E (T)

(T + E)

int + E

Page 33: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) + E T + T T + T + E (T)

(T + E)

int + E

int + T int + T + E

Page 34: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) + E T + T T + T + E (T)

(T + E)

int + E

int + T int + T + E

int + Tint + T + E

Page 35: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(E) + E T + T T + T + E (T)

(T + E)int + Tint + T + E

Page 36: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T T + T + E (T) (T + E)

int + Tint + T + E

(E) + E

Page 37: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T T + T + E (T) (T + E)

int + Tint + T + E

(E) + E

(T) + E (T + E) + E (E) + T (E) + T + E

Page 38: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T T + T + E (T) (T + E)

int + Tint + T + E

(E) + E

(T) + E (T + E) + E (E) + T (E) + T + E

(T) + E(T + E) + E

(E) + T (E) + T + E

Page 39: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T T + T + E (T) (T + E)

int + Tint + T + E(T) + E(T + E) + E

(E) + T (E) + T + E

Page 40: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T + E (T) (T + E) int + T

int + T + E(T) + E(T + E) + E(E) + T

(E) + T + E

T + T

Page 41: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T + E (T) (T + E) int + T

int + T + E(T) + E(T + E) + E(E) + T

(E) + T + E

T + T

int + T (E) + T T + (E)T + int

Page 42: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T + E (T) (T + E) int + T

int + T + E(T) + E(T + E) + E(E) + T

(E) + T + E

T + T

int + T (E) + T T + (E)T + int

int + T (E) + T T + int T + (E)

Page 43: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

T + T + E (T) (T + E) int + T

int + T + E(T) + E(T + E) + E(E) + T

(E) + T + E int + T (E) + T T + int T + (E)

Page 44: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(T) (T + E) int + T int + T + E

(T) + E(T + E) + E(E) + T(E) + T + Eint + T

(E) + T T + int T + (E)

T + T + E

Page 45: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(T) (T + E) int + T int + T + E

(T) + E(T + E) + E(E) + T(E) + T + Eint + T

(E) + T T + int T + (E)

T + T + E

int + T + E (E) + T + E

T + int + ET + (E) + ET + T + TT + T + T + E

Page 46: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(T) (T + E) int + T int + T + E

(T) + E(T + E) + E(E) + T(E) + T + Eint + T

(E) + T T + int T + (E) int + T + E (E) + T + E

T + int + ET + (E) + ET + T + TT + T + T + E

Page 47: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Breadth-First Search Parsing

Worklist

int + int

E → TE → T + ET → intT → (E)

(T) (T + E) int + T int + T + E

(T) + E(T + E) + E(E) + T(E) + T + Eint + T

(E) + T T + int T + (E) int + T + E (E) + T + E

T + int + ET + (E) + ET + T + TT + T + T + E

Page 48: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

BFS is Slow

● Enormous time and memory usage:● Lots of wasted effort:

– Generates a lot of sentential forms that couldn't possibly match.

– But in general, extremely hard to tell whether a sentential form can match – that's the job of parsing!

● High branching factor:– Each sentential form can expand in (potentially)

many ways for each nonterminal it contains.

Page 49: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Reducing Wasted Effort

● Suppose we're trying to match a string γ.● Suppose we have a sentential form τ = αω,

where α is a string of terminals and ω is a string of terminals and nonterminals.

● If α isn't a prefix of γ, then no string derived from τ can ever match γ.

● If we can find a way to try to get a prefix of terminals at the front of our sentential forms, then we can start pruning out impossible options.

Page 50: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Reducing the Branching Factor

● If a string has many nonterminals in it, the branching factor can be high.● Sum of the number of productions of each

nonterminal involved.

● If we can restrict which productions we apply, we can keep the branching factor lower.

Page 51: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost Derivations

● Recall: A leftmost derivation is one where we always expand the leftmost symbol first.

● Updated algorithm:● Do a breadth-first search, only considering

leftmost derivations.– Dramatically drops branching factor.– Increases likelihood that we get a prefix of nonterminals.

● Prune sentential forms that can't possibly match.– Avoids wasted effort.

Page 52: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

E

Page 53: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E

E → TE → T + ET → intT → (E)

E

Page 54: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E

E → TE → T + ET → intT → (E)

T T + E

E

Page 55: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

E

T T + E

E

T T + E

Page 56: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist T T + E

Page 57: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

T

Worklist T + E

T

Page 58: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

T

int (E)

Worklist T + E

T

Page 59: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

T

int (E)

T

Page 60: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

Page 61: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + ET + E

Page 62: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

int + E (E) + E

T + E

Page 63: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

int + E (E) + E

T + E

Page 64: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

T + E

int + E (E) + E

T + E

int + E

Page 65: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + E

Page 66: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + Eint + E

Page 67: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E

int + T int + T + E

int + E

Page 68: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + E

int + T int + T + E

int + E

int + T int + T + E

int + E

int + T int + T + E

Page 69: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + T int + T + E

Page 70: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + Tint + T

int + T + E

Page 71: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

int + T

int + int int + (E)

int + T

Worklist int + T + E

Page 72: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + T + E

int + T

int + int int + (E)

int + T

Page 73: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

int + T

int + int int + (E)

int + T

Worklist int + T + E int + int

Page 74: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + T + E int + int

Page 75: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

int + T + E

Worklist int + int

int + T + E

Page 76: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

int + T + E

int + int + E int + (E) + E

Worklist int + int

int + T + E

Page 77: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + int

int + T + E

int + int + E int + (E) + E

int + T + E

Page 78: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

int + int

E → TE → T + ET → intT → (E)

Worklist int + int

Page 79: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + int

Worklist

int + int

Page 80: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

Worklist

int + int

E → TE → T + ET → intT → (E)

int + int

Worklist

int + int

Page 81: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS

● Substantial improvement over naïve algorithm.

● Will always find a valid parse of a program if one exists.

● Can easily be modified to find if a program can't be parsed.

● But, there are still problems.

Page 82: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

A → Aa | Ab | c

Page 83: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Page 84: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

A

Page 85: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

A

Page 86: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

A

Aa Ab c

Page 87: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

A

Aa Ab c

Page 88: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

A

Aa Ab c

Aa Ab

Page 89: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aa Ab

Page 90: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aa Ab

Page 91: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aa Ab

Page 92: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Ab

Aa

Page 93: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Ab

Aa

AbaAaa ca

Page 94: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Ab

Aa

AbaAaa ca

Page 95: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Ab

Aa

AbaAaa ca

Aaa Aba

Page 96: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Ab Aaa Aba

Page 97: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba

Ab

Page 98: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba

Ab

AbbAab cb

Page 99: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba

Ab

AbbAab cb

Page 100: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba

Ab

AbbAab cb

Aab Abb

Page 101: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba Aab Abb

Page 102: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost BFS Has Problems

Worklist

caaaaaaaaaa

A → Aa | Ab | c

Aaa Aba Aab Abb

Page 103: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Problems with Leftmost BFS

● Grammars like this can make parsing take exponential time.

● Also uses exponential memory.● What if we search the graph with a

different algorithm?

Page 104: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

● Idea: Use depth-first search.● Advantages:

● Lower memory usage: Only considers one branch at a time.

● High performance: On many grammars, runs very quickly.

● Easy to implement: Can be written as a set of mutually recursive functions.

Page 105: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

E → TE → T + ET → intT → (E)

Page 106: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

E → TE → T + ET → intT → (E)

Page 107: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

E

E → TE → T + ET → intT → (E)

Page 108: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET

E → TE → T + ET → intT → (E)

Page 109: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ETint

E → TE → T + ET → intT → (E)

Page 110: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ETint

E → TE → T + ET → intT → (E)

Page 111: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET

E → TE → T + ET → intT → (E)

Page 112: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET(E)

E → TE → T + ET → intT → (E)

Page 113: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET(E)

E → TE → T + ET → intT → (E)

Page 114: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET

E → TE → T + ET → intT → (E)

Page 115: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET

E → TE → T + ET → intT → (E)

Page 116: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

E

E → TE → T + ET → intT → (E)

Page 117: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET + E

E → TE → T + ET → intT → (E)

Page 118: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET + Eint + E

E → TE → T + ET → intT → (E)

Page 119: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET + Eint + Eint + T

E → TE → T + ET → intT → (E)

Page 120: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET + Eint + Eint + Tint + int

E → TE → T + ET → intT → (E)

Page 121: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Leftmost DFS

int + int

ET + Eint + Eint + Tint + int

E → TE → T + ET → intT → (E)

Page 122: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Problems with Leftmost DFS

A → Aa | c

c

AAaAaaAaaaAaaaa

Page 123: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Left Recursion

● A nonterminal A is said to be left-recursive iff

A ⇒* Aω

for some string ω.● Leftmost DFS may fail on left-recursive

grammars.● Fortunately, in many cases it is possible to

eliminate left recursion (see Handout 08 for details).

Page 124: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Summary of Leftmost BFS/DFS

● Leftmost BFS works on all grammars.

● Worst-case runtime is exponential.

● Worst-case memory usage is exponential.

● Rarely used in practice.

● Leftmost DFS works on grammars without left recursion.

● Worst-case runtime is exponential.

● Worst-case memory usage is linear.

● Often used in a limited form as recursive descent.

Page 125: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

Page 126: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

● The leftmost DFS/BFS algorithms are backtracking algorithms.● Guess which production to use, then back up

if it doesn't work.● Try to match a prefix by sheer dumb luck.

● There is another class of parsing algorithms called predictive algorithms.● Based on remaining input, predict (without

backtracking) which production to use.

Page 127: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Tradeoffs in Prediction

● Predictive parsers are fast.● Many predictive algorithms can be made to

run in linear time.● Often can be table-driven for extra

performance.

● Predictive parsers are weak.● Not all grammars can be accepted by

predictive parsers.

● Trade expressiveness for speed.

Page 128: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Exploiting Lookahead

● Given just the start symbol, how do you know which productions to use to get to the input program?

● Idea: Use lookahead tokens.● When trying to decide which production

to use, look at some number of tokens of the input to help make the decision.

Page 129: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Implementing Predictive Parsing

● Predictive parsing is only possible if we can predict which production to use given some number of lookahead tokens.

● Increasing the number of lookahead tokens increases the number of grammars we can parse, but complicates the parser.

● Decreasing the number of lookahead tokens decreases the number of grammars we can parse, but simplifies the parser.

Page 130: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

int + ( int + int )

Page 131: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

Page 132: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

Page 133: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

T + E

Page 134: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

T + E

Page 135: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

T + E

Page 136: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

int + ( int + int )

int + ET + E

Page 137: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

T + E

Page 138: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

T + E

Page 139: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

T + E

Page 140: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

T + E

int + T

Page 141: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ int + int )

int + E

int

T + E

int + T

(

Page 142: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

T + E

int + T

Page 143: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

T + E

int + T

Page 144: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

T + E

int + T

Page 145: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

T + E

int + T

Page 146: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

T + E

int + T

Page 147: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)int + (T + E)

T + E

int + T

Page 148: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)int + (T + E)

T + E

int + T

Page 149: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

T + E

int + T

int + (T + E)

Page 150: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

int + (int + E)

T + E

int + T

int + (T + E)

Page 151: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int + int )

int + E

int

int + (E)

int + (int + E)

T + E

int + T

int + (T + E)

Page 152: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int + int )int

E

int + E

int + (E)

int + (int + E)

T + E

int + T

int + (T + E)

Page 153: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int + int )int

E

int + E

int + (E)

int + (int + E)

T + E

int + T

int + (T + E)

Page 154: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int + int )int

E

int + E

int + (E)

int + (int + E)int + (int + T)

T + E

int + T

int + (T + E)

Page 155: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int +int int )

E

int + E

int + (E)

int + (int + E)int + (int + T)

T + E

int + T

int + (T + E)

Page 156: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int +

int + E

int

int + (E)

int + (int + E)

int + (int + T)

int )

E

int + E

int + (E)

int + (int + E)int + (int + T)

T + E

int + T

int + (T + E)

Page 157: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int +int int )

E

int + E

int + (E)

int + (int + E)

int + (int + int)int + (int + T)

T + E

int + T

int + (T + E)

Page 158: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int +int int )

E

int + E

int + (E)

int + (int + E)

int + (int + int)int + (int + T)

T + E

int + T

int + (T + E)

Page 159: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive Parsing

E → TE → T + ET → intT → (E)

+ ( int +int int )

E

int + E

int + (E)

int + (int + E)

int + (int + int)int + (int + T)

T + E

int + T

int + (T + E)

Page 160: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Predictive ParsingE

E → TE → T + ET → intT → (E)

+ ( int +

int + E

int

int + (E)

int + (int + E)

int + (int + int)

int )

int + (int + T)

T + E

int + T

int + (T + E)

Page 161: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

A Simple Predictive Parser: LL(1)

● Top-down, predictive parsing:● L: Left-to-right scan of the tokens● L: Leftmost derivation.● (1): One token of lookahead

● Construct a leftmost derivation for the sequence of tokens.

● When expanding a nonterminal, we predict the production to use by looking at the next token of the input. The decision is forced.

Page 162: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Parse Tables

Page 163: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Parse Tables

E → intE → (E Op E)Op → +Op → *

Page 164: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Parse Tables

E → intE → (E Op E)Op → +Op → *

int ( ) + *

E

Op

int (E Op E)

*+

Page 165: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))

LL(1) Parsing

Page 166: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))E

LL(1) Parsing

Page 167: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))E

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 168: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$E$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 169: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$E$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

The $ symbol is the end-of-input marker and is used by the parser to detect when we have reached the end of the input. It is not

a part of the grammar.

The $ symbol is the end-of-input marker and is used by the parser to detect when we have reached the end of the input. It is not

a part of the grammar.

Page 170: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$E$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 171: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$E$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

The first symbol of our guess is a nonterminal. We then look at our

parsing table to see what production to use.

This is called a predict step.

The first symbol of our guess is a nonterminal. We then look at our

parsing table to see what production to use.

This is called a predict step.

Page 172: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$E$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 173: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$E$

(E Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 174: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$E$

(E Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

The first symbol of our guess is now a terminal symbol. We thus match it against the first symbol

of the string to parse.

This is called a match step.

The first symbol of our guess is now a terminal symbol. We thus match it against the first symbol

of the string to parse.

This is called a match step.

Page 175: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

E$(E Op E)$E Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 176: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

E$(E Op E)$E Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 177: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

E$(E Op E)$E Op E)$int Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 178: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 179: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 180: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 181: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 182: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 183: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 184: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 185: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 186: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 187: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 188: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 189: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 190: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 191: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 192: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$int))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 193: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

int))$

))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$int))$))$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 194: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

int))$

))$

)$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$int))$))$)$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 195: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

int))$

))$

)$

$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$int))$))$)$$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 196: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

(int + (int * int))$

(int + (int * int))$

int + (int * int))$

int + (int * int))$

+ (int * int))$

+ (int * int))$

(int * int))$

(int * int))$

int * int))$

int * int))$int * int))$

* int))$

* int))$

int))$

int))$

))$

)$

$

E$(E Op E)$E Op E)$int Op E)$

Op E)$+ E)$E)$

(E Op E))$E Op E))$int Op E))$

Op E))$* E))$E))$int))$))$)$$

int ( ) + *

E

Op

1 2

43

LL(1) Parsing

Page 197: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 198: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$E$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 199: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$E$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 200: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$

int + int$

E$int $

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 201: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$

int + int$

+ int$

E$int $

$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 202: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$

int + int$

+ int$

E$int $

$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 203: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

int + int$

int + int$

+ int$

E$int $

$

LL(1) Error Detection

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 204: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 205: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$E$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 206: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$E$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 207: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

E$(E Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 208: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

E$(E Op E)$E Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 209: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

E$(E Op E)$E Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 210: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

int (int))$

E$(E Op E)$E Op E)$int Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 211: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

int (int))$

(int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 212: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

int (int))$

(int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 213: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

(int (int))$

(int (int))$

int (int))$

int (int))$

(int))$

E$(E Op E)$E Op E)$int Op E)$

Op E)$

LL(1) Error Detection, Part II

(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *

int ( ) + *

E

Op

1 2

43

Page 214: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

The LL(1) Algorithm

● Suppose a grammar has start symbol S and LL(1) parsing table T. We want to parse string ω

● Initialize a stack containing S$.

● Repeat until the stack is empty:● Let the next character of ω be t.● If the top of the stack is a terminal r:

– If r and t don't match, report an error.

– Otherwise consume the character t and pop r from the stack.

● Otherwise, the top of the stack is a nonterminal A:– If T[A, t] is undefined, report an error.

– Replace the top of the stack with T[A, t].

Page 215: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

A Simple LL(1) Grammar

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 216: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

A Simple LL(1) Grammar

id -> id;

while not zero? id do --id;

if not zero? id then if not zero? id then constant -> id;

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 217: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=→ ;while do

Page 218: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=→ ;while do

Page 219: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

9 10

→ ;while do

Page 220: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

9 10

→ ;while do

Page 221: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

5 6 7 8

9 10

→ ;while do

Page 222: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

5 6 7 8

9 10

→ ;while do

Page 223: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

5 6 7 8 4 4

9 10

→ ;while do

Page 224: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

5 6 7 8 4 4

9 10

→ ;while do

Page 225: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 226: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 227: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 228: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 229: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 230: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5)

| not EXPR (6) | ++ id (7)

| -- id (8)

TERM → id (9) | constant (10)

Constructing LL(1) Parse Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

Page 231: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Can we find an algorithm forconstructing LL(1) parse tables?

Page 232: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Filling in Table Entries

● Intuition: The next character should uniquely identify a production, so we should pick a production that ultimately starts with that character.

● T[A, t] should be a production A → ω iffω derives something starting with t.

● More rigorously:

T[A, t] = Bω iff A → ω and ω ⇒* tω'

Page 233: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

In what follows, assume that our grammar does not contain any ε-productions.

(We'll relax this restiction later.)

Page 234: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets

● We want to tell if a particular nonterminal A derives a string starting with a particular nonterminal t.

● We can formalize this with FIRST sets.

FIRST(A) = { t | A ⇒* tω for some ω }

● Intuitively, FIRST(A) is the set of terminals that can be at the start of a string produced by A.

● If we can compute FIRST sets for all nonterminals in a grammar, we can efficiently construct the LL(1) parsing table. Details soon.

Page 235: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Computing FIRST Sets

● Initially, for all nonterminals A, set

FIRST(A) = { t | A → tω for some ω }

● Then, repeat the following until no changes occur: For each nonterminal A, for each production A → Bω, set

FIRST(A) = FIRST(A) ∪ FIRST(B) ● This is known a fixed-point iteration or

a transitive closure algorithm.

Page 236: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 237: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERMSTMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 238: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhile

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 239: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhile

zero?not++--

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 240: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhile

zero?not++--

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 241: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhile

zero?not++--

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 242: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 243: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 244: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 245: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--id

constant

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 246: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--id

constant

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 247: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--

zero?not++--id

constant

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 248: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--id

constant

zero?not++--id

constant

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 249: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Iterative FIRST Computations

STMT EXPR TERM

ifwhilezero?not++--id

constant

zero?not++--id

constant

idconstant

STMT → if EXPR then STMT | while EXPR do STMT | EXPR ;

EXPR → TERM -> id | zero? TERM | not EXPR | ++ id | -- id

TERM → id | constant

Page 250: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

From FIRST Sets to LL(1) Tables

Page 251: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

Page 252: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=→ ;while do

Page 253: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=→ ;while do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 254: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=→ ;while do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 255: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

→ ;while do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 256: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

→ ;while do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 257: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 258: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 259: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 260: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 261: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 262: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 263: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 264: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 265: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 266: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 267: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 268: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 269: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 270: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 271: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 272: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 273: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 274: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 275: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 276: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 277: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

From FIRST Sets to LL(1) Tables

STMT

EXPR

TERM

if then while zero? not ++ -- id const=

1 3 3 3 3 3 3

5 6 7 8 4 4

9 10

→ ;while

2

do

STMT EXPR TERMif

whilezero?not++--id

constant

zero?not++--id

constant

idconstant

Page 278: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

● The following algorithm constructs an LL(1) parse table for a grammar with noε-productions.

● Compute the FIRST sets for all nonterminals in the grammar.

● For each production A → tω, set T[A, t] = tω.

● For each production A → Bω, set T[A, t] = Bω for each t ∈ FIRST(B).

ε-Free LL(1) Parse Tables

Page 279: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Expanding our Grammarid → id;

while not zero? id do --id;

if not zero? id then if not zero? id then constant → id;

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

Page 280: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Expanding our Grammarid → id;

while not zero? id do --id;

if not zero? id then if not zero? id then constant → id;

STMT → if EXPR then STMT (1) | while EXPR do STMT (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

BLOCK→ STMT (11) | { STMTS } (12)

STMTS→ STMT STMTS (13) | ε (14)

Page 281: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Expanding our Grammarid → id;

while not zero? id do --id;

if not zero? id then if not zero? id then constant → id;

STMT → if EXPR then BLOCK (1) | while EXPR do BLOCK (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

BLOCK→ STMT (11) | { STMTS } (12)

STMTS→ STMT STMTS (13) | ε (14)

Page 282: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

Expanding our Grammarid → id;

while not zero? id do --id;

if not zero? id then if not zero? id then constant → id;

if zero? id then while zero? id do { constant → id; constant → id; }

STMT → if EXPR then BLOCK (1) | while EXPR do BLOCK (2) | EXPR ; (3)

EXPR → TERM -> id (4) | zero? TERM (5) | not EXPR (6) | ++ id (7) | -- id (8)

TERM → id (9) | constant (10)

BLOCK→ STMT (11) | { STMTS } (12)

STMTS→ STMT STMTS (13) | ε (14)

Page 283: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) with ε-Productions

● Computation of FIRST is different.● What if the first nonterminal in a production

can produce ε?

● Building the table is different.● What action do you take if the correct

production produces the empty string?

Page 284: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with ε

Page 285: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Page 286: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits More

Page 287: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

+ -

More

Page 288: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

+ -

More

Page 289: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

+ - + -

More

Page 290: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

+ - + -

More

Page 291: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

+ - + -

More

Page 292: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -

More

Page 293: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -

More

Page 294: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -

More

Page 295: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -

More

0 51 62 73 84 9

Page 296: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -

More

0 51 62 73 84 9

Page 297: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -ε

More

0 51 62 73 84 9ε

Page 298: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ - + -ε

More

0 51 62 73 84 9ε

Page 299: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Page 300: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Sets with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | 2 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Page 301: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST and ε

● When computing FIRST sets in a grammar with ε-productions, we often have to “look through” nonterminals.

● Rationale: Might have a derivation like this:

A ⇒ Bt ⇒ t

● So t ∈ FIRST(A).

Page 302: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Computation with ε

● Initially, for all nonterminals A, set

FIRST(A) = { t | A → tω for some ω }

● For all nonterminals A where A → ε is a production, add ε to FIRST(A).

● Repeat the following until no changes occur:● For each production A → α, where α is a string of nonterminals

whose FIRST sets contain ε, set FIRST(A) = FIRST(A) ∪ { ε }.● For each production A → αtω, where α is a string of

nonterminals whose FIRST sets contain ε, setFIRST(A) = FIRST(A) ∪ { t }

● For each production A → αBω, where α is string of nonterminals whose FIRST sets contain ε, setFIRST(A) = FIRST(A) ∪ (FIRST(B) - { ε }).

Page 303: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

A Notational Diversion

● Once we have computed the correct FIRST sets for each nonterminal, we can generalize our definition of FIRST sets to strings.

● Define FIRST*(ω) as follows:● FIRST*(ε) = { ε }● FIRST*(tω) = { t }● If ε ∉ FIRST(A):

– FIRST*(Aω) = FIRST(A)● If ε ∈ FIRST(A):

– FIRST*(Aω) = (FIRST(A) - { ε }) ∪ FIRST*(ω)

Page 304: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FIRST Computation with ε

● Initially, for all nonterminals A, set

FIRST(A) = { t | A → tω for some ω }

● For all nonterminals A where A → ε is a production, add ε to FIRST(A).

● Repeat the following until no changes occur:● For each production A → α, set

FIRST(A) = FIRST(A) ∪ FIRST*(α)

Page 305: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Page 306: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg

Hi

End

hello heya yo world!

Page 307: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

Msg

Hi

End

hello heya yo world!

Page 308: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 309: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 310: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 311: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 312: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 313: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Page 314: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

Page 315: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

Page 316: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

Page 317: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

Page 318: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

Page 319: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with εMsg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg Hi End

worldε

helloheyayo

helloheyayo

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

Page 320: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

Page 321: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

Page 322: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

Page 323: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

hello $Hi End $

Page 324: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

hello $Hi End $

Page 325: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

hello $Hi End $

hello $hello End $

Page 326: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

Page 327: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

Page 328: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

ε is Complicated

● When constructing LL(1) tables with ε-productions, we need to have an extra column for $.

Msg → Hi EndHi → hello | heya | yoEnd → world! | ε

Page 329: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

ε is Complicated

● When constructing LL(1) tables with ε-productions, we need to have an extra column for $.

Msg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

$

Page 330: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

ε is Complicated

● When constructing LL(1) tables with ε-productions, we need to have an extra column for $.

Msg → Hi EndHi → hello | heya | yoEnd → world! | ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

$

ε

Page 331: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Page 332: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

Page 333: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

$

ε

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

Page 334: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

$

ε

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

Page 335: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

LL(1) Tables with ε

Msg

Hi

End

hello heya yo world!

Hi End Hi End Hi End

hello heya yo

world!

$

ε

hello $Msg $

hello $Hi End $

hello $hello End $

$End $

$$

Page 336: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Page 337: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num

Sign

Digits

More

Digit

+ - # $

Page 338: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $

Page 339: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $

Page 340: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

Page 341: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

Page 342: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -

Page 343: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -

Page 344: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Page 345: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Page 346: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Digits

Page 347: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Digits

Page 348: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Digits

#

Page 349: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Digits

#

Page 350: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits

+ -Digits More

Digits

#

Page 351: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ -Digits More

Digits

#

Page 352: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ -Digits More

Digits

#

Page 353: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits

#

Page 354: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits

#

Page 355: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits

#

Page 356: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits ε

#

Page 357: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits ε

#

Page 358: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

It Gets TrickierNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9

Num Sign Digit Digits

0 51 62 73 84 9

0 51 62 73 84 9

+ -0 51 62 73 84 9

+ -ε

More

0 51 62 73 84 9ε

Num

Sign

Digits

More

Digit

+ - # $Sign Digits Sign Digits Sign Digits

+ - εDigits More

Digits ε

#

Page 359: Top-Down Parsing - Stanford University...Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, initially just the start symbol S. While the worklist

FOLLOW Sets

● With ε-productions in the grammar, we may have to “look past” the current nonterminal to what can come after it.

● The FOLLOW set represents the set of terminals that might come after a given nonterminal.

● Formally:

FOLLOW(A) = { t | S ⇒* αAtω for some α, ω }

where S is the start symbol of the grammar.● Informally, every nonterminal that can ever come after

A in a derivation.