I used bison2.4.1, yacc1.9.1 and flex2.5.4 to create a simple parser under
windows XP platform, but I found that yyparse return zero when syntax is error,
this is or not a bug?
The parser's yacc grammar is as follows:
/*statement.flex*/
/*
*/
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[ \t]+ ;
[\n]+ ;
if return IF;
else return ELSE;
print return PRINT;
true {yylval.value = 1; return TRUE;}
false {yylval.value = 1; return FALSE;}
[1-9][0-9]+ {yylval.value = atoi(yytext); return NUMBER;}
. return yytext[0];
%%
int yywrap(void)
{
return 1;
}
/*
statement
*/
%{
#include <stdio.h>
%}
%union
{
int value;
}
%token IF ELSE PRINT
%token <value> NUMBER TRUE FALSE
%type <value> condition
%%
program: statement_list
;
statement_list: statement_list statement
| statement
statement: IF condition statement
{
printf("single if statement.\n");
}
| IF condition statement ELSE statement
{
printf("if else statement.\n");
}
| PRINT NUMBER ';'
{
printf("number = %d\n", $2);
}
| '{' statement_list '}'
{
printf("compound statement.\n");
}
;
condition: TRUE
| FALSE
| error {}
;
%%
extern FILE *yyin;
extern char *yytext;
int main(int argc, char *argv[])
{
int nRet;
if (argc < 2)
return 1;
yyin = fopen(argv[1], "r");
nRet = yyparse();
if (nRet == 0)
{
printf("This is a corrent program!\n");
}
else
{
printf("This is a incorrent program!\n");
}
return 0;
}
int yyerror(const char *str)
{
fprintf(stdout, "yytext = %s, %s\n", yytext, str);
return 0;
}
The parser's flex grammar is as follows:
/*
*/
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[ \t]+ ;
[\n]+ ;
if return IF;
else return ELSE;
print return PRINT;
true {yylval.value = 1; return TRUE;}
false {yylval.value = 1; return FALSE;}
[1-9][0-9]+ {yylval.value = atoi(yytext); return NUMBER;}
. return yytext[0];
%%
int yywrap(void)
{
return 1;
}
//////////////////////////////////////////////////////////////////////////
I forgot the test file.
The test file is as follows:
/* 1.st */
if true
if true
{
if a
print 123;
}
else
print 456;
In addition, the parser runs normal under linux platform. (yyparse returns
non-zero under linux platform, but yyparse return zero in windows xp platform,
why? It it a bug?
Who can explain the cause?
Best regards,
2009-02-08
_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison