E ----> E + E (AMBIGUOUS) E ----> E * E E ----> ( E ) E ----> idto
E ----> E + T (NOT AMBIGUOUS) E ----> T T ----> T * F T ----> F F ----> ( E ) F ----> id
stmt ----> "if" "(" expr ")" stat (AMBIGUOUS)
stmt ----> "if" "(" expr ")" stat "else" stat
E ----> T { "+" T } ({} means "zero of more times")
T ----> F { "*" F } (terminals are in quotes or id)
F ----> "(" E ")"
F ----> id
S ---> b M b ("S" is the start symbol)
M ---> ( L
M ---> a
L ---> M a )
Use the following shift-reduce table for this grammar:
| b | a | ( | ) | $ |
-----+-----+-----+-----+-----+------+
S | | | | | acc | ( "s" means "shift")
M | s | s | | | |
L | r | r | | | | ( "r" means "reduce")
b | | s | s | | r |
a | r | r | | s | | ( "acc" means "accept")
( | s | s | s | | |
) | r | r | | | |
$ b ( ( a a ) a ) b $
(Remember that you should initially shift the starting
$ and then shift the next symbol.)
P ---> A { A } '$'
A ---> lcletter '=' E ';'
E ---> T {('+'|'-') T}
T ---> S {('*'|'/') S}
S ---> F '^' S
F ---> lcletter | '(' E ')' | digit
This is a grammar for a simple language consisting of a sequence
of assignment statements, terminated by semicolons. The whole
program is terminated by a sharp (#). Operands are single lowercase
letters or single digits. Operators are = + - * / ^Here is a sample program (sentence) in this language:
a = 4;
b = (3 + a)*6;
c = a^2 + b^2;
$
Consider the following simplified symbolic form of quadruples:
(#,4, ,A)
(=,A, ,a)
(#,3, ,B)
(+,B,a,C)
(#,6, ,D)
(*,C,D,E)
(=,E, ,b)
(#,2, ,F)
(^,a,F,G)
(#,2, ,H)
(^,b,H,I)
(+,G,I,J)
(=,J, ,c)
x = 12;
y = 13;
z = x^2 + y^2;
$