8

I have a homework assignment where I need to convert a grammar into LL(1). I've already removed the left recursion, but I'm having trouble doing left-factoring. All of the examples I've found are simple, and look something like this:

A -> aX | aY
becomes:
A -> aZ
Z -> X | Y

I understand that. However, my grammar looks more like this:

X -> aE | IXE | (X)E
E -> IE | BXE | ϵ
I -> ++ | --
B -> + | - | ϵ

I'm not sure how to apply the simpler example to this. I've been trying for at least a couple of hours and I've lost track of all of the things I've tried. Generally, my attempts have looked something like this:

X  -> X' | IXE
X' -> aE | (X)E
E  -> IE | BIX'E | BX'E | ϵ

And I then try to convert the E rules into ones having only one production starting with + or -:

X  -> X' | IXE
X' -> aE | (X)E
B' -> + | -
E  -> IE | B'IX'E | IX'E | B'X'E | X'E | ϵ

And then...

X  -> X' | IXE
X' -> aE | (X)E
B' -> + | -
E  -> +P | -M | ϵ
P  -> +E | IX'E | +X'E | X'E
M  -> -E | IX'E | -X'E | X'E

And so on. But I continually end up with a lot of extra nonterminals, and some very long productions / chains of productions, without actually having left-factored it. I'm not sure how to approach this - I can't seem to eliminate some nonterminal having multiple productions starting with a + and with a -.

Raphael
  • 72,336
  • 29
  • 179
  • 389
Kami's Aibou
  • 183
  • 1
  • 1
  • 5
  • Welcome! 1) Have you looked at the formal definition of left-factoring? 2) Are you certain your language can be described by an LL(1) grammar? Not all can. See also here. 3) Check out other questions about left-factoring. – Raphael Oct 04 '12 at 09:38
  • Thanks! 1) Yes, we had assigned reading in our textbook covering left factoring, as well as lecture slides on it. 2) I'm pretty sure it should be - converting the grammar to LL(1) is the first part of the homework, and then we need to write a recursive descent parser for the converted grammar. I know the same assignment has been used for this class in the past. 3) I did look on here and Google, but everything I found was an explanation of the purpose of left factoring and/or the simple example I included at the beginning of my question - I didn't find any more complex examples. – Kami's Aibou Oct 05 '12 at 01:03
  • 1+4) Given the definition, examples should be superfluous. 2) Long-standing "wrong" homework assignments are not unheard of, but I suspect your phrasing "more like this" now. What is the original grammar? Maybe you left out essential parts without realising. – Raphael Oct 05 '12 at 09:33
  • I usually find that I learn better from examples than from definitions. Anyway, it turns out that the input will be tokenized first, so the grammar doesn't have to decide between a pair of + and a single +. (Cue headdesk moment...) – Kami's Aibou Oct 08 '12 at 02:19
  • Examples often have the disadvantage of being simpler than some things you meet in "practice". I think they are best used for understanding the definition, so then the definition can be applied in all situations. 2) How does the lexer distinguish ++ from + +? By whitespace?
  • – Raphael Oct 08 '12 at 07:50
  • It uses a greedy scanner, so if we get 1+++2, that would be 1, ++, +, 2. It also does use whitespace, so if instead it was 1+ ++2 that would be 1, +, ++, 2. – Kami's Aibou Oct 08 '12 at 17:07