Please help a n00b write a compiler :-)

A

Anton Gavrilov

Hi all,

I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long before
I reach break-even, but it doesn't hurt trying, does it?

I'm a C addict reluctant to switch to C++, partly because I can't fully
grok (and accept) the OOP paradigm. Nevertheless I like C++'s arguable
improvements such as overloading of functions and operators, references,
const types etc. I also miss Pascal's way-less-cryptic typedefs and
consistent array types.

I suppose that rather than writing my experimental compiler from scratch
it might be a better idea to take an existing one and hack it towards my
notion of 'the ideal multi-purpose language'. I had some experience
with QuakeC (a byte-code compiler for the game Quake) so I know what a
very simple compiler looks like, but things like parsing and handling of
composite data types and generation of x86 machine code are still beyond
my understanding.

I have taken a look at GCC source once and was startled to death.
Obviously it's a highly advanced, extendible and portable compiler and
this implies quite a lot of unnecessary complexity.

Is there something like 'The Art of Writing Compilers for Dummies?'? Is
there a simple C compiler somewhere for which source is available under
a GPL-compatible license?

Any help is appreciated.

BR,
Anton 'Tonik' Gavrilov
 
S

Seth Morecraft

Try Flex (LEX) (http://www.gnu.org/software/flex/) and Bison (YACC)
(http://www.gnu.org/software/bison/bison.html).
good luck!

Seth

Anton said:
Hi all,

I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long before
I reach break-even, but it doesn't hurt trying, does it?

I'm a C addict reluctant to switch to C++, partly because I can't fully
grok (and accept) the OOP paradigm. Nevertheless I like C++'s arguable
improvements such as overloading of functions and operators, references,
const types etc. I also miss Pascal's way-less-cryptic typedefs and
consistent array types.

I suppose that rather than writing my experimental compiler from scratch
it might be a better idea to take an existing one and hack it towards my
notion of 'the ideal multi-purpose language'. I had some experience
with QuakeC (a byte-code compiler for the game Quake) so I know what a
very simple compiler looks like, but things like parsing and handling of
composite data types and generation of x86 machine code are still beyond
my understanding.

I have taken a look at GCC source once and was startled to death.
Obviously it's a highly advanced, extendible and portable compiler and
this implies quite a lot of unnecessary complexity.

Is there something like 'The Art of Writing Compilers for Dummies?'? Is
there a simple C compiler somewhere for which source is available under
a GPL-compatible license?

Any help is appreciated.

BR,
Anton 'Tonik' Gavrilov

~ Let us linux ~
 
J

John Bode

Anton Gavrilov said:
Hi all,

I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long before
I reach break-even, but it doesn't hurt trying, does it?

I'm a C addict reluctant to switch to C++, partly because I can't fully
grok (and accept) the OOP paradigm. Nevertheless I like C++'s arguable
improvements such as overloading of functions and operators, references,
const types etc. I also miss Pascal's way-less-cryptic typedefs and
consistent array types.

I suppose that rather than writing my experimental compiler from scratch
it might be a better idea to take an existing one and hack it towards my
notion of 'the ideal multi-purpose language'. I had some experience
with QuakeC (a byte-code compiler for the game Quake) so I know what a
very simple compiler looks like, but things like parsing and handling of
composite data types and generation of x86 machine code are still beyond
my understanding.

I have taken a look at GCC source once and was startled to death.
Obviously it's a highly advanced, extendible and portable compiler and
this implies quite a lot of unnecessary complexity.

Is there something like 'The Art of Writing Compilers for Dummies?'? Is
there a simple C compiler somewhere for which source is available under
a GPL-compatible license?

Any help is appreciated.

BR,
Anton 'Tonik' Gavrilov


Niklaus Wirth has a small volume called "The Compiler Construction
Handbook". It's written for Oberon rather than C, but most of the
lessons should translate.

I admire you. I've started a half-dozen toy compiler projects and
always lost steam before getting too far.
 
B

Ben Pfaff

Anton Gavrilov said:
I seek your advice on where to start if I want to write a compiler for
a toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long
before I reach break-even, but it doesn't hurt trying, does it?

Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

For your convenience, the list below contains topics that are not
on-topic for comp.lang.c, and suggests newsgroups for you to
explore if you have questions about these topics. Please do
observe proper netiquette before posting to any of these
newsgroups. In particular, you should read the group's charter
and FAQ, if any. If those fail to answer your question then you
should browse through at least two weeks of recent articles to
make sure that your question has not already been answered.

* OS-specific questions, such as how to clear the screen,
access the network, list the files in a directory, or read
"piped" output from a subprocess. These questions should be
directed to OS-specific newsgroups, such as
comp.os.ms-windows.programmer.misc, comp.unix.programmer, or
comp.os.linux.development.apps.

* Compiler-specific questions. Ask about these in
compiler-specific newsgroups, such as gnu.gcc.help or
comp.os.ms-windows.programmer.misc. Questions about writing
compilers are appropriate in comp.compilers.

* Processor-specific questions, such as questions about
assembly and machine code. x86 questions are appropriate in
comp.lang.asm.x86, embedded system processor questions may
be appropriate in comp.arch.embedded.

* ABI-specific questions, such as how to interface assembly
code to C. These questions are both processor- and
OS-specific and should typically be asked in OS-specific
newsgroups.

* Algorithms, except questions about C implementations of
algorithms. "How do I implement algorithm X in C?" is not a
question about a C implementation of an algorithm, it is a
request for source code. Newsgroups comp.programming and
comp.theory may be appropriate.

* Making C interoperate with other languages. C has no
facilities for such interoperation. These questions should
be directed to system- or compiler-specific newsgroups. C++
has features for interoperating with C, so consider
comp.lang.c++ for such questions.

* The C standard, as opposed to standard C. Questions about
the C standard are best asked in comp.std.c.

* C++. Please do not post or cross-post questions about C++
to comp.lang.c. Ask C++ questions in C++ newsgroups, such
as comp.lang.c++ or comp.lang.c++.moderated.

news.groups.questions is a good place to ask about the appropriate
newsgroup for a given topic.
 
T

Thomas Matthews

Anton said:
Hi all,

I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long before
I reach break-even, but it doesn't hurt trying, does it? [snip]



Any help is appreciated.

BR,
Anton 'Tonik' Gavrilov
In my Compiler Design class, we first tackled a simple expression
parser, then a language lexer. After the lexer worked, we moved
on to emitting executable code for the compiler. The language
was the instructor's "D-" which a very simple subset of the C
language.

The course was one year long and dreaded by all of the Computer
Science students. Kind of our Final Project.

I don't wish the task on any single person.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
E

E. Robert Tisdale

Anton said:
I seek your advice on where to start
if I want to write a compiler for a toy C-like language
that I invented (or, rather, am in the process of inventing).
I know I'm crazy and the very idea is brain-damaged and all that.

No it isn't brain damaged. It's a good idea.
Every computer scientist does this in the course of University classes.

Check out Yet Another Compiler Compiler (YACC).
Use Google to look for book for books by Brinch Hansen.
Subscribe to a compilers newsgroup and ask them.
The comp.lang.c newsgroup is a good place
to get *bad* advice about compiler design.
 
M

Malcolm

Anton Gavrilov said:
I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing).
It's a lot harder to write a compiler than you might imagine. I recommend
starting with a BASIC interpreter. If you are serious, you could do worse
than purchase the dragon book (Aho, Sethi and Ullman, Compilers, principles
and tools).
 
M

Martin Dickopp

E. Robert Tisdale said:
The comp.lang.c newsgroup is a good place
to get *bad* advice about compiler design.

Actually, I think bad advice about compiler design would be
as off-topic as good advice about compiler design here.

Martin
 
N

nrk

E. Robert Tisdale said:
No it isn't brain damaged. It's a good idea.
Every computer scientist does this in the course of University classes.

Subscribe to a compilers newsgroup and ask them.
The comp.lang.c newsgroup is a good place
to get *bad* advice about compiler design.

ITYM clc is the *wrong* place to ask for any kind of advice about compiler
design.

To OP:
comp.compilers would be a better place to ask, but do read their FAQ (if
they have one) before you do.

<OT> You might want to look around a bit for options other than C here.
JavaCC and ANTLR have worked pretty well for me in the past </OT>

-nrk.
 
A

Anton Gavrilov

Malcolm said:
It's a lot harder to write a compiler than you might imagine. I recommend
starting with a BASIC interpreter.
BASIC? Yeah... sure... should I write the interpreter itself in BASIC, too?
 
A

Anton Gavrilov

Ben said:
Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

Ok, ok, I beg pardon of whoever I disturbed. I'm kinda new to
newsgroups and I couldn't find a better place to ask than comp.lang.c.
Now I see it should've been comp.compilers.
news.groups.questions is a good place to ask about the appropriate
newsgroup for a given topic.
Yeah, news.group.questions should be a good idea of a newsgroup; it
can't, however, completely eliminate off-topic questions, for instance,
"Where's that newsgroup where you can ask for an appropriate newsgroup
for a given topic?" :)

BR,
Anton
 
J

Joona I Palaste

Anton Gavrilov said:
BASIC? Yeah... sure... should I write the interpreter itself in BASIC, too?

You can, but you don't have to. Writing a BASIC interpreter in any
language is usually easier than writing a C compiler in that language.
Try writing an interpreter for a simple version of BASIC (perhaps one
from a 1980's home computer, minus all the graphics and sound) in ANSI
C.
 
C

Christian Bau

Anton Gavrilov said:
Hi all,

I seek your advice on where to start if I want to write a compiler for a
toy C-like language I invented (or, rather, am in the process of
inventing). Yes, yes, I know I'm crazy and the very idea is
brain-damaged and all that. Chances are I will lose interest long before
I reach break-even, but it doesn't hurt trying, does it?

Fraser/Hanson "A retargetable C Compiler" is quite easy. Complete source
code for a C compiler to get you started.
 
M

Malcolm

Anton Gavrilov said:
Yeah, news.group.questions should be a good idea of a newsgroup; it
can't, however, completely eliminate off-topic questions, for instance,
"Where's that newsgroup where you can ask for an appropriate
newsgroup for a given topic?" :)
That's why we have a FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top