making a compiler

Started by
27 comments, last by jumpjumpjump 20 years, 1 month ago
As long as you have a good command of a programming language, a scripting language shouldn''t be an unreasonable project.

A lot of what you need to know to understand formal language processing/compiler construction is pretty specific to that problem. When I took my first compiler class, I felt like I was learning the topic from scratch. It was important to know a programming language so I could actually put the concepts into code, and I needed a decent understanding of data structures (you should know how to use linked lists and hash tables in order to implement the compiler''s internals).

Understanding assembly, at least at the conceptual level, becomes necessary for implementing a code generator. I was able to pick that up along the way, but if you want an interpreted scripting language that''s not going to be necessary.

As someone already mentioned, the Dragon Book (Compilers: Principles, Techniques, and Tools) by Aho, Sethi and Ullman, has been the standard text for decades (some may be shocked to learn that a new edition is scheduled for release!) It may be a little bit deep and heavy duty if you''re more interested in results than being a CS theory nerd. The sections on code optimization are also out of date; Their data-flow analysis stuff has long been superceded by static single assingment form.

If you can spare a few bucks, Game Scripting Mastery is where it''s at. Even with some pretty extensive compiler development experience, I''ve felt like the resources available on the net are inadequate. So far I am finding GSM very useful, readable and to the point, and the specific emphasis on integration with games is very helpful.

The examples are in C, so as long as you can understand them, you should be able to get something simple and useful working relatively quickly, and then take it as far as you need or want to.

[Edited by - The_Incubator on June 14, 2006 2:52:25 PM]
Advertisement
I created a scripting language as a school project in Java last year - was meant for a game where you can program robots to fight other robots but I must admit I never got around to finishing the game itself after my exam. Its output language was "machine code" for a virtual machine I wrote - simple stack machine with static memory allocation.

I also recommend the dragon book - It was the one we used and although it can be hard to read it's worth it.

Aim for an LL-grammar and go for recursive descent unless you want to use a compiler-compiler like yacc. Lexical analysis is quite easy to do by hand - but of course might require a lot of typing for a large language.

ps- try this link http://www.cs.luther.edu/~leekent/tutorials/ll1.html

***
For Java games and Java related resources, go to http://www.javaengines.dk
***

[edited by - blackone on January 26, 2004 9:52:20 AM]

[edited by - blackone on January 26, 2004 9:53:02 AM]

Developer journal: Multiplayer RPG dev diary

quote:Original post by jumpjumpjump
I am working on making my own scripting language thing and I want to know if there is any way to write a compiler for it in VB.NET, is there and what are some good resources on making compilers? I have a few bookmarks on flipcode. i also have some C++ knowledge, if that helps.


Are you the guy Nervo talked about? The one making his own 3D engine and wanting to write his on ASM based scripting language? If so, and even if not, you are taking on a too difficult project for a beginner.

It''s like starting out with an MMORPG in 3D, while learning Visual Basic.

Toolmaker




-Earth is 98% full. Please delete anybody you can.


I created a 16-bit assembler for a practice project (trying to teach myself C) when I was 14. It''s a pretty good newbie project.

.bas

[sPiKie] mmorpg isnt hard in vb
.basprintf ( "And for the %dth time, I'm not American!", ++lIdiots );My homepage
yehyeh... brag on, a good practice for a 14 years old...
why not really try help him instead...

Unfortunately I have no knowledge of any scripting language at all. But this is how I have thought about implementing my scripting language for creating scripts for buttons etc...

The goal is to let the user of my map/monster/spell/etc... editor to write scripts for different objects.

Though, I wasn''t thinking of generating asm code. This is maybe more like an interpretor...

This is very simple...

Have a class "Script". This class has a static map of some sort. This map includes a string and a variable of some sort. (the string is the variables name). This map will represent the variables that are available to the user that will be scripting. (If he types the variablename, that will later be parsed to the variable that is mapped to...)

Then you need some guidlines for what can be done in your scripting language. eg, If you can do loops, if-statements, etc...

Then the "Script" class will have a "Parser" that will read the script file as a string and whenever it "reads" something sinilar to your guidelines, it does whatever that is mapped to... And if it finds a "variable" string it replaces it with the mapped variable...

And in your real application, you might want to "expose" the variables that the "Script" engine want''s to use. So you will need some function that maps a string and that variable to the map in the "Script" class...

And of course in the guideline you will need to include basic types like int, duoble etc...

Well, I''m sorry that this is so loosely explained... but since I have never done a scripting engine I can only "talk" about it

Anyway, that is how i would do my script/interpretor for a my game.
I am going to start back on this as soon as i finish my current game. I got a lot done on it today. All i have left is get health and stuff put in and bam done!! Then I will start back on parser.
quote:Original post by Toolmaker
Are you the guy Nervo talked about? The one making his own 3D engine and wanting to write his on ASM based scripting language? If so, and even if not, you are taking on a too difficult project for a beginner.

I mentioned that person, and he''s not a gamedev person. Also, he''d never ask for help.
quote:Original post by kordova
I mentioned that person, and he''s not a gamedev person. Also, he''d never ask for help.


Ok, so I got the person who said wrong . It sounded so familiar to me

Toolmaker




-Earth is 98% full. Please delete anybody you can.

I haven''t tried writing a compiler but I took CS 467 (language theory, basically) and let''s see if I can try to apply it.

In a statement like 1 - ((4 + 6) * 7) you need to break it down into an expression tree. The tree for that example is:
        -       / \      1   *         / \        +   7       / \      4   6

Get it? Each operator node (+,-,*, or /) performs an operation on its two children. A child can be a number or another operation. The root node is a subtraction operator, subtracting the right child from the left. The left is 1, the right is another expression -- the multiple of 7 and yet another expression, which itself is the sum of 4 and 6.

This is interpreted recursively. That''s the important point -- recursion is necessary. It''s like:

Result = Op( 1, ''-'', Op( Op( 4, ''+'', 6), ''*'', 7 ) )

So anyway you have to take a line and try to create an expression tree -- then it''s intuitive to compile it or run it or whatever you''re doing. Creating the tree is a little complicated and involves a "grammar", but to explain this, I''d probably first need to write my own interpreter to make sure I understand it. Needless to say, it can be tricky.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
just wondering if u anyone has managed to finishing a complete script engine (at least with if statement, while loop, and function defn and call features)
i m currently using ParserGenerator which is basically lex+yacc, i got stuck at the if statement and while loop part. i think there must be some problems with my design. could anyone cover sth. detail about it?

p.s: my script engine can currently store and read variables and do arithmatic operations, also type checking.

This topic is closed to new replies.

Advertisement