[antlr-interest] Question on recognizing a pattern
Brian Smith
brian-l-smith at uiowa.edu
Wed Jun 12 11:04:38 PDT 2002
parser rules must start with a lowercase letter.
lexer rules must start with an uppercase letter.
ANTLR assumes that, because all your rules start with an uppercase
letter, they are meant to be lexer rules.
- Brian
byeung128 wrote:
> I am just a beginner on Antlr and I really think it is a gift for any
> developers. After reading some examples, I am trying to write a
> parser to recognize a address pattern like "NUMBER STREETNAME
> STREETTYPE DIRECTION". When I compiled the following source, I got
> error messages like "Lexical rule STREETYPE defined outside of
> lexer...". However, I declare all these rules inside the parser
> section not lexer section. Could you tell me what goes wrong ? Could
> you give me some advice on this grammar ? Thanks in advance.
>
> class MyParser extends Parser;
> {
> int i_count = 0 ;
> }
>
> startRule: a:address
> { System.out.println ( "->>>>>ADDRESS:" + a.getText() ); };
>
>
> address : NUMBER STREETNAME STREETTYPE DIRECTION ;
>
> STREETTYPE: AVENUE
> | STREET
> ;
>
> AVENUE: "AVENUE"
> | "AVENUE."
> | "AV"
> | "AV."
> | "AVE"
> | "AVE."
> ;
>
> STREET: "STREET"
> | "STR"
> | "STR."
> | "ST."
> | "ST"
> ;
>
> DIRECTION: EAST
> | WEST
> | SOUTH
> | NORTH
> | NORTHWEST
> | SOUTHWEST
> | NORTHEAST
> | SOUTHEAST
> ;
>
> EAST: "EAST"
> | "E."
> | "E"
> ;
>
> WEST: "WEST"
> | "W."
> | "W"
> ;
>
> NORTH: "NORTH"
> | "N."
> | "N"
> ;
>
> SOUTH: "SOUTH"
> | "S."
> | "S"
> ;
>
> NORTHWEST: "NORTHWEST"
> | "NW."
> | "NW"
> ;
>
> NORTHEAST: "NORTHEAST"
> | "NE."
> | "NE"
> ;
>
> SOUTHWEST: "SOUTHWEST"
> | "SW."
> | "SW"
> ;
>
> SOUTHEAST: "SOUTHEAST"
> | "SE."
> | "SE"
> ;
>
> class MyLexer extends Lexer;
> options {
> charVocabulary = '\3'..'\377' ;
> testLiterals=false ;
> k = 2 ;
> }
>
> {
> int i_STREETNAMECount = 0;
> int i_DICount = 0;
> int i_WSCount = 0;
> }
>
> WS : ( ' ' | '\t' | '\n' | '\r' )
> { $setType(Token.SKIP) ;
> System.out.println ( "WS:" + (++i_WSCount) ); } ;
>
> STREETNAME
> options { testLiterals = true ; }
> : ( 'a'..'z' | 'A'..'Z' )+
> { System.out.println ( "CSTREETNAME:" + $getText ) ;
> System.out.println ( "STREETNAME:" + (++i_STREETNAMECount) ); } ;
>
> NUMBER: ( '0'..'9' )+
> { System.out.println ( "CNUMBER:" + $getText ) ;
> System.out.println ( "NUMBER:" + (++i_DICount) ); } ;
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list