Lex/Yacc Memory Leaks

"Kamron M. Hays" <c23kmh@eng.delcoelect.com>
16 Mar 1996 00:08:09 -0500

          From comp.compilers

Related articles
Lex/Yacc Memory Leaks c23kmh@eng.delcoelect.com (Kamron M. Hays) (1996-03-16)
| List of all articles for this month |

From: "Kamron M. Hays" <c23kmh@eng.delcoelect.com>
Newsgroups: comp.compilers,comp.unix.programmer
Date: 16 Mar 1996 00:08:09 -0500
Organization: Delco Electronics Corp.
Keywords: yacc, question

I'm writing a command interpreter using lex & yacc (flex & bison
actually). I've come across the problem, however, of a few instances
where lex dynamically allocates memory for one or more tokens in the
command, but there turns out to be a syntax error in the command (an
error rule is reduced). The error rule does not have access to the
pointer(s) of the dynamically allocated memory, so this memory is
essentially lost (no way to free it).

This seems like a common occurance to handle. There may even be a
standard way to handle it, but I don't know what it is. Is there
anyone that has dealt with this problem that could give me some tips?
The best idea I could come up with is to keep a list of dynamically
allocated pointers, and free all the items on the list if the error
rule is reduced. I may resort to doing this if there is not a more
elegant way of handling it.

I've read the O'Reilly book "lex & yacc", and while this book has been
extremely helpful, the above sort of problem is not addressed. Is
there something I've missed?

Any suggestions are much appreciated.

[I didn't say much about it in the book. Usually what I do is to do
my own allocation, typically by writing myalloc() which chains
together all of the space it allocates, then after the parse I free
the whole myalloc chain. As noted, trying to free all of the storage
allocated in a yacc parser on the fly is a losing battle. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.