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

Johannes Luber jaluber at gmx.de
Sat Aug 4 09:47:18 PDT 2007


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


More information about the antlr-interest mailing list