Re: Two pass compiler for a language similar to Java

Chris Dollin <kers@hplb.hpl.hp.com>
13 Sep 2004 12:34:13 -0400

          From comp.compilers

Related articles
Two pass compiler for a language similar to Java joanpujol@gmail.com (=?ISO-8859-1?Q?Joan_Jes=FAs_Pujol_Espinar?=) (2004-09-03)
Re: Two pass compiler for a language similar to Java kamalp@acm.org (2004-09-07)
Re: Two pass compiler for a language similar to Java jjan@cs.rug.nl (J.H.Jongejan) (2004-09-08)
Re: Two pass compiler for a language similar to Java lhp+news@toft-hp.dk (Lasse =?ISO-8859-1?Q?Hiller=F8e?= Petersen) (2004-09-13)
Re: Two pass compiler for a language similar to Java vidar@hokstad.name (2004-09-13)
Re: Two pass compiler for a language similar to Java joanpujol@gmail.com (Joan Pujol) (2004-09-13)
Re: Two pass compiler for a language similar to Java tzvetanmi@yahoo.com (2004-09-13)
Re: Two pass compiler for a language similar to Java kers@hplb.hpl.hp.com (Chris Dollin) (2004-09-13)
Re: Two pass compiler for a language similar to Java kamalp@acm.org (2004-09-21)
| List of all articles for this month |
From: Chris Dollin <kers@hplb.hpl.hp.com>
Newsgroups: comp.compilers
Date: 13 Sep 2004 12:34:13 -0400
Organization: HPLB
References: 04-09-032 04-09-051
Keywords: OOP, design
Posted-Date: 13 Sep 2004 12:34:13 EDT

Kamal R. Prasad wrote:


> Joan Jesús Pujol Espinar <joanpujol@gmail.com> wrote
>
>> I'm doing a pseudocode language similar to Java (it is posible to use
>> things before are defined).
>> I know (or I think ;) ) that the best way to manage this is with two
>> pass using ASTs. And is the strategy that I'm using.
>
> A procedural language does require a 2-pass compiler.


Counterexample: Pascal. Also C, I believe.


>> But only for curiosity, it's possible to do the semantic analysis of my
>> language in a syntatic directed compiler with only one pass. The
>> strategy will be to put in the symbol table what you are waiting to get
>> and when you find the definition compare if what are you waiting is what
>> is defined... It's a lot more dificult, but it's posible, or there are
>> problems that I can't see?
>
> How will you identify errors in function parameters? A symbol table
> isn't the only one that needs to deal with yet-to-be-encountered
> stuff.


It depends on the details of his semantics, I think, and what the
geenrated code expects. If one finds a call


        f(x)


where both f and x are as-yet undeclared, you add to the symbol
table


        f: SOMETYPE -> SOMEOTHERTYPE
        x: SOMETYPE


Now when (say) you find


        f(17)


you know that SOMETYPE "must" be a type that 17 is a legal value
of, so you note


        SOMETYPE:: oneof (small int, medium int, large int ...)


When you find


        f( "hello, whirled" )


if the ... doesn't include String, you can flag an error (or
warning, depending on your code geenration strategy).


When you find the declaration of f, you check that the recorded
uses are consistent with the declaration.


>> A lot of thanks in advance and excuseme for my little english.
>
> If you want an interpreter -it makes sense to go in for a 1-pass
> compiler. Else, 2-pass is what one would use.


I don't see that whether or not it's an interpreter (whatever that
means) makes a difference. You can have 1-pass compilers for
interpretable code and for actual real true machine code, and you
can have >1-pass compilers for ditto, even when [if] the notion of
pass *is* well-defined.


What matters more than the number of passes, surely, is whether the
code "fits" (in space/time terms) into the budget, and whether it
is "clean" (expresses the solution well, is usefully [de]coupled,
is robust against change, etc).


--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html


Post a followup to this message

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