lex 和 yacc 之后的第二个输入中的语法错误
Syntax error in the second input onwards lex and yacc
我有一个 lex 和 yacc 文件来为给定的输入生成抽象语法树。它对第一个 lex 输入工作正常,然后对第二个输入显示 yyerror() 语法错误。 (输入正确)
%{
#include<stdio.h>
#include<math.h>
#include<string.h>
int yylex(void);
#include "y.tab.h"
struct tree
{
char *val;
struct tree* left;
struct tree* right;
};
struct tree* make(struct tree* left, char * , struct tree* right);
void display(struct tree* );
%}
%union
{
struct tree * node;
char * sexy;
}
%token<sexy> INTEGER
%type<node> expr
%type<node> term
%type<sexy> factor
%%
line :
expr '\n' {display();}
| '\n'
expr :
expr '+' term { $$ = make(,"+",);}
| expr '-' term { $$ = make(,"-",);}
| term { $$ = ; }
term :
term '*' factor { $$ = make(,"*",);}
| term '/' factor{ $$ = make(,"/",);}
| factor { $$ = ;}
factor :
'(' expr ')' { $$ = ; }
| INTEGER { $$ = make(NULL,,NULL);}
%%
struct tree* make (struct tree* lft, char * value , struct tree* rght)
{
struct tree *temp=(struct tree*)malloc(sizeof(struct tree));
char *str = malloc(strlen(value)+1);
strcpy(str,value);
temp->val=str;
temp->right=rght;
temp->left=lft;
return (temp);
}
void display(struct tree * dis)
{
int i;
printf("Current node is : %s\n",dis->val);
if(dis->left)
printf("Left child is : %s\n",dis->left->val);
if(dis->right)
printf("Right child is : %s\n\n",dis->right->val);
if(dis->left)
display(dis->left);
else
printf("Left child is empty\n");
if(dis->right)
display(dis->right);
else
printf("Right child is empty\n\n");
}
下面给出的 Lex 代码
%{
#include<stdio.h>
#include "y.tab.h"
int number=0,i=0;
%}
digit [0-9]
letter [a-zA-z]
id {letter}({letter}|{digit})*
%%
{digit}+ {yylval.sexy = yytext; return INTEGER;}
[+-/*] {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
\n {return *yytext;}
%%
void yyerror(char *str)
{
fprintf(stderr, "%s\n", str);
}
int main()
{
yyparse();
return(0);
}
在 YACC 中添加另一个语法规则以适应多行输入
program :
line program
| line
我有一个 lex 和 yacc 文件来为给定的输入生成抽象语法树。它对第一个 lex 输入工作正常,然后对第二个输入显示 yyerror() 语法错误。 (输入正确)
%{
#include<stdio.h>
#include<math.h>
#include<string.h>
int yylex(void);
#include "y.tab.h"
struct tree
{
char *val;
struct tree* left;
struct tree* right;
};
struct tree* make(struct tree* left, char * , struct tree* right);
void display(struct tree* );
%}
%union
{
struct tree * node;
char * sexy;
}
%token<sexy> INTEGER
%type<node> expr
%type<node> term
%type<sexy> factor
%%
line :
expr '\n' {display();}
| '\n'
expr :
expr '+' term { $$ = make(,"+",);}
| expr '-' term { $$ = make(,"-",);}
| term { $$ = ; }
term :
term '*' factor { $$ = make(,"*",);}
| term '/' factor{ $$ = make(,"/",);}
| factor { $$ = ;}
factor :
'(' expr ')' { $$ = ; }
| INTEGER { $$ = make(NULL,,NULL);}
%%
struct tree* make (struct tree* lft, char * value , struct tree* rght)
{
struct tree *temp=(struct tree*)malloc(sizeof(struct tree));
char *str = malloc(strlen(value)+1);
strcpy(str,value);
temp->val=str;
temp->right=rght;
temp->left=lft;
return (temp);
}
void display(struct tree * dis)
{
int i;
printf("Current node is : %s\n",dis->val);
if(dis->left)
printf("Left child is : %s\n",dis->left->val);
if(dis->right)
printf("Right child is : %s\n\n",dis->right->val);
if(dis->left)
display(dis->left);
else
printf("Left child is empty\n");
if(dis->right)
display(dis->right);
else
printf("Right child is empty\n\n");
}
下面给出的 Lex 代码
%{
#include<stdio.h>
#include "y.tab.h"
int number=0,i=0;
%}
digit [0-9]
letter [a-zA-z]
id {letter}({letter}|{digit})*
%%
{digit}+ {yylval.sexy = yytext; return INTEGER;}
[+-/*] {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
\n {return *yytext;}
%%
void yyerror(char *str)
{
fprintf(stderr, "%s\n", str);
}
int main()
{
yyparse();
return(0);
}
在 YACC 中添加另一个语法规则以适应多行输入
program :
line program
| line