Download presentation
Presentation is loading. Please wait.
1
Bottom-Up Syntax Analysis
Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3 Explains the automaton first
2
Efficient Parsers Pushdown automata Deterministic
Report an error as soon as the input is not a prefix of a valid program Not usable for all context free grammars context free grammar bison “Ambiguity errors” parser tokens parse tree
3
Kinds of Parsers Top-Down (Predictive Parsing) LL Bottom-Up LR
Construct parse tree in a top-down matter Find the leftmost derivation For every non-terminal and token predict the next production Bottom-Up LR Construct parse tree in a bottom-up manner Find the rightmost derivation in a reverse order For every potential right hand side and token decide when a production is found
4
Bottom-Up Syntax Analysis
Input A context free grammar A stream of tokens Output A syntax tree or error Method Construct parse tree in a bottom-up manner Find the rightmost derivation in (reversed order) For every potential right hand side and token decide when a production is found Report an error as soon as the input is not a prefix of valid program
5
Plan Pushdown automata Bottom-up parsing (given a parser table)
Constructing the parser table Interesting non LR grammars
6
Pushdown Automaton input u t w $ V control parser-table $ stack
7
Bottom-Up Parser Actions
reduce A Pop | | symbol from the stack Apply the associated action Push a symbol goto[top, A] on the stack shift X Push X onto the stack Advance the input accept Parsing is complete error Report an error
8
A Parser Table for S a S b|
state description a b $ S initial s1 r S 4 1 parsed a 2 parsed S s3 3 parsed a S b r S a S b final acc Manual Construction?
9
state a b $ S s1 r S 4 1 2 s3 3 r S a S b acc stack input action aabb$ s1 0 1(a) abb$ 0 1(a)1(a) bb$ r S 0 1(a)1(a)2(S) s3 0 1(a)1(a)2(S)3(b) b$ r S a S b 0 1(a)2(S) 0 1(a)2(S)3(b) $ 0 4(S) acc
10
A Parser Table for S AB | A A a B a
state description a $ A B S initial s1 2 5 1 parsed a from A r A a parsed A s3 r S A 4 3 parsed a from B r B a parsed AB r S AB final acc
11
state a $ A B S s1 2 5 1 r A a s3 r S A 4 3 r B a r S AB acc stack input action aa$ s1 0 1(a) a$ r A a 0 2(A) s3 0 2(A)3(a) $ r B a 0 2(A)4(B) r S AB 0 5(S) acc
12
A Parser Table for E E + E| id
state description id + $ E initial s1 2 1 parsed id r E id parsed E s3 acc 3 parsed E+ 4 parsed E+E r E E + E r E E+E
13
The Challenge How to construct a parser-table from a given grammar
LR(1) grammars Left to right scanning Rightmost derivations (reverse) 1 token Different solutions Operator precedence SLR(1) Simple LR(1) CLR(1) Canonic LR(1) LALR(1) Look Ahead LR(1) Yacc, Bison, JCUP
14
Grammar Hierarchy Non-ambiguous CFG CLR(1) LL(1) LALR(1) SLR(1)
15
Constructing an SLR parsing table
Add a production S’ S$ Construct a finite automaton accepting “valid stack symbols” The states of the automaton becomes the states of parsing-table Determine shift operations Determine goto operations Construct reduce entries by analyzing the grammar
16
A finite Automaton for S’ S$ S a S b|
1 2 3 S 4 state description a b $ S initial s1 4 1 parsed a 2 parsed S s3 3 parsed a S b final
17
Constructing a Finite Automaton
NFA For X X1 X2 … Xn [X X1 X2 …XiXi+1 … Xn] “prefixes of rhs (handles)” X1 X2 … Xi is at the top of the stack and we expect Xi+1 … Xn The initial state [S’ .S$] ([X X1…XiXi+1 … Xn], Xi+1 = [X X1 …XiXi+1 … Xn] For every production Xi+1 ([[X X1 X2 …XiXi+1 … Xn], ) = [Xi+1 ] Convert into DFA
18
NFA S’ S$ S a S b| S b a [S .aSb] [S a.Sb] [S aS.b]
19
DFA a S [S .aSb] [S a.Sb] [S aS.b] [S’ .S$] b S [S .] [S aSb.]
20
[S a.Sb] S a [S .aSb] [S aS.b] b [S aSb.] S a [S’ S.$] [S’ .S$]
state items a b $ S [S .S$, S .aSb, S .] s1 4 1 [S a.Sb$, S .aSb, S .] 2 [S aS.b] s3 3 [S aSb.] [S’ S.$] acc
21
Filling reduce entries
For an item [A .] we need to know the tokens that can follow A in a derivation from S’ Follow(A) = {t | S’ * At} See the textbook for an algorithm for constructing Follow from a given grammar
22
[S a.Sb] S a [S .aSb] [S aS.b] b [S aSb.] S a [S’ S.$]
Follow(S) = {b, $} state items a b $ S [S .S$, S .aSb, S .] s1 4 1 [S a.Sb$, S .aSb, S .] 2 [S aS.b] s3 3 [S aSb.] [S’ S.$] acc r S r S r S r S r S a S b
23
Example E E + E| id
24
Interesting Non SLR(1) Grammar
S’ S$ S L = R | R L *R | id R L Partial DFA [S L=.R] [R .L] [L .*R] [L .id] [S’ .S$] [S .L=R] [S .R] [L .*R] [L .id] [R L] [S L.=R] [R L.] = L Follow(R)= {$, =}
25
LR(1) Parser Item [A ., t] LR(1) State LALR(1) State
is at the top of the stack and we are expecting t LR(1) State Sets of items LALR(1) State Merge items with the same look-ahead
26
Interesting Non LR(1) Grammars
Ambiguous Arithmetic expressions Dangling-else Common derived prefix A B1 a b | B2 a c B1 B2 Optional non-terminals St OptLab Ass OptLab id : | Ass id := Exp
27
Summary But some grammars need to be tuned LR is a powerful technique
Generates efficient parsers Generation tools exit Bison, yacc, CUP But some grammars need to be tuned Shift/Reduce conflicts Reduce/Reduce conflicts Efficiency of the generated parser
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.