Reputation: 1619
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();
}
Upvotes: 1
Views: 8345
Reputation: 753455
You need to generate the header from bison
that defines the constants. You need to include that header in the lexical analyzer.
bison -d stojk_3.y # generates stojk_3.tab.c and stojk_3.tab.h
You need to include stojk_3.tab.h
therefore in the Flex code.
The print()
function might be a typo for printf()
; it certainly isn't a standard function.
The multiple definition of yylval
is likely because you defined it but don't need to (Bison does that for you). You may need to declare extern int yylex(void);
or equivalent in your code. The conversion complaints mean you passed a literal string to a function that takes a char *
(but a string is formally non-modifiable, so the function should be declared and defined to take a const char *
instead).
Without seeing more of your code, it is difficult to be more specific.
The problem with print()
is a typo for printf()
, as predicted. There's a strong argument that should be fprintf(stderr, ...)
instead; errors should go to stderr
and not stdout
.
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:
gcc -O3 -g -std=c99 -Wall -Wextra -Wstrict-prototypes stojk_3.tab.c -o stojk_3.tab
In file included from stojk_3.y:50:0:
lex.yy.c:1112:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
lex.yy.c:1153:16: warning: ‘input’ defined but not used [-Wunused-function]
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
int yylex(void);
static
void yyerror(char *s)
{
printf("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(void);
int yyparse(void);
int main(void)
{
return yyparse();
}
I'm not getting the warnings about non-const character pointers; I'm not sure what the problem is there.
Upvotes: 0
Reputation: 126120
The first set of errors looks like you failed to #include
your .tab.h file in the .l file. The second set of errors look like there's something wrong with your .y file, but its tough to tell exactly what without seeing the .y file.
Upvotes: 2