最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】antlr v4测试Hello结果出错:syntax error: unterminated rule (missing ‘;’) detected at ‘c ‘ while looking for lexer rule element

ANTLR crifan 4482浏览 0评论

【问题】

参见:

【已解决】antlr出错no viable alternative at input ‘__LBL__show’

中别人的回复:

Hi,你好!对你关于ANTLR的博客表示很感兴趣!

我发现解析器规则(parser rule)中使用的token好像会和词法分析器(lexer)中定义的token冲突!不知道你是否知道如何解决呢?

有个简单的例子:
grammar Test;

r1: A;
r2: ‘a’ ‘c’;

A: ‘a’ | ‘b’;

grun Test r1 -tree时,输入‘a’会出现和你此处一样的错误:

line 1:0 mismatched input ‘a’ expecting A

所以去解决此问题。

【解决过程】

1. 用antrl v4的语法:

grammar Test;

r1: A;
r2: ‘a’ ‘c’;

A: ‘a’ | ‘b’;

去编译结果出错:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>antlr4 Test.g4

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>java org.antlr.v4.Tool Test.g4

org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 6:4 required (…)+ loop did not match anything at input ‘a’

org\antlr\v4\parse\GrammarTreeVisitor.g: node from after line 6:4 mismatched tree node: ALT expecting <UP>

org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 6:4 required (…)+ loop did not match anything at input ‘a’

org\antlr\v4\parse\GrammarTreeVisitor.g: node from after line 6:4 mismatched tree node: ALT expecting <UP>

org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 6:4 required (…)+ loop did not match anything at input ‘a’

org\antlr\v4\parse\GrammarTreeVisitor.g: node from after line 6:4 mismatched tree node: ALT expecting <UP>

org\antlr\v4\parse\GrammarTreeVisitor.g: node from line 6:4 required (…)+ loop did not match anything at input ‘a’

org\antlr\v4\parse\GrammarTreeVisitor.g: node from after line 6:4 mismatched tree node: ALT expecting <UP>

Exception in thread "main" java.lang.NullPointerException

        at org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:116)

        at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:399)

        at org.antlr.v4.Tool.process(Tool.java:376)

        at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:343)

        at org.antlr.v4.Tool.main(Tool.java:190)

2.把lexer的token都放到前面,并且把中文引号"’"改为英文的引号"’":

grammar Test;

A:  'a | 'b';

r1: A;
r2: 'a' 'c';

试试,结果出现别的错误:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>antlr4 Test.g4

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>java org.antlr.v4.Tool Test.g4

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test\Test.g4 line 6:12 mismatched character ‘<EOF>’ expecting ”’

error(50): D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test\Test.g4:6:9: syntax error: unterminated rule (missing ‘;’) detected at ‘c <EOF>’ while

looking for lexer rule element

 

3.再去在最后添加一个换行,变成:

new line after last line

试试,结果问题依旧如上。

4.感觉说是r2这个lexer没有依赖任何的,合法的token规则?

所以随便改为别的,比如:

grammar Test;

A:  'a | 'b';

r1: A;
r2: 'a' | 'c';

然后再试试,结果错误依旧。

5.把r2删除:

grammar Test;

A:  'a | 'b';

r1: A;

试试,结果问题依旧。

6.刚注意,上面不小心把a的右边引号漏掉了。

所以再改正确的为:

grammar Test;

A:  'a' | 'b';

r1: A;
r2: 'a' 'c';

再去试试,然后就可以正常编译了:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>antlr4 Test.g4

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>java org.antlr.v4.Tool Test.g4

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\Test>

7.另外,再试试token放在rule后面的:

grammar Test;

r1: A;
r2: 'a' 'c';

A:  'a' | 'b';

结果也是可以正常编译的,没有任何错误的。

所以说明:

antlr中,lexer的token和parser的rule,没有强制要求前后关系。

 

【总结】

antrl中,注意必须,所有的内容(除了注释),全部都要是英文字符。

尤其是单引号,双引号之类的字符:

不能是中文的那个单引号(’)和双引号(“):

而必须是英文的单引号(‘)和双引号(")

否则antlr必然是不能识别的。

转载请注明:在路上 » 【已解决】antlr v4测试Hello结果出错:syntax error: unterminated rule (missing ‘;’) detected at ‘c ‘ while looking for lexer rule element

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. Hi, crifan!对你有条理的做事风格表示钦佩! 关于问题,我是在Mac终端中进行语法编写的,因此引号问题估计是发表评论时产生的。另外,我这边一直都能编译成功,而是在解析的时候出的问题,不知道你那里解析的结果如何,我这边是: dongli:learn dongli$ grun Test r1 -tree aline 1:0 mismatched input 'a' expecting A (r1 a)
    董理12年前 (2013-07-05)回复
85 queries in 0.184 seconds, using 22.12MB memory