Tut2 LR Parsing
Tut2 LR Parsing
TUTORIAL 2 : LR Parsing
P1. We have seen that left recursive rules in a context free grammar are not suitable for top down
parsing. Does bottom up parsing have an analogous problem ? Examine any bottom up parser from the
LR family with respect to recursive rules. Find out what difference exists, if any, when for the same
language, first left recursive and then a right recursive grammar is used with your chosen bottom up
parser.
P2. The stack contents and the input symbol at some point during parsing by a shift reduce parser is
shown below with the nonterminal C on top of the stack. The terminals on the stack are {a. b, c, d} and
the nonterminals are {A, B, D, C}
STACK : aAb B c Dd C
Which among the following statements, S1 to S4, are true and why ?
P3. Examine the blank entries in the goto part of a LR parsing table (SLR(1), LALR(1) or LR(1)) are
error entries in the sense that the blank entries are error in the Action part.
P5 (a) Identify which of the grammars given below, is LR(0) and/or SLR(1) and/or LALR(1) / LR(1)
and why ?
CS333/Supratim/LR Parsing/1
P6. We would like to process program fragments, as given below only for the purpose of parsing.
integer a [10, 20];
integer b [10, 20];
integer i, j;
i = 10;
j = 16;
b[i+1, j+2] = a [i-1, j+1] + a [i , j – 2] + 25;
(a) Make appropriate changes to the grammar given below for array references, if so required, so that
code fragment, as given above, can be generated along with parsing. Your grammar should be able to
(i) generate declarations of scalars and arrays as denoted by the first 3 lines above, and (ii) also
generate assignments involving arrays and scalars, as given in the last 3 lines of the sample code
above. Assume that an array is stored in row-major representation , if such information is necessary.
S → L = E
E → L
L → elist ] | id | num
elist → elist , E | id [ E
(b) Determine whether the grammar as given, or the one rewritten by you in part (a), will admit a
SLR(1) / LALR (1) / LR(1) ser by constructing the automaton or otherwise. We need the smallest
parser of the family that works for the grammar.
(c) Construct the parse tree for the program fragment of part (a) using your grammar and the parser.
(d) Construct LR(1) and LALR(1) parsing tables for the grammar written by you in part (b). Construct
an input that is generated using all the rules and show its parse tree using the constructed tables.
P7. For the grammar shown below, answer the following questions.
S → id [ E ] : = E
E→ E+T| E
T→ T*F | F
F → ( E ) | id
Construct any one complete automaton from the 3 parsers, and show the kernel items of each state.
Identify all the states of this automation with a self loop and write the set of all viable prefixes for this
state. Justify the validity of all the LR(0) items contained in such states, and report your findings.
P8. Consider the following context free grammar which can generate certain assignment statements
involving array references and arithmetic operators assuming the usual properties of operators.
S → L := E E → L |E+T
T → T*F| F L → elist ] | id
elist → elist , E | id [ E F → id
CS333/Supratim/LR Parsing/2
(a) Construct a SLR(1) parser for the grammar given above, rewriting the same, if required, without
changing the underlying language.
(b) Construct LR(1) and LALR(1) parsing tables for the grammar of part (a).
P9. Consider the grammar given below for generating program fragments with control flow
constructs . The term relop stands for relational operators {<, <=, >, >=, ==, !=}.
(b) Determine whether your grammar will lead to a conflict free SLR(1) Parsing table. If the answer is
in the negative, report the conflicts.
(c) Construct the parse tree for the program fragment of part (a) using your grammar and a SLR(1)
parser.
(d) Construct LR(1) and LALR(1) parsing table for the grammar written by you in part (b). Construct
the parse tree for the program fragment of part (a) using these tables.
CS333/Supratim/LR Parsing/3
P11. Identify the following grammars as LL(1), LR(0), SLR(1), LALR(1) or LR(1)
P12. Consider the SLR(1) automaton for the pointer grammar G, given in the notes
(a) Write 5 distinct strings from (N T)* that are not viable prefixes for G
(b) Consider a state, s, of this automaton that belong to a loop. Write all distinct viable prefixes for the
state s.
(c) Explain why all the LR(0) items in state s are valid for the viable prefixes associated with this state.
CS333/Supratim/LR Parsing/4