Tutorials/How to program your own programming language
From ThorstensHome
(Redirected from How to program your own programming language)
OK, although PHP already exists, we want to program our own programming language. This is an example for it, we call our programming language stark. This article describes all about it. Contents |
Ideas
One idea of stark is that you can find every function definition very easy. For example, to find the definition of foo, search for "def foo".Syntax
The following syntax shall be possible with our programming language:write ("hello world");
a:=42;
b:=a;
def function1()
{
global b;
b:=b-1;
if (b>40) function1();
}
function1();
Implement it
Ok, first, we need a lexical analyzer. This will tell us what is a token (e.g. a command or the = sign), what a string literal and so on. This will be used by a compiler compiler to assign its parameters to every token (e.g. the command "print" can be followed by a number or by a string literal. This compiler compiler will build a command tree out of the source code, e.g. the above code will get:
type=command
name=write
type=string
content="hello world"
type=assignment
assignee=a
value=42
type=assignment
assignee=b
type=compute
content=a
type=function definition
name=function1
type=global
name=b
type=assignment
assignee=b
type=compute
content=b-1
type=if (requires three tokens, lvalue, rvalue and what-if-true)
operator="<"
type=compute
content=b
type=compute
content=40
type=functioncall
content=function1
Your lessons
- program a programming language - lesson 1
- Program a programming language - lesson 2
- Program a programming language - lesson 3
- Program a programming language - lesson 4
- Program a programming language - lesson 5
- Program a programming language - lesson 6
See also
- basick-1 - another approach, php based
- http://www.telenovela-world.com/~spade/linux/howto/Lex-YACC-HOWTO-4.html
- http://www.cs.ucr.edu/~lgao/teaching/bison.html