#antlr #antlr4 #grammar
#antlr #antlr4 #грамматика
Вопрос:
В https://github.com/antlr/grammars-v4
Я нахожу много примеров (X )?
.
Разве их не следует заменить на X*
?
Вот набор экземпляров, которые я нашел с помощью моего собственного инструмента анализа:
sql/plsql/PlSqlParser.g4(1654,11): Warning: rewrite as `X*`, token `?` at offset 39760
sql/plsql/PlSqlParser.g4(1754,27): Warning: rewrite as `X*`, token `?` at offset 42796
sql/plsql/PlSqlParser.g4(1786,31): Warning: rewrite as `X*`, token `?` at offset 43928
sql/plsql/PlSqlParser.g4(1815,31): Warning: rewrite as `X*`, token `?` at offset 44865
sql/plsql/PlSqlParser.g4(2233,74): Warning: rewrite as `X*`, token `?` at offset 56180
sql/plsql/PlSqlParser.g4(2245,12): Warning: rewrite as `X*`, token `?` at offset 56458
sql/plsql/PlSqlParser.g4(2490,82): Warning: rewrite as `X*`, token `?` at offset 62412
sql/plsql/PlSqlParser.g4(2577,137): Warning: rewrite as `X*`, token `?` at offset 64544
sql/mysql/Positive-Technologies/MySqlLexer.g4(1176,50): Warning: rewrite as `X*`, token `?` at offset 53848
sql/mysql/Positive-Technologies/MySqlLexer.g4(1178,52): Warning: rewrite as `X*`, token `?` at offset 53988
sql/mysql/Positive-Technologies/MySqlLexer.g4(1176,50): Warning: rewrite as `X*`, token `?` at offset 53848
sql/mysql/Positive-Technologies/MySqlLexer.g4(1178,52): Warning: rewrite as `X*`, token `?` at offset 53988
sql/tsql/TSqlParser.g4(2410,13): Warning: rewrite as `X*`, token `?` at offset 84325
sql/tsql/TSqlParser.g4(2459,13): Warning: rewrite as `X*`, token `?` at offset 86539
informix/informix.g4(2879,25): Warning: rewrite as `X*`, token `?` at offset 35322
antlr/antlr2/examples/python.g2(660,48): Warning: rewrite as `X*`, token `?` at offset 13664
antlr/antlr2/examples/java.g2(905,4): Warning: rewrite as `X*`, token `?` at offset 25794
cql3/CqlLexer.g4(996,19): Warning: rewrite as `X*`, token `?` at offset 8042
cql3/CqlLexer.g4(996,88): Warning: rewrite as `X*`, token `?` at offset 8111
cql3/CqlLexer.g4(996,19): Warning: rewrite as `X*`, token `?` at offset 8042
cql3/CqlLexer.g4(996,88): Warning: rewrite as `X*`, token `?` at offset 8111
restructuredtext/ReStructuredText.g4(100,49): Warning: rewrite as `X*`, token `?` at offset 2632
fortran77/Fortran77Parser.g4(433,61): Warning: rewrite as `X*`, token `?` at offset 7722
fortran77/Fortran77Parser.g4(437,47): Warning: rewrite as `X*`, token `?` at offset 7790
fortran77/Fortran77Parser.g4(441,48): Warning: rewrite as `X*`, token `?` at offset 7861
verilog/verilog/Verilog2001.g4(366,68): Warning: rewrite as `X*`, token `?` at offset 10098
verilog/verilog/Verilog2001.g4(370,33): Warning: rewrite as `X*`, token `?` at offset 10164
verilog/verilog/Verilog2001.g4(370,67): Warning: rewrite as `X*`, token `?` at offset 10198
prov-n/PROV_N.g4(28,63): Warning: rewrite as `X*`, token `?` at offset 1253
cobol85/Cobol85.g4(174,47): Warning: rewrite as `X*`, token `?` at offset 4773
vb6/VisualBasic6Parser.g4(110,41): Warning: rewrite as `X*`, token `?` at offset 2788
Комментарии:
1. Да, я думаю, вы правы.
(x )?
эквивалентноx*
. Глядя на сгенерированный код и ATN, хотя они могут выглядеть по-разному, сгенерированный код из одного может быть преобразован в другой, со всеми вызовами ErrorHandler . Синхронизация () и TokenStream.LA () совпадает, но код и ATN для версии Kleene star все же более чистые. По крайней мере, один шаблон в обозначении Antlrvsix.Trash для этого есть"//ebnf[blockSuffix/ebnfSuffix/QUESTION and block/altList[@ChildCount = 1]/alternative[@ChildCount = 1]/element/ebnf/blockSuffix/ebnfSuffix/PLUS]"
, но, похоже, есть и другие. Хороший улов.2. Пожалуйста, сделайте запрос на извлечение в репозиторий грамматик с такими изменениями, если сможете.