[antlr-interest] DFA problem

Brent Yates brent.yates at gmail.com
Thu May 22 20:38:48 PDT 2008


I have run across a problem when building gUnit tests for my grammar using
the 3.1b1 release.  The code (java) that ANTLR generates when trying to
predict alternatives changes from being LA() based to DFA based from compile
to compile when I make changes to (seemingly) unrelated sections of the
grammar.  In general, I don't care but in this case the DFA produced fires a
NoViableAltException when the prediction fails (which is correct), but the
calling rule function does not catch the exception and therefore the rule
fails even though it is valid for the predtion to fail (the alt is
optional).

For an example, see the rule below.  The section starting on the line after
the : is optional (see the ending ')?' ).  If I feed in a single
UNSIGNED_NUMGER token followed by a EOF token to the rule, the prediction
DFA fails with a NoViableAltException while the LA() predication code fails,
but the integral number rule returns ok.  The NoViableAltException is not
caught by the calling rule code.

integral_number
    :    (s=UNSIGNED_NUMBER -> ^(TOK_NUM_RAW $s))
        (
             (HEX_BASE)     => (HEX_BASE     post_base_number    ->
^(TOK_NUM_HEX ^(TOK_NUM_SIZE $s) post_base_number))
        |    (BINARY_BASE)  => (BINARY_BASE  post_base_number    ->
^(TOK_NUM_BIN ^(TOK_NUM_SIZE $s) post_base_number))
        |    (OCTAL_BASE)   => (OCTAL_BASE   post_base_number    ->
^(TOK_NUM_OCT ^(TOK_NUM_SIZE $s) post_base_number))
        |    (DECIMAL_BASE) => (DECIMAL_BASE post_base_number    ->
^(TOK_NUM_DEC ^(TOK_NUM_SIZE $s) post_base_number))
        )?
    |    HEX_BASE        post_base_number    -> ^(TOK_NUM_HEX
post_base_number)
    |    BINARY_BASE     post_base_number    -> ^(TOK_NUM_BIN
post_base_number)
    |    OCTAL_BASE      post_base_number    -> ^(TOK_NUM_OCT
post_base_number)
    |    DECIMAL_BASE    post_base_number    -> ^(TOK_NUM_DEC
post_base_number)
    ;


So my questions are:

1) What is the mechanism used by ANTLR to decide when to use a DFA versus
expilcit LA() checks?
2) Is it possible to force the use of LA() checks for prediction?
3) If the prediction DFA fails due to NoViableAltException but the predicted
ALT is optional, should the calling rule catch the exception?

Regards,

Brent Yates
brent.yates at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080522/5e888376/attachment.html 


More information about the antlr-interest mailing list