Skip to content

Explicitly resolving if/if/else ambiguity in a grammar #42

Open
parrt/antlr4
#126
@sharwell

Description

@sharwell

I'm working with an ANTLR 4 grammar to correct any construct which results in a call to reportAmbiguity. In many cases this is done by refactoring or combining rules, but the case for an if/if/else construct is proving more challenging.

Starting with the following grammar:

grammar T;
stmt : ifStmt | ID;
ifStmt : 'if' ID stmt ('else' stmt)?;

ID : [a-zA-Z]+;
WS : ' '+ -> skip;

Given the input if x if x a else b, I'm looking for a way to modify the grammar such that no call to reportAmbiguity is needed, while following the standard if/else rule of binding to the closest unmatched if statement.

In ANTLR 3, a syntactic predicate allowed the user to explicitly handle this situation. In ANTLR 4, the correct decision is made through min-alt resolution, but it always wants to emit a "warning" regarding the situation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions