[antlr-interest] Need help with a simple grammer.

Russell russell.contact at gmail.com
Tue Apr 10 13:05:12 PDT 2007


I am new to Antlr and am trying to create a parser that will parse a syntax
similar to the
WHERE clause of an sql statement.

eg. SalesOrderNumber of SalesOrder = SalesOrderNumber of Invoice

It also needs to support parenthesis and the logical operators AND and
OR

eg. (CaptureDate of SalesOrder = CaptureDate of Invoice) and
    (CaptureDate of Invoice = Date of PublicHolidays)

I created and tested simple grammer using the GOLD Parser
Builder<http://www.devincook.com/GOLDParser/index.htm>
 as
follows

*      "Start Symbol" = <Condition>

      <Condition> ::=
                    <Condition> 'or' <AndCondition>
                    | <AndCondition>

      <AndCondition> ::=
                   <AndCondition> 'and' <NegCondition>
                    | <NegCondition>

      <NegCondition> ::=
                     'not' <SimpleCondition>
                     | <SimpleCondition>

      <SimpleCondition> ::=
                       <TableField> '=' <TableField>
                        | '(' <Condition> ')'

      <TableField>  ::=
                      ID 'of' ID


      ID = {Letter}{Alphanumeric}*

*Which seems to work fine in GOLD. Then I tried converting it to Antlr with
the following grammer:

*      class KOParser extends Parser;

      condition : condition LOR andcondition
                  | andcondition
      ;

      andcondition : andcondition LAND negcondition
                     | negcondition
      ;

      negcondition : NOT simplecondition
                     | simplecondition
      ;

      simplecondition : tablefield EQUALS tablefield
                        | LPAREN condition RPAREN
      ;

      tablefield  : ID "of" ID
      ;


      class KOLexer extends Lexer;

      tokens {
        LAND = "and";
        LOR = "or";
        NOT = "not";
        EQUALS = "=";
      }

      LPAREN : '(';
      RPAREN : ')';


      ID:
      (('A'..'Z') | ('a'..'z'))+ ('0'..'9')*
      ;

      WS:
      (' '|'\n') {$setType(Token.SKIP);}
      ;

*However when I try to build this I get the following errors:

C:\Java\Workspaces\NetBeans\RuleParser\src>java -cp
C:\antlr\277\lib\antlr.jar a
ntlr.Tool koParse.g
ANTLR Parser Generator   Version 2.7.7 (20060930)   1989-2005
koParse.g:6:13: infinite recursion to rule condition from rule condition
koParse.g:10:16: infinite recursion to rule andcondition from rule
andcondition
koParse.g:7:15: infinite recursion to rule andcondition from rule condition
koParse.g:6:13: infinite recursion to rule condition from rule condition
koParse.g:7:15: infinite recursion to rule andcondition from rule condition
koParse.g:6: warning:nondeterminism between alts 1 and 2 of block upon
koParse.g:6:     k==1:"not",LPAREN,ID
Exiting due to errors.

If anybody point me in the right direction it would be much appreciated.

Thanks in advance,
Russell.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070410/d2f92677/attachment.html 


More information about the antlr-interest mailing list