免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2751 | 回复: 1
打印 上一主题 下一主题

用flex和bison做的一个程序用gcc编译的时候出来莫名其妙的错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-10 01:17 |只看该作者 |倒序浏览
我用flex和bison写一个很简单的程序,可是用gcc编译的时候怎么都不对,请大虾帮我看看。我已经快不行了,感觉这些编译的错误简直是莫名其妙

compile_6_30.l

  1. %{
  2. #include <stdio.h>;
  3. #include "common.h"
  4. #include "compile_6_30.tab.h"
  5. void yyerror(char *str);
  6. %}

  7. %%

  8. "int" return INT;
  9. "bool" return BOOL;
  10. "array" return ARRAY;
  11. "of" return OF;
  12. "or" return OR;
  13. "true" return TRUE;
  14. "false" return FALSE;
  15. "if" return IF;
  16. "then" return THEN;

  17. [a-zA-Z]*    {
  18. /*int len=;*/
  19.         yylval.id=(char *)malloc(strlen(yytext)+1);
  20.         strncpy(yylval.id , yytext , strlen(yytext)+1);
  21.         printf("id:%s\n" , yylval.id);
  22.         return ID;
  23. }
复制代码


compile_6_30.y

  1. %{
  2.   #include <stdio.h>;
  3.   #include <stdlib.h>;
  4.   #include "common.h"

  5.   void yyerror(char *s);
  6.   /*the function will try to search whether the id has been inserted
  7.     if id has been inserted, the program exit with an error
  8.    */
  9.   void insert(char *id , nodetype *type);
  10.   nodetype*new_type(nodetype *);
  11.   void type_error(const char *);
  12.   /*the nodetype is just a type expression
  13.     so return nodetype pointer here
  14.    */
  15.   nodetype *lookup(const char *);
  16.   /*
  17.     if the length of one of arrays is zero, just compare their type
  18.    */
  19.   int type_equal(nodetype * , nodetype *);

  20.   nodetype node1,node2,node3;
  21. #define INTEGER (&node1)
  22. #define BOOLEAN (&node2)
  23. #define ARRAYTYPE (&node3)

  24.   table global_table;
  25. %}

  26. %union{
  27.   char *id;
  28.   int num;
  29.   nodetype *ptr;
  30. }

  31. %token<id>; ID
  32. %token<num>; NUM
  33. %token INT BOOL IF THEN ASSIGN OF ARRAY OR TRUE FALSE
  34. %type <ptr>; program var_decls stmts var_decl type_exp stmt exp

  35. %%

  36. program:
  37. var_decls ';' stmts {}
  38. ;

  39. var_decls:
  40. var_decls ';' var_decl{}
  41. |var_decl {}

  42. var_decl:
  43. ID ':' type_exp {
  44.   insert($1 , $3);
  45.   free($3);
  46. }

  47. type_exp:
  48. INT {$$=new_type(INTEGER);}
  49. |BOOL {$$=new_type(BOOLEAN);}
  50. |ARRAY '[' NUM ']' OF type_exp {
  51.   ARRAYTYPE->;len=$3;
  52.   ARRAYTYPE->;child_type=$6;
  53.   $$=new_type(ARRAYTYPE);
  54.   free($6);
  55. }
  56. ;

  57. stmts:
  58. stmts ';' stmt {}
  59. |stmt {}
  60. ;

  61. stmt:
  62. IF exp THEN stmt {
  63.   if(!type_equal($2 , BOOLEAN))
  64.     type_error("exp in if-statement is not boolean");
  65.   free($2);
  66. }
  67. |ID ASSIGN exp{
  68.   if(!type_equal(lookup($1) , $3))
  69.     type_error("the type is not coincident in the assign-statement");
  70.   free($3);
  71. }
  72. ;

  73. exp:
  74. exp '+' exp {
  75.   if(!type_equal($1 , INTEGER) || !type_equal($3 , INTEGER))
  76.     type_error("the two exp are not coincident in add-statement");
  77.   else
  78.     $$=new_type(INTEGER);
  79.   free($1);
  80.   free($3);
  81. }
  82. |exp OR exp {
  83.   if(!type_equal($1 , BOOLEAN) || !type_equal($3 , BOOLEAN))
  84.     type_error("the two exp are not coincident in or-statement");
  85.   else
  86.     $$=new_type(BOOLEAN);
  87.   free($1);
  88.   free($3);
  89. }
  90. |exp '[' exp ']' {
  91.   if(!type_equal($1 , ARRAYTYPE) || !type_equal($3 , INTEGER))
  92.     type_error("the two exp are not coincident in array-statement");
  93.   else
  94.     $$=new_type($1->;child_type);
  95.   free($1);
  96.   free($3);
  97. }
  98. |NUM {
  99.   $$=new_type(INTEGER);
  100. }
  101. |TRUE {
  102.   $$=new_type(BOOLEAN);
  103. }
  104. |FALSE {
  105.   $$=new_type(BOOLEAN);
  106. }
  107. |ID {
  108.   $$=new_type(lookup($1));
  109. }
  110. ;

  111. %%
  112. void yyerror(char *s)
  113. {
  114.   printf("%s\n" , s);
  115.   exit(1);
  116. }
  117. /*the function will try to search whether the id has been inserted
  118.   if id has been inserted, the program exit with an error
  119.   and type will be free after the function,
  120.   so here we must allocate a new space for it
  121. */
  122. void insert(char *id , nodetype *type)
  123. {
  124.   int i;
  125.   //check the parameter
  126.   if(type == NULL)
  127.     yyerror("the parameter of insert()--type is NULL");
  128.   //check whether id is used
  129.   for(i=0 ; i < global_table.size ; i++){
  130.     if(strcmp(global_table.node[i].id , id) == 0){
  131.       printf("id:%s has been used\n" , id);
  132.       exit(1);
  133.     }
  134.   }
  135.   //check whether there is enough space
  136.   if(global_table.size == global_table.capacity){
  137.     global_table.capacity<<=2;
  138.     global_table.node=(tablenode *)realloc(global_table.node , global_table.capacity);
  139.     if(global_table.node == NULL)
  140.       yyerror("there is no enough space");
  141.   }

  142.   /*append the new node*/
  143.   global_table.node[global_table.size].id=id;
  144.   global_table.node[global_table.size].type=(nodetype *)malloc(sizeof(nodetype));
  145.   memcpy(global_table.node[global_table.size].type , type , sizeof(nodetype));
  146.   global_table.size++;
  147. }

  148. nodetype*new_type(nodetype *node)
  149. {
  150.   if(node == NULL)
  151.     yyerror("the parameter of new_type() -- node is NULL");
  152.   nodetype *tmp=(nodetype *)malloc(sizeof(nodetype));
  153.   memcpy(tmp , node , sizeof(nodetype));
  154.   return tmp;
  155. }

  156. void type_error(const char *str)
  157. {
  158.   yyerror(str);
  159. }
  160. /*the nodetype is just a type expression
  161.   so return nodetype pointer here
  162. */
  163. nodetype *lookup(const char *id)
  164. {
  165.   int i;

  166.   if(id == NULL){
  167.     printf("the parameter of lookup() -- id is NULL");
  168.     return NULL;
  169.   }
  170.   for(i=0 ; i < global_table.size ; i++){
  171.     if(strcmp(global_table.node[i].id , id))
  172.       return (global_table.node+i)->;type;
  173.   }
  174.   return NULL;
  175. }
  176. /*
  177.   if the length of one of arrays is zero, just compare their type
  178. */
  179. int type_equal(nodetype *node1 , nodetype *node2)
  180. {
  181.   if(node1 == NULL){
  182.     printf("the parameter of type_equal() -- node1 is NULL\n");
  183.     return 0;
  184.   }
  185.   if(node2 == NULL){
  186.     printf("the parameter of type_equal() -- node2 is NULL\n");
  187.     return 0;
  188.   }

  189.   if(node1->;type != node2->;type)
  190.     return 0;
  191.   if(node1->;type == ARRAY){
  192.     if(node1->;child_type->;type != node2->;child_type->;type)/*need modify*/
  193.       return 0;
  194.     return node1->;len == 0 || node2->;len == 0 || node1->;len == node2->;len;
  195.   }
  196.   return 1;
  197. }

  198. int main(int argc , char *argv[])
  199. {
  200.   bzero(INTEGER , sizeof(nodetype));
  201.   bzero(BOOLEAN , sizeof(nodetype));
  202.   bzero(ARRAYTYPE , sizeof(nodetype));
  203.   INTEGER->;type=INT;
  204.   BOOLEAN->;type=BOOL;
  205.   ARRAYTYPE->;type=ARRAY;
  206.   
  207.   global_table.capacity=ONE_BUF;
  208.   global_table.size=0;
  209.   global_table.node=(tablenode *)malloc(ONE_BUF);

  210.   yyparse();
  211.   exit(0);
  212. }
