LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 7075|回复: 10

Lex与Yacc学习实例(二) —— 解释器的编制

[复制链接]
发表于 2005-5-27 00:28:38 | 显示全部楼层 |阅读模式
2005-5-29-21:00更新:基本把bug去除,加入若干重要数学函数支持;参考help命令
2005-5-28-12:40更新:重构文件组织,lex和yacc模块从main模块独立;去除一些bug(变量未定义时出现段错误),增补自增(++) ,自减(--)运算
2005-5-27-20:19更新:去除一些bug,增补模运算、逻辑与、逻辑或运算

本来计划用yacc辅助做一个C语言子集编译器的,无奈往往有心无力没时间。 :beat
后来随便做个小解释器玩玩,算是对lex及yacc学习的一个了结。 :cool:

[简介]
  nepc-0.2是上一个版本的进化:
1、加入了print、if语句、 if-else和 while语句的支持。从而你可以用nepc进行简单编程,比如输出非波纳妾数列啊、求质数数列啊等等(附件截图)。
2、数据类型增加为 整数、浮点数、字符串。并提供类型转换。
3、变量可以自由定义了,而且必须先定义再使用。
  nepc相当于一个最mini的解释器。(什么是解释器? :ask 早期的gw-basic或者小霸王上的fbasic知道吗?)。解释器和编译器还是有差的,前者效率往往较低。
  nepc-0.2采用yacc建立语法树,然后按深度优先遍历之,边访问边解释代码;当然,遍历时可以生成中间代码。
  nepc-0.2仍旧只能算作教学程序。   

[文法]

  1. program:
  2.   parsed EXIT              
  3.   ;
  4. parsed:
  5.     parsed stmt            
  6.   | parsed error separator
  7.   |
  8.   ;

  9. stmt:
  10.     separator            
  11.   | expr separator        
  12.   | PRINT expr separator  
  13.   | VARIABLE '=' expr separator
  14.   | WHILE '(' expr ')' stmt   
  15.   | IF '(' expr ')' stmt %prec IFX
  16.   | IF '(' expr ')' stmt ELSE stmt
  17.   | '{' stmt_list '}'      
  18.   | CMD                  
  19.   ;

  20. CMD:
  21.     ERASE      
  22.    | HELP      
  23.    | LIST      
  24.    | CLEAR     
  25.    | HEX     
  26.    | DEC      
  27.    | OCT     
  28.    ;
  29. stmt_list:
  30.     stmt                 
  31.   | stmt_list stmt   
  32.    ;

  33. separator:   ';'
  34.   ;
  35. expr:
  36.     INTEGER            
  37.   | FLOAT                 
  38.   | STRING              
  39.   | VARIABLE           
  40.   | '-' expr %prec UMINUS
  41.   | expr PP
  42.   | expr SS
  43.   | expr '+' expr        
  44.   | expr '-' expr        
  45.   | expr '*' expr      
  46.   | expr '/' expr        
  47.   | expr '<' expr      
  48.   | expr '>' expr
  49.   | expr '%' expr
  50.   | expr AND expr
  51.   | expr OR expr   
  52.   | expr GE expr   
  53.   | expr LE expr   
  54.   | expr NE expr     
  55.   | expr EQ expr      
  56.   | '(' expr ')'
  57.   | functions          // 一些数学函数,此处省略。详见附件源码
  58.   ;
复制代码


