[antlr-interest] formulaGrammar without backtracking
Романов Артем
arteminus at yandex.ru
Thu Aug 25 05:00:21 PDT 2011
grammar formulaGrammar;
options{
backtrack=true;
}
formula : expr ;
expr : atom ( OPERATOR expr )*
| '(' expr ( OPERATOR expr )* ')' ;
atom : ( NUMBER | ID | function ) ;
function: ID '(' ( expr ',' )* expr ')' ;
NUMBER : ( INT | FLOAT );
fragment INT : '0'..'9'+ ;
fragment FLOAT : ('0'..'9')+ '.' ('0'..'9')* ;
ID : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
OPERATOR: ('+'|'-'|'*'|'/'|'^');
WS : ( ' ' | '\t' ) {$channel=HIDDEN;} ;
This grammar is work, but can i do this without backtracking(without backtracking not compiling)?
sampleinput: func(p1,func2(p1,func4(b)),p3)
And i can not understand why from "sqrt(a)" input grammer return this parse tree:
-formula
-expr
-atom
-function
|sqrt
|(
-expr
-atom
|a
-expr
-atom
|a
|)
instead:
..
|sqrt
|(
-expr
-atom
|a
|)
More information about the antlr-interest
mailing list