[antlr-interest] Re: Differentiating keywords in parser and identifiers in lexers

Sharon Li hushlee83 at yahoo.com.sg
Wed Jan 29 14:52:55 PST 2003


yup i did. my codes are as follows:
class AntlrParser extends Parser;
options { k=4; }
{
 ViewFileInfo file = new ViewFileInfo();
 
 public ViewFileInfo getViewFileInfo(){
  return file;
 }
}
keyword : ANGLE FACTOR 
     {
      System.out.println("Keyword: angle factor");
     }
    | INITIAL COLOR 
     {
      System.out.println("Keyword: initial color");
     }
    | COLOR INCREMENT
     {
      System.out.println("Keyword: color increment");
     }
    | INITIAL LINE WIDTH
     {
      System.out.println("Keyword: initial line width");
     }
    | LINE WIDTH INCREMENT 
     {
      System.out.println("Keyword: line width increment");
     }
    | VIEWPOINT
     {
      System.out.println("Keyword: viewpoint");
     }
    | VIEW REFERENCE POINT
     {
      System.out.println("Keyword: view reference point");
     }
    | TWIST
     {
      System.out.println("Keyword: twist");
     }
    | PROJECTION
     {
      System.out.println("Keyword: projection");
     }
    | FRONT DISTANCE
     {
      System.out.println("Keyword: front distance");
     }
    | BACK DISTANCE
     {
      System.out.println("Keyword: back distance");
     }
    | SCALE FACTOR
     {
      System.out.println("Keyword: scale factor");
     }
    | Z BUFFER
     {
      System.out.println("Keyword: z buffer");
     }
    | CUE RANGE
     {
      System.out.println("Keyword: cue range");
     }
    | RENDER MODE
     {
      System.out.println("Keyword: render mode");
     }
    | LINE STYLE
     {
      System.out.println("Keyword: line style");
     }
    | LIGHT DIRECTION
     {
      System.out.println("Keyword: light direction");
     }
    | DIFFUSE REFLECTION
     {
      System.out.println("Keyword: diffuse reflection");
     }
    | TROPISM DIRECTION
     {
      System.out.println("Keyword: tropism direction");
     }
    | INITIAL ELASTICITY
     {
      System.out.println("Keyword: initial elasticity");
     }
    | ELASTICITY INCREMENT
     {
      System.out.println("Keyword: elasticity increment");
     }
    | FUNCTION
     {
      System.out.println("Keyword: function");
     }
    ;
    
value : IDENT | NUMERIC (SYMBOL NUMERIC)? (IDENT)? 
    ;
file   : ( line (NEWLINE line)*(NEWLINE)? EOF)
       {System.out.println("file matched");}
       ;
line   : ((record)+ )
       ;
record : ((keyword) (c:COLON)? (value) (COMMENT)?)
      ;
    
/**---------------------------------- Lexer class ------------------------------------------------**/
class AntlrLexer extends Lexer;
options { 
 charVocabulary='\3'..'\377'; 
 k = 4;
}
tokens
{
 ANGLE       = "angle";
 FACTOR      = "factor";
  INITIAL     = "initial";
  COLOR    = "color";
  LINE    = "line";
  WIDTH    = "width";
  VIEWPOINT   = "viewpoint";
  VIEW    = "view";
  REFERENCE  = "reference";
 POINT    = "point";
 TWIST    = "twist";
 PROJECTION = "projection";
 FRONT    = "front";
 DISTANCE  = "distance";
 BACK    = "back";
 SCALE    = "scale";
 Z           = "z";
 BUFFER   = "buffer";
 CUE     = "cue";
 RANGE    = "range";
 RENDER      = "render";
 MODE        = "mode";
 STYLE    = "style";
 LIGHT    = "light";
 DIRECTION  = "direction";
 DIFFUSE   = "diffuse";
 REFLECTION = "reflection";
 TROPISM   = "tropism";
 FUNCTION  = "function";
 ELASTICITY  = "elasticity";
 INCREMENT   = "increment";
}
protected SYMBOL : ('-'|'+'|'%'|'/'|'.'|',')+;
protected DIGIT   : ( '0'..'9' ) ; 
protected LETTER  : ( 'a'..'z' | 'A'..'Z' ) ; 
NUMERIC :(SYMBOL)? (DIGIT)+ (SYMBOL DIGIT)* 
    {
     System.out.println("Printed in lexer NUMERIC: "+getText());
    }
    ;
IDENT  options { testLiterals = true; }:  LETTER ((SYMBOL)? LETTER)* 
   {
    System.out.println("Printed in lexer IDENT: "+getText());
   }
   ;
COLON : ':' {System.out.println(getText());};
COMMENT : "/*" (options {greedy=false;} :.)* "*/" ;
NEWLINE : ('\r''\n')=> '\r''\n' //DOS
        | '\r'                  //MAC
        | '\n'                  //UNIX
        { newline(); }
        ;
    
WS      : (' '|'\t') { $setType(Token.SKIP); } ;
 
 "micheal_jor <open.zone at virgin.net>" <open.zone at virgin.net> wrote:--- In antlr-interest at yahoogroups.com, Sharon Li 
wrote:
> 
> hi,
> 
> how do I prevent Antlr from recognising keywords to be identifiers? 
I can't think of a way in which i can specify which words can be 
identifiers and which should not.

Have you actually tried the advice from my previous reply about the 
using the "tokens {...}" section in your grammar?

http://www.antlr.org/doc/metalang.html#TokensSection

> I have looked at previous messages but it still doesn't register. 
Perhaps someone can provide an example. 

The link above has one that shows "void" and "int" as "keywords" and 
not identifiers.

Micheal





Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 


 Yahoo! Travel
- Valentine surprise deals. Book now!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20030130/82d390a8/attachment.html


More information about the antlr-interest mailing list