每次我尝试解决此问题时,都会出现此错误。我不知道我在做什么错。这是我的yacc文件和lexx文件:
这里是我的YFILE。Y
%{
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE double
#include "y.tab.h"
extern "C" FILE * yyin;
extern "C" int yylex();
extern int yyparse(void *);
using namespace std;
int yyerror(const char* s);
%}
%token DIGIT CHARACTER OP LEFT_PAR RIGHT_PAR SEMICOLON EQUAL NEWLINE OTHER
%union{
char *s;
}
%%
assignment:
id EQUAL expression SEMICOLON NEWLINE
;
expression:
id OP id '{OP id}'
| id OP id '{'LEFT_PAR'}' '{'expression'}' '{'RIGHT_PAR'}' NEWLINE
| id OP id '{'LEFT_PAR expression'}' '{'RIGHT_PAR'}' NEWLINE
;
id:
CHARACTER
|id CHARACTER
|id DIGIT
;
%%
int yyerror(const char *s)
{
cout <<"error" << s << endl;
return -1;
}
main (){
FILE *yyin = fopen("ex.txt","r");
yyparse();
fclose(yyin);
printf("hi");
return 0;
}
这里是我的语法。
%{
#include "y.tab.h"
#include <stdio.h>
#define YY_DECL extern "C" int yylex()
using namespace std;
#include <iostream>
void ERROR();
%}
digit [0-9]+
char [a-zA-Z]
op [+\-*/%]
%%
{digit} {return (DIGIT);}
{char} {return (CHARACTER);}
{op} {return (OP);}
"(" {return (LEFT_PAR);}
")" {return (RIGHT_PAR);}
"=" {return (EQUAL);}
";" {return (SEMICOLON);}
"\n" {return (NEWLINE);}
. {return (OTHER);}
%%
void ERROR(){
cout << OTHER <<endl;
}
我有语法规则,并且在'yyparse()'中得到对'yylex'的错误未定义引用
答案 0 :(得分:0)
如果遇到此问题,则yacc和lex文件必须具有相同的名称。例如snazzle.l和snazzle.y。