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

时间:2010-09-26 18:30:30

标签: antlr grammar ambiguity antlrworks

我有以下用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的使用不正确?

1 个答案:

答案 0 :(得分:2)

您已将'drop''put'置于两个不同的词法规则中:

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

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

所以,解析字符串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