[英]Coco/r: Factor deletable
I am trying to implement a language in Coco/r for arithmetic operations in C# which takes operator precedence into consideration. 我正在尝试在Coco / r中为C#中的算术运算实现一种语言,该语言考虑了运算符的优先级。 My ATG code looks like this:
我的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.
The operators other than '+' and '-' should have lower precedence. 除“ +”和“-”以外的其他运算符应具有较低的优先级。 Moreover, the '&' operator should have lower precedence than '|'.
此外,“&”运算符的优先级应低于“ |”。
The problem is that when I try to test the code I get the following errors: 问题是,当我尝试测试代码时,出现以下错误:
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
I am really new in Coco/r and EBNF. 我真的是Coco / r和EBNF的新手。 I had a look into Coco\\r's manual, but I don't really see what is the problem;
我看了一下Coco \\ r的手册,但我并没有真正看到问题所在。 What am i missing?
我想念什么?
Thank you in advance! 先感谢您!
I think, in Factor
, rather than 我认为,在
Factor
而不是
Factor<out int n>
=
{
"==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
|
...
}
you really want something like 你真的想要像
Factor<out int n>
=
Term<out n1>
[
"==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
|
...
]
That is, you want to unconditionally require the lead Term
, which can then optionally be followed by exactly one relation. 也就是说,您要无条件地要求Lead
Term
,然后可以选择只跟一个关系。 Otherwise you would be allowing statements like a < b > c == d
. 否则,您将允许类似
a < b > c == d
语句。
Coco/R is an LL(1) parser. Coco / R是LL(1)解析器。 The errors are basically telling you that you have written grammar that it cannot resolve by looking 1 symbol ahead.
这些错误基本上是在告诉您您编写的语法无法通过在前面看1个符号来解决。 You either need to refactor the grammar or provide conflict resolvers.
您要么需要重构语法,要么提供冲突解决程序。 I'd try to refactor grammar as I don't think you need conflict resolvers here.
我会尝试重构语法,因为我认为您不需要在这里解决冲突。 See the user manual on "conflicts" for more details.
有关更多详细信息,请参见“冲突”用户手册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.