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

Hill, Robert rhill03 at eds.com
Mon Jun 20 05:18:27 PDT 2005


in the Lexer, create a token block which contains all your tokens. then
in the lexer rule for your identifier
turn on the option to test literals
 
so , below your lexers options (or the lexer class declaration) do
 
tokens {
    TOK_or = "or"
    TOK_const = "const"
}
.... etc
and in your lexer rule for the indentifiers do
 
ID
options { testLiterals = true; }
    :('a'..'z')*
    ;
 
this will cause the lexer to pass your literals as tokens and not ID's
 
me being a relative antlr n00b someone please feel free to correct me!
:)
 
cheers,
 
/2ob
 

Rob Hill

EDS  UKNMSC                '       +44 (0) 114 291 1928 
Innovation Centre               6       +44 (0) 114 291 2000 
217 Portobello          *       +44 (0) 791 732 1227 
Sheffield                       *       rhill03 at eds.com
<mailto:rhill03 at eds.com>  
S1 4DP 


 


________________________________

	From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] 
	Sent: 20 June 2005 11:07
	To: antlr-interest at antlr.org
	Subject: [antlr-interest] Basic Grammar (Literal / Lexer)
	
	

	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/84f62935/attachment-0001.html


More information about the antlr-interest mailing list