Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: warning: assignment makes pointer from integer without a cast[enabled by default] yylval=atoi(yytext);

588 views
Skip to first unread message

Danny Pereira

unread,
Feb 24, 2016, 8:23:26 AM2/24/16
to
Hello all,

I am trying to write a simple calculator application using re-entrant lex
and yacc. Here I wish to create two threads (parsers) which should parse
input provided in input file. Lines to be parsed in input file are divided
among two threads..

My lex code for simple calculator is

%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*/

. ;

%%

*My yacc file for simple calculator(reentrant) is*

%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>
FILE * 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]=fopen(argv[1],O_RDONLY);
fseek(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);
}


*and my input file is*

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

*I am compiling above programs as *

*$lex purelex.l followed with *

*$ yacc pureyacc.y and*


* $gcc -o purecalc lex.yy.c y.tab.c -lpthread*

*I am getting all zero values as output. I am expecting results of
arithmetic expressions provided in input file as output . When I compile
using gcc I am getting following warning *

*purelex.l:12:8: warning: assignment makes pointer from integer without a
cast [enabled by default] yylval=atoi(yytext); *


*Also I would like to restrict first thread to process fist 6 line and
second thread to process next six lines which is not happening right now..*

*Help to resolve this issue is appreciated..*

*Thank you..*


>> I have posted my question on stack-overflow. Link of question is
>> http://stackoverflow.com/questions/35459610/segmentation-fault-in-re-entrant-yacc-parser-code

--
Regards

Pereira Danny Jeron
I/c Head,Department Of Computer Engineering,
Govt. College of Engg and Research, Avasari(Kd),
Pune
[I suggested he debug it the usual way, by putting breakpoints in the
C code to narrow down the places where it was smashing stuff. -John]

Kaz Kylheku

unread,
Feb 24, 2016, 11:54:43 PM2/24/16
to
On 2016-02-22, Danny Pereira <dannyj...@gmail.com> wrote:
> *purelex.l:12:8: warning: assignment makes pointer from integer without a
> cast [enabled by default] yylval=atoi(yytext); *

This tells you that yylval does not have type int as you expect; it is
in fact a pointer to something.

Though presented as a "warning" in GCC, assigning an integer to a
pointer is a constraint violation according to ISO C, requiring a
diagnostic from a conforming implementation, and the behavior
is not well defined if the code is translated and executed anyway.

It is a bad idea to ignore this diagnostic.

Your yylval is a pointer because of the "Bison bridge" option.
This is documented in the GNU Flex manual:

A.2 C Scanners with Bison Parsers

[... few paragraphs down ...]

Note that the macros `yylval' and `yylloc' evaluate to pointers.

George Neuner

unread,
Feb 24, 2016, 11:55:15 PM2/24/16
to
On Mon, 22 Feb 2016 09:28:49 +0530, Danny Pereira
<dannyj...@gmail.com> wrote:

< snipped - program code >


>*$lex purelex.l followed with *

Lex is not reentrant, so you must be using _Flex_.

>When I compile using gcc I am getting following warning *
>
>*purelex.l:12:8: warning: assignment makes pointer from integer without a
>cast [enabled by default] yylval=atoi(yytext); *

The default yylval type is a pointer. Look at Bison's %union
directive and see how it modifies the %token directive.


>*Also I would like to restrict first thread to process fist 6 line and
>second thread to process next six lines which is not happening right now..*

First make it work without threads. Then make it work with 1 thread.
Look carefully for an error in scanfunc().

George
0 new messages