Reputation: 7958
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 ?
Upvotes: 3
Views: 2682
Reputation: 71578
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
Upvotes: 7