Download presentation
Presentation is loading. Please wait.
Published byRudolph Holmes Modified over 9 years ago
1
CS780(Prasad)L13BUPBasic1 Bottom-Up Parsing Basics
2
CS780(Prasad)L13BUPBasic2 Example: Rightmost Derivation S -> ABc A -> Aa | a B -> bF | b F -> f F | f Input String: aaabffc S A B c A a b F A a f F a f S => ABc => AbFc => AbfFc => Abffc => Aabffc =>* aaabffc
3
CS780(Prasad)L13BUPBasic3 Example: Symbol Stack Input String: aa | abffc Symbol Stack: A 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 The symbol stack summarizes the consumed string.
4
CS780(Prasad)L13BUPBasic4 Example: Item (NFA state) Input String: aa | abffc NFA State : A -> A. a 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 The NFA state shows the part of a rule reachable using the consumed string.
5
CS780(Prasad)L13BUPBasic5 Example: State transitions Input String: aaa | bffc aaab|ffc aaabf|fc S A B c A a b F A a f F a f S => A. B c => A b. F c => A b f. F c S -> A. Bc B ->. bF B ->. b … B -> b. F B -> b. F ->. f F F ->. f … b State Transition
6
CS780(Prasad)L13BUPBasic6 Symbol stack and State stack S ->. ABc A ->. Aa A ->. a Input String: $ aaa b f | fc Symbol Stack: $ A b f S -> A. Bc A -> A. a B ->. bF B ->. b B -> b. F B -> b. F ->. f F F ->. f F -> f. F F -> f. A bf State stack reduceshift
7
CS780(Prasad)L13BUPBasic7 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.
8
CS780(Prasad)L13BUPBasic8 Why trace the entire symbol stack? S -> [ A ] | { B } A -> a A | a B -> a B | a It is not possible to determine the “current” production (state) by merely inspecting a fixed number of stack symbols. Input 1: $ [ a a a | a ] Symbol Stack: $ [ a a a State: A -> a. A A ->. a Input 2: $ { a a a | a } Symbol Stack: $ { a a a State: B -> a. B B ->. a
9
CS780(Prasad)L13BUPBasic9 DFA State : Input strings with eqvt. history : RHS of rules sharing a prefix Input String1: ab | ffc Input String2: ab | ge Symbol Stack: $ A b State B -> b. F B -> b. F ->. f F F ->. f D -> b. g … The DFA state captures all possible rule prefixes reachable from the start symbol using the consumed input. S -> ABc | ADe A -> Aa | a B -> bF | b D -> bg | dB F -> f F | f
10
CS780(Prasad)L13BUPBasic10 Stacking states is adequate S => A B c => A b cS => 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 S -> AB. c D -> d. B B ->. b B -> b. B b D -> dB. B S -> A. De D ->. dB D ->. bg d b S -> AD. e D
11
CS780(Prasad)L13BUPBasic11 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.
12
CS780(Prasad)L13BUPBasic12 Relation to Grammars The DFA naturally corresponds to a left-linear grammar. S -> A. Bc B ->. b B -> b. b Grammar Rule: P Q b Q P The DFA state is obtained from the NFA states using the standard construction.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.