How to remove ANTLR3 Multiple Alternatives Warning

I found several "multiple alternatives" questions on stackoverflow but nothing helped. Here is part of my g file in antlr3.

statement:
    selection_stmt
    | expression_stmt 
    | compound_stmt
    | iteration_stmt
    | return_stmt
    ;

selection_stmt:
    IF OPENB expression CLOSB statement (ELSE statement)?
    ;

expression:
    (var ASSIGN expression) => assignment 
    | simple_expression
    ;

      

The problem I'm running into is the following warning for the phrase ELSE statement

above.

(200): Solution could match an input like "ELSE" using multiple alternatives: 1, 2. As a result, alternative 2 was disabled for this input

Can someone explain to me what is going on here? Thank.

PS When I use the syntactic predicate like ((ELSE)=>ELSE statement)?

, the warning disappears. I also have no reason for this.

+3


source to share


1 answer


Bhatia wrote:

Can someone explain to me what is going on here? Thank.

Suppose your input is:

if (A) if (B) return 1 else return 2

      

Your grammar is ambiguous: the parser doesn't "know" how to interpret it. It could have done it in two ways (I added curly braces to emphasize which if

block the block belongs to else

):

1

if (A) 
{
  if (B)
  {
    return 1
  }
  else
  {
    return 2
  }
}

      

enter image description here



2

if (A)
{
  if (B)
  {
    return 1
  }
}
else
{
  return 2
}

      

enter image description here

Bhatia wrote:

PS When I use the syntactic predicate like ((ELSE)=>ELSE statement)?

, the warning disappears. I don't understand the reason for this either.

By adding the predicate before else

, you force the parser to choose for parameter # 1 (and the parser no longer warns, because you explicitly tell it to choose alternative # 1 over # 2). This results in the same thing as not putting the predicate there, but then ANTLR will warn you about this and mention that it will ignore option # 2 and also select # 1 (message: "As a result, alternative 2 was disabled for this input" ).

Now the answer to your question is in the heading "how to remove the warning" multiple alternatives "?" would have to make your grammar unambiguous (or just leave the predicate there, but understand that there will never be parsing as shown in option # 2!). You can do this by entering some kind of delimiter block expressions such as {

and }

. Or do as it makes Python: make sure that the amount of whitespace indentation different from that to which belongs if

a else

. The choice is yours.

+5


source







All Articles