570 likes | 655 Views
Bottom-Up Syntax Analysis. Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Textbook:Modern Compiler Design Chapter 2.2.5. 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.
E N D
Bottom-Up Syntax Analysis Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Textbook:Modern Compiler Design Chapter 2.2.5
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 parser tokens Efficient Parsers bison “Ambiguity errors” parse tree
Top-Down (Predictive Parsing) LL 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 Kinds of Parsers
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 Bottom-Up Syntax Analysis
Pushdown automata Bottom-up parsing (informal) Bottom-up parsing (given a parser table) Constructing the parser table Interesting non LR grammars Plan
Pushdown Automaton input u t w $ V control parser-table $ stack
input stack tree i + i $ $ input stack tree + i $ i$ i Informal Example S E$ E T | E + T T i | ( E ) shift
input input stack stack tree tree + i $ T + i $ i$ T$ i i Informal Example S E$ E T | E + T T i | ( E ) reduce T i
tree input stack stack tree E + i $ T + i $ T$ E$ i i Informal Example S E$ E T | E + T T i | ( E ) input T reduce E T
tree input tree input stack stack E E i $ T T + i $ +E$ E$ i i + Informal Example S E$ E T | E + T T i | ( E ) shift
input tree tree stack input E stack $ E i $ i+E$ T T +E$ i i i + + Informal Example S E$ E T | E + T T i | ( E ) shift
input input tree tree stack stack E E $ $ T T+E$ i+E$ T T i i + + i i Informal Example S E$ E T | E + T T i | ( E ) reduce T i
tree E input stack input tree E stack $ E E$ T $ T T+E$ T T i i + i + Informal Example S E$ E T | E + T T i | ( E ) reduce E E + T
tree tree E E input input stack stack E E $ $ E $ E$ T T T T i i + + Informal Example S E$ E T | E + T T i | ( E ) shift
tree E E T T i + Informal Example S E$ E T | E + T T i | ( E ) input stack $ $ E $ reduce Z E $
Informal Example reduce Z E $ reduce E E + T reduce T i reduce E T reduce T i
Handles • Identify the leftmost node (nonterminal) that has not been constructed but all whose children have been constructed • A
Identifying Handles • Create a finite state automaton over grammar symbols • Accepting states identify handles • Report if the grammar is inadequate • Push automaton states onto parser stack • Use the automaton states to decide • Shift/Reduce
stack input stack shift 5 s0($) i + i $
stack input s5 (i) s0 ($) reduce T i + i $
stack input s6 (T) s0 ($) + i $ reduce E T
stack input shift 3 s1(E) s0 ($) + i $
stack input shift 5 s3 (+) s1(E) s0 ($) i $
input s5 (i) s3 (+) s1(E) s0($) reduce T i $
input stack reduce E E + T s4 (T) s3 (+) s1(E) s0($) $
stack input s1 (E) s0 ($) $ shift 2
stack input s2 ($) s1 (E) s0 ($) reduce Z E $
input stack shift 7 s0($) ((i) $
stack input shift 7 s7(() s0($) (i) $
stack s7 (() s7(() s0($) input shift 5 i) $
stack s5 (i) s7 (() s7(() s0($) input reduce T i ) $
stack s6 (T) s7 (() s7(() s0($) input reduce E T ) $
stack s8 (E) s7 (() s7(() s0($) input shift 9 ) $
s9 ()) s8 (E) s7 (() s7(() s0($) stack input reduce T ( E ) $
stack s6 (T) s7(() s0($) input reduce E T $
stack s8 (E) s7(() s0($) input err $
Grammar Hierarchy Non-ambiguous CFG CLR(1) LL(1) LALR(1) SLR(1) LR(0)
Constructing LR(0) parsing table • Add a production S’ S$ • Construct a finite automaton accepting “valid stack symbols” • States are set of items A • The states of the automaton becomes the states of parsing-table • Determine shift operations • Determine goto operations • Determine reduce operations
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 …XXi+1 … Xn, ) = Xi+1 • Convert into DFA
T i T (E ) i E T T i ) T ( T ( E ) T (E ) T (E ) E T E S E $ E E E + T E E + T E E+ T E E+T + S E $ E T $ S E $ S E$ E T | E + T T i | ( E )
Filling Parsing Table • A state si • reduce A • A si • Shift • A t si • Goto(si, X) = sj • A X si • (si, X) = sj • When conflicts occurs the grammar is not LR(0)
4 EE+ E EE+E E i 0 2 SE$ EE+E E i SE$ EE+E + E + $ E i 5 3 EE+ E EE+E 1 E i S E $ i Example S E $ E E + E | i
0 SE$ EE+E E i 4 EE+ E EE+E E i 2 SE$ EE+E + E + $ i E 5 3 1 EE+ E EE+E E i S E $ i