Re: Making my first compiler

torbenm@app-1.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
18 Sep 2006 09:49:24 -0400

          From comp.compilers

Related articles
Making my first compiler szigetir@gmail.com (2006-09-16)
Re: Making my first compiler pjb@informatimago.com (Pascal Bourguignon) (2006-09-18)
Re: Making my first compiler tommy.thorn@gmail.com (Tommy Thorn) (2006-09-18)
Re: Making my first compiler idknow@gmail.com (idknow@gmail.com) (2006-09-18)
Re: Making my first compiler torbenm@app-1.diku.dk (2006-09-18)
Re: Making my first compiler jeffrey.kenton@comcast.net (Jeff Kenton) (2006-09-25)
Re: Making my first compiler firefly@diku.dk (Peter \Firefly\Lund) (2006-09-25)
Re: Making my first compiler monnier@iro.umontreal.ca (Stefan Monnier) (2006-11-23)
Re: Making my first compiler ValdoFerrari@libero.it (Valdo Ferrari) (2006-11-24)
Re: Making my first compiler torbenm@app-0.diku.dk (2006-11-27)
Re: Making my first compiler z2345678998765432y@sbcglobal.net (John) (2006-11-29)
[6 later articles]
| List of all articles for this month |

From: torbenm@app-1.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.compilers
Date: 18 Sep 2006 09:49:24 -0400
Organization: Department of Computer Science, University of Copenhagen
References: 06-09-087
Keywords: design
Posted-Date: 18 Sep 2006 09:49:24 EDT

szigetir@gmail.com writes:


> I'm trying to create a pascal subset interpreter/compiler.
>
> Do I HAVE to create a syntax tree? Or can I go straight to creating
> Intermediate Code (Quadruples) in Yacc's reduce actions?


You can generate code directly, most compilers pre 1990 did that. It
makes it a bit harder to optimize code, but for simple code it will
work, especially if you output textual assembler (with labels), so you
don't have to worry about back-patching branch targets.


> What's a basic structure for a syntax tree in C?


A union of structs where all the structs share a tag field as the
first component.


> Say I want my compiler to convert Pascal code to C code, which steps
> are absolutely necessary to do this correctly?
> Im thinking:
> Build Syntax Tree -> Convert it to Three Address Code (Quadruples) ->
> then into C
> Was this correct?


Going through three-address code seems like taking a detour. Most
constructs in Pascal can be directly translated into source-level
constructs in C. The main problems would be nested function
declarations and functions as parameters, but you can handle these
with lambda lifting (discussed here recently).


So my steps would be:


  1. Build syntax tree
  2. Do lambda lifting
  3. Emit C.


Torben


Post a followup to this message

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