質問

I am building a grammar in ANTLR4, and I am getting this warning

TL4.g4:224:12: greedy block ()* contains wildcard; the non-greedy syntax ()*? may be preferred

Here is the line of code it is referring to

block
    : ( statement | functionDecl )* (Return expression ';')?
    ;

What does the warning mean, How can I correct it ?

役に立ちましたか?

解決

The warning is telling you that the block ()* is greedy, meaning that it will try to match maximum occurrences of statement or functionDec1 which, depending on the situation, might not be what you expect.

Changing it to ()*? makes it non-greedy, as suggested by the warning. This means it will match minimum occurrences of statement or functionDec1.

Expression examples with strings:

Samples:

foofoobar
foobarbar
foofoobarbarbar

Expression:

(foo|bar)*bar

Will give result:

foofoobar
foobarbar
foofoobarbarbar

Expression:

(foo|bar)*?bar

Will give result:

foofoobar
foobar
foofoobar

For the last one, the result will stop at the first bar

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top