代码量1000多行,只贴出lex和yacc文件内容,其他所有文件都放在附件压缩包里头供下载。另外一个很有用的是how to use lex&yacc .pdf ,也放在附件。
>>nepc.l<<
  1. %{

  2. #include "y.tab.h"
  3. #include "nepc.h"

  4. %}

  5. digit       [0-9]
  6. xdigit      [0-9a-fA-F]
  7. odigit      [0-7]

  8. dec_int     0|[1-9]{digit}*
  9. dec_flt   (0\.{digit}*)|([1-9]{digit}*\.{digit}*)
  10. oct_int     0{odigit}+
  11. hex_int     0(x|X){xdigit}+

  12. letter      [a-zA-Z]
  13. variable    {letter}({letter}|{digit})*

  14. %x          ASTRING
  15. %%
  16.                 char *s, *v;
  17.                 char tmpstr[MAX_STRLEN];
  18. "              { BEGIN ASTRING; s = tmpstr; }
  19. <ASTRING>\\n    { *s++ = '\n'; /* CARE BUFFER OVERFLOW HERE!!! */ }
  20. <ASTRING>\\t    { *s++ = '\t'; /* CARE BUFFER OVERFLOW HERE!!! */ }
  21. <ASTRING>\\"   { *s++ = '"'; /* CARE BUFFER OVERFLOW HERE!!! */ }
  22. <ASTRING>"     {
  23.                     *s = '\0';
  24.                     BEGIN 0;
  25.                     yylval.pStr = tmpstr;
  26.                     return STRING;
  27.                 }
  28. <ASTRING>\n     { printf("invalid string"); exit(1); }
  29. <ASTRING>.      { *s++ = *yytext; }

  30. "while"     return WHILE;
  31. "if"        return IF;
  32. "else"      return ELSE;
  33. "print"     return PRINT;

  34. "exit"      return EXIT;
  35. "quit"      return EXIT;
  36. "erase"     return ERASE;
  37. "clear"     return CLEAR;
  38. "cls"       return CLEAR;
  39. "ls"        return LIST;
  40. "list"      return LIST;
  41. "help"      return HELP;
  42. "dec"       return DEC;
  43. "hex"       return HEX;
  44. "oct"       return OCT;

  45. "acos"      return ACOS;
  46. "asin"      return ASIN;
  47. "atan"      return ATAN;
  48. "ceil"      return CEIL;
  49. "cos"       return COS;
  50. "cosh"      return COSH;
  51. "exp"       return EXP;
  52. "fabs"      return FABS;
  53. "floor"     return FLOOR;
  54. "log"       return LOG;
  55. "log10"     return LOG10;
  56. "sin"       return SIN;
  57. "sinh"      return SINH;
  58. "sqrt"      return SQRT;
  59. "tan"       return TAN;
  60. "tanh"      return TANH;

  61. {variable}  {
  62.                 v=malloc(yyleng+1);
  63.                 strncpy(v, yytext, yyleng);
  64.                 v[yyleng]='\0';
  65.                 yylval.pStr = v;
  66.                 return VARIABLE;
  67.             }

  68. {dec_int}   {
  69.                 yylval.iValue = STR_TO_INT(yytext);
  70.                 return INTEGER;
  71.             }

  72. {dec_flt}   {
  73.                 yylval.fValue = STR_TO_FLT(yytext);
  74.                 return FLOAT;
  75.             }

  76. {oct_int}   {
  77.                 int i=1;
  78.                 INT val=0;
  79.                 while(i<yyleng)
  80.                 {
  81.                     val=(val<<3)+yytext[i]-'0';
  82.                     i++;
  83.                 }
  84.                 yylval.iValue=val;
  85.                 return INTEGER;
  86.             }

  87. {hex_int}   {
  88.                 int i=2;
  89.                 INT val=0;
  90.                 while(i<yyleng)
  91.                 {
  92.                     if(islower(yytext[i])) val=(val<<4)+yytext[i]-'a'+10;
  93.                     else if(isupper(yytext[i])) val=(val<<4)+yytext[i]-'A'+10;
  94.                     else val=(val<<4)+yytext[i]-'0';
  95.                     i++;
  96.                 }
  97.                 yylval.iValue=val;
  98.                 return INTEGER;
  99.             }

  100. [-()<>=+*/{}.;%] {
  101.                     return *yytext;
  102.                 }
  103. ">="            return GE;
  104. "<="            return LE;
  105. "=="            return EQ;
  106. "!="            return NE;
  107. "&&"            return AND;
  108. "||"            return OR;
  109. "++"            return PP;
  110. "--"            return SS;

  111. [ \t\n]+          ;       /* ignore whitespace */
  112. .               yyerror("Unknown character");


  113. %%
  114. int yywrap(void)
  115. {
  116.     return 1;
  117. }
复制代码


