[antlr-interest] ANTLR grammar: Clarifications needed

Bharath S bharath at starthis.com
Wed Apr 28 14:34:53 PDT 2004


Hi Mark,

If I have rules in the lexer which go:

BOOLEAN: '1'|'0';
NUMBER: ('0'..'9')+;

I get non-determinisms, as expected, due to '1' and '0'. 

1) If I make BOOLEAN "protected", I am unable to refer it in the PARSER. 

2) If I make BOOLEAN "protected" and use $setType command in another rule, 
----------------
Number_or_bit: ('0'|'1') {$setType(BOOLEAN); $setType(NUMBER);} | ('2'..'9')
{$setType(NUMBER);}
----------------
It doesn't work.

3) If I say "boolean: "1"|"0";" in the parser, it doesn't work as I thought.

How can I use BOOLEAN and NUMBER based on the context in which they appear
without having non-determinisms?

Thanks a mil for your comments!

Bharath

-----Original Message-----
From: Mark Lentczner [mailto:markl at glyphic.com] 
Sent: Wednesday, April 28, 2004 4:25 PM
To: antlr-interest at yahoogroups.com
Subject: Re: [antlr-interest] ANTLR grammar: Clarifications needed


> Also, I am wondering how to differenciate between two rules, BOOLEAN 
> and
> INTEGER in the lexer. They are of the form:
>
> BOOLEAN: '1'|'0';
> INTEGER: ('0'..'9')+(UNDERSCORE ('0'..'9')+ )?;
>
> Can I differenciate these rules without making them "protected"?

Well, that depends on what you mean by these rules.  How should these 
parse? :
	" 1 " ==> BOOLEAN<1> or NUMBER<1> ?
	" 10 " ==> NUMBER<10> or BOOLEAN<1>, BOOLEAN<0> ?
If the answer depends on the parsing context, then no, you generally 
cannot differentiate these rules in the lexer.  (Actually you can, by 
having the parser set state back in the lexer, but that should be 
considered a last-resort sort of solution.)

Typically, the lexer would simply tokenize these as all NUMBER, and 
then it the parser, which knows when it is in a boolean context, can 
place a constraint on the values of NUMBER that it accepts.

	- Mark



 
Yahoo! Groups Links



 






 
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