English 中文(简体)
语法不工作( 无效允许例外)
原标题:Grammar not working (NoViableAltException)
  • 时间:2012-05-24 06:42:30
  •  标签:
  • antlr
  • antlr3

我对ANTLR很新,我正尝试玩它。这是我所能想到的最简单的语法。 当我分析一个变数“ ID123” 时, 它仍然不起作用( 不可置信的例外), 但对于“ abc1, "ab", "c1d2f3" 却有效。

我使用ANTRR 3.1.3 和 ANTRL Works 1.4。

options 
{
    language =  CSharp2 ;
    output = AST;
}

assign  :   variable  =  value;
value   :   (variable|constant);
variable:   LETTER (LETTER|DIGIT)*;
constant:   (STRING|INTEGER);

DIGIT   :    0 .. 9 ;
NATURAL :   (DIGIT)+;   
INTEGER :   ( - )? NATURAL; 
REAL    :   (INTEGER  .  NATURAL);

LETTER  :   ( a .. z | A .. Z );

CR      :    
         { $channel = HIDDEN; }; 
LF      :    
         { $channel = HIDDEN; }; 
CRLF    :   CR LF       { $channel = HIDDEN; }; 
SPACE   :   (   | 	 )  { $channel = HIDDEN; };

STRING  :    "  (~ " )*  " ;
最佳回答

ANTLR s Lexer 试图尽可能匹配。 当两个( 或更多) 规则匹配相同数量的字符时, 首先定义的规则将“ 赢 ” 。 因此, 当名词员在数位数上跌倒时, 将创建 < code> DIGIT 符号, 因为该符号在 < code> NATURAL 之前被定义 :

DIGIT   :    0 .. 9 ;
NATURAL :   (DIGIT)+;   

但对于输入的 “ id123” , 词汇生成了以下3个标记:

LETTER           i 
LETTER           d 
NATURAL          123 

因为词典贪婪地匹配, 因此创建了一个 < code> NATURAL , 并创建了 < em> not 三个 < code > DIGIT 符号 。

您应该做的是制定 可变 的词法规则, 代之以 :

assign   :   VARIABLE  =  value;
value    :   (VARIABLE | constant);
constant :   (STRING | INTEGER | REAL);

VARIABLE :   LETTER (LETTER|DIGIT)*;
INTEGER  :   ( - )? NATURAL; 
REAL     :   (INTEGER  .  NATURAL);
SPACE    :   (    |  	  |  
  |  
 )  { $channel = HIDDEN; };
STRING   :    "  (~ " )*  " ;

fragment NATURAL :   (DIGIT)+;   
fragment DIGIT   :    0 .. 9 ;
fragment LETTER  :   ( a .. z  |  A .. Z );

请注意,我做了几条名词规则 < code> fragment 。 这意味着名词规则永远不会产生 NATURAL DIGIT LETTER 符号。 这些 "code > fragment 规则只能由其他名词规则使用。 换句话说, 您的名词规则将只产生 Variblep INTEGER > /code > 和 string 标语 * (因此,这些是您在解译规则中只能使用的! ) 。

*和 = 符号,当然...

问题回答

暂无回答




相关问题
ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse ...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)?

JavaCC Problem - Generated code doesn t find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain ...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because %s means something to ANTLR: $ cat T.g grammar T; options { language = ...

What s the matter with this Grammar?

grammar Test; IDHEAD: ( a .. z | A .. Z | _ ); IDTAIL: (IDHEAD | 0 .. 9 ); ID: (IDHEAD IDTAIL*); fragment TYPE: ( text | number | bool ); define: define ID as TYPE; The problem ...