pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feed[Beginner question]How to solve multiple definition of `yylval'?
2+ messages / 2 participants
[nested] [flat]
* [Beginner question]How to solve multiple definition of `yylval'?
@ 2023-05-11 11:52 Wen Yi <chuxuec@outlook.com>
0 siblings, 1 reply; 2+ messages in thread
From: Wen Yi @ 2023-05-11 11:52 UTC (permalink / raw)
To: PostgreSQL General <pgsql-general@lists.postgresql.org>
Hi team,
I am studying on the yacc & lex to help me understand the parser of the postgres, but now I'm facing some trouble.
-------------------
/*
1-3.y
*/
%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
typedef char* string;
#define YYSTPYE string
%}
%token NUMBER
%token ADD SUB MUL DIV ABS
%token NEWLINE
%%
calclist: /* Empty rule */
| calclist expression NEWLINE { printf (" = %lf", $2); }
;
expression: factor { $$ = $1; }
| expression ADD factor { $$ = $1 + $3; }
| expression SUB factor { $$ = $1 - $3; }
;
factor: term { $$ = $1; }
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: NUMBER { $$ = $1; }
| ABS term { $$ = $2 > 0 ? $2 : - $2; }
%%
int main(int argc, char *argv[])
{
yyparse();
}
int yyerror(char *s)
{
puts(s);
}
---------
/*
1.3.l
Calc Program
Jing Zhang
*/
%option noyywrap
%option noinput
%{
#include <stdlib.h>
enum yyTokenType
{
NUMBER = 258,
ADD,
SUB,
MUL,
DIV,
ABS,
NEWLINE
};
double yylval;
%}
%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
[0-9]+ { yylval = atof (yytext); return NUMBER;}
\n { return NEWLINE; }
[ \t] { ; }
. { ; }
%%
-------
When I use the yacc & lex to compile,
yacc -d 1-3.y
lex 1-3.l
gcc 1-3.tab.c lex.yy.c
/usr/bin/ld: /tmp/ccYqqE5N.o:(.bss+0x28): multiple definition of `yylval'; /tmp/ccdJ12gy.o:(.bss+0x4): first defined here
Can someone provide me a solution?
Thanks in advance!
Yours,
Jing Zhang.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: [Beginner question]How to solve multiple definition of `yylval'?
@ 2023-05-11 13:40 Tom Lane <tgl@sss.pgh.pa.us>
parent: Wen Yi <chuxuec@outlook.com>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2023-05-11 13:40 UTC (permalink / raw)
To: Wen Yi <chuxuec@outlook.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Wen Yi <chuxuec@outlook.com> writes:
> When I use the yacc & lex to compile,
> yacc -d 1-3.y
> lex 1-3.l
> gcc 1-3.tab.c lex.yy.c
> /usr/bin/ld: /tmp/ccYqqE5N.o:(.bss+0x28): multiple definition of `yylval'; /tmp/ccdJ12gy.o:(.bss+0x4): first defined here
Bison provides the declaration of yylval; don't add one yourself.
BTW, this isn't right:
#define YYSTPYE string
first because you misspelled YYSTYPE, and second because you
evidently want yylval to be double not a string.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2023-05-11 13:40 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-05-11 11:52 [Beginner question]How to solve multiple definition of `yylval'? Wen Yi <chuxuec@outlook.com>
2023-05-11 13:40 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox