gpt4 book ai didi

c - Lex&YACC编译错误: expected ')' before '.' token

转载 作者:行者123 更新时间:2023-11-30 16:23:03 25 4
gpt4 key购买 nike

我必须为迷你语言编写一个解析器,但我遇到了一些问题。这是 YACC 文件:

%{
#include <stdio.h>
int yylex();
void yyerror(char *s);

%}

%union {int num; char id; double d; char *s;}
%start program
%token <num> DIGIT
%token <s> IDENTIFIER
%token <num> NO
%type <num> term condition
%type <s> expression assignstmt stmt
%%
program : "##LAZY###" "vars" decllist cmpdstmt {;}
;
decllist : declaration {;}
| declaration decllist {;}
;
declaration : "in" IDENTIFIER {int $2;}
| "in" '[' NO ']' IDENTIFIER {int $5[$3];}
;
cmpdstmt : "exec" stmtlist "stop" {;}
stmtlist : stmt {;}
| stmt stmtlist {;}
;
stmt : assignstmt {;}
| ifstmt {;}
| whilestmt {;}
;
assignstmt : IDENTIFIER '=' expression {$1 = $3;}
;
expression : expression '+' term {$$ = $1 + $3;}
| term '+' term {$$ = $1 + $3;}
;
term : DIGIT {$$ = $1;}
| IDENTIFIER {$$ = $1;}
;
ifstmt : "if" '(' condition ')' '{' stmt '}' {if($3){$6;}}
;
whilestmt : "wh" '(' condition ')' '{' stmt '}' {while($3){$6;}}
;
condition : expression "<" expression {$$ = ($1 < $3);}
| expression "<=" expression {$$ = ($1 <= $3);}
| expression "==" expression {$$ = ($1 == $3);}
| expression "!=" expression {$$ = ($1 != $3);}
| expression ">=" expression {$$ = ($1 >= $3);}
| expression ">" expression {$$ = ($1 > $3);}
;
%%

int main() {
printf("WORKING\n");
return yyparse();
}

void yyerror(char*s) { printf("%s\n", s); }

但是当我尝试使用以下命令编译它时: cc lex.yy.c y.tab.c 我收到以下错误,但我不知道如何修复它们或为什么收到它们:

lazy.y: In function ‘yyparse’:
lazy.y:21:19: error: expected ‘)’ before ‘.’ token
declaration : "in" IDENTIFIER {int $2;}
^
lazy.y:22:19: error: expected ‘)’ before ‘.’ token
| "in" '[' NO ']' IDENTIFIER {int $5[$3];}

如果需要,我也会发布 Lex 文件。

最佳答案

来自

declaration : "in" IDENTIFIER                    {int $2;}
| "in" '[' NO ']' IDENTIFIER {int $5[$3];}
;

错误来自{int $2;}{int $5[$3];}

你对他们有什么期望?

这是合法的:

declaration : "in" IDENTIFIER                    {char * s = $2;}
| "in" '[' NO ']' IDENTIFIER {int i = $5[$3];}
;

当然,这些变量是局部的,因此没有真正的意义

关于c - Lex&YACC编译错误: expected ')' before '.' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54159906/

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