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

Loring Craymer lgcraymer at yahoo.com
Tue Apr 10 13:14:27 PDT 2007


In LALR BNF parsers (like yacc and GOLD), left
recursion is commonly used for implementing iterative
constructs via recursion.  In LL parsers, left
recursion is evil.

Change your definition of condition to

condition:
     and_condition (LOR and_condition)*
     ;

and that should make ANTLR happy; do the equivalent
for each of the other left-recursive definitions and
your problems should disappear (for this round, at
least).

--Loring

--- Russell <russell.contact at gmail.com> wrote:

> 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.
> 



       
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 


More information about the antlr-interest mailing list