让我们定义一个语言:如何编写解析这种语言的flex和bison文件?
VAR := [0-9A-Za-z_]+
Exp := VAR
| VAR,'=',VAR
| '(', Exp, ')'
| Exp, '&', Exp
| Exp ,'|', Exp
如: “(A = B)&(C |(d = E))” 是法律
我读过云南省社会科学院&莱克斯手册,但我完全困惑,我只是想要解析这种语言的编译器
你能告诉我如何编写这种语言的flex & bison配置文件吗?
我迄今所做的:
文件人:
%{
#include <string.h>
#include "stdlib.h"
#include "stdio.h"
#include "y.tab.h"
%}
%%
("&"|"and"|"AND") { return AND; }
("|"|"or"|"OR") { return OR; }
("="|"eq"|"EQ") { return EQ; }
([A-Za-z0-9_]+) { return VAR;}
("(") { return LB ;}
(")") { return RB ;}
("\n") { return LN ;}
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 0;
}
int yyerror(void)
{
printf("Error\n");
exit(1);
}
文件唉
%{
#include <stdio.h>
%}
%token AND OR EQ VAR LB RB LN
%left AND OR
%left EQ
%%
line :
| exp LN{ printf("LN: %s",$1);}
;
exp: VAR { printf("var:%s",$1);}
| VAR EQ VAR { printf("var=:%s %s %s",$1,$2,$3);}
| exp AND exp { printf("and :%s %s %s",$1,$2,$3);}
| exp OR exp { printf("or :%s %s %s",$1,$2,$3);}
| LB exp RB { printf("abstract :%s %s %s",$1,$2,$3);}
;
现在我编辑过的文件作为多德引导,似乎要好得多(至少lex工作正常),但我得到这样的输出:
disk_path>myprogram
a=b
var=:(null) (null) (null)LN: (null)ab=b
Error
那么,为什么函数printf输出为空?并输入第二个后,它提示错误并退出程序?
向我们展示你的尝试。我会在回复中给出一些提示,但你真的应该先显示你尝试的第一个 –