'=' 标记前的预期表达式
expected expression before '=' token
我想做解析器,它将表达式打印到它们的计算步骤中。当我编译我的代码时,我无法解决这些问题。我总是收到错误
code.l:13:1: error: expected expression before '=' token
yylval.name = strdup(yytext);
^
code.l:18:1: error: expected expression before '=' token
yylval.name = strdup(yytext);
^
我尝试了很多不同的东西,我认为有问题,但没有成功。
code.l
%{
#include <stdio.h>
#include <string.h>
#include "Assignment.tab.h"
%}
%%
" " ;
"\t" ;
[a-zA-Z]+
{
yylval.name = strdup(yytext);
return(ID);
}
[0-9]+
{
yylval.name = strdup(yytext);
return(NUM);
}
[-+=()*/\n]
{
return yytext[0];
}
.
{
yyerror("unknown character");
}
%%
code.y
%{
#include<stdio.h>
int temp = 0;
%}
%start list
%union
{
char *name;
}
%token <name> ID
%token <name> NUM
%type <name> list stat expr
%left '+' '-'
%left '*' '/'
%left UMINUS
%%
list:
|
list stat '\n'
|
list error '\n'
{
yyerrok;
}
;
stat:
expr
{
printf("stat:t = (%s)\n:stat",);
}
|
ID '=' expr
{
printf("stat:(%s) = (%s):stat", , );
}
;
expr:
'(' expr ')'
{
$$ = ;
}
|
expr '*' expr
{
printf("t = (%s) * (%s)", , );
$$ = "t";
}
|
expr '/' expr
{
printf("t = (%s) / (%s)", , );
$$ = "t";
}
|
expr '+' expr
{
printf("t = (%s) + (%s)", , );
$$ = "t";
}
|
expr '-' expr
{
printf("t = (%s) - (%s)", , );
$$ = "t";
}
|
'-' expr %prec UMINUS
{
printf("t = -(%s)", );
$$ = "t";
}
|
ID
{
$$ = ;
}
|
NUM
{
$$ = ;
}
;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
我的最终项目不需要解决方案,我只需要找出导致错误的原因。任何想法都有帮助。
编辑:
Assignment.tab.h 文件
#ifndef YY_YY_ASSIGNMENT_TAB_H_INCLUDED
# define YY_YY_ASSIGNMENT_TAB_H_INCLUDED
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
ID = 258,
NUM = 259,
UMINUS = 260
};
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 2058 of yacc.c */
#line 10 "Assignment.y"
char *name;
/* Line 2058 of yacc.c */
#line 67 "Assignment.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_YY_ASSIGNMENT_TAB_H_INCLUDED */
lex 规则中的操作必须与模式在同一行开始。所以你需要这样写,比如
[a-zA-Z]+ {
yylval.name = strdup(yytext);
return(ID);
}
对于它的价值,此要求在 flex manual section on the format of an input file 中明确说明:
The rules section of the flex input contains a series of rules of the form:
pattern action
where the pattern must be unindented and the action must begin on the same line.
据我所知,此限制存在于所有 lex 实现中,尽管结果不同。我引用了 Flex 手册,因为我发现它比 Posix 描述更具可读性,而且它还描述了许多有用的 flex-only 功能。
如果您查看生成的 lex.yy.c
文件,您最终会发现如下代码:
case 4:
YY_RULE_SETUP
#line 13 "code.l"
= strdup(yytext);
YY_BREAK
这显然不是您想要的。如 Rici in their 所述,问题是动作的开始必须与模式在同一行。
[a-zA-Z]+ {
yylval.name = strdup(yytext);
return(ID);
}
Flex 也给出警告:
code.l:18: warning, rule cannot be matched
code.l:23: warning, rule cannot be matched
你最好在编译生成的 C 代码之前修复这样的警告。
我想做解析器,它将表达式打印到它们的计算步骤中。当我编译我的代码时,我无法解决这些问题。我总是收到错误
code.l:13:1: error: expected expression before '=' token
yylval.name = strdup(yytext);
^
code.l:18:1: error: expected expression before '=' token
yylval.name = strdup(yytext);
^
我尝试了很多不同的东西,我认为有问题,但没有成功。
code.l
%{
#include <stdio.h>
#include <string.h>
#include "Assignment.tab.h"
%}
%%
" " ;
"\t" ;
[a-zA-Z]+
{
yylval.name = strdup(yytext);
return(ID);
}
[0-9]+
{
yylval.name = strdup(yytext);
return(NUM);
}
[-+=()*/\n]
{
return yytext[0];
}
.
{
yyerror("unknown character");
}
%%
code.y
%{
#include<stdio.h>
int temp = 0;
%}
%start list
%union
{
char *name;
}
%token <name> ID
%token <name> NUM
%type <name> list stat expr
%left '+' '-'
%left '*' '/'
%left UMINUS
%%
list:
|
list stat '\n'
|
list error '\n'
{
yyerrok;
}
;
stat:
expr
{
printf("stat:t = (%s)\n:stat",);
}
|
ID '=' expr
{
printf("stat:(%s) = (%s):stat", , );
}
;
expr:
'(' expr ')'
{
$$ = ;
}
|
expr '*' expr
{
printf("t = (%s) * (%s)", , );
$$ = "t";
}
|
expr '/' expr
{
printf("t = (%s) / (%s)", , );
$$ = "t";
}
|
expr '+' expr
{
printf("t = (%s) + (%s)", , );
$$ = "t";
}
|
expr '-' expr
{
printf("t = (%s) - (%s)", , );
$$ = "t";
}
|
'-' expr %prec UMINUS
{
printf("t = -(%s)", );
$$ = "t";
}
|
ID
{
$$ = ;
}
|
NUM
{
$$ = ;
}
;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
我的最终项目不需要解决方案,我只需要找出导致错误的原因。任何想法都有帮助。
编辑: Assignment.tab.h 文件
#ifndef YY_YY_ASSIGNMENT_TAB_H_INCLUDED
# define YY_YY_ASSIGNMENT_TAB_H_INCLUDED
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
ID = 258,
NUM = 259,
UMINUS = 260
};
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 2058 of yacc.c */
#line 10 "Assignment.y"
char *name;
/* Line 2058 of yacc.c */
#line 67 "Assignment.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_YY_ASSIGNMENT_TAB_H_INCLUDED */
lex 规则中的操作必须与模式在同一行开始。所以你需要这样写,比如
[a-zA-Z]+ {
yylval.name = strdup(yytext);
return(ID);
}
对于它的价值,此要求在 flex manual section on the format of an input file 中明确说明:
The rules section of the flex input contains a series of rules of the form:
pattern action
where the pattern must be unindented and the action must begin on the same line.
据我所知,此限制存在于所有 lex 实现中,尽管结果不同。我引用了 Flex 手册,因为我发现它比 Posix 描述更具可读性,而且它还描述了许多有用的 flex-only 功能。
如果您查看生成的 lex.yy.c
文件,您最终会发现如下代码:
case 4:
YY_RULE_SETUP
#line 13 "code.l"
= strdup(yytext);
YY_BREAK
这显然不是您想要的。如 Rici in their
[a-zA-Z]+ {
yylval.name = strdup(yytext);
return(ID);
}
Flex 也给出警告:
code.l:18: warning, rule cannot be matched
code.l:23: warning, rule cannot be matched
你最好在编译生成的 C 代码之前修复这样的警告。