[antlr-interest] Disambiguating Decimal and Integer '.'
Kaleb Pederson
kaleb.pederson at gmail.com
Thu Oct 1 12:12:17 PDT 2009
Everything is an object in the language I'm creating. Thus, I allow:
"string".someFunction(), and
1234.equals(1234)
I'm using the latter as a testcase. It's being parsed as follows:
1234. =>
1. Take object production
2. Take alt2
3. Consume 1234
4. See '.'
4. Try DECIMAL production
5. Consume '.'
6. Throw execption because expected INTEGER is not found
After parsing the above the DECIMAL non-terminal throws an exception
since the missing INTEGER non-terminal isn't present. How can I get
ANTLR to accept the INTEGER production instead?
And here's the relevant grammar portions:
object
: ID ('.' ID)* objectSuffix?
| literal ('.' ID objectSuffix)?;
objectSuffix
: '(' arguments? ')';
arguments
: argument (',' argument)*;
argument
: object;
literal:
QUOTED_STRING
| INTEGER
| DECIMAL;
fragment NUMBER: '0'..'9';
INTEGER
: NUMBER+;
DECIMAL
: INTEGER '.' INTEGER;
fragment ID_BASIC_CHARSET: ('a'..'z' | 'A'..'Z' | '_');
ID : ID_BASIC_CHARSET (ID_BASIC_CHARSET | NUMBER)*;
As an alternative, I tried the following (IIRC):
object
: ID ('.' ID)* objectSuffix?
| (INTEGER '.' ID objectSuffix) => INTEGER '.' ID objectSuffix
| literal ('.' ID objectSuffix)?;
I didn't step through the above, but it seemed to give the exact same
behavior as the above cases. I believe there's enough syntactical
information that I should be able to make this work. Suggestions?
Thanks.
--Kaleb
http://twitter.com/kalebpederson
http://kalebpederson.com
More information about the antlr-interest
mailing list