lex&&yacc 编译通过,但是检测语法时老是只执行yyerror
lex&&yacc 编译通过,但是检测语法时总是只执行yyerror
搞了四五天的 lex&&yacc,各种问题,现在算是能正确编译了。
但遇到执行的问题,搞了好久但是不知道错在哪里,有点迷。
所以贴出来希望熟悉的朋友指点一下,感谢。
lex的源代码CFanay.l:
%{
#include <stdlib.h>
#include <stdio.h>
#include "CFanay.tab.h"
%}
%%
"begin" {return ENTRY ;}
"end" {return END ;}
[0-9]+ {yylval = atoi(yytext); return NUMBER ;}
"-" {return '-';}
"+" {return '+';}
"*" {return '*';}
"/" {return '/';}
"#" {return '#';}
"(" {return '(' ;}
")" {return ')';}
";" {return ';';}
[a-zA-Z]+[0-9]* {return ID;}
":=" {return EQUAL ;}
[" "\t\n] ;
. ;
%%
yacc的源代码CFanay.y:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror();
%}
%token ENTRY END NUMBER ID EQUAL
%left '+' '-'
%left '*' '/'
%%
program : program
ENTRY staments END '#' {printf("success\n");}
|
;
staments : stament
| stament ';' stament
;
stament : assignment;
assignment : ID EQUAL expression ;
expression : term
| term '+' term {$$=$1+$2;}
|term '-' term {$$=$1-$2;}
;
term : factor
| factor '*' factor {$$=$1*$2;}
| factor '/' factor {$$=$1/$2;}
;
factor : ID
| NUMBER {$$=$1;}
| '(' expression ')'
;
%%
int main()
{
yyparse();
return 0;
}
void yyerror()
{
printf("error\n");
}
unbuntu下的命令行:
bison -d CFanay.y
flex CFanay.l
cc -o parser *.c -ly -ll
测试文件test.txt:
x:=a+b*c end #
(命令行:./parser <test.txt
应该提示error!)
测试文件test1.txt:
begin a:=9; x:=2*3; b:=a+x end #
(命令行./parser <test1.txt
应该提示success!,可是还是提示error)
------解决思路----------------------
staments : stament
------解决思路----------------------
stament ';' stament
;
==>
staments : stament
------解决思路----------------------
stament ';' staments
;
搞了四五天的 lex&&yacc,各种问题,现在算是能正确编译了。
但遇到执行的问题,搞了好久但是不知道错在哪里,有点迷。
所以贴出来希望熟悉的朋友指点一下,感谢。
lex的源代码CFanay.l:
%{
#include <stdlib.h>
#include <stdio.h>
#include "CFanay.tab.h"
%}
%%
"begin" {return ENTRY ;}
"end" {return END ;}
[0-9]+ {yylval = atoi(yytext); return NUMBER ;}
"-" {return '-';}
"+" {return '+';}
"*" {return '*';}
"/" {return '/';}
"#" {return '#';}
"(" {return '(' ;}
")" {return ')';}
";" {return ';';}
[a-zA-Z]+[0-9]* {return ID;}
":=" {return EQUAL ;}
[" "\t\n] ;
. ;
%%
yacc的源代码CFanay.y:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror();
%}
%token ENTRY END NUMBER ID EQUAL
%left '+' '-'
%left '*' '/'
%%
program : program
ENTRY staments END '#' {printf("success\n");}
|
;
staments : stament
| stament ';' stament
;
stament : assignment;
assignment : ID EQUAL expression ;
expression : term
| term '+' term {$$=$1+$2;}
|term '-' term {$$=$1-$2;}
;
term : factor
| factor '*' factor {$$=$1*$2;}
| factor '/' factor {$$=$1/$2;}
;
factor : ID
| NUMBER {$$=$1;}
| '(' expression ')'
;
%%
int main()
{
yyparse();
return 0;
}
void yyerror()
{
printf("error\n");
}
unbuntu下的命令行:
bison -d CFanay.y
flex CFanay.l
cc -o parser *.c -ly -ll
测试文件test.txt:
x:=a+b*c end #
(命令行:./parser <test.txt
应该提示error!)
测试文件test1.txt:
begin a:=9; x:=2*3; b:=a+x end #
(命令行./parser <test1.txt
应该提示success!,可是还是提示error)
------解决思路----------------------
staments : stament
------解决思路----------------------
stament ';' stament
;
==>
staments : stament
------解决思路----------------------
stament ';' staments
;