Re: code generation (Torben Ęgidius Mogensen)
17 Nov 2004 11:38:23 -0500

          From comp.compilers

Related articles
code generation (2002-01-24)
Re: code generation (Ira D. Baxter) (2002-01-28)
Re: code generation (Boris Boesler) (2002-01-28)
Re: code generation (Pietro) (2002-01-28)
Re: code generation (=?ISO-8859-15?q? (2002-02-06)
code generation (Viesturs Rikards) (2004-11-14)
Re: code generation (2004-11-17)
Code generation mcvax!ruuinf! (1989-02-14)
| List of all articles for this month |

From: (Torben Ęgidius Mogensen)
Newsgroups: comp.compilers
Date: 17 Nov 2004 11:38:23 -0500
Organization: Department of Computer Science, University of Copenhagen
References: 04-11-038
Keywords: code, design
Posted-Date: 17 Nov 2004 11:38:22 EST

Viesturs Rikards <> writes:

> What datatype (data structure) would you suggest to use for
> nonterminals like 'conditional_expr', 'expr', 'identifier_expr' etc
> in yacc file for a C like language.

This depends on whether you want to produce an abstract syntax tree
for further processing or if you want to produce code directly (i.e.,
make a "one-pass compiler").

For very simple languages, a one-pass compiler may be the easiest, but
it requires such things as declaration before use of variables and
functions (or back-patching, which may make things ugly). So for
anything nontrivial, I would suggest you make abstract sntax during
parsing and then work on this.

Abstract syntax is in C described most naturally by a tagged union of
structs, i.e., a struct with a tag field (which is an enumerated
type), and a union such that the element of the union is determined by
the tag. Each union element is a struct type with fields for pointers
to subtrees and attributes.

See Andrew Appel's "Modern Compiler Implementation in C" for a
(slightly nonstandard) treatment on how to build and work with
abstract syntax in C.

If you produce code directly, you may only need a symbol table as a
data structure, as code is printed as it is generated.

Aho, Sethi & Ullman's "Compilers - Principles, Techniques and Tools"
is a good source for how to write one-pass compilers.


Post a followup to this message

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