0

I'm attemping to do compilers on javacc but I'm not sure if the following is legal when removing left recursion:

A = B AP APP
      | C AP APP

AP = A AP | {}

APP = (D AP) APP | {}
  • There's not enough information in the question to answer it. Which are the terminals and which are the non-terminals? Which nonterminal is the start non-terminal? Once those questions are answered, you can tell whether the grammar is LL(1). If it is, then it should work with JavaCC. – Theodore Norvell Nov 11 '18 at 13:58

1 Answers1

0

I'm going to assume that B, C, and D are terminals. And I'm going to add one more nonterminal

START --> A <EOF>

A --> <B> AP APP
     | <C> AP APP

AP --> A AP | {}

APP --> <D> AP APP | {}

The first choice is clearly resolvable on one token of look ahead. For the second choice we need that the first set of A AP be disjoint from the follow set of AP; the former is {<B>, <C>} and the latter is {<EOF>,<D>}. For the third choice, we need that <D> is not in the follow set of APP, which is {<EOF>}.

We now know the grammar is LL(1), so it should work with JavaCC.

Note: Determining whether a grammar will work with JavaCC is a bit more complicated, because JavaCC doesn't calculate follow sets exactly like the theory says and because JavaCC has lots of mechanisms that allow it to work with non-LL(1) grammars. But generally if your grammar is LL(1), JavaCC will handle it fine. Also if JavaCC isn't going to work, it gives a warning message.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45