Antlr 分析器无法从输入中识别规则
我试图弄明白为什么我的Antlr生成的解析器没有将输入的一部分识别为与我的一条规则匹配(“and_converge”规则,“gateway”的一部分)。我的语法看起来像:Antlr 分析器无法从输入中识别规则,antlr,antlr4,Antlr,Antlr4,我试图弄明白为什么我的Antlr生成的解析器没有将输入的一部分识别为与我的一条规则匹配(“and_converge”规则,“gateway”的一部分)。我的语法看起来像: process : PROCESS id CURLY_OPEN (stmt_list | pool_list) CURLY_CLOSE EOF ; /* starting rule */ stmt_list : (stmt STMT_TERM?)* ; stmt
process : PROCESS id CURLY_OPEN (stmt_list | pool_list) CURLY_CLOSE EOF ; /* starting rule */
stmt_list : (stmt STMT_TERM?)* ;
stmt : sequence | sequence_elem | association ;
sequence : sequence_elem sequence_flow sequence_elem (sequence_flow sequence_elem)* ;
sequence_elem : activity | gateway | event | link ;
activity : task | subprocess ;
task : SQRE_OPEN task_type id
(VERT_LINE (input_set)? (output_set)?)? /* input/output sets */
(VERT_LINE attr_list)? /* attributes for activity */
(VERT_LINE boundary_event)* /* associated boundary events */
SQRE_CLOSE ;
task_type : USER | SERVICE | SCRIPT ;
event : PAREN_OPEN event_type id (VERT_LINE attr_list)? PAREN_CLOSE ;
gateway : ANGLE_OPEN (fork_diverge | condition_diverge | event_diverge | and_converge | or_converge) ANGLE_CLOSE ;
fork_diverge : FORK id (VERT_LINE attr_list)? VERT_LINE outflows ;
event_diverge : EVENT_SPLIT VERT_LINE event_links ;
condition_diverge : (OR_SPLIT | XOR_SPLIT) id (VERT_LINE attr_list)? VERT_LINE cond_outflows ;
and_converge : JOIN id (VERT_LINE attr_list)? (VERT_LINE inflows)? ;
or_converge : (XOR_JOIN | OR_JOIN) id (VERT_LINE attr_list)? (VERT_LINE inflows)? ;
inflows : IN ':' link_list ;
outflows : OUT ':' link_list ;
cond_outflows : OUT ':' cond_outflow (',' cond_outflow)* (DEFAULT ':' link)?;
cond_outflow : expression ':' link ;
为简洁起见,我省略了大部分语法,但您可以在此处看到完整版本:
当我对语法进行如下输入时,它失败了:
/* A fork followed by a join, 4 "sequences" altogether */
process fork_join {
(> start) ==> [user t1] ==>
<fork g1 | out: #[t2], #[t3]>
[user t2] ==> #<g2>
[user t3] ==> #<g2>
<join g2> ==> (/ end)
}
/*一个fork后跟一个join,共4个“序列”*/
进程fork\u连接{
(>开始)==>[用户t1]==>
[用户t2]==>#
[用户t3]==>#
==>(/end)
}
它在带有“连接”网关的线路上失败:线路7:4如果您运行以下代码,则在输入“处没有可行的替代方案:
String source=“/*一个fork后跟一个join,共4个\“sequences\”*/\n+
“进程fork\u join{\n”+
“(>开始)==>[用户t1]==>\n”+
“\n”+
“[用户t2]==>\n”+
“[用户t3]==>\n”+
“==>(/end)\n”+
"}";
DOTBPMLexer lexer=新的DOTBPMLexer(CharStreams.fromString(source));
CommonTokenStream=新的CommonTokenStream(lexer);
stream.fill();
for(令牌t:stream.getTokens()){
System.out.printf(“type=%-20s text=`%s`%n”,DOTBPMLexer.词汇表.getDisplayName(t.getType()),t.getText().replace(“\n”,“\\n”);
}
并检查其输出:
type=PROCESS text=`process`
type=ID text=`fork_join`
type='{' text=`{`
type='(' text=`(`
type='>' text=`>`
type=ID text=`start`
type=')' text=`)`
type='==>' text=`==>`
type='[' text=`[`
type=USER text=`user`
type=ID text=`t1`
type=']' text=`]`
type='==>' text=`==>`
type='<' text=`<`
type=FORK text=`fork`
type=ID text=`g1`
type='|' text=`|`
type=OUT text=`out`
type=':' text=`:`
type='#' text=`#`
type='[' text=`[`
type=ID text=`t2`
type=']' text=`]`
type=',' text=`,`
type='#' text=`#`
type='[' text=`[`
type=ID text=`t3`
type=']' text=`]`
type='>' text=`>`
type='[' text=`[`
type=USER text=`user`
type=ID text=`t2`
type=']' text=`]`
type='==>' text=`==>`
type='#' text=`#`
type='<' text=`<`
type=ID text=`g2`
type='>' text=`>`
type='[' text=`[`
type=USER text=`user`
type=ID text=`t3`
type=']' text=`]`
type='==>' text=`==>`
type='#' text=`#`
type='<' text=`<`
type=ID text=`g2`
type='>' text=`>`
type='<' text=`<`
type=ID text=`join`
type=ID text=`g2`
type='>' text=`>`
type='==>' text=`==>`
type='(' text=`(`
type='/' text=`/`
type=ID text=`end`
type=')' text=`)`
type='}' text=`}`
type=EOF text=`<EOF>`
请注意所有这些lexer规则中的[Jj}[Oo]
,它们将匹配单个字符(其中一个:J
,J
,}
,[
,O
或O
)
你(很可能)的意思是:
JOIN : [Jj][Oo][Ii][Nn] ; /* synchronized merge (AND) */
OR_JOIN : [Oo][Rr]'-'[Jj][Oo][Ii][Nn] ; /* structured synchronized merge (OR) */
XOR_JOIN : [Xx][Oo][Rr]'-'[Jj][Oo][Ii][Nn] ; /* unsynchronized merge (XOR) */
我建议打印您的lexer识别的标记,看看是否与您(和您的解析器)期望的匹配。是的,您可以从解析树中看到。解析错误之前的所有内容都与预期完全一致,甚至是(/end)
(“事件”)正确识别错误后。只有
(序列流)令牌丢失。
JOIN : [Jj][Oo][Ii][Nn] ; /* synchronized merge (AND) */
OR_JOIN : [Oo][Rr]'-'[Jj][Oo][Ii][Nn] ; /* structured synchronized merge (OR) */
XOR_JOIN : [Xx][Oo][Rr]'-'[Jj][Oo][Ii][Nn] ; /* unsynchronized merge (XOR) */