help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: segmentation fault problem


From: Tomas Crhak
Subject: Re: segmentation fault problem
Date: Wed, 27 Nov 2002 14:42:05 +0100

Hi,

You cannot copy (sprintf) a string to $$ without allocating a memory for it.
E.i. in the rule
intestazione: Program ID SEQOP { sprintf($$,"%s()",$2);};

you should rather do something like
intestazione: Program ID SEQOP { $$ = malloc(strlen($2)+3); 
sprintf($$,"%s()",$2); free($2); };

Tomas

> Hi all, that's the first time I write on this list.
> I'm just learning bison's use, and after a couple
> experiment I started with a mini translator from a
> subset of pascal to C (I think this' quite a "hello
> world " in the parsers' world).
> My main problem is that even if the parser I generate
> works fine in most cases, it crashes when I pass more
> than one statement in the loop/conditional blocks:
> I.E.
> 
> if l<10 then
> write (l)
> else
> l:=10
> 
> works fine, but
> 
> if l<10 then
> write (l);
> l:=11
> else
> l:=11
> 
> gives segfault.
> 
> I made little research on google and found similar
> problems discussed here,so I tried to apply the
> suggested solution:
> 
> - put a malloc() when strcpy'ing yytext in  
> yylval.string 
> 
> - malloc yyleng+1 
> - use strncpy instead of strcpy
> 
> Anyway, I still get a sigsegv.
> 
> I even tried to memzero the string, thinking that
> maybe the yylval.string was not \0 terminated, but not
> even this worked.
> 
> 
> 
> I attach the .y & .fl files, the'y re just 2Kb 
> 
> Please, help me, I cannot understand what's happening
> in my code.
> 
> PS
> sorry about my english, I'm italian and I've just
> studied it when I was at the high school
> 
> =====
> icq #69488917
> 
> ______________________________________________________________________
> Per te Blu American Express รจ gratis!
> http://it.yahoo.com/mail_it/foot/?http://www.americanexpress.it/land_yahoo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]