Group terminals into set
Asked Answered
B

1

10

Group terminals into sets

What does this warning mean ? How do I solve it ?

Here is the code I am referring to

expression : expression operator=DIV expression
           | expression operator=MUL expression
           | expression operator=ADD expression
           | expression operator=SUB expression
           | INT
           | FLOAT
           | BOOLEAN
           | NULL
           | ID
           ;
Baghdad answered 26/4, 2013 at 9:14 Comment(0)
D
12

The ANTLR 4 parser generator can combine groups of transitions to form a single "set transition" in certain cases, reducing static and dynamic memory overhead as well as improving runtime performance. This can only occur if all alternatives of a block contain a single element or set. For example, the following code allows INT and FLOAT to be combined into a single transition:

// example 1
number
    :   INT
    |   FLOAT
    ;

// example 2, elements grouped into a set
primary
    :   '(' expression ')'
    |   (INT | FLOAT)
    ;

However, in the following situation the elements cannot be combined by the compiler so they'll be treated separately:

primary
    :   '(' expression ')'
    |   INT
    |   FLOAT
    ;

The hint suggests places where the simple addition of ( ... ) is enough to allow the compiler to collapse a set that it would otherwise not be able to. Altering your code to the following would address the warning.

expression
    :   expression operator=DIV expression
    |   expression operator=MUL expression
    |   expression operator=ADD expression
    |   expression operator=SUB expression
    |   (   INT
        |   FLOAT
        |   BOOLEAN
        |   NULL
        |   ID
        )
    ;
Dortch answered 26/4, 2013 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.