By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Please help a n00b write a compiler :-)

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Anton Gavrilov wrote:
Any help is appreciated.


http://google.com/groups?group=comp.compilers

Nov 14 '05 #2

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

Seth

Anton Gavrilov wrote:
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 ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #3

P: n/a
On Thu, 18 Dec 2003 13:15:38 GMT, Anton Gavrilov <sp*****@fromru.com>
wrote:
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


Try this:
http://compilers.iecc.com/crenshaw/

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #4

P: n/a
Anton Gavrilov <sp*****@fromru.com> wrote in message news:<_L******************@news1.nokia.com>...
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.
Nov 14 '05 #5

P: n/a
Anton Gavrilov <sp*****@fromru.com> writes:
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.
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
Nov 14 '05 #6

P: n/a
Anton Gavrilov wrote:
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

Nov 14 '05 #7

P: n/a
Anton Gavrilov wrote:
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.

Nov 14 '05 #8

P: n/a

"Anton Gavrilov" <sp*****@fromru.com> wrote in message

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).
Nov 14 '05 #9

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
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
Nov 14 '05 #10

P: n/a
nrk
E. Robert Tisdale wrote:
Anton Gavrilov wrote:
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.


<snip>
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.
Nov 14 '05 #11

P: n/a
Malcolm wrote:
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?
Nov 14 '05 #12

P: n/a
Ben Pfaff wrote:
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
Nov 14 '05 #13

P: n/a
Anton Gavrilov <sp*****@fromru.com> scribbled the following:
Malcolm wrote:
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?


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
Nov 14 '05 #14

P: n/a
In article <_L******************@news1.nokia.com>,
Anton Gavrilov <sp*****@fromru.com> wrote:
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.
Nov 14 '05 #15

P: n/a

"Anton Gavrilov" <sp*****@fromru.com> wrote in message
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.
Nov 14 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.