检查以下语法以解析单词SELECT。 我正在尝试遵循其词法分析器ATN的逻辑来实现自动完成功能,但这没有任何意义。但是词法分析器正在以某种方式工作...
看词法分析器ATN,似乎两个E片段都使用相同的状态(下面的状态7)实现为一个逻辑实体,尽管它们对如何继续没有相同的约束之后。
这使我的自动完成逻辑混乱了,但是以某种方式却没有使ANTLR4词法分析器接受诸如SELELELECT之类的输入有效。 词法化时,我是否错过了一些隐藏在ATN或运行时中某些地方的额外状态?
完整语法:
grammar Expr;
prog: SELECT;
SELECT : S E L E C T;
WS: [ \t\n] -> channel(HIDDEN);
fragment S : [sS];
fragment E : [eE];
fragment L : [lL];
fragment C : [cC];
fragment T : [tT];
生成的词法分析器ATN规则。对于每个规则,我们将打印其开始状态和停止状态。请注意,E以7开头。
Rule 0 SELECT start: 1 stop: 2
Rule 1 WS start: 3 stop: 4
Rule 2 S start: 5 stop: 6
Rule 3 E start: 7 stop: 8
Rule 4 L start: 9 stop: 10
Rule 5 C start: 11 stop: 12
Rule 6 T start: 13 stop: 14
这是ATN转换。每行表示从前一行到达的状态到行末打印的状态号的可能转换。请注意带有RuleTransition@4eb7f003-> E RuleStartState 7*
的行-星号表示回到状态7会创建循环的事实。
TokensStartState 0
epsilon-> SELECT RuleStartState 1
epsilon-> SELECT BasicState 15
RuleTransition@ed9d034-> S RuleStartState 5
epsilon-> S BasicState 26
{83, 115}-> S BasicState 27
epsilon-> S RuleStopState 6
epsilon-> SELECT BasicState 16
RuleTransition@6121c9d6-> E RuleStartState 7
epsilon-> E BasicState 28
{69, 101}-> E BasicState 29
epsilon-> E RuleStopState 8
epsilon-> SELECT BasicState 17
RuleTransition@87f383f-> L RuleStartState 9
epsilon-> L BasicState 30
{76, 108}-> L BasicState 31
epsilon-> L RuleStopState 10
epsilon-> SELECT BasicState 18
RuleTransition@4eb7f003-> E RuleStartState 7*
epsilon-> SELECT BasicState 19
RuleTransition@eafc191-> C RuleStartState 11
epsilon-> C BasicState 32
{67, 99}-> C BasicState 33
epsilon-> C RuleStopState 12
epsilon-> SELECT BasicState 20
RuleTransition@612fc6eb-> T RuleStartState 13
epsilon-> T BasicState 34
{84, 116}-> T BasicState 35
epsilon-> T RuleStopState 14
epsilon-> SELECT BasicState 21
epsilon-> SELECT RuleStopState 2
epsilon-> WS RuleStartState 3
epsilon-> WS BasicState 22
{9..10, 32}-> WS BasicState 23
epsilon-> WS BasicState 24
action_1:0-> WS BasicState 25
epsilon-> WS RuleStopState 4