[antlr-interest] Basic Grammar (Literal / Lexer)

Craig Main craig at palantir.co.za
Mon Jun 20 03:07:14 PDT 2005


Hi,

 

I have a basic grammar with conditional statements specified as follows.

Hopefully I am on the right track here.

I am creating a simple expression and rule parser.

 

I have specified the logicalOr as LogicalAnd ("or" LogicalAnd)*

 

When I try and put the literal "or" in the lexer (where one assumes it
should be) I get a non determinism between or and an identifier (which is a
c style identifier). What is the correct way to specify the literal "or"
without interfering with the other lexer elements, or should the "or" remain
in the parser?

 

Regards

Craig.

 

 

rules                   : (statement STATEMENT_SEP)* EOF

                        ;

statement               : assignment_statement 

                        | constant_declaration

                        | condition

                        ;

assignment_statement    : ID ASSIGN expression 

                        ;

constant_declaration    : "const" ID ASSIGN atom

                        ;

condition               : "if" LPAREN! conditional_expression RPAREN!
statement

                                  (// CONFLICT: the old "dangling-else"
problem...

                                       //           ANTLR generates proper
code matching

                                       //              as soon as possible.
Hush warning.

                                       options {

                                             warnWhenFollowAmbig = false;

                                       }

                                   : (STATEMENT_SEP)? "else"! statement

                                  )?

                        ;

conditional_expression  : logicalOr

                        ;

logicalOr               : logicalAnd ("or" logicalAnd)*

                        ;

logicalAnd              : equality ("and" equality)*

                        ;

equality                : relational ((EQUAL | NOT_EQUAL) relational)*

                        ;

relational              : expression

                          (( LTHAN

                          |  GTHAN

                          |  LTE

                          |  GTE) expression)*

                        ;

expression              : term ((PLUS|MINUS) term)*

                        ;

term                    : factor ((MULTIPLY|DIVIDE) factor)*

                        ;

factor                  : atom (DASH atom)*

                        ;

atom                    : FLOAT

                        | ID

                        ;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050620/97210cd6/attachment-0001.html


More information about the antlr-interest mailing list