Loading ...
Sorry, an error occurred while loading the content.

7909Re: [antlr-interest] Treewalking : non-determinism error

Expand Messages
  • Ric Klaren
    Apr 1, 2003
      Hi,

      On Tue, Apr 01, 2003 at 11:56:28AM +0100, Anthony Youngman wrote:
      > I think I know what the problem is, I just don't know how to fix it ...
      >
      > In my main parser, I have the following two rules:
      >
      > printst : (pr:"PRINT"^ expr (COLONPRINT)? {System.out.println(pr+" print
      > ");} );
      > expr : ( catexpr | logicexpr ) ;
      >
      > This seems to be fine - the traces are printing what I expect.
      >
      > In my treewalker I then have the rule:
      >
      > printst
      > : #(PRINT expr COLONPRINT) {System.out.println("PRNT");}
      > | #(PRINT expr) {System.out.println("OUTP");}
      > ;

      Provided I don't have a coffee shortage or overdose ;)

      Tree parsers only use a lookahead of 1 so the above rule will indeed give
      nondeterminism. One way to fix it might be:

      printst
      : ( #(PRINT expr COLONPRINT) ) => #(PRINT expr COLONPRINT) {System.out.println("PRNT");}
      | #(PRINT expr) {System.out.println("OUTP");}
      ;

      Alternatively:

      printst { bool colon = false; }
      : #(PRINT expr ( COLONPRINT { colon = true; } )? )
      {
      if( colon )
      System.out.println("PRNT");
      else
      System.out.println("OUTP");
      }
      ;

      Cheers,

      Ric
      --
      -----+++++*****************************************************+++++++++-------
      ---- Ric Klaren ----- j.klaren@... ----- +31 53 4893722 ----
      -----+++++*****************************************************+++++++++-------
      "You know how to use that thing?" [pointing to the sword]
      "Sure.. The pointy end goes into the other guy."
      --- The Mask of Zorro
    • Show all 7 messages in this topic