Creating a small programming language for beginners

10,428

Solution 1

My take on this is that the simpler approach is not to mess with the hardcore tools intended to automate creation of lexers and parsers but rather to embark on creating a simple interpreter of a reasonably simple syntax, for which the parser could be written by hand — involving a simple state machine.

I'd say an almost ideal interpreted language for the task is Tcl, with the reasons for this being:

  • Super-simple syntax (almost absense of it): the syntax mostly defines grouping of characters into words.

    A set of words parsed from a source line of the script being processed is then interpreted this way: the first word is the name of a command and the rest of them are positional arguments to that command. Get the quick overview of it on Wikipedia.

  • Everything is a string as interpretation of the arguments is in the hands of the commands processing them.

  • It's possible to gradually make the interpreter more "beefy". For instance, for a start one might omit implementing variable substitutions (Tcl < 2.0 did not have them) and all the advanced features (both syntactic and semantic, like namespaces). Likewise, it's possible to start with no commands or almost no commands available to the scripts of such a toy interpreter. Then they might be gradually added.

You can get a quick sense of what Tcl is by working through its tutorial. More documentation is there. You can get Tcl to play with it there.

You can ask for help on the #tcl IRC channel on irc.freenode.net and on the comp.lang.tcl newsgroup (available via Google Groups).

Solution 2

You might consider learning to build a compiler from a fabulous 1964 (yes, you read that right) paper META II: A Syntax-Oriented Compiler Writing Language on how to build "meta compilers".

This paper contains, in 10 pages, a compiler writing philosophy, a definition of a virtual compiler instruction set that is easy to implement, a compiler-compiler, and an example compiler built using the compiler-compiler.

I learned initially how to build compilers from this paper back in 1970 odd. It is astonishing how clever and conceptually simple it is.

If there was a paper I'd make every computer science student read, this would be it.

You can get the paper, see a tutorial and an implementation of MetaII in JavaScript here. The guy behind the tutorial is Dr. James Neighbors, source of the term "domain analysis".

Solution 3

I created a very small language in yacc for a lecture in one of the higher semesters in college. It is a very complicated thing, I wouldn't recommend to do it just like that.

You could create a library, for example in C#, defining some simple instructions like "message(string)" and mapping them to more complex things like opening a window and displaying the message. It would not be an independent language, but you could define your own basic instructions, even if they just wrap basic instructions of the used language.

Solution 4

Defining your own programming language is an interesting challenge and test of creativity. A question you should consider is the extent to which you want the "language" to operate as something that completely analyzes a program before trying to execute anything, something that interprets commands in context-free fashion as it's going along, or something like Forth or PostScript in which commands are processed as they come from an input stream, but which has the ability to append input to a list, or to read commands from a list as though they were coming from an input stream, this allowing one to effectively define procedures by reading all the instructions into a list, and then later executing all the instructions in the list.

Another related question is whether you want your language to be practical for large-scale application development. If an application won't need more than 26 variables, for example, using a fixed mapping of the letters a-z to 26 variables may be simpler than trying to implement a symbol table or allocate space for variables. Local variables and parameter passing can be somewhat complicated; having all variable be in one global scope and requiring that code pass parameters using those global variables will simplify the language design, but writing code in such a language can easily become unmanageable especially if one wants to avoid defining an excessive number of single-use variables [e.g. because there are only 26 variables available].

I've used a fair number of domain-specific languages, including some I've designed; I've liked some and disliked others. The most important thing in designing a language is to decide one's objectives. That's not to suggest that defining the objectives will make everything else fall into place, but if you don't have a sense of your objectives you'll have no way of knowing which design ideas will let you meet those objectives and should be followed, and which design ideas would be incompatible with your objectives and should be abandoned.

Solution 5

I made a small programming language that makes the user program a pattern to control the lights of the keyboard (caps lock,etc) . It plays a light show . I based it on VBS.

Share:
10,428
Gabe
Author by

Gabe

Updated on June 04, 2022

Comments

  • Gabe
    Gabe almost 2 years

    I would like to create my own programming language. Maybe not exactly a programming language from scratch but maybe base it on another language.

    I've heard of Yacc. So, I installed Flex and Bison. But I do not understand how to make a compiler with it. I have made the Hello world project in it, but how would I make a compiler in it?

    Are there any easy ways of creating a small programming language, I have heard of translating a language as in taking, e.g., Write() and making the computer understand it as Print().

    Is this be possible?.