%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$
因为这里被注释掉了吧?
>
>但是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.
>
>
>>
>>但是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.
>
>
--
不去想是否能成功,既然选择了远方,便只顾风雨兼程;不去想身后会不会袭来寒流,既然目标是地平线,留给世界的只能是背影。
恩。。。。这里的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;
--
lex.yy.c要和y.tab.c一起编译。。。
gcc -ll -o mycc lex.yy.o y.tab.o
prefect , thanks a lot.
把Makefile这样一改就好了:
$(TARGET):lex.yy.o y.tab.o
$(CC) $(CFLAGS) $(LD) -o $@ $?