lex&yacc的一个问题?

242 views
Skip to first unread message

李则良

unread,
May 7, 2009, 10:07:49 AM5/7/09
to Linux小组
lzel@lzel-laptop:8$ cat simple.l
%{
#include "y.tab.h"
#include <math.h>
//extern int yylval;
extern double vbltable[26];
%}
%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
yylval.dval = atof(yytext);
return NUMBER;
}
[ \t]+ ;
[a-z] {
yylval.vblno = yytext[0] - 'a';
return NAME;
}
"$" {
return 0;
}
\n |
. {
return yytext[0];
}
%%
int yywrap()
{
return 1;
}
lzel@lzel-laptop:8$ cat simple.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylex();
int yyerror(char *s);
double vbltable[26];
%}
%union {
double dval;
int vblno;
}
%token <vblno> NAME
%token <dval> NUMBER
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS

%type <dval> expression
%%
statement_list: statement '\n'
| statement_list statement '\n'
;
statement: NAME '=' expression { vbltable[$1] = $3; }
| expression { printf("= %g\n", $1); }
;
expression: expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression {
if($3 == 0.0)
yyerror("divide by zero");
else
$$ = $1 / $3;
}
| '-' expression %prec UMINUS { $$ = - $2; }
| '(' expression ')' { $$ = $2; }
| NUMBER
| NAME { $$ = vbltable[$1]; }
;
%%
extern FILE *yyin;
int main(int argc, char *argv[])
{
while(!feof(yyin))
yyparse();
return 0;
}
int yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
return 1;
}
lzel@lzel-laptop:8$ cat Makefile
#
# Copyleft (C) lizeliang <lizelia...@gmail.com>
# Date:
# 2009.5.7
#
TARGET= mycc

# DEBUG= y

LEX_FILE= simple.l
YACC_FILE= simple.y

CC= gcc
CFLAGS= -Wall
LEX= flex
YACC= yacc
YACCFLAGS= -d
INCLUDE= .
LD= -ll

ifeq ($(DEBUG), y)
CFLAGS += -g
endif

$(TARGET):lex.yy.o y.tab.o
$(CC) $(CFLAGS) $(LD) -o $@ $<
lex.yy.o: lex.yy.c y.tab.h
$(CC) -I$(INCLUDE) $(CFLAGS) -c $<
y.tab.o: y.tab.c
$(CC) -I$(INCLUDE) $(CFLAGS) -c $<
lex.yy.c: $(LEX_FILE)
$(LEX) $<
y.tab.c: $(YACC_FILE)
$(YACC) $(YACCFLAGS) $<
y.tab.h: $(YACC_FILE)
$(YACC) $(YACCFLAGS) $<

clean:
@rm *.o lex.yy.c y.tab.h y.tab.c
distclean:
@rm $(TARGET)

lzel@lzel-laptop:8$

但是make时出现如下错误,不知该如何解决,看大家有没有碰到过这个问题:

lzel@lzel-laptop:8$ make
yacc -d simple.y
gcc -I. -Wall -c lex.yy.c
lex.yy.c:1104: warning: ‘yyunput’ defined but not used
lex.yy.c:1147: warning: ‘input’ defined but not used
gcc -I. -Wall -c y.tab.c
gcc -Wall -ll -o mycc lex.yy.o
lex.yy.o: In function `yylex':
lex.yy.c:(.text+0x230): undefined reference to `yylval'
lex.yy.c:(.text+0x24f): undefined reference to `yylval'
collect2: ld returned 1 exit status
make: *** [mycc] Error 1
lzel@lzel-laptop:8$

Américo Wang

unread,
May 7, 2009, 10:15:16 AM5/7/09
to 李则良, Linux小组
On Thu, May 07, 2009 at 10:07:49PM +0800, 李则良 wrote:
>
>lzel@lzel-laptop:8$ cat simple.l
>%{
>#include "y.tab.h"
>#include <math.h>
>//extern int yylval;


因为这里被注释掉了吧?


>
>但是make时出现如下错误,不知该如何解决,看大家有没有碰到过这个问题:
>
>lzel@lzel-laptop:8$ make
>yacc -d simple.y
>gcc -I. -Wall -c lex.yy.c
>lex.yy.c:1104: warning: ‘yyunput’ defined but not used
>lex.yy.c:1147: warning: ‘input’ defined but not used
>gcc -I. -Wall -c y.tab.c
>gcc -Wall -ll -o mycc lex.yy.o
>lex.yy.o: In function `yylex':
>lex.yy.c:(.text+0x230): undefined reference to `yylval'
>lex.yy.c:(.text+0x24f): undefined reference to `yylval'
>collect2: ld returned 1 exit status
>make: *** [mycc] Error 1

