hello,
i have a doubt in the parser design program using flex and bison.
can someone please help me find the error.
my scan.l code:
Code:
%{
/* a pure (reentrant) parser and scanner */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int my_yy_flex_debug; /* my flex debug flag */
extern int fileno(FILE *); /* avoids gcc warning */
static int number(char *, YYSTYPE *);
static int string(char *, YYSTYPE *);
static char *estrdup( const char *s)
{
char *r = malloc( strlen(s) + 1);
if( r == NULL)
abort();
return strcpy( r, s);
}
%}
D [0-9]
E [Ee][+\-]?{D}+
A [A-Za-z_][A-Za-z_0-9]*
%%
%{
yy_flex_debug = my_yy_flex_debug;
%}
[ \t\v\f] { /* ignore whitespace */ }
[()=+*/\-\n] { return yytext[0]; }
{D}+({E})? { return number(yytext, yylval); }
{D}*"."{D}+({E})? { return number(yytext, yylval); }
{D}+"."{D}*({E})? { return number(yytext, yylval); }
'+' { return PLUS; }
'-' { return MINUS; }
'*' { return MULT; }
"/" { return DIVI; }
"reshape" { return RESHAPE; }
".+" { return DPLUS; }
".-" { return DMINUS; }
".*" { return DMULT ; }
"./" { return DDIVI; }
\"[^"]*\" { return string( yytext, yylval); }
{A}({A}|{D})* { yylval->string = estrdup( yytext); return ID; }
'=' { return EQUAL; }
abs { return ABS; }
avg { return AVG; }
"+/" { return SPLUS; }
"*/" { return SMULT; }
"-/" { return SMINUS; }
"//" { return SDIVI; }
"#".* { /* ignore comments */ }
. { return yytext[0]; /* bad token */ }
%%
void (*jj_junk)(int,char *,yyscan_t) = yyunput; /* avoids gcc warning */
int number( char *s, YYSTYPE *y)
{
if( sscanf( s, "%lf", &(y->number) ) != 1 ) return 0;
return NUMBER;
}
int string( char *s, YYSTYPE *y)
{
y->string = estrdup( s);
return STRING;
}
extern int yyparse( yyscan_t scanner);
int main( int argc, char *argv[])
{
yyscan_t scanner;
#if YYDEBUG
yydebug = argc > 1;
#endif
my_yy_flex_debug = argc > 1;
yylex_init( &scanner);
yyparse( scanner);
yylex_destroy( scanner);
return 0;
}
/* called at EOF */
int yywrap( yyscan_t scanner)
{
return 1;
}
/* called for parser syntax error */
parse.y code:
Code:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void *yyscan_t;
%}
%union{
int count;
double number;
char *string;
}
%token <number> NUMBER
%token <string> ID
%token PLUS MINUS DIVI MULT SPLUS SMINUS SMULT SDIVI DPLUS DMINUS DDIVI DMULT EQUAL ABS AVG RESHAPE
%type <count> elist
%{
%}
%%
expr : pri
| MINUS expr { printf( "unary - \n"); }
| expr MULT expr { printf( "binary * \n"); }
| expr DIVI expr { printf( "binary / \n"); }
| expr MINUS expr { printf( "binary - \n"); }
| expr PLUS expr { printf( "binary + \n"); }
| expr DPLUS expr { printf( "binary .+ \n"); }
| expr DMINUS expr { printf( "binary .- \n"); }
| expr DMULT expr { printf( "binary .* \n"); }
| expr DDIVI expr { printf( "binary ./ \n"); }
| expr SPLUS expr { printf( "unary +/ \n"); }
| expr SMINUS expr { printf( "unary -/ \n"); }
| expr SMULT expr { printf( "unary */ \n"); }
| expr SDIVI expr { printf( "umary // \n"); }
|expr RESHAPE expr { printf( "binary reshape \n"); }
| ABS expr { printf( "unary abs\n"); }
| AVG expr { printf( "unary avg\n"); }
| ID EQUAL expr { printf( "binary = \n"); }
;
elist : expr { $$ = 1; }
| elist ',' expr { $$ += 1; }
;
pri
: NUMBER { printf( "push: %f\n", $1);}
| '(' expr ')'
| '{' { printf( "list start:\n"); }
elist '}' { printf( "list end \n"); }
| ID
{
printf( "push: %s\n", $1);
}
;
%%
errors:
gcc -ansi -pedantic -Wall -DDEBUG=1 -c -o scan.o scan.c
scan.l:12: parse error before "YYSTYPE"
scan.l:13: parse error before "YYSTYPE"
scan.l: In function `yylex':
scan.l:44: `yylval' undeclared (first use in this function)
scan.l:44: (Each undeclared identifier is reported only once
scan.l:44: for each function it appears in.)
scan.l:48: `PLUS' undeclared (first use in this function)
scan.l:49: `MINUS' undeclared (first use in this function)
scan.l:50: `MULT' undeclared (first use in this function)
scan.l:51: `DIVI' undeclared (first use in this function)
scan.l:52: `RESHAPE' undeclared (first use in this function)
scan.l:53: `DPLUS' undeclared (first use in this function)
scan.l:54: `DMINUS' undeclared (first use in this function)
scan.l:55: `DMULT' undeclared (first use in this function)
scan.l:56: `DDIVI' undeclared (first use in this function)
scan.l:58: `ID' undeclared (first use in this function)
scan.l:59: `EQUAL' undeclared (first use in this function)
scan.l:61: `ABS' undeclared (first use in this function)
scan.l:62: `AVG' undeclared (first use in this function)
scan.l:64: `SPLUS' undeclared (first use in this function)
scan.l:65: `SMULT' undeclared (first use in this function)
scan.l:66: `SMINUS' undeclared (first use in this function)
scan.l:67: `SDIVI' undeclared (first use in this function)
scan.l: At top level:
scan.l:78: parse error before "yyscan_t"
scan.l:80: parse error before "YYSTYPE"
scan.l: In function `number':
scan.l:82: `s' undeclared (first use in this function)
scan.l:82: `y' undeclared (first use in this function)
scan.l:83: `NUMBER' undeclared (first use in this function)
scan.l: At top level:
scan.l:86: parse error before "YYSTYPE"
scan.l: In function `string':
scan.l:88: `y' undeclared (first use in this function)
scan.l:88: `s' undeclared (first use in this function)
scan.l:89: `STRING' undeclared (first use in this function)
scan.l: At top level:
scan.l:92: parse error before "scanner"
scan.l: In function `main':
scan.l:97: `yyscan_t' undeclared (first use in this function)
scan.l:97: parse error before "scanner"
scan.l:104: warning: implicit declaration of function `yylex_init'
scan.l:104: `scanner' undeclared (first use in this function)
scan.l:108: warning: implicit declaration of function `yylex_destroy'
scan.l: At top level:
scan.l:115: parse error before "scanner"
can some someone please help me out with this one.
Thanx a lot
Regards,
Akshara