>>nepc.y<<

  1. %{
  2. #include "nepc.h"
  3. #include "nepc.c"  /* routines for parse */
  4. #include "lex.yy.c"
  5. %}

  6. %union{
  7.     INT iValue;
  8.     FLT fValue;
  9.     char * pStr;
  10.     nodeType *nPtr;
  11. };

  12. %token <iValue> INTEGER
  13. %token <fValue> FLOAT
  14. %token <pStr> VARIABLE
  15. %token <pStr> STRING
  16. %token <iValue> ACOS ASIN ATAN CEIL COS COSH EXP FABS FLOOR
  17. %token <iValue> LOG LOG10 POW10 SIN SINH SQRT TAN TANH

  18. %type <nPtr> stmt expr stmt_list function

  19. %token EXIT ERASE CLEAR LIST HELP DEC HEX OCT
  20. %token WHILE IF PRINT

  21. %nonassoc IFX
  22. %nonassoc ELSE

  23. %left OR
  24. %left AND
  25. %left GE LE EQ NE '>' '<'
  26. %left '+' '-'
  27. %left '*' '/' '%'
  28. %left PP SS
  29. %nonassoc UMINUS


  30. %%
  31. program:
  32.     init parsed EXIT        { freeSymTable(); exit(0); }
  33.   ;
  34. init: /* NULL*/             { initSymTable(); WAITINPUT;}
  35.   ;
  36. parsed:
  37.     parsed stmt             { free(interpret($2)); freeNode($2); WAITINPUT; }
  38.   | parsed error separator  { yyerrok; WAITINPUT;}
  39.   | /* NULL */
  40.   ;

  41. stmt:
  42.     separator               { $$ = opr(';', 2, NULL, NULL);  }
  43.   | expr separator          { $$ = opr(PRINT, 1, $1); }
  44.   | PRINT expr separator    { $$ = opr(PRINT, 1, $2); }
  45.   | VARIABLE '=' expr separator     { $$ = (($3 == NULL)?NULL:(symname_install($1),opr('=', 2, var($1), $3)));}
  46.   | WHILE '(' expr ')' stmt         { $$ = opr(WHILE, 2, $3, $5);  }
  47.   | IF '(' expr ')' stmt %prec IFX  { $$ = opr(IF, 2, $3, $5); }
  48.   | IF '(' expr ')' stmt ELSE stmt  { $$ = opr(IF, 3, $3, $5, $7); }
  49.   | '{' stmt_list '}'       { $$ = $2; }
  50.   | CMD                     { $$ = NULL; }
  51.   ;

  52. CMD:
  53.     ERASE       { freeSymTable(); printf(">> All variables erased!\n"); }
  54.   | HELP        {
  55.                        printf(">> COMMANDS:\n");
  56.                        printf("> help: Display this help section.\n");
  57.                        printf("> clear/cls: Clear the screen.\n");
  58.                        printf("> dec: Decimal mode to display numbers or variables.\n");
  59.                        printf("> hex: Hexadecimal mode to display numbers or variables.\n");
  60.                        printf("> oct: Octal mode to display numbers or variables.\n");
  61.                        printf("> list/ls: List the values of variables.\n");
  62.                        printf("> erase: Reset all variables to 0.\n");
  63.                        printf("> exit/quit: Quit this program.\n");
  64.                        printf("> functions supported: acos,asin,atan,ceil,cos,cosh,exp,\n");
  65.                        printf(">   fabs,floor,log,log10,sin,sinh,sqrt,tan,tanh.\n");
  66.                 }
  67.   | LIST        {
  68.                     int i;
  69.                     symType * p;
  70.                     for(i=0;i<symtop;i++)
  71.                     {   
  72.                         p=&symtab[i];
  73.                         if(symtab[i].type==typeInt)
  74.                         {
  75.                             printf("\tINT %s = ", p->name);
  76.                             PRINTINT(p);
  77.                         }
  78.                         else if(symtab[i].type==typeFlt)
  79.                         {
  80.                             printf("\tFLT %s = ", p->name);
  81.                             PRINTFLT(p);
  82.                         }
  83.                         else if(symtab[i].type==typeStr)
  84.                         {
  85.                             printf("\tSTR %s = ", p->name);
  86.                             PRINTSTR(p);
  87.                         }
  88.                         printf("\n");
  89.                     }
  90.                 }
  91.    | CLEAR      { printf("\033[2J\033[1;1H\n"); }
  92.    | HEX        { dflag=HEX_ON; printf(">> HEX display mode on!\n"); }
  93.    | DEC        { dflag=DEC_ON; printf(">> DEC display mode on!\n"); }
  94.    | OCT        { dflag=OCT_ON; printf(">> OCT display mode on!\n"); }
  95.    ;

  96. stmt_list:
  97.     stmt                    { $$ = $1; }
  98.   | stmt_list stmt          { $$ = opr(';', 2, $1, $2); }

  99.   ;

  100. separator:
  101.     ';'
  102.   ;
  103. expr:
  104.     INTEGER                 { $$ = con_int($1); }
  105.   | FLOAT                   { $$ = con_flt($1); }
  106.   | STRING                  { $$ = con_str($1); }
  107.   | VARIABLE                { $$ = var($1); }
  108.   | '-' expr %prec UMINUS   { $$ = opr(UMINUS, 1, $2); }
  109.   | VARIABLE PP             { $$ = ((var($1)==NULL) ? NULL: opr(PP, 1, var($1))); }
  110.   | VARIABLE SS             { $$ = ((var($1)==NULL) ? NULL: opr(SS, 1, var($1))); }
  111.   | expr '+' expr           { $$ = opr('+', 2, $1, $3); }
  112.   | expr '-' expr           { $$ = opr('-', 2, $1, $3); }
  113.   | expr '*' expr           { $$ = opr('*', 2, $1, $3); }
  114.   | expr '/' expr           { $$ = opr('/', 2, $1, $3); }
  115.   | expr '<' expr           { $$ = opr('<', 2, $1, $3); }
  116.   | expr '>' expr           { $$ = opr('>', 2, $1, $3); }
  117.   | expr '%' expr           { $$ = opr('%', 2, $1, $3); }
  118.   | expr AND expr           { $$ = opr(AND, 2, $1, $3); }
  119.   | expr OR expr            { $$ = opr(OR, 2, $1, $3); }
  120.   | expr GE expr            { $$ = opr(GE, 2, $1, $3); }
  121.   | expr LE expr            { $$ = opr(LE, 2, $1, $3); }
  122.   | expr NE expr            { $$ = opr(NE, 2, $1, $3); }
  123.   | expr EQ expr            { $$ = opr(EQ, 2, $1, $3); }
  124.   | '(' expr ')'            { $$ = $2; }
  125.   | function                { $$ = $1; }
  126.   ;

  127. function:
  128.     ACOS '(' expr ')'       { $$ = opr(ACOS,1,$3); }
  129.   | ASIN '(' expr ')'       { $$ = opr(ASIN,1,$3); }
  130.   | ATAN '(' expr ')'       { $$ = opr(ATAN,1,$3);; }
  131.   | CEIL '(' expr ')'       { $$ = opr(CEIL,1,$3); }
  132.   | COS  '(' expr ')'       { $$ = opr(COS,1,$3); }
  133.   | COSH '(' expr ')'       { $$ = opr(COSH,1,$3); }
  134.   | EXP '(' expr ')'        { $$ = opr(EXP,1,$3); }
  135.   | FABS '(' expr ')'       { $$ = opr(FABS,1,$3); }
  136.   | FLOOR '(' expr ')'      { $$ = opr(FLOOR,1,$3); }
  137.   | LOG '(' expr ')'        { $$ = opr(LOG,1,$3); }
  138.   | LOG10 '(' expr ')'      { $$ = opr(LOG10,1,$3); }
  139.   | SIN '(' expr ')'        { $$ = opr(SIN,1,$3); }
  140.   | SINH '(' expr ')'       { $$ = opr(SINH,1,$3); }
  141.   | SQRT '(' expr ')'       { $$ = opr(SQRT,1,$3); }
  142.   | TAN '(' expr ')'        { $$ = opr(TAN,1,$3); }
  143.   | TANH '(' expr ')'       { $$ = opr(TANH,1,$3); }
  144.   ;

  145. %%

  146. void yyerror(char *s)
  147. {
  148.     fprintf(stdout, "%s\n", s);
  149. }

