[antlr-interest] Re: New to antlr pls help

solmalik boy_wonder48 at hotmail.com
Thu May 15 07:48:59 PDT 2003


hi thanks for the reply, it seems to be workinf fine, except for one 
thing which is that the gramar seems to be accepting any sequece of 
letters e.g "aaa" when it should only accept the one letter "a", how 
would it be possible to reflect this in the grammar? Thanks.
--- In antlr-interest at yahoogroups.com, jbb at a... wrote:
> 
> solmalik <boy_wonder48 at h...> queried as follows:
> >Hi there I am trying to write a formal grammar using antlr but am 
> >having some problems in getting the right syntax.  The grammr is 
to 
> >be used in order to parse process expression, some examples of the 
> >expressions are:
> >
> >(a || b) ^ (a || c)
> >
> >or 
> >(a;b)
> >
> >or e sy b sy a
> >
> >I have tried to capture the three different above forms in the 
> >grammar but am having trouble getting it right, is there anyone 
who 
> >has an idea on how to do it?
> 
> Hope this helps...
> //BEGIN-----------------------cut here cut here---------------------
--
> class P extends Parser;
> 
> startRule
> 	: (  equation NEWLINE )+ EOF
>     	;
> 
> equation: mineqn (operation mineqn)*
> 	;
> 
> mineqn	: ID
> 	| OPEN equation CLOSE
> 	;
> 
> operation
> 	: SEMICOLN
> 	| BAR_BAR
> 	| CARET
> 	| ASTERISK
> 	| SY_OP
> 	| RS_OP
> 	;
> 
> class L extends Lexer;
> 
> options {
>     testLiterals = true;
> }
> 
> tokens {
>     SY_OP = "sy";
>     RS_OP = "rs";
> }
> 
> OPEN:	'(' ;
> 
> CLOSE:	')' ;
> 
> SEMICOLN:	';' ;
> BAR_BAR	:	"||";
> CARET	:	'^' ;
> ASTERISK:	'*' ;
> 	
> ID	:  ('a' .. 'z' | 'A' .. 'Z')
> 	   ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_')*; 
> 
> NEWLINE : ('\r''\n')=> '\r''\n' //DOS
>         | '\r'                  //MAC
>         | '\n'                  //UNIX
>         { newline(); }
>         ;
>    
> WS      : (' '|'\t') { $setType(Token.SKIP); } ;
> //END------------------------cut here cut here----------------------
--


 

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




More information about the antlr-interest mailing list