Más contenido relacionado



  1. Bottom Up Parsing With Examples
  2. BOTTOM UP PARSING • A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). • Bottom-up parsing is a parser that reduces the string to the start symbol of the grammar.  TYPE OF BOTTOM UP PARSER:
  3. DIFFERENCE BETWEEN LR(0) AND LR(1) CANONICAL ITEM SET:  Unlike LR(0) canonical item set, in LR(1) canonical item set each time the look-ahead has to be maintained even in the produced productions.  The look-ahead of the produced productions depends on the First (leftover of the production producing it).
  4. GRAMMAR 1 (LR(0)) 1. S  AA A  aA / b Checking this grammar for LR(0) Augmented grammar – S’ -> . S S -> . AA ---(1) A -> . aA ---(2) A -> . b ---(3) Steps for constructing the LR parsing table : • Writing augmented grammar & check ambiguity • LR(0) collection of items to be found • Defining 2 functions: goto(list of terminals) and action(list of non- terminals) in the parsing table.  The terminals of this grammar are {a, b}  The non-terminals of this grammar are {S, A}
  6. STATE ACTION GOTO a b $ A S I0 S3 S4 2 1 I1 Accept I2 S3 S4 5 I3 S3 S4 6 I4 r3 r3 r3 I5 r1 r1 r1 I6 r2 r2 r2 PARSING TABLE Since there is no conflict like S-R or S-S, we can say this is a LR(0) grammar. GRAMMAR 1 COLLECTION OF ITEMS
  7. Now we will parse an input string & create parse tree: GRAMMAR 1 PARSING STACK INPUT ACTION 1 $0 aabb$ Shift a3 2 $0a3 abb$ Shift a3 3 $0a3a3 bb$ Shift b4 4 $0a3a3b4 b$ reduce r3 (A -> b) 5 $0a3a3A6 b$ reduce r2 (A -> aA) 6 $0a3A6 b$ reduce r2 (A -> aA) 7 $0A2 b$ Shift b4 8 $0A2b4 $ reduce r3 (A -> b) 9 $0A2A5 $ reduce r1 (S -> AA) 10 $0S1 $ Accept
  9. GRAMMAR 2 (CLR(1)) 2. E  BB B  cB / d Checking this grammar for LR(1): The step involves in CLR(1) parsing is given below: •Write a CFG for the given input string •Check if the grammar is ambiguous or not. •Add an augmented grammar. •Create a canonical LR(1) item. •Draw DFA •Construct an CLR(1) parsing table. Augmented grammar – E’ -> . E, $ E -> . BB, $ B -> . cB / .d, c/d
  11. GRAMMAR 2 STATE ACTION GOTO c d $ E B I0 S3 S4 1 2 I1 Accept I2 S6 S7 5 I3 S3 S4 8 I4 r3 r3 I5 r1 I6 S6 S7 9 I7 r3 I8 r2 r2 I9 r2 PARSING TABLE This is a LR(1) grammar. COLLECTION OF ITEMS
  12. GATE QUESTIONS  Which of the following statements about parser is/are CORRECT? I. Canonical LR is more powerful than SLR. II. SLR is more powerful than LALR. III. SLR is more powerful than Canonical LR.? (A) I only (B) II only (C) III only (D) II & III both • Answer: (A) • Explanation: LR parsers in terms of their power: CLR > LALR > SLR > LR (0)
  13. GATE QUESTIONS A bottom-up parser generates : (A) Left-most derivation in reverse (B) Right-most derivation in reverse (C) Correct Answer (D) Left-most derivation • Answer: (B)
  14.  What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a) to parse a string with n tokens? (A) n/2 (B) n-1 (C) 2n-1 (D) 2n • Answer: (B) • Explanation: Maximum number of reduce moves : Only one terminal is reduced and last 2 tokens will take only 1 move. Input string of n tokens: n- 2+1 = n-1 reduce moves. GATE QUESTIONS
  15.  Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals {S, A} and terminals {a, b}. S → aA {print 1} S → a {print 2} A → Sb {print 3} Using the above SDTS, the output printed by a bottom-up parser, for the input aab is: (A) 1 3 2 (B) 2 2 3 (C) 2 3 1 (D) Syntax error GATE QUESTIONS Answer: (C) Explanation: In parse tree, we stopped at ‘aab’ because it matches with the required string. It prints the output string from bottom to top i.e. 2 3 1
  16. GATE QUESTIONS  Which of the following statements is false? A) An unambiguous grammar has same leftmost and rightmost derivation B) An LL(1) parser is a top-down parser C) LALR is more powerful than SLR D) An ambiguous grammar can never be LR(k) for any k Answer: (A)