View Full Version : Building a compiler in What language ?
kalium
10-25-2005, 10:51 AM
Hi,
In the not too distant future I will be undertaking a compiler course where we have to take a fictional language and create a compiler for it.
Although we are able to do it in any language we want, most people use C or YACC/Bison. I was thinking of doing it in LISP. Would this be a suitable langauge ? I dont know much about LISP (except the bare basics), but I think that it would be interesting to learn, and this may be a good opportunity for learning/using it.
Can anyone reccomend any other languages which would take well to this kind of project..I would love to hear your opinions on this.
Ray
Ch
TheShaner
10-25-2005, 03:42 PM
C, C++, and Java can all be used to build a compiler, along with YACC of course. I'm sure there are others out there, but I've never used any of them.
You will not be able to make a compiler in LISP (not that I'm aware of). LISP is primarily used in the artificial intelligence field. LISP stands for LISt Processor. You create lists and definitions of things you know and you build upon each thing you know, thus gaining a certain knowledge about whatever you're processing. LISP uses the "accumulating data and rules" approach to AI, rather than the neural network approach where the program doesn't know anything and attempts to learn it. So LISP is pretty much out of the question, hehe.
-Shane
kalium
10-25-2005, 04:06 PM
C, C++, and Java can all be used to build a compiler, along with YACC of course. I'm sure there are others out there, but I've never used any of them.
You will not be able to make a compiler in LISP (not that I'm aware of). LISP is primarily used in the artificial intelligence field. LISP stands for LISt Processor. You create lists and definitions of things you know and you build upon each thing you know, thus gaining a certain knowledge about whatever you're processing. LISP uses the "accumulating data and rules" approach to AI, rather than the neural network approach where the program doesn't know anything and attempts to learn it. So LISP is pretty much out of the question, hehe.
-Shane
Err...are you sure about that. LISP IS used in the AI field AND whole bunch of other areas (EMACS, GIMP anyone ?) . First of all, you can do pretty much anything with any language, the difficulty is the varying factor. Why does the model you proposed rule out parsing languages ?
Ray
liorean
10-25-2005, 04:11 PM
TheShaner: You have no idea what you're talking about.
LISP is one of the absolutely best languages for writing a compiler in. It's in fact one of the things that Scheme excel at, which is one of the things that makes Scheme popular for programming theory courses with many universities. At least the ease-of-writing part of it. However, the LISP languages are generally slower than C/C++, especially the free implementations.
There is also the question about what you want to compile into too. LISPs are best for interpreters run on top of LISP, but if you want to compile into machine code it's possible, just not as easy. Java is an excellent choice if you want to compile into Java bytecode, or run it as an interpreter. It's got the same problems compiling to machine code as LISP has: It can't bootstrap the compiler. C/C++ is good for compilers into machine code because it can compile into it's own native structures.
TheShaner
10-25-2005, 04:40 PM
Err...are you sure about that. LISP IS used in the AI field AND whole bunch of other areas (EMACS, GIMP anyone ?) . First of all, you can do pretty much anything with any language, the difficulty is the varying factor. Why does the model you proposed rule out parsing languages ?
Ray
Well, I said that I wasn't aware of LISP being able to be used to build a compiler. I didn't state it as fact. I also stated that it's PRIMARILY used for AI, but didn't state it was the only thing. Second, I did not propose any "model" and rule out parsing languages. I just stated the 3 languages I've seen used for creating a compiler. All 3 are well structured and have all the necessary capabilities to build a compiler. I even stated that they are more languages out there that can do it, but I just had never used any others. You can also you LEX, which is one I forgot about and frequently used to develop compilers. LEX is a parsing langauge. I did mention YACC since you had mentioned it, which is another parsing language. So I never ruled out parsing languages. Parsing languages are very capable for building compilers.
-Shane
TheShaner
10-25-2005, 04:47 PM
TheShaner: You have no idea what you're talking about.
Why thanks for pointing that out. Yes, I don't know much about LISP, but I was stating that I had never run into using LISP for building a compiler. Sorry for my ignorance. But what I did state about LISP was true, so I do know a little :p You even further backed up my claims for C/C++ and Java, so I'm not blowing out hot air about which languages are used for buiilding compilers. Yes, you know more than me about it all, but I wasn't completely off basis. Thanks.
-Shane
hyperbole
10-25-2005, 06:50 PM
Actually you can write a compiler in any language. It is best to choose a language you are familiar and comfortable with. You DON'T want to be trying to learn the language you're writing in while you are trying to learn how to write the compiler.
If your objective is to learn as much as you can about compilers and how they work, write the compiler in C (not C++). You will be forced to create a lot of the tools you need to write the compiler and will learn about the inner working of these tools as you write your compiler.
It seems to me that writing a compiler in Lex/YACC is not really learing how to write compilers, but is just learning how to use the syntax of Lex and YACC. If you just want to complete the project without learning a lot about how a compiler works, this would be a good choice.
I once wrote a compiler in SNOBOL. It's a wonderful language for writing language parsers because all the lexical analysis, parsing, symbol table storage/retreval and even the syntax analysis can be embodied in the SNOBOL code. Again, not a language I would recommend if your goal is to learn how a compiler works.
kalium
10-26-2005, 04:04 AM
liorean, What you said pretty much sums up what I too had concluded about the use of LISP as a compiler. Excellent for parsing language but prehaps not the quickest. Luckliy, Speed is of little concern to me though.
HyperBol, I have written small compilers before in C, so although I am no expert on construction of compilers, I do have somewhat of an idea. I guess ideally I would like to get a better understanding of compilers and their construction, AND learn a new language! I also agree with your comment about yacc. Maybe your right though, probably not the best thing to use a language I am not familiar with.
The Shaner, I know you meant well, But I was specificly seeking the advice of those more learnered and familiar with the construction of compilers and/or the use of LISP. I have written a small parser in another functional language before, so I know that the use of the List as the sole Data Type was not a hinderance to the project.
Ray
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.