[antlr-interest] newbie question about nondeterminism between keywords and identifiers

Martin Nordin martin.nordin at gmail.com
Fri Feb 2 21:35:36 PST 2007


Hi again.

Here is a version that allows date : date; and does some printouts :)

header {
import java.io.*; }

class DateLexer extends Lexer;

options { k=1; }
WS
  :
  (' '
  | '\t'
  | '\r' '\n' { newline(); }
  | '\n'      { newline(); }
  )
  { $setType(Token.SKIP); } ;

IDENT
options {testLiterals=true;}
  : ('_'|'a'..'z')('_'|'a'..'z'|'0'..'9')*
  ;

COLON  : ':';
SEMI   : ';';

class DateParser extends Parser;

{
  public static void main(String[] args)
  {

    // Use a try/catch block for parser exceptions
    try {
      InputStream input  = new StringBufferInputStream("date : date; date1 :
date; an_int : int;");
      DateLexer   lexer  = new DateLexer(input);
      DateParser  parser = new DateParser(lexer);
      parser.declarations();
    }
    catch (Exception e) {
      System.err.println("parser exception: "+e);
      e.printStackTrace();   // so we can get stack trace
    }
  }
}

decl:
  theVar:IDENT COLON theType:IDENT SEMI { System.out.println("Type
declaration. Variable: " + theVar.getText() + " Type: " + theType.getText()
); }
  ;

declarations :
  (decl)*
  ;




On 2/2/07, David Guy <dguy at bea.com> wrote:
>
>  I have tried putting my IDENT lexer rule last, but TYPE_DATE still
> matches first. No matter what order I have tried, TYPE_DATE matches first.
>
>
>
> If "mydate : date" , "mydate" gets tokenized as IDENT, TYPE_DATE,
>
>
>
> I am using ANTLR 2.7.2 in case that matters.
>
>
>
> I am parsing a pre-existing language that I cannot change. Actually,
> "date" can be an identifier and a type.
>
> "date : date" is valid.
>
>
>
>
>
> _______________________________________________________________________
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual
> or entity named in this message. If you are not the intended recipient,
> and have received this message in error, please immediately return this
> by email and then delete it.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070203/45eee229/attachment.html 


More information about the antlr-interest mailing list