复制代码


>>nepcmain.c<<

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <math.h>

  6. #include "y.tab.c"
  7. extern int yyparse();
  8. extern FILE * yyin;

  9. void motd()
  10. {
  11.     printf("--------------------------------------------------------------\n");
  12.     printf(">> nepc-0.2 - A mini limited precision interpreting calculator.\n");
  13.     printf(">> Copyright (C) 2005 neplusultra@linuxsir.cn\n");
  14.     printf(">> nepc is open software; you can redistribute it and/or modify\n\
  15.    it under the terms of the version 2.1 of the GNU Lesser \n\
  16.    General Public License as published by the Free Software\n\
  17.    Foundation.\n>> Type 'exit' to quit, type 'help' to get help.\n");
  18.     printf("--------------------------------------------------------------\n");
  19. }

  20. int main(void)
  21. {
  22.     motd();
  23.     yyin=stdin; /* set parse stream to standard input */
  24.     yyparse(); /* begin parse */
  25.     fclose(yyin);
  26.     return 0;
  27. }
复制代码

[编译]

  1. $ tar jxvf nepc-0.2-r1.tar.bz2
  2. $ lex nepc.l
  3. $ yacc -d nepc.y
  4. $ gcc -o your-bin-name nepcmain.c -lm
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2005-5-27 08:12:02 | 显示全部楼层
微软的qbasic也是经典的解释器
回复 支持 反对

