3 Replies Latest reply: Mar 5, 2010 7:07 PM by EJP RSS

    javacc/jjtree question tree

    843810
      Not sure if this is the good forum, but since it has to do with compilers maybe someone here can help.

      I'm trying to make a really simple parser using jjtree and javacc. It actually works but the tree is not generated the way I want.

      It's basically a search query parser.

      The grammar is like this:
      searchExp ::= searchTerm ( (and | or) searchTerm)*
      searchTerm ::= (not)? ( (stringLiteral)+ | phrase)
      and ::= "and"
      or ::= "or"
      not ::= "not"
      stringLiteral ::= (~[ "\""," " ])+
      phrase ::= "\"" ~["\""] "\""
      My jjt is like this:
      SimpleNode searchExp() #search :
      {}
      {
        multiTerm()
        (
          (
            < AND > multiTerm() #and(2)
        | < OR > multiTerm() #or(2)
      )
      )*
      < EOF >
      {
        return jjtThis;
      }
      
      }
      
      void multiTerm() #multiTerm :
      {
        Token t;
      }
      {
        (
          searchTerm()
        )+
      }
      
      void searchTerm() #term :
      {
        Token t;
        boolean not = false;
      }
      {
        (
        {
          not = true;
        }
        < NOT >)?
        (
          t = < STRING_LITERAL >
        | t = < PHRASE >
        ) #not(not)
      {
        jjtThis.jjtSetValue(t.image);
      }
      
      }
      I added the multiTerm production but the core is pretty much the same. Anyway, the problem here is that the #not nodes get added as a child of the term node. I want this to be the other way around. However doing something like #not(2) will fail in the case where "not" isn't used. I've tried many variations but I can't get this to work.