[antlr-interest] formulaGrammar without backtracking
John B. Brodie
jbb at acm.org
Thu Aug 25 08:06:41 PDT 2011
Greetings!
On Thu, 2011-08-25 at 16:00 +0400, Романов Артем wrote:
> 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)
try these:
expr : atom ( OPERATOR expr )? ;
atom : NUMBER | ID | function
| '(' expr ')' ;
function: ID '(' ( expr (',' expr)* )? ')' ;
Note that in the above I have added the capability for functions to have
zero parameters......
> 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
> |)
>
do not know about this. does not happen with the above changes. sorry.
hope this helps...
-jbb
More information about the antlr-interest
mailing list