Making a Compiler?

Programming, for all ages and all languages.
Berserk

Making a Compiler?

Post by Berserk »

Hey,

I was just wondering, what would i have to do to make a new programming language? (A Compiler)

I'm thinking of Making One, ...Later...(Not Soon)

But i just wanted to know what i'm in for, and also doesn anybody know where i can get some good tutorials??

Cya 8)
sonneveld

Re:Making a Compiler?

Post by sonneveld »

Two tools that will probably be useful are lex and yacc (or flex/bison which are the gnu equivalants)

lex - splits text into tokens.. like strings, and reserved words
yacc - takes the tokens and makes sure the source is syntactically correct. ie a function is

symbol '(' arg ', ' arg ')' '{' .... stuff like that.

I couldn't find any useful tutorials on the net that deal with lex/yacc and decent compilers. There are some decent books on the subject though. The "dragon" book was pretty good and I found that in my local uni science library.

- Nick
Tom

Re:Making a Compiler?

Post by Tom »

There's no such thing as a good compiler tutorial ;)

Buy "Compilers And Interpreters".

I need to ask you 2 questions:
1. Why make a new language?
2. Why do this while just learning AND makeing a OS?
Schol-R-LEA

Re:Making a Compiler?

Post by Schol-R-LEA »

The good news is that there are loads of compiler design texts out there, and any college library should have at least a half dozen of them. The bad news is, most of them are outrageously expensive to buy, and finding the one which makes the most sense to you can take some time.

The standard University text on compiler design is Compilers: Principles, Techniques, and Tools (Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman;ISBN: 0-201-10088-6; Addison Wesley; 1986; Cloth, 500 pp). However, it is a rather dense book, and heavy on mathematics and theory; it is also over 16 years old, and is showing its age.

If you can afford it, I would use it as a reference in conjunction with a more nuts-and-bolts text such as Compiler Design in C (Allan Holub; Prentice Hall; ASIN: 0131550454; 2nd edition, March 1994; hardcover, 924 pp) , Crafting a Compiler with C (Charles Fischer, Richard LeBlanc;Addison-Wesley; ISBN: 0805321667; January 1991; cloth, 812pp), or the more recent Modern Compiler Implementation series by Andrew Appel (with versions using examples in C, Java and ML; the C book is not quite as good as the others, as it is essentially a port from the Java, which in turn was based on the original ML version). HTH.
Berserk

Re:Making a Compiler?

Post by Berserk »

Hey Hey,

hold on...i didn't say that i was going to jump in and start making a compiler while making an OS, NO WAY.]

I was just asking, what would it take to make one??
Schol-R-LEA

Re:Making a Compiler?

Post by Schol-R-LEA »

Berserk wrote: Hey Hey,

hold on...i didn't say that i was going to jump in and start making a compiler while making an OS, NO WAY.]

I was just asking, what would it take to make one??
Here are what I would consider the rock-bottom requirements for compiler programming:
- Time
- Patience
- a lot of programming skill in the implementation language
- A good understanding of the assembly language (or p-code) that you are compiling into
- The ability to read and design BNF grammars and/or 'railroad' style syntax diagrams
- A clear idea of what language you want to implement, both the syntax and semantics
- A basic understanding of the four main stages of a compiler: lexical analysis, parsing, semantic analysis, and code generation.

It is entirely possible to write a passable compiler with just these things, using a hand-coded recursive-decent parser; see the Small-C page for a[n in]famous example. The results are usually a bit crude, and far from optimal, but they do work.

Tools that could be useful include:
- A byte-code interpreter (which makes an easier target than a real processor, and is usually a bit more portable; the Java Virtual Machine and UCSD p-System are two classic examples of this)
- a lexical analyser generator such as LEX or Flex
- a parser generator such as YACC, Bison, CUP, ANTLR, etc. A google search on 'parser generator' will turn up dozens; you should have no trouble finding one you like.

That's about it; the hard part is learning how to do it. Expect to do a lot of study on it, and a lot of practice at toy languages and toy compiilers, before you can do anything serious. However, like many other things, it is worth the effort, in that it will help you understand other aspects of programming better.

As difficult as compiler programming is, language design is an order of magnitude harder - but that hasn't stopped people like Chuck Moore (the designer of FORTH) from coming up with interesting ones despite a total lack of experience. If you think you really know how to build a better mousetrap, don't feel shy about trying it out. YMMV.

HTH.
Tom

Re:Making a Compiler?

Post by Tom »

I will have to make a interpreter for FritzOS's config files when it's finished....so I bought a book called "Writing Compilers and Interpreters".

It is good...but sadly it doesnt' show you how to translate its's pascal compiler's code to direct binary :-[....instead it translates to asm.
K.J.

Re:Making a Compiler?

Post by K.J. »

Yeah, I found that out too about that book. But actually, most C compilers convert code to ASM, then assemble that code to binary.

K.J.
Tom

Re:Making a Compiler?

Post by Tom »

yea...but that book was about a pascal compiler....
elias

Re:Making a Compiler?

Post by elias »

hwo would i write an assembler? ive been thinking of writing one for the TI-83 plus, but i need to know how. i really have no idea how. can anyone help me?
Tom

Re:Making a Compiler?

Post by Tom »

Simple Idea the might work...

1. parse the syntax and generate a object file...
2. linker reads the object file for your! ;D

OR...if there is no linker for that type of computer :-\

...
2. Make the linker read the object file...
3. generate binary ( not easy )
elias

Re:Making a Compiler?

Post by elias »

its a graphing calculator. i use it in school, and when bored in amth calss i thoguth it might be nice to do some assembly. im afraid i will have to generate binary, cuz binary is generated somewhere along the line. you know how its done?
Tom

Re:Making a Compiler?

Post by Tom »

I don't even know how to make bins for the INTEL arcutecture...and the type of bins you need to make for that computers's arcutecture must be really different.....
Schol-R-LEA

TI-83+ assembly language

Post by Schol-R-LEA »

I don't know much about the calculator in question, but a quick google search turns up, ticalc.org, which looks to be a resource for this sort of thing. I gather that the CPU is a Z80A, a venerable and well-loved chip from the mid-1970s. They also have an assembler and loader available - which doesn't mean you can't write your own if you wish, esp. since the Z80 is a lot easier to write one for than the x86 is.

As for assemblers, they are similar to compilers but much simpler. The standard assembly language, excluding macro preprocessor, runs in two passes over the code:

Pass 1: Read in every line of code, calculating the size of every instruction and data directive, keep a count of the size in bytes, and store identifier and address of each label you encounter in the symbol table.

Pass 2: read in each instruction again, replacing the label identifiers with their addresses as they appear in the instruction arguments. emit the corresponding opcode and/or data in binary for each instruction or directive.

This is just a very basic outline, and doesn't address issues of binary format, error handling, etc. But it is the classic approach to assembler design. There are a few alternative approaches, but this is the classic one taught and used since the earliest days of computing. HTH.
elias

Re:Making a Compiler?

Post by elias »

it is a Z80, and Ticalc.org dosnt help. theres an assembler for teh PC, and you can transfer your programs to the calculator form teh PC, btu i wanted to write an assembler to work on the calc. so i need a built in table of every possibnle isntruciton and its opcode, take in a text file, and replace every opcode with its binmary value, and output to a file?
Post Reply