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

Multiple optional fields question

Expand Messages
  • mynetadm
    Hi list I m doing an parser to test de ANTLR capabilities and I have one problem,,,, we have something like : test+1200+234EM123+1+TEST; where fields : test,
    Message 1 of 2 , Dec 13, 2004
    • 0 Attachment
      Hi list

      I'm doing an parser to test de ANTLR capabilities and I have one
      problem,,,,

      we have something like :

      test+1200+234EM123+1+TEST;

      where fields : test, 1200 , 234EM123 are mandatory and 1 and TEST are
      optional. I have to support options like :

      test+1200+234EM123++TEST; or test+1200+234EM123+1 or test+1200+234EM123;

      so I made :

      class myParser extends Parser;
      options { k=4; }
      test: ( field1:TEXT DELIM field2:NUMBER DELIM copositeField
      (DELIM (field3:DIGIT)?)?
      (DELIM (field4:TEXT)?)?
      ENDLINE
      );
      compositeField: (
      na:NUMBER t:TEXT nb:NUMBER
      )
      class myLexer extends Lexer;
      DIGIT: ('0'..'9');
      NUMBER: (DIGIT)(DIGIT)+;
      CHAR: ('a'..'z' | 'A'..'Z');
      TEXT: (CHAR)(CHAR)+;
      ENDLINE: ";";


      the problem is that :

      test+1200+234EM123+1+TEST; interprets corectly
      test+1200+234EM123+1; interprets corectly
      test+1200+234EM123++TEST; interprets corectly
      test+1200+234EM123; gives me an unexpected end of line

      Can anyone help me please?


      BR,
      alecs
    • Alexey Demakov
      Hi alecs, The problem is in lookahead k=4. Your grammar is simple enough for k=1. Of course, ( DELIM( field3:DIGIT )? )? will conflict with the next ( DELIM (
      Message 2 of 2 , Dec 14, 2004
      • 0 Attachment
        Hi alecs,

        The problem is in lookahead k=4.
        Your grammar is simple enough for k=1.
        Of course, ( DELIM( field3:DIGIT )? )? will conflict with the next
        ( DELIM ( field4:TEXT )? )? but it can be managed
        with
        ( options { warnWhenFollowAmbig = false; } :
        DELIM (field3:DIGIT)?
        )?
        because default ANTLR behaviour is to consume DELIM (field3:DIGIT)?
        if DELIM is found.

        I use k=1 almost always, local conflicts can be resolved
        using predicates.

        Regards,
        Alexey

        -----
        Alexey Demakov
        TreeDL: Tree Description Language: http://treedl.sourceforge.net
        RedVerst Group: http://www.unitesk.com

        ----- Original Message -----
        From: "mynetadm" <alecsandru.chirosca@...>
        To: <antlr-interest@yahoogroups.com>
        Sent: Tuesday, December 14, 2004 10:52 AM
        Subject: [antlr-interest] Multiple optional fields question

        > Hi list
        >
        > I'm doing an parser to test de ANTLR capabilities and I have one
        > problem,,,,
        >
        > we have something like :
        >
        > test+1200+234EM123+1+TEST;
        >
        > where fields : test, 1200 , 234EM123 are mandatory and 1 and TEST are
        > optional. I have to support options like :
        >
        > test+1200+234EM123++TEST; or test+1200+234EM123+1 or test+1200+234EM123;
        >
        > so I made :
        >
        > class myParser extends Parser;
        > options { k=4; }
        > test: ( field1:TEXT DELIM field2:NUMBER DELIM compositeField
        > (DELIM (field3:DIGIT)?)?
        > (DELIM (field4:TEXT)?)?
        > ENDLINE
        > );
        > compositeField: (
        > na:NUMBER t:TEXT nb:NUMBER
        > );
        > class myLexer extends Lexer;
        > DIGIT: ('0'..'9');
        > NUMBER: (DIGIT)(DIGIT)+;
        > CHAR: ('a'..'z' | 'A'..'Z');
        > TEXT: (CHAR)(CHAR)+;
        > ENDLINE: ";";
        >
        >
        > the problem is that :
        >
        > test+1200+234EM123+1+TEST; interprets corectly
        > test+1200+234EM123+1; interprets corectly
        > test+1200+234EM123++TEST; interprets corectly
        > test+1200+234EM123; gives me an unexpected end of line
        >
        > Can anyone help me please?
        >
        >
        > BR,
        > alecs
      Your message has been successfully submitted and would be delivered to recipients shortly.