![]() |
#2
flyingcloude2010-08-04 08:30
|

以下为lex文件代码Name.l
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
// extern char* yylval;
%}
char [A-Za-z]
num [0-9]
eq [=]
name {char}+
age {num}+
%%
{name} { yylval = strdup(yytext); return NAME;}
{eq} { return EQ; }
{age} { yylval = strdup(yytext); return AGE; }
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
// extern char* yylval;
%}
char [A-Za-z]
num [0-9]
eq [=]
name {char}+
age {num}+
%%
{name} { yylval = strdup(yytext); return NAME;}
{eq} { return EQ; }
{age} { yylval = strdup(yytext); return AGE; }
%%
int yywrap()
{
return 1;
}

以下为yacc程序代码Name.y
%{
#include <stdio.h>
#include "lex.yy.c"
//typedef char* string; /* to specify token types as char* */
//#define YYSTYPE string /* Yacc variable which has the value of returned token */
%}
%token NAME EQ AGE
%%
file : record file
| record
;
record : NAME EQ AGE {
printf("%s is %s years old!!!\n", $1, $3); }
;
%%
int main()
{
yyparse();
return 0;
}
int yyerror(char *msg)
{
printf("Error encountered: %s \n", msg);
}
Name.l用到了需要Name.y文件经yacc处理达到的y.tab.h文件,反之Name.y用到了需要Name.l文件经lex处理达到的lex.yy.c文件
理论上应该是,不管先编译哪个程序,都会有找不到头文件的文件,但是lex和yacc却没有提示相关的错误信息,这很奇怪呀!
%{
#include <stdio.h>
#include "lex.yy.c"
//typedef char* string; /* to specify token types as char* */
//#define YYSTYPE string /* Yacc variable which has the value of returned token */
%}
%token NAME EQ AGE
%%
file : record file
| record
;
record : NAME EQ AGE {
printf("%s is %s years old!!!\n", $1, $3); }
;
%%
int main()
{
yyparse();
return 0;
}
int yyerror(char *msg)
{
printf("Error encountered: %s \n", msg);
}
Name.l用到了需要Name.y文件经yacc处理达到的y.tab.h文件,反之Name.y用到了需要Name.l文件经lex处理达到的lex.yy.c文件
理论上应该是,不管先编译哪个程序,都会有找不到头文件的文件,但是lex和yacc却没有提示相关的错误信息,这很奇怪呀!