Dear all,
I am a newbie using lex and yacc.
I really don't know what's wrong with my code, because the output is not
what I want.
My yacc rules don't match, and yylex seems just read only one line of the
text file.
Please help...thx.
test file:
---------------------------------------
*******
SLICE 1,17
*
*******
*
VERSION : 3.2.5
---------------------------------------
test.h
---------------------------------------
#ifndef YYERRCODE
#define YYERRCODE 256
#endif
#define SLICE 257
#define VERSION 258
#define INTEGER 259
typedef union {
int iInteger;
char* szRecord;
} YYSTYPE;
extern YYSTYPE yylval;
---------------------------------------
lex:
---------------------------------------
%{
#include "test.h"
%}
comment "*".*
whitespace [ \t\r\n]+
%%
{whitespace} ;
{comment} ;
SLICE {yylval.szRecord=strdup(yytext);printf("In lex %s
",yylval.szRecord);return SLICE;}
VERSION {yylval.szRecord=strdup(yytext);printf("In lex %s
",yylval.szRecord);return VERSION;}
[0-9]+ {yylval.iInteger=atoi(yytext);printf("In lex..%d
",yylval.iInteger);return INTEGER;}
.. ;
%%
---------------------------------------
yacc:
---------------------------------------
%{
/* MEBES job file parser */
#include <stdio.h>
%}
%union {
int iInteger;
char* szRecord;
}
%token <szRecord> SLICE VERSION
%token <iInteger> INTEGER
%%
test : SLICE {printf("Got SLICE\n");}
| SLICE INTEGER ',' INTEGER {printf("In SLICE,no1=%d,no2=%d\n",$2,$4);}
;
test2 : VERSION {printf("%s \n",$1);}
;
%%
extern FILE *yyin;
int main(int argc,char** argv)
{
yyin=fopen(argv[1],"r");
if (yyin) {
while (!feof(yyin)) {
yyparse();
}
printf("Finish.........\n");
fclose(yyin);
return 0;
}
else {
return -1;
}
}
yyerror(char* errmsg)
{
printf("%s\n");
}
---------------------------------------