复制代码


common.h

  1. typedef struct nodeType
  2. {
  3.   int type;
  4.   char *id;
  5.   struct
  6.   {
  7.     int len;
  8.     struct nodeType *child;
  9.   }u;
  10. #define len u.len
  11. #define child_type u.child
  12. }nodetype;

  13. typedef struct
  14. {
  15.   char *id;
  16.   nodetype *type;
  17. }tablenode;

  18. typedef struct
  19. {
  20.   int capacity;
  21.   int size;
  22.   tablenode *node;
  23. }table;

  24. #define ONE_BUF 32
复制代码


我用下面的方式编译flex产生的.c文件:
bison -d compile_6_30.y
flex compile_6_30.l
gcc -c lex.yy.c
结果出来下面的错误
-*- mode: compilation; default-directory: "/windows/E/homework/project/" -*-
gcc -c lex.yy.c
compile_6_30.l: In function `yylex':
compile_6_30.l:33: warning: return makes integer from pointer without a cast
lex.yy.c: In function `yy_scan_string':
lex.yy.c:1394: error: syntax error before '.' token
lex.yy.c:1395: error: `u' undeclared (first use in this function)
lex.yy.c:1395: error: (Each undeclared identifier is reported only once
lex.yy.c:1395: error: for each function it appears in.)
lex.yy.c:1398: warning: passing arg 1 of `yy_scan_bytes' discards qualifiers from pointer target type
lex.yy.c: At top level:
lex.yy.c:1412: error: parse error before '.' token
lex.yy.c: In function `yy_scan_bytes':
lex.yy.c:1413: error: number of arguments doesn't match prototype
lex.yy.c:241: error: prototype declaration
lex.yy.c:1420: error: `u' undeclared (first use in this function)
lex.yy.c:1426: error: `bytes' undeclared (first use in this function)

Compilation exited abnormally with code 1 at Fri Jun 10 00:29:30

编译结果怎么会说有些变量找不到呢?
请大家帮帮忙
谢谢

论坛徽章:
0
2 [报告]
发表于 2005-06-10 18:13 |只看该作者

用flex和bison做的一个程序用gcc编译的时候出来莫名其妙的错误

实在是不好意思
大家能不能帮我编译一下看看,怎么会出这种编译错误
实在是不应该这样发贴问大家的,只是我实在是没有办法,实在是调不出来
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP