Archived

This topic is now archived and is closed to further replies.

jumpjumpjump

making a compiler

Recommended Posts

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. Boo!! I know all.

Share this post


Link to post
Share on other sites
Depending on how complex your scripting language is you may want to start out by mapping it out in EBNF. Then simply work out the EBNF in VB.

For example (not sure about the syntax):

digit := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
// A Digit is either a 0, 1, 2, ... 8, or 9

number := digit[digit];
// A Number is one or more digits

expression := number '+' expression | number '-' expression;


This maps out a syntax for adding or subtracting 2 or more numbers

"2+34-2" or "5-2+44-342+1" are both valid strings

this would be implemented by doing creating a couple of functions :
IsDigit(), IsNumber(), and IsExpression()
Which would determine if the input string follows the EBNF guidelines.

Hope this helps.


[edited by - enfekted on January 23, 2004 8:20:34 PM]

Share this post


Link to post
Share on other sites
Writing compilers may not be a very suitable task for a beginner (as I will assume you are, posting in the For Beginners forum ...); it''s a large and complicated topic. In any case, the Dragon Book is the canonical book on the subject. Some people have also spoken favourably about Programming Language Processors in Java, which my university''s compiler course uses as a textbook (though as it turned out, the professor got most of his examples from the Dragon Book).

Share this post


Link to post
Share on other sites
quote:
Original post by jumpjumpjump
What is the best assembly language to use if i decided to try that?


The assembly language that your processor uses is not only best, but the only one that would work. Chances are thats x86, which would be Intel-like processors (Intel, AMD, etc.). For the sake of correctness, not all Intel processors are x86 but chances are you don't have one that isn't.

Edit: If you are a beginner, which as mentioned seems to be the case because of your posting in the For Beginners forum, a scripting language may not be a good project for you and assembly language may be a problem for you, too.

[edited by - Aerolithe on January 25, 2004 2:23:35 AM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 [url]http://www.cs.luther.edu/~leekent/tutorials/ll1.html[/url]

***
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanx for ur quick responses!
What I am quite confused now is whether it is possible to write a script engine without generating the intermediate code, machine code and creating my own virtual machine. I m now finding it''s quite hard to deal with the condition statement, while loop statement without using any assembly-like language and creating my own VM. I just want to know whether it''s "theoretically" possible.

Share this post


Link to post
Share on other sites
quote:
Original post by Ekas78
This is from a class i''m taking right now.

http://www.masda.vxu.se/multimedia/datalogi/compiler/index.htm


Hi, Ekas,
are u asked to write a VM by ur own? do u have any assignment for this course?

Share this post


Link to post
Share on other sites