Lexer predicates and DFA State Cache - resettable?

35 views
Skip to first unread message

jaapsp...@googlemail.com

unread,
Jun 13, 2013, 6:24:34 PM6/13/13
to antlr-di...@googlegroups.com
Hi,

In a language, the following are valid floating-point values:
47.11
47.
.11

The language also has a construct to define a range such as:
a[3..5] or {1, 2, 3}[$-1..$]

So I defined

TO: '..';
FLOAT: INT? '.' INT?;

Using them in that way, the lexer/parser correctly identify the difference between the two as long as the range expression uses spaces around the .. sequence.

So I added a semantic predicate to my lexer at the point it has found the '.' at which point it only considers the thing a FLOAT if it's not followed by a second '.', i.e. like so:
INT? '.' { _input.LA(1) != '.' }? INT?

And yay me! Works a treat; my unit test runs green.

But! When I run all my unit tests together a number of tests involving FLOAT values start failing. All these tests pass successfully if I run them individually (method by method or class by class).

Did a lot of digging around, and found something about caching of DFA states even when instantiating new lexers and parsers for every run.

Question is: could it be that the DFA State "cache" does not handle the presence of the semantic predicate properly. Alternatively (probably the more likely scenario), did I define the predicate wrongly?

Sorry if this was unclear, it's late and I've had a long day.

(Using ANTLR 4 -- retrieved by Maven 08May2013, using JUnit 4.11 in Eclipse Juno)

Thanks for any help,

Sam Harwell

unread,
Jun 13, 2013, 8:33:04 PM6/13/13
to antlr-di...@googlegroups.com
Try being more explicit with your FLOAT rule.

FLOAT
: INT '.' INT
| INT '.' {_input.LA(1) != '.'}?
| '.' INT
;

--
Sam Harwell
Owner, Lead Developer
http://tunnelvisionlabs.com
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


jaapsp...@googlemail.com

unread,
Jun 14, 2013, 12:04:08 AM6/14/13
to antlr-di...@googlegroups.com, jaapsp...@googlemail.com

Thanks! That did indeed do the trick.

Jaap

Reply all
Reply to author
Forward
0 new messages