[antlr-interest] (newbie) very basic grammar for simple text and integer

ali azimi aliaazimi at yahoo.com
Sun Aug 5 04:27:12 PDT 2007


Hi,
   
  Thank you so much for replying to me. I have tried the indstructions, however the problem still remains. I am simply not able to make the following grammar understand to parse a simple text or integer. When being debugged, and inputed some simple text or integer, the grammar emits the "MismatchedTokenException" error message.
   
  Could you please help me?
   
  The grammar is:
  
text  :Text;
integer :Integer;
 
Text      :(AlphaNumeric|Special|Space|Apostrophe)* ;
Integer       :Decimaldigit+ ;
   
  fragment Apostrophe:'\'';
fragment Space           :(' ')*;
fragment Word            : '.'* AlphaNumeric ( AlphaNumeric | '.' )* ;
fragment CHARACTERSTRING : '\'' ( options{greedy=false;}: (~('\''|'\r'|'\n')| '\'' '\''))* '\'';
fragment Special         :'+'|'-'|'!'|'/'|'>'|'8'|'('|')'|'"'|','|';'|'<'|'='|':'|'?'|'&'|'%'|'.'|'_';  
fragment AlphaNumeric    :Uppercase|National|Lowercase|Decimaldigit;
fragment Decimaldigit    :'0'..'9' ;
fragment National        :'#'|'@'|'"'|'$'|'['|']'|'{'|'}'|'^'|'~' ;
fragment Lowercase       :'a'..'z' ;
fragment Uppercase       :'A'..'Z' ;
   
  NEWLINE:'\r' ? '\n' {skip();};
WS : (' ' |'\t' |'\n' |'\r' )+ {skip();} ;

   
  I am very grateful.
   
  Best regard,
   
  Al
  
Johannes Luber <jaluber at gmx.de> wrote:
  ali azimi wrote:
> Hi,
> 
> Thank you very much in advance for reading this.
> 
> I am trying to write a simple grammar which can parse something like:
> 
> SIGNAL
> Newgame,Probe,Result,
> Endgame,Win,Lose,Score(Integer),Bump (144, 4665);
> 
> Just a simple text and a double integer at the end.
> 
> For this purpose I wrote the following which is a part of much larger
> grammar. It seems that I can not figure out how to write a simple
> grammar for text and integers which causes problem for the rest of the
> grammar.
> 
> Is rule for space ok?
> Is rule for INT ok? (integer starts from 0)
> 
> The grammar is:
> 
> textDigit: 'text' Text DoubleInteger;
> 
> fragment DoubleInteger: '(' INT ',' INT' )' ;
> fragment Special 
> :'+'|'-'|'!'|'/'|'>'|'8'|'('|')'|'"'|','|';'|'<'|'='|':'|'?'|'&'|'%'|'.'|'_';
> fragment Text :(AlphaNumeric|Special|Space|Apostrophe)* ;
> fragment AlphaNumeric :Uppercase|National|Lowercase|Decimaldigit;
> fragment Decimaldigit :'0'..'9' ;
> fragment National :'#'|'@'|'"'|'$'|'['|']'|'{'|'}'|'^'|'~' ;
> fragment Lowercase :'a'..'z' ;
> fragment Uppercase :'A'..'Z' ;
> fragment INT :('0'..'9')+;
> fragment Space :(' ')+;
> fragment Apostrophe :'\'';
> 
> I appreciate a lot.
> 
> Best regards,
> 
> Al 

Without testing I may overlook some problems, but the error I noted is
that you call fragment rules from a parser rule. Only lexer rules may
call them. So remove fragment at least from Text and DoubleInteger. And
possibly change INT to "fragment INT : Decimaldigit+;". I don't see any
problem with Space.

Best regards,
Johannes Luber


       
---------------------------------
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070805/0f5790e0/attachment.html 


More information about the antlr-interest mailing list