[antlr-interest] Re: Expression Parser - identifying invalid expressions

Eric Mahurin eric_mahurin at yahoo.com
Mon Aug 16 19:28:00 PDT 2004


How does your lexer define the tokens?  Assuming LOR is "||" and LAND
is "&&", I don't see where the parser below even accepts "|" and "&".

You might consider separating your expressions into logical/boolean
and numeric.  Then call whichever is appropriate dependent on the context.

Eric

p.s. I suggestion not calling a "conditional" expression a logical or
expression.  Most consider a conditional expression to use the
conditional operators "?" and ":".

--- In antlr-interest at yahoogroups.com, "kumarsriram"
<sriram.kumar at n...> wrote:
> Hi,
> 
> I'm writing a glorified formula parser and borrowed some of the 
> concepts from java.g bundled with the examples in antlr. I'm pretty 
> new to the world of parsers, so pardon my ignorance.
> 
> I have the following sub-rules for conditional, relational, 
> multiplicative, additive expressions that handle simple formulae.
> 
> The trouble is, my parser doesn't seem to catch invalid cases. For 
> example, if there's a conditional expression of the 
> form "expr1>expr2 & expr3<expr4" it doesn't throw an exception to 
> indicate that "&" is invalid (it must be "&&"). It just takes the 
> first predicate "expr1>expr2" and matches it against a "relExpr" 
> rule and proceeds further.
> 
> So I wrote an explicit look ahead as follows:
> 
> // logical or (||)  
> condExpr
> 	:(logicalAndExpression VAR )=>logicalAndExpression (LOR^ 
> expr2:logicalAndExpression)+ 
> 	| logicalAndExpression
> 	;
>     }
> // logical and (&&)  
> logicalAndExpression
> 	:(equalityExpr VAR)=>(expr1:equalityExpr) (LAND^ 
> equalityExpr)+
> 	| equalityExpr
> 	;
>     }
> 
> Is there any other simpler way of specifying the same? Otherwise, I 
> need to perform an explicit lookahead in every rule.
> 
> Regards,
> Sriram
> 
> 
> The complete grammar is:
> 
> class MyParser extends Parser;
> options {
> 	buildAST = true;
> 	k=1;
> }
> 
> // logical or (||)  
> condExpr
> 	:(logicalAndExpression VAR )=>logicalAndExpression (LOR^ 
> expr2:logicalAndExpression)+ 
> 	| logicalAndExpression
> 	;
> 	exception
> 	catch [RecognitionException ex] {
>        throw ex;
>     }
> // logical and (&&)  
> logicalAndExpression
> 	:(equalityExpr VAR)=>(expr1:equalityExpr) (LAND^ 
> equalityExpr)+
> 	| equalityExpr
> 	;
>     }
> // logical and (&&) 
> equalityExpr
> 	:(relExpr (NOT_EQUAL^ | JAVA_EQUAL^))=> relExpr ((NOT_EQUAL^ 
> | JAVA_EQUAL^) relExpr)* 
> 	| relExpr
> 	;
>     }
> // boolean relational expressions 
> relExpr
> 	:	addExpr	 (GT^|GT_EQ^|LT^|LT_EQ^addExpr))*
> 	;
> 		
> // binary addition/subtraction 
> addExpr
> 	:	multExpr ((PLUS^ | MINUS^) multExpr)*
> 	;
> 
> // multiplication/division/modulo 
> multExpr
> 	:	unaryExpr ((STAR^ | DIV^ | MOD^ ) unaryExpr)*
> 	;
> 
> unaryExpr
> 	:	MINUS^ unaryExpr
> 	|	PLUS^ unaryExpr
> 	|	unaryExprNotPlusMinus
> 	;
> 
> unaryExprNotPlusMinus
> 	:	LNOT^ unaryExpr
> 	|	postfixExpr
> 	;
> 
> postfixExpr:
> 	(id:VAR LPAREN)=>
> 		// Matches function call syntax like "pow(a,b)" 
> 		id2:VAR^
> 		(
>  	      	parenArgs
> 		)?
> 	|atom;
> 
> parenArgs:	
> 	LPAREN^ (addExpr(COMMA addExpr)*)? RPAREN^;
> 
> atom:
> 	VAR 
> 	| NUM
> 	| CHAR_LITERAL
> 	| STRING_LITERAL
> 	| INPUT_VAR
> 	| LPAREN! condExpr RPAREN!;



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

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





More information about the antlr-interest mailing list