这个语法有什么问题? (ANTLRWorks 1.4)

发布于 2024-09-25 03:52:00 字数 1203 浏览 1 评论 0原文

我在 ANTLRWorks 1.4 中编写了以下代码,

grammar hmm;

s           :   (put_a_in_b)|(put_out_a)|(drop_kick)|(drop_a)|(put_on_a);

put_a_in_b  :   (PUT_SYN)(ID)(IN_SYN)(ID);  
put_out_a   :   (PUT2_SYN)(OUT_SYN)(ID) | (E1)(ID); 
drop_kick   :   ('drop')('kick')(ID);
drop_a      :   (DROP_SYN)(ID);
put_on_a    :   (E2)(ID);

PUT_SYN     :   'put' | 'place' | 'drop';
PUT2_SYN    :   'put' | 'douse';
IN_SYN      :   'in' | 'into' | 'inside' | 'within';    
OUT_SYN     :   'out';
E1          :   'extinguish'|'douse';
DROP_SYN    :   'drop' | 'throw' | 'relinquish';
WS          :   ( ' '  | '\t' | '\r' | '\n' ) {$channel=HIDDEN;};
ID          :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
E2          :   'put on'|'don'|'wear';
COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

当我使用输入运行它时:

drop object

我收到 MismatchedTokenException(5 != 15)。

通过输入:

put o1 in o2

我得到一个 NoViableAltException。

虽然

place o2 in o2

我对此很陌生,但它运行得很好,但似乎存在歧义?或者我对 ANTLR 的使用不正确?

I have the following code written in ANTLRWorks 1.4

grammar hmm;

s           :   (put_a_in_b)|(put_out_a)|(drop_kick)|(drop_a)|(put_on_a);

put_a_in_b  :   (PUT_SYN)(ID)(IN_SYN)(ID);  
put_out_a   :   (PUT2_SYN)(OUT_SYN)(ID) | (E1)(ID); 
drop_kick   :   ('drop')('kick')(ID);
drop_a      :   (DROP_SYN)(ID);
put_on_a    :   (E2)(ID);

PUT_SYN     :   'put' | 'place' | 'drop';
PUT2_SYN    :   'put' | 'douse';
IN_SYN      :   'in' | 'into' | 'inside' | 'within';    
OUT_SYN     :   'out';
E1          :   'extinguish'|'douse';
DROP_SYN    :   'drop' | 'throw' | 'relinquish';
WS          :   ( ' '  | '\t' | '\r' | '\n' ) {$channel=HIDDEN;};
ID          :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
E2          :   'put on'|'don'|'wear';
COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

When I run it with the input:

drop object

I get a MismatchedTokenException(5 != 15).

And with the input :

put o1 in o2

I get a NoViableAltException.

Though it runs fine with

place o2 in o2

I'm new to this, but it seems like there's ambiguities? Or maybe my usage of ANTLR is incorrect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-10-02 03:52:00

您已将 'drop''put' 放入两个不同的词法分析器规则中:

PUT_SYN  : 'put' | 'place' | 'drop';          // drop & put
PUT2_SYN : 'put' | 'douse';                   //        put
...
DROP_SYN : 'drop' | 'throw' | 'relinquish';   // drop

当词法分析器遇到 put 时,PUT_SYN 将始终是与其匹配的规则,因此可以(或应该)从 PUT2_SYN 规则中删除 'put'

因此,解析字符串 drop object 时出现问题:解析器将尝试匹配 drop_a : (DROP_SYN)(ID);"drop" 将在词法分析器规则 PUT_SYN 中进行匹配。

编辑

这些同义词列表可以更好地制作成解析器规则(而不是词法分析器规则)。这是一个小演示:

grammar TextAdventure;

parse
  :  command (EndCommand command)* EOF
  ;

command
  :  put_syn_1 OtherWord in_syn OtherWord
  |  put_syn_2 out_syn_1 OtherWord
  |  out_syn_2 OtherWord
  |  Drop Kick OtherWord
  |  drop_syn OtherWord
  ;

drop_syn
  :  Drop
  |  Throw 
  |  Relinquish
  ;

in_syn
  :  In
  |  Into
  |  Inside
  |  Within
  ; 

put_syn_1
  :  Put
  |  Place
  |  Drop
  ;

put_syn_2
  :  Put
  |  Douse
  ;

out_syn_1
  :  Out
  ;

out_syn_2
  :  Extinguish
  |  Douse
  ;

Space      : (' ' | '\t' | '\r' | '\n'){$channel=HIDDEN;};
EndCommand : ';';
Put        : 'put';
Place      : 'place';
Drop       : 'drop';
Douse      : 'douse';
In         : 'in';
Into       : 'into';
Inside     : 'inside';
Within     : 'within';    
Out        : 'out';
Extinguish : 'extinguish';
Throw      : 'throw';
Relinquish : 'relinquish';
Kick       : 'kick';
OtherWord  : ('a'..'z' | 'A'..'Z')+;

在解释以下源代码时:

drop object ; put yourself in myshoes ; place it in avase

您将看到 ANTLRWorks 生成以下解析树:

alt text

You've put 'drop' and 'put' in two different lexer-rules:

PUT_SYN  : 'put' | 'place' | 'drop';          // drop & put
PUT2_SYN : 'put' | 'douse';                   //        put
...
DROP_SYN : 'drop' | 'throw' | 'relinquish';   // drop

When put is encountered by the lexer, PUT_SYN will always be the rule that matches it, so 'put' could (or should) be removed from the PUT2_SYN rule.

So, your problem with parsing the string drop object: the parser will try to match drop_a : (DROP_SYN)(ID); but the "drop" will be matched in the lexer rule PUT_SYN.

EDIT

Those synonym-lists can be better made into parser rules (instead of lexer-rules). Here's a small demo:

grammar TextAdventure;

parse
  :  command (EndCommand command)* EOF
  ;

command
  :  put_syn_1 OtherWord in_syn OtherWord
  |  put_syn_2 out_syn_1 OtherWord
  |  out_syn_2 OtherWord
  |  Drop Kick OtherWord
  |  drop_syn OtherWord
  ;

drop_syn
  :  Drop
  |  Throw 
  |  Relinquish
  ;

in_syn
  :  In
  |  Into
  |  Inside
  |  Within
  ; 

put_syn_1
  :  Put
  |  Place
  |  Drop
  ;

put_syn_2
  :  Put
  |  Douse
  ;

out_syn_1
  :  Out
  ;

out_syn_2
  :  Extinguish
  |  Douse
  ;

Space      : (' ' | '\t' | '\r' | '\n'){$channel=HIDDEN;};
EndCommand : ';';
Put        : 'put';
Place      : 'place';
Drop       : 'drop';
Douse      : 'douse';
In         : 'in';
Into       : 'into';
Inside     : 'inside';
Within     : 'within';    
Out        : 'out';
Extinguish : 'extinguish';
Throw      : 'throw';
Relinquish : 'relinquish';
Kick       : 'kick';
OtherWord  : ('a'..'z' | 'A'..'Z')+;

When interpreting the following source:

drop object ; put yourself in myshoes ; place it in avase

you'll see ANTLRWorks generate the following parse-tree:

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文