简体   繁体   English

SableCC语法文件存在问题

[英]Problems With SableCC Grammar File

I seem to be having issues with SableCC generating the relevant lexer, node and parse stuff that it normally automatically generates from a grammar file. 我似乎在SableCC生成相关词法分析器,节点和解析通常由语法文件自动生成的内容方面遇到问题。 I'm not implementing an Abstract Syntax Tree at the moment. 我目前未实现抽象语法树。

When I try to run SableCC with the grammar file below, I get the following error: 当我尝试使用下面的语法文件运行SableCC时,出现以下错误:

[41,33] Redefinition of AFunctionHead.Id. [41,33]重新定义AFunctionHead.Id。 I have know idea what the problem is, but it seems to be something in the productions area. 我知道问题出在哪里,但这似乎是生产领域中的问题。 Am I perhaps missing something? 我可能想念什么吗?

Package Grammar_Specification;

Helpers

  digit = ['0'..'9'];
  letter = (['a'..'z'] | ['A'..'Z']);
  underscore = '_';
  plus = '+';
  minus = '-';
  mult = '*';
  div = '/';
  equals = '=';
  l_par = '(';
  r_par = ')';
  l_curly = '{';
  r_curly = '}';
  unicode_input_character = [0..0xffff];
  lf  = 0x000a;
  cr  = 0x000d;
  line_terminator =  lf | cr | cr lf;
  input_character = [unicode_input_character - [cr + lf]];
  not_star = [input_character - '*'] | line_terminator;            
  not_star_not_slash = [input_character - ['*' + '/']] | line_terminator;
  multi_line_comment = '/*' not_star+ '*'+ (not_star_not_slash not_star* '*'+)* '/';
  line_comment = '//' input_character* line_terminator?;

Tokens

  func = 'FUNC';
  id = (letter(letter | digit | underscore)* | underscore(letter | digit | underscore)*);
  float_literal = minus? digit (digit)* ('.' (digit)*)? (('e' | 'E') (plus | minus)? digit (digit)*)?;
  whitespace = (' ' | '\t' | '\n' | '\r')+;
  comment = multi_line_comment | line_comment;

Productions

  program = function_decl*statement*;

  function_decl = function_head function_body;

  function_head = func id l_par id r_par;

  function_body = l_curly statement* r_curly;

  statement = id equals expression;

  expression = expression plus term |
             expression minus term |
             term;

  term = term mult factor |
         term div factor |
         factor;

  factor = l_par expression r_par |
           identifier l_par expression r_par |
           float_literal |
           id;

This is explained in the SableCC documentation , aka Étienne Gagnon's master's thesis: 这在SableCC文档 (又称为ÉtienneGagnon的硕士论文)中进行了解释:

Unlike alternatives, elements have an obvious candidate for name which is the identifier of the element itself. 与替代方案不同,元素具有明显的名称候选者,即元素本身的标识符。 This will work, as long as an element does not appear twice in the same alternative. 只要一个元素在同一替代方案中不出现两次,这将起作用。 In that case the current version SableCC requires a name for at least one of the two elements. 在这种情况下,当前版本的SableCC要求至少两个元素之一的名称。 (For backward compatibility, one occurrence of the repeated element can remain unnamed). (为了向后兼容,重复元素的一次出现可以保持未命名)。 SableCC will issue an error if not enough names are provided. 如果没有提供足够的名称,SableCC将发出错误。 Element names are specified by prefixing the element with an identifier between square brackets followed by a colon. 元素名称通过在方括号之间用标识符加上前缀和冒号来指定。

In other words, you can't use id twice in the production for function_head without giving at least one of them a name (whether or not you're generating an AST). 换句话说,您不能在生产中为function_head使用id两次,而不能给它们至少一个名称(无论您是否生成AST)。

Try something like this: 尝试这样的事情:

function_head = func id l_par [parameter]:id r_par;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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