History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: ANTLR-29
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Terence Parr
Reporter: Terence Parr
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
ANTLR v3

semantic predicates not hoisted properly

Created: 08/Nov/06 02:19 PM   Updated: 08/Nov/06 02:26 PM
Component/s: ANTLR Core
Affects Version/s: None
Fix Version/s: 3.0b5


 Description  « Hide
TJP: interesting...this in fact has 2 alts that lead to the same recursive rule invocation. I need to modify this message so it shuts up with predicates, but it will have to unravel back to k=1. When you get that message, it means ANTLR will never be able to build the DFA.

[from a user]
Hi,

semantic predicates in the parser don't seem to work in beta 3 as they did in ANTLR 2. Will they be supported in the final?

For example the following grammar:

grammar Test;

document : (element)* EOF ;
element : theA | B | C ;

theA
:
A
(
{ someCondition }?=>
theElement=element
{
// do something with theElement
}
|
// quit the rule
)
;

A : 'a' ;
B : 'b' ;
C : 'c' ;
ANTLR can't handle it:

"[fatal] rule theA has non-LL(*) decision due to recursive rule invocations in alts 1,2. Resolve by left-factoring or using syntactic predicates with fixed k lookahead or using backtrack=true option."
At my opinion, ANTLR should use the first alternative if someCondition is true and the second one if not.

My second approach was to swap the alternatives:

theA
:
A
(
{ !someCondition }?=>
// quit the rule
|
theElement=element
{
// do something with the element
}
)
;
ANTLR compiles this without any errors, but the resulting code is really stange:

[...]
else if ( (LA3_0==A) ) {
else {
NoViableAltException nvae = [...];
throw nvae;
}
}
[...]
It seems that there is a bug in the code generation. But also the decisions are wrong:

[...]
int alt3=2;
int LA3_0 = input.LA(1);
if ( (LA3_0==EOF) && ( !someCondition )) {
alt3=1;
}
[...]
Why should it only quit the rule if an EOF is following? There may be an A, B or C too.

I hope this will get fixed until the final, because semantic predicates are a really powerful feature.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Terence Parr - 08/Nov/06 02:26 PM
fixed as byproduct of ANTLR-13 fix.