Search code examples
compiler-constructionjavaccleft-recursion

JavaCC left recursion parsing question about legality of breaking down a grammar


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 | {}

Solution

  • 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.