简体   繁体   中英

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. My ATG code looks like this:

/* 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. I had a look into Coco\\r's manual, but I don't really see what is the problem; What am i missing?

Thank you in advance!

I think, in Factor , rather than

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. Otherwise you would be allowing statements like a < b > c == d .

Coco/R is an LL(1) parser. The errors are basically telling you that you have written grammar that it cannot resolve by looking 1 symbol ahead. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM