help-bison
[Top][All Lists]
Advanced

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

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


From: Fabricio Pellegrini
Subject: Unable to remove memory leak when the parser find an error before it is fully reduced to the main production-rule.
Date: Tue, 12 Jun 2012 17:42:21 +0530

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

 



reply via email to

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