gpt4 book ai didi

java - SableCC 语法文件的问题

转载 作者:行者123 更新时间:2023-12-02 05:28:32 25 4
gpt4 key购买 nike

我似乎在 SableCC 生成通常从语法文件自动生成的相关词法分析器、节点和解析内容时遇到问题。我目前没有实现抽象语法树。

当我尝试使用下面的语法文件运行 SableCC 时,出现以下错误:

[41,33] AFunctionHead.Id 的重新定义。 我知道问题是什么,但似乎是生产领域的问题。我可能错过了什么吗?

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;

最佳答案

这在 SableCC documentation 中有解释。 ,又名 Étienne Gagnon 的硕士论文:

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. (For backward compatibility, one occurrence of the repeated element can remain unnamed). SableCC will issue an error if not enough names are provided. Element names are specified by prefixing the element with an identifier between square brackets followed by a colon.

换句话说,如果不给其中至少一个命名(无论您是否生成AST)。

尝试这样的事情:

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

关于java - SableCC 语法文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com