ANTLR4在解析结束时打印

时间:2015-03-18 18:29:04

标签: parsing printing antlr4

有没有办法在解析结束时在ANTLR4中打印@members?

grammar test;
@members{
int count = 0;
} 

sentence :  (a..z|A..Z)* r;
r        :  [\r\n]+ {count++;}; 

1 个答案:

答案 0 :(得分:0)

我不确定我的问题是否正确,但正如StephanA所说,你的语法不符合ANTLR4。我稍微修改了你的语法:

grammar Test;

@members{
int count = 0;
} 

sentence :  WORD (r WORD)* {System.out.println(count);};
r        :  WS {count++;};

WORD : ('a'..'z'|'A'..'Z')+;
WS :  [ \r\n]+;

此语法将接受This is a test(最后count = 3),但最后会拒绝This is a testThis is a testWS)。我添加了一个代表解析树根的root规则,并在此规则的末尾显示count

这是你想要的吗?

编辑

我刚刚重构了语法,root规则实际上并没有用(我的不好)。

相关问题