[antlr-interest] Re: Expression Parser - identifying invalid expressions
kumarsriram
sriram.kumar at nomissolutions.com
Mon Aug 16 21:21:32 PDT 2004
Thanks for the response. You are right about the terminology
(logicalOrExpr instead of condExpr). I was just lazy not to change
the name. I'm enclosing the lexer definition.
>> is "&&", I don't see where the parser below even accepts "|"
and "&".
I don't want to process '&' and '|'. Occurence of these characters
in a logical (compound) expression must result in an error.
*************************
class MyLexer extends Lexer;
options {
defaultErrorHandler=false;
testLiterals=false;
k=4;
}
//The following Lexer rule takes care of skipping white space
characters in the formula
WS:
(' ' |'\t'|'\n'|'\r'){ _ttype = Token.SKIP; };
RPAREN:
')' ;
LPAREN:
'(' ;
COMMA:
',' ;
PLUS:
'+';
MINUS:
'-';
MOD:
'%';
DIV:
'/';
STAR:
'*';
SEMI:
';';
DOT:
'.';
LNOT:
'!';
GT:
'>';
LT:
'<';
JAVA_EQUAL:
"==";
NOT_EQUAL:
"!=";
GT_EQ:
">=";
LT_EQ:
"<=";
LAND:
"&&";
LOR:
"||";
protected
DIGIT:
'0'..'9';
NUM:
(DIGIT)+ (DOT (DIGIT)*)?;
VAR:
(scope:ID) (DOT varName:ID)?;
INPUT_VAR: "$" id1:ID (DOT id2:ID)? ;
// character literals
CHAR_LITERAL
: '\'' ( ESC | ~('\''|'\n'|'\r'|'\\') ) '\''
;
// string literals
STRING_LITERAL
: '"' (ESC|~('"'|'\\'|'\n'|'\r'))* '"'
;
protected
ESC
: '\\'
( 'n'
| 'r'
| 't'
| 'b'
| 'f'
| '"'
| '\''
| '\\'
protected
ID
options {
testLiterals = false;
}
: ('a'..'z'|'A'..'Z'|'_')
('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;
--- In antlr-interest at yahoogroups.com, "Eric Mahurin"
<eric_mahurin at y...> wrote:
> 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