W301 Transparent alternative whose shape depends on the inputΒΆ

An alternative of a transparent rule ?name gives way to its child where a match leaves exactly one visible child, and makes a name node otherwise. This alternative does both, depending on the input. So what stands in the tree at that place is a node for one input and the child for another, and code that reads the tree has to handle both without the grammar saying which to expect.

start: sum
?sum: NUMBER ("+" NUMBER)*
NUMBER: [0-9]+
grammar.fpeg:2:7: warning[W301]: this alternative makes a sum node for some inputs and gives way to its one child for others

Write one alternative for each shape. An alternative with an alias always makes its node, and can have fields.

start: sum
?sum: terms=NUMBER ("+" terms=NUMBER)+ -> sum | NUMBER
NUMBER: [0-9]+

The warning counts the visible children that a match can leave: none, one or several. A rule with parameters is not reported, since the count depends on its arguments. The count takes every part to match as it is written, so an alternative that never matches, as N* N, where N* takes every N, is counted as if it could.