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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.