Re: Using Lex/Yacc to transform a parse tree

"Virendra Kr. Mehta" <c1veeru@watson.ibm.com>
Wed, 19 Oct 1994 18:15:50 GMT

          From comp.compilers

Related articles
Using Lex/Yacc to transform a parse tree wing@research.canon.oz.au (1994-10-11)
Re: Using Lex/Yacc to transform a parse tree c1veeru@watson.ibm.com (Virendra Kr. Mehta) (1994-10-19)
| List of all articles for this month |

Newsgroups: comp.compilers
From: "Virendra Kr. Mehta" <c1veeru@watson.ibm.com>
Keywords: translator, comment
Organization: Compilers Central
References: 94-10-084
Date: Wed, 19 Oct 1994 18:15:50 GMT

|o| My first question is, what is the simplest way to parse multiple files.
|o| E.g. In C, we have #include <filename>, and there is a similar statement
|o| in vhdl. The preprocessor will (at least) need to extract the constants
|o| and enumerated types defined in these included files, before processing
|o| the main source file. How can I do this in Yacc/Lex???


Lex generated scanners read their input from a FILE pointer - yyin which is
initialized to stdin. Thus, this pointer is reset if the input is needed from
a file. Also there is a function yywrap() which is invoked when the EOF is
reached. If it returns 1, the lexical analyzer sends an EOF to the parser, o/w
(for 0) it continues to read from yyin.


Thus you can follow the simple strategy of saving the current yyin on a stack
when you get the inclusion directive and set it to the new file. At the end of
this file, when yywrap is called, just check if there are any open files, and
pop yyin if so returning 0. Otherwise return 1.


|o| My other question is how I can convert a branch of a parse tree, from one
|o| form to another. An example of this conversion is as follows:


|o| How should I perform this conversion? Should I ask Yacc to build a complete
|o| parse tree for the whole input file, and then after yyparse() has returned,
|o| execute the code to traverse the parse tree and modify the branches
|o| corresponding to the above "<label> : for i in .... begin <statements> end;
  "?
|o|
|o| Could this type of conversion be carried out while yyparse() is parsing the
|o| input file, e.g. use the actions (or embedded actions) to convert the
|o| above 'generate' statement once a "<label> : ... begin <statements> end"
  block is found?


Assuming that you have a physical tree being constructed in the memory, you
can follow both the approaches.


|o| Wing Chung wing@research.canon.oz.au
--
    Virendra K Mehta
  c1veeru@watson.ibm.com
J2-C72, TJWatson Res. Centre
  30, Sawmill River Road,
  Hawthorne, NY - 10532.
  (914) 784 - 7774 (off)
  (914) 734 - 2656 (res)
[Warning: that lex hack only works in AT&T lex. If you use flex, you
have to use its built-in buffer switching features. -John]
--


Post a followup to this message

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