如果去掉注释还不行的话贴一下lex.yy.c。

--
Live like a child, think like the god.

李则良

unread,
May 7, 2009, 10:18:32 AM5/7/09
to Américo Wang, Linux小组
2009/5/7 Américo Wang <xiyou.w...@gmail.com>:

> On Thu, May 07, 2009 at 10:07:49PM +0800, 李则良 wrote:
>>
>>lzel@lzel-laptop:8$ cat simple.l
>>%{
>>#include "y.tab.h"
>>#include <math.h>
>>//extern int yylval;
>
>
> 因为这里被注释掉了吧?
这里去掉注释:
lzel@lzel-laptop:8$ make
flex simple.l

yacc -d simple.y
gcc -I. -Wall -c lex.yy.c
simple.l:4: error: conflicting types for ‘yylval’
y.tab.h:70: error: previous declaration of ‘yylval’ was here
simple.l: In function ‘yylex’:
simple.l:9: error: request for member ‘dval’ in something not a
structure or union
simple.l:14: error: request for member ‘vblno’ in something not a
structure or union
make: *** [lex.yy.o] Error 1

>
>
>>
>>但是make时出现如下错误,不知该如何解决,看大家有没有碰到过这个问题:
>>
>>lzel@lzel-laptop:8$ make
>>yacc -d simple.y
>>gcc -I. -Wall -c lex.yy.c
>>lex.yy.c:1104: warning: ‘yyunput’ defined but not used
>>lex.yy.c:1147: warning: ‘input’ defined but not used
>>gcc -I. -Wall -c y.tab.c
>>gcc -Wall -ll -o mycc lex.yy.o
>>lex.yy.o: In function `yylex':
>>lex.yy.c:(.text+0x230): undefined reference to `yylval'
>>lex.yy.c:(.text+0x24f): undefined reference to `yylval'
>>collect2: ld returned 1 exit status
>>make: *** [mycc] Error 1
>
> 如果去掉注释还不行的话贴一下lex.yy.c。

在simle.y 中加上
#include "lex.yy.c"却是:
lzel@lzel-laptop:8$ make
flex simple.l


yacc -d simple.y
gcc -I. -Wall -c lex.yy.c
lex.yy.c:1104: warning: ‘yyunput’ defined but not used
lex.yy.c:1147: warning: ‘input’ defined but not used
gcc -I. -Wall -c y.tab.c
lex.yy.c:1104: warning: ‘yyunput’ defined but not used
lex.yy.c:1147: warning: ‘input’ defined but not used
gcc -Wall -ll -o mycc lex.yy.o
lex.yy.o: In function `yylex':
lex.yy.c:(.text+0x230): undefined reference to `yylval'
lex.yy.c:(.text+0x24f): undefined reference to `yylval'
collect2: ld returned 1 exit status
make: *** [mycc] Error 1

lzel@lzel-laptop:8$

我很郁闷!


>
> --
> Live like a child, think like the god.
>
>

--
不去想是否能成功,既然选择了远方,便只顾风雨兼程;不去想身后会不会袭来寒流,既然目标是地平线,留给世界的只能是背影。

李则良

unread,
May 7, 2009, 10:34:02 AM5/7/09
to Américo Wang, Linux小组
这个问题都快困惑我一天了,在网上找,也只有相同的问题,却没有相关的解决方法,比如这个简单一点的例子:
lzel@lzel-laptop:7$ cat simple.l
%{
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return NUMBER;
}
[ \t]+ ;
\n return 0;

. return yytext[0];
%%
int yywrap()
{
return 1;
}
lzel@lzel-laptop:7$ cat simple.y
%{
#include <stdio.h>
%}
%token NAME NUMBER
%%
statement: NAME '=' expression
| expression { printf("= %d\n",$1); }
;
expression: expression '+' NUMBER { $$ = $1 + $3; }
| expression '-' NUMBER { $$ = $1 - $3; }
| NUMBER { $$ = $1; }
;
%%
extern FILE *yyin;
int main(int argc,char **argv)
{
while (!feof(yyin)) {

yyparse();
}
return 0;
}
int yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
return 1;
}
lzel@lzel-laptop:7$
lzel@lzel-laptop:7$ make

