Copyright © 2006 Gradiance Corporation.
LR(1) Parsers
LR(1), SLR, and LALR parsing; YACC.
1.
Construct the LR(1) sets of items for the grammar:
S' → S
S → SS+ | a
Then, identify, in the list below, the one set that is NOT a set of LR(1) items for this grammar.
a)
[S→a., +a]
b)
[S'→S., $], [S→S.S+, $a], [S→.SS+, +a], [S→.a, +a]
c)
[S→SS.+, $a], [S→S.S+, +a], [S→.SS+, +a], [S→.a, +a]
d)
[S→SS.+, $+a], [S→S.S+, +a], [S→.SS+, +a], [S→.a, +a]
2.
Construct the LALR(1) sets of items for the grammar:
S → +SS | a
Then, identify, in the list below, one of the LALR(1) sets of items for this grammar.
[S→+SS., $+a]
[S→+.SS, $+a], [S→.+SS, $+a], [S→.a, $+a]
[S→+.SS, $], [S→.+SS, +a], [S→.a, +a]
3.
Here are the LR(1) sets of items for the grammar:
1) S → AaAb
2) S → BbBa
3) A → ε
4) B → ε
Construct the Action and Goto portions of the LR(1) parsing table for this grammar. In this table:
Identify, from the list of choices below, the incorrect table entry.
Action(6,b) = s8
Action(0,b) = r4
Goto(8,$) = 1
Action(4,b) = r3
4.
It has a set of LR(0) items that are the same, but with the lookaheads removed. For example, I4 is just {[S→Aa.Ab], [A→.]}.
While the LR(1) parsing table has no conflicts, when we construct the SLR parsing table we get conflicts. Construct both tables, using the following conventions:
Which of the following entries appears in the SLR table (possibly as one of several choices for that entry) but does not appear in the LR(1) table?
r1 in Action(9,$)
r4 in Action(0,a)
r1 in Action(8,b)
acc in Action(1,b)
5.
[S→SS+., $+a]
[S'→.S, $], [S→.SS+, $+a], [S→.a, $+a]
[S→SS.+, +a], [S→S.S+, +a], [S→.SS+, +a], [S→.a, +a]
[S'→S., $], [S→S.S+, $+a], [S→.SS+, +a], [S→.a, +a]
6.
Here is the outline of a Yacc progam for parsing expressions over the noncommutative operators - and /. However, these operators may have nonstandard precedence and/or associativity. NUMBER is a token standing for any integer. Its lexical value is the value of that integer.
. . .
%token NUMBER
%right '-' %right '/'
expr : expr '-' expr {$$ = $1 - $3}
| expr '/' expr {$$ = $1 / $3}
| NUMBER
Suppose the input to the lexical analyzer and parser is:
6 - 5 - 4 / 3 / 2
What is the value returned when this string is parsed as an expr?
-2
11/3
5/6
-1/2