[antlr-interest] New to antlr pls help

jbb at acm.org jbb at acm.org
Wed May 14 11:11:56 PDT 2003


solmalik <boy_wonder48 at hotmail.com> 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