[antlr-interest] Questions on parsing data
    byeung128 
    byeung at etrade.ca
       
    Mon Jun 17 14:23:38 PDT 2002
    
    
  
Hi all,
Since I am a beginner in Antlr, I have read the Antlr example 
of "calc.g" and I am writing my first parser program.
The following grammar is trying to group the address information into 
a class "AddressInfo". 
For example, "180 Yonge street West"  
-> AddressInfo.StreetNumber=180
-> AddressInfo.StreetName=Yonge
-> AddressInfo.StreetType=street
-> AddressInfo.StreetDirection=West.
Before grouping the information, I try to print out the data.
The assignments in the AddrTreeWalker (TreeParser) give me the 
compliation error such as "Assignment from token reference only 
allowed in lexer". How can I resolve it ? How can I change the Parser 
rule and TreeParser rules to group the address information into a 
class "AddressInfo" ? Could you give me some hints ?
Thanks in advance for giving me any hint.
Here is the grammar I have written.
class MyParser extends Parser;
options {
  defaultErrorHandler=false;
  buildAST=true ;
}
{
  int i_count = 0 ;
}
startRule: address
   { System.out.println ( "->>>>>ADDRESS:" + 1 ) ; };
address : NUMBER^ STREETNAME streettype (direction)? ;
streettype:  avenue 
          |  street
          ;
avenue: "AVENUE" 
      | "AVENUE."
      | "AVE."
      | "AVE"
      | "AV."
      | "AV"
      ;
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) ; } ;
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) ); 
     } ;
class AddrTreeWalker extends TreeParser;
myexpr returns [ String r ] 
{
   String a, b, c, d, e;
}
   : #(a=NUMBER b=STREETNAME c=streettype d=direction) 
     { System.out.println(  " a=" + a  +
                            ",b=" + b  +
                            ",c=" + c  +
                            ",d=" + d  ) ;
     }
   ;
                            
 
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
    
    
More information about the antlr-interest
mailing list