bison - 警告 : assignment makes pointer from integer without a cast yylval=atoi(yytext);

标签 bison flex-lexer yacc lex bisonc++

我正在尝试使用可重入 lex 和 yacc 编写一个简单的计算器应用程序。在这里我希望创建两个线程(解析器)来解析输入文件中提供的输入。输入文件中要解析的行被分成两个线程..

我的简单计算器 lex 代码是

%option reentrant bison-bridge
%option noyywrap
%{
#include<stdio.h>
void yyerror(char *);
#include "y.tab.h"
%}

%%

[0-9]+ {
        yylval=atoi(yytext);
        return INTEGER;
        }

[-+\n]  return *yytext;

[ \t]   ;/* Skip whitespaces*/

.       ;

%%

我的简单计算器(可重入)的 yacc 文件是

%pure-parser
%lex-param {void * scanner}
%parse-param {void * scanner}
%{
        #include <pthread.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        #include<stdio.h>
        #include <stdlib.h>
        int f[2];
        pthread_mutex_t lock;
        int cnt=0;
        void* scanfunc(void * );
%}

%token INTEGER

%%

program: program expr '\n'      {printf("%d\n", $2);}
        |
        ;

expr:   INTEGER                 {$$=$1;}
        |expr '+' expr          {$$=$1+$3;}
        |expr '-' expr          {$$=$1-$3;}
        ;

%%

void yyerror(char *s){
        fprintf(stderr,"%s\n",s);
}
int main(int argc, char *argv[]){
        pthread_t threads[2];
        int n=2,i;//Number of threads to be created
        //set_file_ptr();

        if(argc!=2){
                printf("Insufficient nos. of arguments\n");
                exit(0);
        }
        for(i=0;i<n;i++){
                f[i]=open(argv[1],O_RDONLY);
                lseek(f[i],i*30,SEEK_SET);
        }
        printf("File pointer set\n");

        pthread_mutex_init(&lock,NULL);
        for(i=0;i<n;i++){
                pthread_create(&threads[i], NULL, (void *)scanfunc, (void *)&i);
        }

        //join all threads
        for(i=0;i<n;i++){
                pthread_join(threads[i],NULL);
        }

        pthread_mutex_destroy(&lock);
        //yyparse();
        return 0;
}
void* scanfunc(void * i)
{
    void * scanner;
   // printf("Value of i is %d\n",*(int *)i);
    printf("Starting thread %d...\n", *(int *)i);
    yylex_init(&scanner);
    printf("Done scanner init\n");
    pthread_mutex_lock(&lock);
    printf("Thread with id %d obtained lock\n",cnt);
    yyset_in(f[cnt],scanner);
    cnt++;
    pthread_mutex_unlock(&lock);
    yyparse(scanner);
    yylex_destroy(scanner);
}

我的输入文件是

12+12
14-12
23-11
12-12
23-45
67+45
11+23
45-12
67-12
90-34
56-56
90-45

当我编译并运行这个程序时,我得到以下输出

File pointer set
Starting thread 0...
Starting thread 0...
Done scanner init
Thread with id 0 obtained lock
Done scanner init
Thread with id 1 obtained lock
Segmentation fault (core dumped)

当我使用 gdb 调试这个程序时,它说程序接收到信号 SIGSEGV,下面一行出现段错误。

yyparse(scanner);

我不知道如何调试这个程序。 在这方面的帮助表示赞赏。 谢谢

最佳答案

解决了 Warning : assignment makes pointer from integer without a cast yylval=atoi(yytext); 的问题

引用Making bison/flex parser reentrant with integral YYSTYPE发布以了解我必须对代码进行以下更改才能消除警告。

*yylval=atoi(yytex)

谢谢

关于bison - 警告 : assignment makes pointer from integer without a cast yylval=atoi(yytext);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459610/

相关文章:

C with Bison+Flex 检查规则文件

c - 是否可以在 lex yacc 中使用箭头键?

c - 冲突的 Bison 循环

sublimetext2 - Flex 和 Bison 是否有 Sublime Text 语法?

parsing - 我应该如何在 yacc/bison 和 lex 中制定递归规则?

c - 用于匹配一组标记的 Bison 语法规则

parsing - 用于很好地格式化 Bison/Yacc 语法文件的实用程序?

c++ - 弹性和 Bison : string literal

c - 简单的弹性程序

c++ - 柔性/莱克斯 : Regular Expression matches double characters