help-bison
[Top][All Lists]
Advanced

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

Re: Unable to remove memory leak when the parser find an error before it


From: Luca
Subject: Re: Unable to remove memory leak when the parser find an error before it is fully reduced to the main production-rule.
Date: Wed, 13 Jun 2012 19:40:49 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

Using Bison you can free discarded symbols using %destructor directive:
http://www.gnu.org/software/bison/manual/html_node/Destructor-Decl.html

typically the action is to delete a node, or to free a string.
Note that bison is able to generate also a C++ based parser, and not only C.


Anyway, if the parser is a single batch process, when the process exits all the memory is freed, so you don't have to care about memory leaks (in this situation free/delete will only slow down your compiler) . Memory leaks are important if you're developing an interactive parser, (eg a shell parser), or if the parser is always running as a background service (eg visual studio intellisense)



Luca




On 12/06/2012 14:12, Fabricio Pellegrini wrote:
Hello,

First of all, I'm using:

-          Window 7 Enterprise 32bits

-          Visual Studio 2010

-          Bison++ Version 1.21-8, adapted from GNU bison by
<mailto:address@hidden>  address@hidden



Currently we are using Bison++, to parse our proprietary script. During the
parsing we are just building a tree-structured, based on the Interpreter
Pattern, that later will be saved was a proprietary format and loaded by our
server. So we make use of dynamic allocation inside the action's blocks. The
problem is that recently, I found out, if the parser gets any syntax error
after not being able to reduce anymore, we get memory leaks due its
allocation during the parser.

For example, let's say that I have the following grammar:

stmt:

       ref '=' expr ';'

{

       $$ = Create(ASSIGN); //The factory will create and return a new
"ASSIGN" object

       $$->SetOp1($1);

       $$->SetOp2($3);

}

;

expr :

       expr '+' expr

{

       $$ = Create(ADD); //The factory will create and return a new "ADD"
object

       $$->SetOp1($1);

       $$->SetOp2($3);

}

|     ref

{

$$ = $1;

}

;

ref :

REF

{

$$ = Create(REF); //The factory will return a new "REF" object

$$->SetName($1);

}

;

Assuming that we insert the following input:

x = a + b  //The ';' isn't present

The parse will rise an error, because it can't reduce using the "stmt ->  ref
'=' expr ';' " rule.

So, after it exits, We can't delete the memory that was allocated during the
reducing of expr and ref.

Reading the O'Reilly's book "YACC&  Flex", I found out that I could use
"error recovering" to get back the control of the parser and release the
memory when it finds an error.

Then, I tried to add the rule "stmt ->  ref '=' expr error';'" to be able to
delete the memory allocated during the reducing of expr and ref, like you
can see below:

stmt:

ref '=' expr ';'

{

       $$ = Create(ASSIGN); //The factory will create and return a new
"ASSIGN" object

       $$->SetOp1($1);

       $$->SetOp2($3);

}

|     ref '=' expr error';'

{

       $$ = NULL;

delete $1;

       delete $3;

}

It will work for the preview input, x = a + b, but won't for, x = + b;

So I would like to know, if adding more recovery points is the right way to
avoid memory leaks on my case. If no, is there a better approach that I
could use?

Thank you for your time





Fabricio G. Pellegrini

Software Developer



EDiSPHERE Software Private Limited | 215, Congress Nagar, Nagpur 440012

Email:<mailto:address@hidden>
address@hidden | Website:<http://www.edisphere.com/>
http://www.edisphere.com



_______________________________________________
address@hidden https://lists.gnu.org/mailman/listinfo/help-bison





reply via email to

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