使用道具 举报

发表于 2005-5-27 10:29:29 | 显示全部楼层
Excelent work But why not a makefile?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-27 20:33:57 | 显示全部楼层
3x
不会弄makefile,也暂时没空去学了;
如何编译在文末有提到,很简单。

感兴趣的可以下载最新的版本玩玩
有朋友问到使用问题,其实看看文法就知道了;提示以下几点:
1、记得语句后面要用一个";"结尾 ,几个命令字可以不要,这是文法设计的原因。
2、print 目前仅跟一个打印对象,它可以用()括起来,也可以不用;如果是字符串,支持转义字符'\t'  '\n' '\“‘。
3、字符串也可以相加;对于不支持的运算操作,会返回0值;
回复 支持 反对

使用道具 举报

发表于 2005-5-28 10:45:21 | 显示全部楼层
ok,非常不错!
回复 支持 反对

使用道具 举报

发表于 2005-5-28 16:00:56 | 显示全部楼层
强人,值的学习
回复 支持 反对

使用道具 举报

发表于 2005-6-16 02:14:23 | 显示全部楼层
为什么我完全按照楼主的做法去做,却出现以下结果呢:
$gcc -o myyacc nepcmain.c -lm
In file include from nepc.y:3
                     from nepcmain.c:3:
nepc.c:In function 'var':
nepc.c:107:parse error before '*'
nepc.c:110:'nodeSize' undeclared (first used in this function)
nepc.c:...................
nepc.c:...................

明明已经定义了,为什么还说我没有定义呢
急!!期待热情高手帮忙!!
回复 支持 反对

使用道具 举报

发表于 2006-3-17 21:56:14 | 显示全部楼层
Post by 千年沉冰
为什么我完全按照楼主的做法去做,却出现以下结果呢:
$gcc -o myyacc nepcmain.c -lm
In file include from nepc.y:3
                     from nepcmain.c:3:
nepc.c:In function 'var':
nepc.c:107:parse error before '*'
nepc.c:110:'nodeSize' undeclared (first used in this function)
nepc.c:...................
nepc.c:...................

明明已经定义了,为什么还说我没有定义呢
急!!期待热情高手帮忙!!


我也一样。。打印如下:
[root@YanXT:nepc-0.2](0)# lex nepc.l
[root@YanXT:nepc-0.2](0)# yacc -d nepc.y
[root@YanXT:nepc-0.2](0)# gcc -o nepc nepcmain.c
In file included from nepc.y:3,
                 from nepcmain.c:3:
nepc.c: In function `interpret':
nepc.c:222: `WHILE' undeclared (first use in this function)
nepc.c:222: (Each undeclared identifier is reported only once
nepc.c:222: for each function it appears in.)
nepc.c:238: `IF' undeclared (first use in this function)
nepc.c:257: `PRINT' undeclared (first use in this function)
nepc.c:288: `UMINUS' undeclared (first use in this function)
nepc.c:295: `PP' undeclared (first use in this function)
nepc.c:310: `SS' undeclared (first use in this function)
nepc.c:614: `AND' undeclared (first use in this function)
nepc.c:647: `OR' undeclared (first use in this function)
nepc.c:680: `GE' undeclared (first use in this function)
nepc.c:717: `LE' undeclared (first use in this function)
nepc.c:754: `NE' undeclared (first use in this function)
nepc.c:791: `EQ' undeclared (first use in this function)
nepc.c:828: `ACOS' undeclared (first use in this function)
nepc.c:837: `ASIN' undeclared (first use in this function)
nepc.c:845: `ATAN' undeclared (first use in this function)
nepc.c:853: `CEIL' undeclared (first use in this function)
nepc.c:861: `COS' undeclared (first use in this function)
nepc.c:876: `COSH' undeclared (first use in this function)
nepc.c:891: `EXP' undeclared (first use in this function)
nepc.c:906: `FABS' undeclared (first use in this function)
nepc.c:921: `FLOOR' undeclared (first use in this function)
nepc.c:936: `LOG' undeclared (first use in this function)
nepc.c:951: `LOG10' undeclared (first use in this function)
nepc.c:966: `SIN' undeclared (first use in this function)
nepc.c:981: `SINH' undeclared (first use in this function)
nepc.c:996: `SQRT' undeclared (first use in this function)
nepc.c:1011: `TAN' undeclared (first use in this function)
nepc.c:1026: `TANH' undeclared (first use in this function)
In file included from nepcmain.c:3:
nepc.y: At top level:
nepc.y:12: conflicting types for `YYSTYPE'
y.tab.h:53: previous declaration of `YYSTYPE'
y.tab.c:470: conflicting types for `yylval'
y.tab.h:54: previous declaration of `yylval'
回复 支持 反对

使用道具 举报

发表于 2006-3-17 21:56:17 | 显示全部楼层
Post by 千年沉冰
为什么我完全按照楼主的做法去做,却出现以下结果呢:
$gcc -o myyacc nepcmain.c -lm
In file include from nepc.y:3
                     from nepcmain.c:3:
nepc.c:In function 'var':
nepc.c:107:parse error before '*'
nepc.c:110:'nodeSize' undeclared (first used in this function)
nepc.c:...................
nepc.c:...................

明明已经定义了,为什么还说我没有定义呢
急!!期待热情高手帮忙!!


我也一样。。打印如下:

  1. [root@YanXT:nepc-0.2](0)# lex nepc.l
  2. [root@YanXT:nepc-0.2](0)# yacc -d nepc.y
  3. [root@YanXT:nepc-0.2](0)# gcc -o nepc nepcmain.c
  4. In file included from nepc.y:3,
  5.                  from nepcmain.c:3:
  6. nepc.c: In function `interpret':
  7. nepc.c:222: `WHILE' undeclared (first use in this function)
  8. nepc.c:222: (Each undeclared identifier is reported only once
  9. nepc.c:222: for each function it appears in.)
  10. nepc.c:238: `IF' undeclared (first use in this function)
  11. nepc.c:257: `PRINT' undeclared (first use in this function)
  12. nepc.c:288: `UMINUS' undeclared (first use in this function)
  13. nepc.c:295: `PP' undeclared (first use in this function)
  14. nepc.c:310: `SS' undeclared (first use in this function)
  15. nepc.c:614: `AND' undeclared (first use in this function)
  16. nepc.c:647: `OR' undeclared (first use in this function)
  17. nepc.c:680: `GE' undeclared (first use in this function)
  18. nepc.c:717: `LE' undeclared (first use in this function)
  19. nepc.c:754: `NE' undeclared (first use in this function)
  20. nepc.c:791: `EQ' undeclared (first use in this function)
  21. nepc.c:828: `ACOS' undeclared (first use in this function)
  22. nepc.c:837: `ASIN' undeclared (first use in this function)
  23. nepc.c:845: `ATAN' undeclared (first use in this function)
  24. nepc.c:853: `CEIL' undeclared (first use in this function)
  25. nepc.c:861: `COS' undeclared (first use in this function)
  26. nepc.c:876: `COSH' undeclared (first use in this function)
  27. nepc.c:891: `EXP' undeclared (first use in this function)
  28. nepc.c:906: `FABS' undeclared (first use in this function)
  29. nepc.c:921: `FLOOR' undeclared (first use in this function)
  30. nepc.c:936: `LOG' undeclared (first use in this function)
  31. nepc.c:951: `LOG10' undeclared (first use in this function)
  32. nepc.c:966: `SIN' undeclared (first use in this function)
  33. nepc.c:981: `SINH' undeclared (first use in this function)
  34. nepc.c:996: `SQRT' undeclared (first use in this function)
  35. nepc.c:1011: `TAN' undeclared (first use in this function)
  36. nepc.c:1026: `TANH' undeclared (first use in this function)
  37. In file included from nepcmain.c:3:
  38. nepc.y: At top level:
  39. nepc.y:12: conflicting types for `YYSTYPE'
  40. y.tab.h:53: previous declaration of `YYSTYPE'
  41. y.tab.c:470: conflicting types for `yylval'
  42. y.tab.h:54: previous declaration of `yylval'
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-17 22:19:49 | 显示全部楼层
看来大家对lex, yacc颇有兴趣啊。
推荐一个http://cs.gmu.edu/~white/CS540/
——这个老师教compiler大概有8,9年的历史了 。

大家可以看看slides, assigment,example.

对optimization有兴趣的朋友可以看看:
http://cs.gmu.edu/~white/CS640/
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表