Advertisement

How to make a compiler

Started by October 11, 2000 03:40 AM
13 comments, last by BuruhBinaan 24 years, 3 months ago
I just need to make a program in c or c++ That can read a source code in pascal And print out the error Much like a compiler I know you must read the source code Line by line and token by token How can I do this ?? Please help meeeeee…..
Get a book on compiler writing - one I can recommend is Modern Compiler Implementation in C, I believe it''s written by Appel.

This is really not an easy project though - you could take a month or more writing something like this.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
Excuse me, but I can´t understand why don´t you use a compiler to do this?
I''m currently writting a full compiler, to compile C style code to my own virtual machines byte code. Anyway, you need to reserch somthing like this, but as a rough idea, from what I can pick up-

-Load the file into a linked list of lines.
-As it''s only pascal, you should be able to do it in a single pass.
-In this pass you need to check everything, the main check should be to keep a database of tokens found, with properties, to check that any varibles/functions etc used are valid.
-You also need to check the syntax - find pascals BNF''s - they should be on the internet somewhere, or in a manual,(If you don''t know what BNF is, learn - should be in any A-Level computing book, or try in the RFC''s at faqs.org) then use a stack to check that you get an expresion when you want an expresion etc.
-As you are only checking the code, that should be enough.

You also need to check maths statements - revise binary trees and polish notation for that one.
-Lethe
I would like to see reverse polish maths used, they are so much easier to calculate for. Everything gets split into a binary tree and parsed node by node... Really easy

-Chris Bennett of Dwarfsoft - Site:"The Philosophers' Stone of Programming Alchemy" - IOL
The future of RPGs - Thanks to all the goblins over in our little Game Design Corner niche
          
I''ve heard that gcc uses for it''s first two steps (I think they were parsing and interpreteing or something like that) other open source packets called Flex (a scanner generator, that means a program which reads the code and cuts into pieces, roughly)
and YACC (Yet another compiler compiler, this program also parses
the code, don''t know any details here)

Unfortunately, I don''t know where to get them on the net.
Advertisement
Lookup Lex and Yacc (or TPLex and TPYacc). These programs generate source code for lexical analyzers and parsers. There are very good manuals and tutorials on the programs and they have been ported to a lot of different platforms AND they are "free".

By the way they are included in almost any unix distribution.

A delphi 3 version of TP Lex and Yacc is available at:

http://www.win.tue.nl/~wsinswan/softwaretools/tply41.tue.zip
(it will be probably be removed from there SOON)

Edited by - Will-o-the-wisp on October 11, 2000 8:30:02 AM
The sourcecode to lex/flex and yacc/bison are freely available, and quite easily recompiled - so there are ports of them available on just about every platform. Try a yahoo search on "flex win32" and you''ll probably come up with Wilbur Street''s port of flex and bison, and they work pretty well.

( I''ve modified them slightly myself to work better within MFC applications )



People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Jack Crenshaw''s "Let''s make a compiler!" tutorials. Great if you''re just starting out.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Well Muzzafarath where I can find those tutorials.
What´s your opinion about making own script language.

This topic is closed to new replies.

Advertisement