首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Coco/r:因素可拆

Coco/r:因素可拆
EN

Stack Overflow用户
提问于 2014-10-21 10:56:55
回答 2查看 659关注 0票数 0

我试图在Coco/r中为C#中的算术运算实现一种考虑运算符优先的语言。我的ATG代码如下所示:

代码语言:javascript
复制
/* 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.

除“+”和“-”以外的运算符应具有较低的优先级。此外,'&‘运算符的优先级应该低于’\‘。

问题是,当我尝试测试代码时,我会得到以下错误:

代码语言:javascript
复制
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

我真的是新来的可可/r和EBNF。我看了一下可可的手册,但我不知道是什么问题,我遗漏了什么?

提前谢谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-21 11:27:23

我认为,在Factor,而不是

代码语言:javascript
复制
Factor<out int n>
=
{
  "==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
  | 
...
}

你真的想要像

代码语言:javascript
复制
Factor<out int n>
=
  Term<out n1>
[
  "==" Term<out n2> (. if(n1 == n2){ n = 1; } else { n = 2; } .)
  | 
...
]

也就是说,您想无条件地要求铅Term,然后可以选择后面紧跟一个关系。否则,您将允许像a < b > c == d这样的语句。

票数 0
EN

Stack Overflow用户

发布于 2019-09-26 09:45:07

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26485025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档