i’m a newbie with this flex and bison and I have a small question. Once I got my lex.yy.c file and my tab.c file when i complie the lex.yy.c file i get errors:
stojk_2.l: In function ‘int yylex()’:
stojk_2.l:3: error: ‘PLUS’ was not declared in this scope
stojk_2.l:4: error: ‘MINUS’ was not declared in this scope
stojk_2.l:5: error: ‘MULT’ was not declared in this scope
stojk_2.l:6: error: ‘DIVIDE’ was not declared in this scope
stojk_2.l:8: error: ‘LPAREN’ was not declared in this scope
stojk_2.l:9: error: ‘RPAREN’ was not declared in this scope
stojk_2.l:12: error: ‘yylval’ was not declared in this scope
stojk_2.l:13: error: ‘UNSIGNEDINTEGER’ was not declared in this scope
and when i compile the tab.c file i get these errors:
stojk_3.y: In function ‘void yyerror(char*)’:
stojk_3.y:12: error: ‘print’ was not declared in this scope
stojk_3.tab.c: At global scope:
stojk_3.tab.c:1056: error: redefinition of ‘double yylval’
stojk_3.y:8: error: ‘double yylval’ previously declared here
stojk_3.tab.c: In function ‘int yyparse()’:
stojk_3.tab.c:1253: error: ‘yylex’ was not declared in this scope
stojk_3.tab.c:1401: warning: deprecated conversion from string constant to ‘char*’
stojk_3.tab.c:1547: warning: deprecated conversion from string constant to ‘char*’
it seems they cant see each other, but i put them in the same folder so i dont know what I should do…..any help will be appreciated……
Thanks Guys for the help still trying to figure it out but here is my code:
stojk_2.l
%%
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return MULT;}
"/" {return DIVIDE;}
"(" {return LPAREN;}
")" {return RPAREN;}
[0-9]+ {
sscanf(yytext, "%lf", &yylval);
return UNSIGNEDINTEGER;
}
[ \t] { }
[\n] {return yytext[0];}
. {return yytext[0];}
%%
int yywrap()
{
return 0;
}
stojk_3.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
YYSTYPE yylval;
void yyerror(char *s)
{
print("yyerror: %s\n", s);
}
%}
%token PLUS
%token MINUS
%token MULT
%token DIVIDE
%token LPAREN
%token RPAREN
%token UNSIGNEDINTEGER
%left PLUS MINUS
%left MULT DIVIDE
%%
lines : lines expr '\n' {printf("%g\n", $2);}
| lines '\n'
| /*empty*/
;
expr : expr PLUS expr {$$ = $1 + $3;}
| expr MINUS expr {$$ = $1 - $3;}
| expr MULT expr {$$ = $1 * $3;}
| expr DIVIDE expr {$$ = $1 / $3;}
| LPAREN expr RPAREN {$$ = $2;}
| UNSIGNEDINTEGER
;
%%
#include "lex.yy.c"
int yylex();
int yyparse(void);
int main()
{
return yyparse();
}
You need to generate the header from
bisonthat defines the constants. You need to include that header in the lexical analyzer.You need to include
stojk_3.tab.htherefore in the Flex code.The
print()function might be a typo forprintf(); it certainly isn’t a standard function.The multiple definition of
yylvalis likely because you defined it but don’t need to (Bison does that for you). You may need to declareextern int yylex(void);or equivalent in your code. The conversion complaints mean you passed a literal string to a function that takes achar *(but a string is formally non-modifiable, so the function should be declared and defined to take aconst char *instead).Without seeing more of your code, it is difficult to be more specific.
With the code provided
The problem with
print()is a typo forprintf(), as predicted. There’s a strong argument that should befprintf(stderr, ...)instead; errors should go tostderrand notstdout.You don’t need to define
yylval; comment out that line.I declared
int yylex(void);at the top of the grammar file (in the%{ ... %}section).I made
yyerror()into a static function. I was left with two warnings:Fixed code
I’m not getting the warnings about non-const character pointers; I’m not sure what the problem is there.