1 / 12

Bottom-Up Parsing Basics

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

wattan
Download Presentation

Bottom-Up Parsing Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bottom-Up Parsing Basics L13BUPBasic

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related