gcc -Wall -ll -o mycc lex.yy.o
lex.yy.o: In function `yylex':
lex.yy.c:(.text+0x22f): undefined reference to `yylval'

collect2: ld returned 1 exit status
make: *** [mycc] Error 1
lzel@lzel-laptop:7$ make clean
lzel@lzel-laptop:7$ make

flex simple.l
yacc -d simple.y
gcc -I. -Wall -c lex.yy.c
lex.yy.c:1082: warning: ‘yyunput’ defined but not used
lex.yy.c:1125: warning: ‘input’ defined but not used

gcc -I. -Wall -c y.tab.c
y.tab.c: In function ‘yyparse’:
y.tab.c:1220: warning: implicit declaration of function ‘yylex’
y.tab.c:1358: warning: implicit declaration of function ‘yyerror’

gcc -Wall -ll -o mycc lex.yy.o
lex.yy.o: In function `yylex':
lex.yy.c:(.text+0x22f): undefined reference to `yylval'

collect2: ld returned 1 exit status
make: *** [mycc] Error 1
lzel@lzel-laptop:7$

Américo Wang

unread,
May 7, 2009, 11:04:13 AM5/7/09
to 李则良, Américo Wang, Linux小组
On Thu, May 07, 2009 at 10:18:32PM +0800, 李则良 wrote:
>2009/5/7 Américo Wang <xiyou.w...@gmail.com>:
>> On Thu, May 07, 2009 at 10:07:49PM +0800, 李则良 wrote:
>>>
>>>lzel@lzel-laptop:8$ cat simple.l
>>>%{
>>>#include "y.tab.h"
>>>#include <math.h>
>>>//extern int yylval;
>>
>>
>> 因为这里被注释掉了吧?
>这里去掉注释:
>lzel@lzel-laptop:8$ make
>flex simple.l
>yacc -d simple.y
>gcc -I. -Wall -c lex.yy.c
>simple.l:4: error: conflicting types for ‘yylval’
>y.tab.h:70: error: previous declaration of ‘yylval’ was here

恩。。。。这里的yylval并不是int类型。


>gcc -Wall -ll -o mycc lex.yy.o
>lex.yy.o: In function `yylex':
>lex.yy.c:(.text+0x230): undefined reference to `yylval'
>lex.yy.c:(.text+0x24f): undefined reference to `yylval'
>collect2: ld returned 1 exit status
>make: *** [mycc] Error 1


试试加一句:

extern YYSTYPE yylval;

李则良

unread,
May 7, 2009, 11:10:28 AM5/7/09
to Américo Wang, Linux小组
extern YYSTYPE yylval;

extern double vbltable[26];
%}
...

lzel@lzel-laptop:8$ make
flex simple.l
yacc -d simple.y
gcc -I. -Wall -c lex.yy.c
lex.yy.c:1104: warning: ‘yyunput’ defined but not used
lex.yy.c:1147: warning: ‘input’ defined but not used
gcc -I. -Wall -c y.tab.c
lex.yy.c:1104: warning: ‘yyunput’ defined but not used
lex.yy.c:1147: warning: ‘input’ defined but not used
gcc -Wall -ll -o mycc lex.yy.o
lex.yy.o: In function `yylex':
lex.yy.c:(.text+0x230): undefined reference to `yylval'
lex.yy.c:(.text+0x24f): undefined reference to `yylval'
collect2: ld returned 1 exit status
make: *** [mycc] Error 1
lzel@lzel-laptop:8$
还是不行~~

>
> --
> Live like a child, think like the god.
>
>

--

Américo Wang

unread,
May 7, 2009, 11:54:17 AM5/7/09
to 李则良, Américo Wang, Linux小组

lex.yy.c要和y.tab.c一起编译。。。

gcc -ll -o mycc lex.yy.o y.tab.o

李则良

unread,
May 8, 2009, 2:36:34 AM5/8/09
to Américo Wang, Linux小组
{snip}

>
> lex.yy.c要和y.tab.c一起编译。。。
>
> gcc -ll -o mycc lex.yy.o y.tab.o
>
{snip}

prefect , thanks a lot.

把Makefile这样一改就好了:
$(TARGET):lex.yy.o y.tab.o
$(CC) $(CFLAGS) $(LD) -o $@ $?

Reply all
Reply to author
Forward
0 new messages