Coco / r:因子可删除

时间:2014-10-21 10:56:56

标签: c# ebnf cocor

我正在尝试在Coco中实现一种语言,用于C#中的算术运算,它考虑了运算符优先级。我的ATG代码如下所示:

/* Coco/R lexer and parser specification for arithmetic expressions. */
/* 2006-09-14 */

/* Build with:
 *   Coco.exe -namespace Expressions Ex2.ATG
 */

using System.Collections.Generic;

COMPILER Expressions
  public int res;

/*--------------------------------------------------------------------------*/
CHARACTERS
  letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  digit = "0123456789".
  cr  = '\r'.
  lf  = '\n'.
  tab = '\t'.

TOKENS
  ident  = letter {letter | digit}.
  number = digit {digit}.
IGNORE cr + lf + tab

PRODUCTIONS
/*------------------------------------------------------------------------*/
Expr<out int n>   (. int n1, n2; .)
= Term<out n1>  (. n = n1; .)
{
  '+' Term<out n2>  (. n = n+n2; .)  
  | 
  '-' Term<out n2>  (. n = n-n2; .)    
  |
  Factor<out int n>
}
.
Factor<out int n>
=
{
  "==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
  | 
  '<' Term<out n2>  (. if(n1 < n2) { n = 1; } else { n = 0; } .)
  | 
  '>' Term<out n2>  (. if(n1 > n2) { n = 1; } else { n = 0; } .)
  | 
  "!=" Term<out n2> (. if(n1 != n2){ n = 1; } else { n = 0; } .) 
  | 
  "<=" Term<out n2> (. if(n1 <= n2){ n = 1; } else { n = 0; } .)
  | 
  ">=" Term<out n2> (. if(n1 >= n2){ n = 1; } else { n = 0; } .)
  |
  "|" Term<out n2>  (. if(n1 != 0 | n2 != 0) { n = 1; } else { n = 0; } .)
  |
  "&" Term<out n2>  (. if(n1 != 0 & n2 != 0){ n = 1; } else { n = 0; } .)
}
.
Term<out int n> 
= number          (. n = Convert.ToInt32(t.val); .)
{
  '*' number  (. n = n*Convert.ToInt32(t.val); .) 
}
.

Expressions                        (. int n; .)
= Expr<out n>                (. res = n; .)

.


END Expressions.

&#39; +&#39;以外的运营商和&#39; - &#39;应该有较低的优先权。此外,&#39;&amp;&#39;运算符的优先级应低于&#39; |&#39;。

问题是当我尝试测试代码时出现以下错误:

Factor deletable
  LL1 warning in Expr: contents of [...] or {...} must not be deletable
  LL1 warning in Expr: "+" is start of several alternatives
  LL1 warning in Expr: "-" is start of several alternatives
  LL1 warning in Factor: "==" is start & successor of deletable structure
  LL1 warning in Factor: "<" is start & successor of deletable structure
  LL1 warning in Factor: ">" is start & successor of deletable structure
  LL1 warning in Factor: "!=" is start & successor of deletable structure
  LL1 warning in Factor: "<=" is start & successor of deletable structure
  LL1 warning in Factor: ">=" is start & successor of deletable structure
  LL1 warning in Factor: "|" is start & successor of deletable structure
  LL1 warning in Factor: "&" is start & successor of deletable structure

我是Coco / r和EBNF的新手。我看了一下Coco \ r&r'的手册,但我真的不明白这是什么问题;我错过了什么?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

我认为,在Factor,而不是

Factor<out int n>
=
{
  "==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
  | 
...
}

你真的想要像

这样的东西
Factor<out int n>
=
  Term<out n1>
[
  "==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
  | 
...
]

也就是说,您希望无条件地要求引导Term,然后可以选择性地跟随一个关系。否则,您将允许a < b > c == d等语句。

答案 1 :(得分:0)

Coco / R是LL(1)解析器。这些错误基本上是在告诉您您编写的语法无法通过在前面看1个符号来解决。您要么需要重构语法,要么提供冲突解决程序。我会尝试重构语法,因为我认为您不需要在这里解决冲突。有关更多详细信息,请参见用户手册中的“冲突”。