0

I have the following grammar

S -> E | S A | A
E -> if (C) {S} | if (C) {S} else {S}
A -> V := T;
T -> a | b
V -> x | y
C -> V O T | T O V | V O V | T O T
O -> < | >

From this, am I correct in saying this grammar is not LL(1) because for Rule E, "if (C) {S}" can be left-factored out and rule S has a left recursion?

What would be the correct way to fix this? I was thinking something like:

S -> P
P -> ER | AR
R -> AR | ε
E -> if (C) {S} B
B -> else {S}
A -> V := T;
T -> a | b
V -> x | y
C -> V O T | T O V | V O V | T O T
O -> < | >
Kadana Kanz
  • 177
  • 2
  • 11

1 Answers1

0

Left-factoring E

You propose to factor

E -> if (C) {S} | if (C) {S} else {S}

into

E -> if (C) {S} B
B -> else {S}

Now, it's no longer possible to parse the if (C) {S} form, since B can't be empty. So, this transformation is not correct.

Instead, consider this:

B -> else {S} | ε

Removing direct left recursion in S

You propose to replace

S -> E | S A | A

by

S -> P
P -> ER | AR
R -> AR | ε

While this is a correct transformation, the following would also be sufficient:

S -> ER | AR
R -> AR | ε