[antlr-interest] simple novice grammar question
Mark Lentczner
markl at glyphic.com
Tue May 25 07:44:42 PDT 2004
> "(x>3)(y=='somestring')" pass as a valid string when I would like it
> to fail.
Hmmmm, it doesn't pass with the grammar you supplied. Are there
perhaps other rules involved that you didn't list? If startRule is
used in any other rule, then it parsing for startRule will match the
prefix of any string ("(x>3)") and just leave the rest unmatched with
no error ("(y=='somestring')"). If this is the case, put in a
debugging rule that isn't used by any other rule. Top level rules have
an implicit EOF match at the end which is what you need.
> I've attached just the parser below...
Your grammar was a bit too permissive. Here is a clean up:
startRule : filter ;
filter : LPAREN! bool_or RPAREN! ;
bool_or : bool_and (OR^ bool_and)* ;
bool_and : bool_comp (AND^ bool_comp)* ;
bool_comp : factor (bool_op^ factor)? ;
bool_op : EQ | NEQ | LT | LTE | GT | GTE ;
factor : filter | IDENTIFIER | NUMBER | STRING ;
Notes:
1) Generally there is no need to make the LPAREN the head of the tree
in the filter (or factor) rule. The tree implicitly has the correct
order of operations in it and doesn't need grouping operators.
2) bool_comp_seq served no purpose
3) bool_comp shouldn't have a * at the end - bool_op's generally aren't
associative. The question is what should "x < y < z" mean. In
languages where "x < y" returns true or false (such as Java or C++),
then associative bool_ops would result in "x < y < z" being parsed as
"(x < y) < z" which would result in a boolean value being used in a
less than comparison. This would hardly be what the programmer wants.
Languages take one three tacks:
3a) let the programmer suffer, and boolean ops are associative like
all ops (Java, C++)
3b) make bool ops non-associative so "x < y < z" generates a compile
time error (Perl 5)
3c) support combining bool ops so "x < y < z" is the same as "(x < y)
&& (y < z)" but only evaluating y once. (Cobol, Perl 6)
The last is nice, but harder to design into a language.
4) The separate bool_op rule makes the grammar cleaner.
- Mark
Mark Lentczner
markl at wheatfarm.org
http://www.wheatfarm.org/
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