Top-Down Parsing
1
Top-Down Parsing 1 Parsing: Review of the Big Picture (1) - - PowerPoint PPT Presentation
Top-Down Parsing 1 Parsing: Review of the Big Picture (1) Context-free grammars (CFGs) Generation: Recognition: Given , is Translation Given , create a parse tree for Given , create an AST for The AST is
1
2
3
we’ve found a valid parse
4
5
6
I,N L I,N C I,N R Z X N X W F
F ⟶ I W F ⟶ I Y W ⟶ L X X ⟶ N R Y ⟶ L R N ⟶ id N ⟶ I Z Z ⟶ C N I ⟶ id L ⟶ ( R ⟶ ) C ⟶ ,
id id id , ) ( 1,2 2,3 3,4 4,5 5,6 1,3 2,4 3,5 4,6 1,4 2,5 3,6 1,5 2,6 1,6 In general, go up a column and down a diagonal
Video: https://www.youtube.com/watch?v=_ahvzDzKdB0 Text: http://www.cs.virginia.edu/~evans/cs655/readings/steele.pdf
7
8
9
Scanner
10
Selector table “Work to do” Stack EOF a b a a Token Stream Row: nonterminal Column: terminal current Parser
11
( S ) ε { S } ε ε
( ) { }
eof
eof S ) ( } S { “Work to do” Stack ( { } ) eof S current current current current current
Input:
12
eof u t A D C “Work to do” Stack t u eof current
Input:
Not yet seen Already processed
S B A A D C
u t eof The structure that the parser expects to build The structure already seen
13
stack.push(eof) stack.push(Start non-term) t = scanner.getToken() Repeat if stack.top is a terminal y match y with t pop y from the stack t = scanner.next_token() if stack.top is a nonterminal X get table[X,t] pop X from the stack push production’s RHS (each symbol from Right to Left) Until one of the following: stack is empty stack.top is a terminal that does not match t stack.top is a non-term and parse-table entry is empty
reject accept Initial stack is “Start eof”
14
( S ) ε { S } ε ε
( ) { }
eof
15
( S ) ε { S } ε ε
( ) { }
eof
16
If our selector table has 1 production per cell, then grammar is LL(1)
production
17
18
19
XList XList x | x
x XList How should we grow the tree top-down? x XList Current parse tree: Current token: CFG snippet: XList x XList
Correct if there are no more xs Correct if there are more xs We don’t know which to choose without more lookahead
20
XList XList x | x
x XList Current parse tree: Current token: CFG snippet: Parse table:
XList XList x
x
ε
eof
Stack
eof Current x XList XList x XList x XList x (Stack overflow)
21
(for a single immediately left-recursive rule) Where β does not begin with A
22
Exp → Exp – Factor | Factor Factor → intlit | ( Exp )
Exp → Factor Exp’ Exp’ → - Factor Exp’ | ε Factor → intlit | ( Exp )
23
E E
E
F 2 3 4
Exp → Exp – Factor | Factor Factor → intlit | ( Exp ) Exp → Factor Exp’ Exp’ → - Factor Exp’ | ε Factor → intlit | ( Exp )
E F E 2
E
E 3 4 ε 2 – 3 grouped together grouping of 2 – 3 destroyed
24
25
26
27
28
Exp → ( Exp ) | Exp Exp | ( ) Exp → ( Exp ) Exp' | ( ) Exp' Exp' → Exp Exp' | ε Exp -> ( Exp'' Exp'' -> Exp ) Exp' | ) Exp' Exp' -> exp exp' | ε
Remove immediate left-recursion Left-factoring
table:
– FIRST sets – FOLLOW sets
29