Re: Question About YACC's Memory Management

bliss@sp64.csrd.uiuc.edu (Brian Bliss)
Thu, 18 Oct 90 21:32:24 GMT

          From comp.compilers

Related articles
Question About YACC's Memory Management andrea@mprgate.mpr.ca (1990-10-11)
Re: Question About YACC's Memory Management bliss@sp64.csrd.uiuc.edu (1990-10-18)
Re: Question About YACC's Memory Management djones@megatest.uucp (1990-10-21)
Re: Question About YACC's Memory Management pardo@cs.washington.edu (1990-10-23)
| List of all articles for this month |

Newsgroups: comp.compilers
From: bliss@sp64.csrd.uiuc.edu (Brian Bliss)
Keywords: yacc
Organization: Center for Supercomputing Research and Development
References: <2371@kiwi.mpr.ca>
Date: Thu, 18 Oct 90 21:32:24 GMT

In article <2371@kiwi.mpr.ca>, andrea@mprgate.mpr.ca (Jennitta Andrea) writes:
|>
|> I have a parser which is intended to be called repeatedly from a driver
|> program. ...
|>
|> I have typed the value stack to contain pointers to characters. I
|> malloc the memory required for each token in lex (assigning yylval to
|> point to that block of memory) before returning the token to yacc. It
|> appears that this memory is not cleaned up when yacc frees the value stack.
|>
|> Am I required to explicitly free each token once the parser has reduced
|> a rule? ...


3 ways around this:


1) modify yyparse, the file that is included with the yacc-generated
      tables to form the parser, to free the tokens when popping the stack.


2) don't use the yacc value stack, make your own, and free the tokens
      when popping it.


3) merge the lexer into the parser and use alloca () to allocate the
      memory. instead of having lex recognize "xyz", allocate the string
      "xyz", and return the token XYZ, include the yacc rule:


      XYZ: 'x' 'y' 'z' {
                            char *a = alloca (4);
                            sprintf (a, "xyz");
                            push (a);
                        }


        all strings will be automatically freed when yyparse () returns.
        this can get messy if the lex rules are complicated, though.


bb
--


Post a followup to this message

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