Yacc 程序无法识别函数声明

发布于 2024-08-12 16:00:32 字数 1573 浏览 3 评论 0原文

我认为我的程序应该能够将以下内容识别为函数声明:

int fn(int i) { int n; return; }

但事实并非如此。

这是我的 yacc 文件的相关部分:

program : declaration_list ; 

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration 
            | fun_declaration 
            | '$' { printTable();};

var_declaration : type_specifier ID ';' {$2->value = 0; $2->arraysize = 0;};
                | type_specifier ID '[' NUM ']' ';' {$2->arraysize = $4;printf("Array size is %d", $2->arraysize);} ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt {printf("function declaration\n"); printf("Parameters: \n", $2->args); } ;


params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' {printf("exiting scope\n"); } ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

为什么它无法识别它?

I think my program should be able to recognize the following as a function declaration:

int fn(int i) { int n; return; }

but it doesn't.

Here's the relevant part of my yacc file:

program : declaration_list ; 

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration 
            | fun_declaration 
            | '

Why does it not recognize it?

{ printTable();}; var_declaration : type_specifier ID ';' {$2->value = 0; $2->arraysize = 0;}; | type_specifier ID '[' NUM ']' ';' {$2->arraysize = $4;printf("Array size is %d", $2->arraysize);} ; type_specifier : INT | VOID ; fun_declaration : type_specifier ID '(' params ')' compound_stmt {printf("function declaration\n"); printf("Parameters: \n", $2->args); } ; params : param_list | VOID ; param_list : param_list ',' param | param ; param : type_specifier ID | type_specifier ID '[' ']' ; compound_stmt : '{' local_declarations statement_list '}' {printf("exiting scope\n"); } ; local_declarations : local_declarations var_declaration | /* empty */ ; statement_list : statement_list statement | /* empty */ ; statement : expression_stmt | compound_stmt | selection_stmt | iteration_stmt | return_stmt ; expression_stmt : expression ';' | ';' ; selection_stmt : IF '(' expression ')' statement | IF '(' expression ')' statement ELSE statement ; iteration_stmt : WHILE '(' expression ')' statement ; return_stmt : RETURN ';' | RETURN expression ';' ;

Why does it not recognize it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筑梦 2024-08-19 16:00:32

这不是您问题的答案,而是调试建议。

在每个规则之后,添加一个 printf 语句,告诉您该规则已匹配。看看我的回答 获取提示。然后通过语法运行您的输入,看看它在做什么。

提出问题的另一个提示是尝试将问题简化为重现错误的最简单情况。对于上述语法,在发布之前删除您不希望与此特定示例匹配的所有规则。另外,发布一个完整的程序,并带有 %token 声明,以便人们可以编译它并亲自尝试。更好的是也发布词法分析器。

This isn't an answer to your question but a suggestion for debugging.

After each of the rules, add a printf statement which tells you that the rule has been matched. Take a look at my answer here for hints. Then run your input through the grammar and see what it is doing.

Another hint, for asking questions, is to try to reduce your problem to the simplest case which reproduces the error. In the case of the above grammar, strip out all of the rules which you don't expect to match this particular example before posting it. Also, post a complete program, with the %token declarations, so people can compile it and try it out for themselves. Even better is to post the lexer too.

我的影子我的梦 2024-08-19 16:00:32

我认为你需要类似的东西:

declaration : type_specifier ID ';' {$2->value = 0; $2->arraysize = 0;};
            | type_specifier ID '[' NUM ']' ';' {$2->arraysize = $4;printf("Array size is %d", $2->arraysize);} ;
            | type_specifier ID '(' params ')' compound_stmt {printf("function declaration\n"); printf("Parameters: \n", $2->args); } ;

            | '

type_specifier : INT |空白 ;

{ printTable();};

type_specifier : INT |空白 ;

I think you need something like:

declaration : type_specifier ID ';' {$2->value = 0; $2->arraysize = 0;};
            | type_specifier ID '[' NUM ']' ';' {$2->arraysize = $4;printf("Array size is %d", $2->arraysize);} ;
            | type_specifier ID '(' params ')' compound_stmt {printf("function declaration\n"); printf("Parameters: \n", $2->args); } ;

            | '

type_specifier : INT | VOID ;

{ printTable();};

type_specifier : INT | VOID ;

神妖 2024-08-19 16:00:32

只需查看您所在的行

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

该行对我来说看起来很麻烦,因为您的它就像一个递归语句...

希望这能给您提示,
此致,
汤姆.

Just looked at the line where you have

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

That line looks troublesome to me, since you have it like a recursive statement...

Hope this gives you the hint,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文