我的语法怎么了

编程入门 行业动态 更新时间:2024-10-27 16:33:58
本文介绍了我的语法怎么了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试将以下内容输入到我的yacc解析器中:

I try to input the following into my yacc parser:

int main(void) { return; }

根据yacc文件中定义的内容,它对我来说似乎有效,但是返回后我收到语法错误"消息.为什么会这样?

It looks valid to me according to what's defined in the yacc file, but I get a "syntax error" message after the return. Why is that?

yacc文件:

/* C-Minus BNF Grammar */ %{ #include "parser.h" #include <string.h> %} %union { int intval; struct symtab *symp; } %token ELSE %token IF %token INT %token RETURN %token VOID %token WHILE %token <symp> ID %token <intval> NUM %token LTE %token GTE %token EQUAL %token NOTEQUAL type <string> paramlist %% 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"); $2->args = 'a'; 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 ';' ; expression : var '=' expression | simple_expression ; var : ID | ID '[' expression ']' ; simple_expression : additive_expression relop additive_expression | additive_expression ; relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ; additive_expression : additive_expression addop term | term ; addop : '+' | '-' ; term : term mulop factor | factor ; mulop : '*' | '/' ; factor : '(' expression ')' | var | call | NUM ; call : ID '(' args ')' ; args : arg_list | /* empty */ ; arg_list : arg_list ',' expression | expression ; %% /* look up a symbol table entry, add if not present */ struct symtab *symlook(char *s) { printf("Putting %s into the symbol table\n", s); //char *p; struct symtab *sp; for(sp = symtab; sp < &symtab[NSYMS]; sp++) { /* is it already here? */ if(sp->name && !strcmp(sp->name, s)) { yyerror("already in symbol table\n"); exit(1); return sp; } if(!sp->name) { /* is it free */ sp->name = strdup(s); return sp; } /* otherwise continue to next */ } yyerror("Too many symbols"); exit(1); /* cannot continue */ } /* symlook */ yyerror(char *s) { printf( "yyerror: %s\n", s); } printTable() { printf("Print out the symbol table:\n\n"); struct symtab *sp; for(sp = symtab; sp < &symtab[NSYMS]; sp++) { printf("name: %s\t" "args: %s\t" "value %d\t" "arraysize %d\n", sp->name, sp->args, sp->value, sp->arraysize); } }

推荐答案

您的yacc语法文件看不到任何错误;我最好的猜测是您的扫描仪没有正确返回RETURN令牌.如果我与一起编译您的yacc源码

I can't see anything wrong with your yacc grammar file; my best guess is that your scanner isn't returning the RETURN token properly. If I compile your yacc source along with

parser.h

#define NSYMS 100 struct symtab { char *name; int value; int arraysize; char *args; } symtab[NSYMS];

scanner.l

%{ #include "parser.tab.h" extern struct symtab *symlook(char *); extern void printTable(); %} %option noyywrap NUM [0-9][0-9]* ID [_a-zA-Z][_a-zA-Z0-9]* %% else return ELSE; if return IF; int return INT; return return RETURN; void return VOID; while return WHILE; "==" return EQUAL; "!=" return NOTEQUAL; ">=" return LTE; "<=" return GTE; [ \t\n] ; {NUM} { yylval.intval = atoi(yytext); return NUM; } {ID} { yylval.symp = symlook(yytext); return ID; } . return *yytext; %% int main() { yyparse(); printTable(); return 0; }

然后它接受您的示例程序,并且没有错误,并且仅使用 main 符号

Then it accepts your example program just fine with no errors, and prints out the symbol table with just the main symbol

更多推荐

我的语法怎么了

本文发布于:2023-10-14 22:50:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!