120 likes | 216 Views
Bottom-Up Parsing Basics. S A B c A a b F A a f F a f. Example: Rightmost Derivation. Input String: aaabffc. S -> ABc A -> Aa | a B -> bF | b F -> f F | f. S => A B c => Ab F c => Abf F c => A bffc
E N D
Bottom-Up Parsing Basics L13BUPBasic
S A B c A a b F A a f F a f Example: Rightmost Derivation Input String: aaabffc S -> ABc A -> Aa | a B -> bF | b F -> f F | f S => ABc => AbFc => AbfFc => Abffc => Aabffc =>* aaabffc L13BUPBasic
Example: Symbol Stack Input String: aa |abffc Symbol Stack:A The symbol stack summarizes the consumed string. S A B c A a b F A a f F a f Input String: aaab |ffc Symbol Stack:Ab Input String: aaabff |c Symbol Stack:AbF Input String: aaabff |c Symbol Stack:AB L13BUPBasic
Example: Item (NFA state) Input String: aa |abffc NFA State :A -> A.a The NFA state shows the part of a rule reachable using the consumed string. S A B c A a b F A a f F a f Input String: aaab |ffc NFA State :B -> b.F Input String: aaabff |c NFA State :B -> bF. Input String: aaabff |c NFA State :S -> AB.c L13BUPBasic
Example: State transitions Input String: aaa |bffc aaab|ffcaaabf|fc S => A . Bc => A b . Fc => A b f . Fc S A B c A a b F A a f F a f State Transition B -> b.F B -> b. F -> .f F F -> .f … S -> A.Bc B -> .bF B -> .b … b L13BUPBasic
Symbol stack and State stack Input String: $ aaa b f |fc Symbol Stack: $ A b f State stack S -> .ABc A -> .Aa A -> .a S -> A . Bc A -> A.a B -> .bF B -> .b B -> b.F B -> b. F -> . f F F -> . f b f F -> f.F F -> f. A reduce shift L13BUPBasic
Note 1 : Redundancy • The state stack can be generated from the sequence of symbols on the symbol stack, by tracing the derivation from the start symbol, using the productions. • The state stack is used for efficiency. • It avoids running the entire DFA (containing production fragments) to determine the next state each time. L13BUPBasic
Why trace the entire symbol stack? It is not possible to determine the “current” production (state) by merely inspecting a fixed number of stack symbols. S -> [ A ] | { B } A -> a A | a B -> a B | a Input 1:$ [ a a a| a ] Symbol Stack:$ [ a a a State: Input 2:$ { a a a| a } Symbol Stack:$ { a a a State: A -> a . A A -> .a B -> a . B B -> .a L13BUPBasic
DFA State : Input strings with eqvt. history : RHS of rules sharing a prefix Input String1: ab |ffc Input String2: ab |ge The DFA state captures all possible rule prefixes reachable from the start symbol using the consumed input. Symbol Stack: $ A b B -> b.F B -> b. F -> . f F F -> . f D -> b.g … S -> ABc | ADe A -> Aa | a B -> bF | b D -> bg | dB F -> f F | f State L13BUPBasic
Stacking states is adequate S => A B c => A b c S => A D e => A d B e => A d b e The effect of reduction by B -> b depends on the previous state. S -> A.Bc B -> .b B b S -> AB.c b B -> b. S -> A.De D -> .dB D -> .bg D -> d.B B -> .b d D -> dB. B D S -> AD.e L13BUPBasic
Note 2 : Redundancy • The state stack contains enough information to regenerate the symbol stack. Note that all arcs into a DFA state carry the same symbol label. • As a consequence, the symbol stack can be avoided by making the “reduce”-entry in the parse table specify the number of stack elements (length of RHS) to be popped and the (LHS) nonterminal to be pushed to make the transition. • In practice, the (synthesized) semantic attribute associated with a stack symbol is allocated and computed on a parallel stack. L13BUPBasic
Relation to Grammars • The DFA naturally corresponds to a left-linear grammar. S -> A.Bc B -> .b B -> b. b Grammar Rule: P -> Qb Q P • The DFA state is obtained from the NFA states using • the standard construction. L13BUPBasic