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

【未解决】antlr中不识别双引号(double quote)

ANTLR crifan 3869浏览 0评论

【问题】

使用如下代码:

startParse	:	include* identification+;

singleInclude	:	'#include' ' '+ '"' ID '.h"' ;
include		:	singleInclude WS*   -> singleInclude;

去解析对应的include语句:

#include "std_defs.h"

#include "com_tbls.h"

结果出错:

mismatchedtokenexception for double quote

完整对应的log如下:

[15:34:53] warning(200): DDParserDemo.g:97:28:

Decision can match input such as "WS" using multiple alternatives: 1, 2

As a result, alternative(s) 2 were disabled for that input

[15:34:53] warning(200): D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDParserDemo.g:97:28:

Decision can match input such as "WS" using multiple alternatives: 1, 2

As a result, alternative(s) 2 were disabled for that input

[15:35:00] javax.swing.text.BadLocationException: Position not represented by view

    at javax.swing.text.CompositeView.modelToView(Unknown Source)

    at javax.swing.text.BoxView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.text.JTextComponent.modelToView(Unknown Source)

    at org.antlr.works.debugger.input.DBInputProcessorToken$1.run(DBInputProcessorToken.java:304)

    at java.awt.event.InvocationEvent.dispatch(Unknown Source)

    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

    at java.awt.EventQueue.access$200(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

    at java.awt.EventQueue.dispatchEvent(Unknown Source)

    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.run(Unknown Source)

[15:35:01] javax.swing.text.BadLocationException: Position not represented by view

    at javax.swing.text.CompositeView.modelToView(Unknown Source)

    at javax.swing.text.BoxView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.text.JTextComponent.modelToView(Unknown Source)

    at org.antlr.works.debugger.input.DBInputProcessorToken$1.run(DBInputProcessorToken.java:304)

    at java.awt.event.InvocationEvent.dispatch(Unknown Source)

    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

    at java.awt.EventQueue.access$200(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

    at java.awt.EventQueue.dispatchEvent(Unknown Source)

    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.run(Unknown Source)

[15:35:12] javax.swing.text.BadLocationException: Position not represented by view

    at javax.swing.text.CompositeView.modelToView(Unknown Source)

    at javax.swing.text.BoxView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

    at javax.swing.text.JTextComponent.modelToView(Unknown Source)

    at org.antlr.works.debugger.input.DBInputProcessorToken$1.run(DBInputProcessorToken.java:304)

    at java.awt.event.InvocationEvent.dispatch(Unknown Source)

    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

    at java.awt.EventQueue.access$200(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.awt.EventQueue$3.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

    at java.awt.EventQueue.dispatchEvent(Unknown Source)

    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.run(Unknown Source)

对应的output显示的是:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 21:9 mismatched input ‘"std_defs.h"’ expecting ‘"’

 

【解决过程】

1.参考:

[antlr-interest] Problem parsing double quotes

结果也没什么结果。

2.自己改了半天的代码,结果还是不行:

改为反斜杠加双引号:

singleInclude	:	'#include' ' '+ '\"' ID '.h"';

也还是不行。

3.参考:

Using ANTLR with Left-Recursive Rules

说是:

双引号,是antlr v2的语法;

单引号,是antlr v3的语法;

4.参考:

Migrating from ANTLR 2 to ANTLR 3

也说到了:

Literals

Literals are always in single quotes, not double quotes:

ANTLR 2


ANTLR3

x : y | z ;

y : "class" ;


z : "package";


x : y | z ;

y : ‘class’ ‘{‘ ‘}’ ;


z : ‘package’ ;

5.随便乱改,加了fragment:

fragment singleInclude	:	'#include' (' ')+ '"' ID '.h"';

也还是不行。

6.换用antlrworks-1.2.2.jar,错误依旧。而且连output都没有上述的那个输出。

7.再去检查了,错误的详细信息:

missmatched input and recognition exception

可以看到,对于event,经过了三次的LT1,都是 "xxx.h",结果还是无法识别,然后才报错:

Recognition exception MismatchedTokenException

的。

但是很诡异的,此处很明显:

输入是:"std_defs.h"

而语法中,就是先去匹配对应的 ‘“’,即双引号的。

但是为何竟然无法匹配,还是未搞懂。

8.再去,把单个双引号改为两个双引号:

singleInclude	:	'#include' (' '|'\t')+ '""' ID '.h"';

试试效果。

结果问题依旧。

9.特意改为特定的值:

singleInclude	:	'#include' (' '|'\t')+ '"std_defs.h"';

试试效果。

结果倒是可以正常识别了:

can recognize the specific header file with double quote

但是还是不知道为何,正常的

‘"’

无法匹配到双引号。

10.参考:

[antlr-interest] [newbie] Quoted identifiers vs. string literals

其也是,和我此处一样,是用’"’:

UnquotedString
    :   ( ESC_SEQ | ~('\\'|'"'|' '|'\t') )*
    ;

去表示双引号这个字符的。

11.参考了:

[antlr-interest] [newbie] Quoted identifiers vs. string literals

去把双引号去掉,变成:

singleInclude	:	'#include' (' '|'\t')+  ID '.h';

然后试试效果。

结果错误依旧,output为:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 21:9 mismatched input ‘"std_defs.h"’ expecting ID

还是无法匹配。

12.

转载请注明:在路上 » 【未解决】antlr中不识别双引号(double quote)

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.187 seconds, using 22.12MB memory