Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!spdcc!ima!esegue!compilers-sender From: bliss@sp64.csrd.uiuc.edu (Brian Bliss) Newsgroups: comp.compilers Subject: Re: Question About YACC's Memory Management Keywords: yacc Message-ID: <1990Oct18.213224.21195@csrd.uiuc.edu> Date: 18 Oct 90 21:32:24 GMT References: <2371@kiwi.mpr.ca> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: bliss@sp64.csrd.uiuc.edu (Brian Bliss) Organization: Center for Supercomputing Research and Development Lines: 58 Approved: compilers@esegue.segue.boston.ma.us 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 -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.