[antlr-interest] Java.g grammar issues & potential fix
Vincent Mallet
vmallet at jfouffa.com
Wed Apr 7 01:10:45 PDT 2004
Terence & All,
A problem in Clover (code coverage for Java which uses antlr)
prompted me to look at the java.g "1.4 update" grammar and I found
a few problems with the lexer:
1. the grammar won't accept octal-looking floats or doubles
(09d for example)
2. the grammar will accept hexadecimal numbers with a fractional
part (0x12.34 for example)
I have seen a fix for problem #1 in java.g version 1.21-october-2003
but #2 is definitely broken.
In an attempt to clarify that part of the grammar I came up with a
version of the NUM_INT lexer rule which seems to be more correct and
potentially easier to read (indentation might need some work).
If you have an opinion on the new rule I would love to hear it.
Here it goes:
take java.g, truncate evertyhing after "NUM_INT", replace with:
---cut-cut---
// a numeric literal
NUM_INT
: // all floating point literals with a whole-number part
( (DIGIT)+ ('.' | 'e' | 'E' | 'f' | 'F' | 'd' | 'D' ) )=>
(DIGIT)+
( (('.' (DIGIT)* (EXPONENT)? | EXPONENT )
(('f'|'F') { _ttype = NUM_FLOAT; } | ('d' | 'D')? {
_ttype = NUM_DOUBLE; } )
)
| ( ('f'|'F') { _ttype = NUM_FLOAT; } )
| ( ('d'|'D') { _ttype = NUM_DOUBLE; } )
)
| // all floating point literals with no whole-number part
(possibly just '.')
'.' { _ttype = DOT; }
(DIGIT)+ (EXPONENT)?
( ('f'|'F') { _ttype = NUM_FLOAT; } | ('d' | 'D')? { _ttype
= NUM_DOUBLE; } )
| // all decimal literals
( '0' ('0'..'7')+ // octal
| ('1'..'9') (DIGIT)+ // decimal
| '0' ('x' | 'X') (HEX_DIGIT)+ // hexa
| '0' // decimal
)
(('l' | 'L') { _ttype = NUM_LONG; })?
;
// a couple protected methods to assist in matching floating point
numbers
protected
DIGIT
: '0'..'9'
;
protected
EXPONENT
: ('e'|'E') ('+'|'-')? ('0'..'9')+
;
---cut-cut---
Thanks,
Vince.
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