【问题标题】:Bison/Flex creating listBison/Flex 创建列表
【发布时间】:2015-10-29 07:38:14
【问题描述】:

我正在尝试从中缀转换为后缀,并使用列表以正确的顺序捕获字符。这是我的野牛文件:

%{
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
%}
%union{
    char list[100];
    int integer;
};

%token <integer> NUM
%token ENDOFLINE
%left '+' '-'
%left '*' '/'
%right NEGATIVE
%type <list> Exp
%%

Statement: 
    | Statement Line
;
Line: Exp ENDOFLINE{ printf("Result -> %s\n", $1);  }
    | ENDOFLINE
;
Exp:  Exp '+' Exp {add($$,(char)$2);}
    |   Exp '*' Exp {add($$,(char)$2);}
    |   Exp '-' Exp {add($$,(char)$2);}
    |   Exp '/' Exp {add($$,(char)$2);}
    |   '-' Exp %prec NEGATIVE {add($$,(char) $1);}
    |   NUM     {$$ = create_list(); add($$,(char) $1);}
;
%%
char * create_list(){
    char expList [100];
    return expList;
}
void add(char* array, char input){
    int length = stlen(array);
    array[length] = input;
}
int main(){
    yyparse();

}

int yyerror (char *msg) {
    return printf ("error YACC: %s\n", msg);
}

我有多个问题,一个是它说我无法将 int 添加到 char[],另一个说请求成员添加不是结构或联合的东西。我对 Bison/Flex 很陌生,如果我想存储这些值以便我可以在 main 中使用它们(在某些时候),这是正确的实现吗?

编辑:这是我最新的代码。我更新了我的 lex 文件以识别各个运算符并返回该标记,并将正确的函数及其原型添加到我的野牛文件 (a4grammer2.y)。

%{
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>

    char * create_list();
    void add(char* array, char input);
    int yyerror(char* s);
    int main(void);
%}
%union{
    char list[100];
    char character;
    int integer;
};

%token <integer> NUM
%token <character> PLUS
%token <character> MINUS
%token <character> MULTIPLY
%token <character> DIVIDE
%token ENDOFLINE
%right NEGATIVE
%type <list> Exp
%%

Statement: 
    | Statement Line
;
Line: Exp ENDOFLINE{ printf("Result -> %s\n", $1);  }
    | ENDOFLINE
;
Exp:  Exp PLUS Exp {add($$,$2);}
    |   Exp MULTIPLY Exp {add($$,$2);}
    |   Exp MINUS Exp {add($$,$2);}
    |   Exp DIVIDE Exp {add($$,$2);}
    |   MINUS Exp %prec NEGATIVE {add($$,$1);}
    |   NUM     {strcpy($$,create_list()); add($$, $1);}
;
%%
char * create_list(){
    char expList [100];
    return expList;
}
void add(char* array, char input){
    int length = stlen(array);
    array[length] = input +'0';
}
int main(){
    yyparse();

}

int yyerror (char *msg) {
    return printf ("error YACC: %s\n", msg);
}

现在当我使用以下方法编译它时:

yacc -d a4grammer2.y

我收到此错误

warning: 20 shift/reduce conflicts [-Wconflicts-sr]

当我尝试使用 gcc 编译时,我得到了这个:

a4grammer2.y: In function ‘create_list’:
a4grammer2.y:44:5: warning: function returns address of local variable [-Wreturn-local-addr]
     return expList;

【问题讨论】:

  • Exp 的值是一个字符数组,这意味着在Exp 规则中$$ 也是一个字符数组。你不会期望例如list-&gt;add('+') 工作,你愿意吗?您需要实际创建函数以将字符附加到数组中。
  • @JoachimPileborg 我相信我添加了我需要的正确功能。但是,当我尝试传递第二个参数时,它告诉我它没有类型,即使我尝试将其转换为 char
  • 函数调用前需要添加原型声明,否则编译器不知道退出。此外,如果要将数组视为字符串,则需要记住正确地以零结尾。最后,请编辑您的问题以显示 actualcompleteunedited 错误,否则我们所能做的实际上只是猜测。
  • @JoachimPileborg 我编辑了我最新的代码以及我编译它时产生的完整错误/警告。
  • 您正在返回一个指向 create_list 函数中的局部变量的指针,并且您(应该)知道当函数返回时局部变量会超出范围,从而留下一个杂散的指针。你甚至不需要需要那个功能,只要做例如strcpy($$, strtol($1, NULL, 10));

标签: c bison


【解决方案1】:

除了在 cmets 中提到的:

Exp:  Exp '+' Exp {add($$,(char)$2);}

这个,以及每一个类似的作品,都应该是一般的形式

Exp:  Exp '+' Exp {$$ = add($1,$3);}

我不知道是什么让你认为每个 Exp 都可以转换为 char.

【讨论】:

  • 我不想将第一个和第三个变量加在一起。 add 函数将运算符添加到字符数组中。
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2011-07-11
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多