0

I cannot quite understand how to determine whether grammar is LL(1) or not. I have been given the following grammar:

S → Y | 1X 
X → 1X | 0
Y → Y0|1X1|2X2

I stated that this grammar is not LL(1) because it Y0 is left recursive.

So I came up with the following solution:

S → Y | 1X
X → 1X | 0
Y → 1X1F | 2X2F
F → ε | 0F

But still I am not sure if that is correct. I still think that I must have missed some rule like factoring of some sort. Would I have to take 1X and 2X into different variable?

Thanks for help in advance. I would also like to know if there are easier ways to determine whether the grammar is LL(1) I have came across a lot of "first" and "follow" tables, but haven't actually managed to build one myself.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
AlenEviLL
  • 56
  • 1
  • 9

2 Answers2

1

If factor our 1X then I get the following:

S → Y | Z
X → Z | 0
Y → Z1F | 2X2F
F → ε | 0F
Z → 1X | ε

Which means that S → Y | Z and I believe that is not allowed.

AlenEviLL
  • 56
  • 1
  • 9
0

An LL(1) grammar needs to be able to predict a production based on a single ((1)) lookahead token. However, both Y and 1X can start with 1, so it is impossible to predict whether to use S→Y or S→1X given the first input symbol 1. So neither the original grammar nor the transformed grammar are LL(1).

rici
  • 234,347
  • 28
  • 237
  • 341
  • Will I have to factor 1X out into another variable like this?: `S → Y | 1X X → Z | 0 Y → Z1F | 2X2F F → ε | 0F Z → 1X | ε` – AlenEviLL Mar 24 '15 at 18:17
  • @AlenEviLL: Does that deal with the problem indicated in this answer? You will certainly have to do some factoring... – rici Mar 24 '15 at 19:08
  • Help please I am stuck, see my answer, I am even going the right way? @rici – AlenEviLL Mar 28 '15 at 17:58