Creating a small programming language for beginners
Asked Answered
I

7

7

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?.

Inodorous answered 12/7, 2013 at 7:56 Comment(4)
Why don't you create a [Meta-Circular Evaluator][1]? A meta-circular is an evaluator written in the language that it evaluates. [1]:en.wikipedia.org/wiki/Meta-circular_evaluatorEuryale
Here is comprehensive up to date answer for your question using C#: codeproject.com/Articles/272494/…Irena
Gabe, here is a very good answer for a very similar question.... programmers.stackexchange.com/questions/84278/…. also check....ericlippert.com i found it very useful...hope it will help you.Winonawinonah
you may check this vnev.me/implementing-a-programming-language-in-c-part-1Swirl
A
5

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).

Arce answered 12/7, 2013 at 15:18 Comment(0)
D
6

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".

Diluent answered 13/7, 2013 at 16:57 Comment(3)
Ira, that paper is paywalled. ($15 for a pdf seems a bit steep to me, but I've seen much worse.)Henze
I believe there's a copy at the tutorial web site.Diluent
I believe you believe that but I can't find it. So either me eyes or your belief structure need to be re-examined. (And I know the weakness of my eyes; maybe they just need some help.)Henze
A
5

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).

Arce answered 12/7, 2013 at 15:18 Comment(0)
R
1

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.

Reinhart answered 12/7, 2013 at 8:9 Comment(0)
E
1

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.

Encomiastic answered 27/7, 2013 at 18:12 Comment(0)
T
1

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.

Timeconsuming answered 23/7, 2019 at 10:31 Comment(1)
And i am just 15 . Its is not that hard .Timeconsuming
W
0

I am the creator of the web based programming language Jetsam. To create your own programming language first you need to go through all kinds of different programming languages, such as C to C++, Java, QML, HTML, JavaScript, Ruby, Python, and some others. I created it using web and that language is just working on the web. Mess around all these languages and go to their base. You may have a look at book "How to create your own freaking awesome programming language". There is a website which also teaches you how to create a programming language. Hope this helps you.

Welldefined answered 30/8, 2014 at 9:56 Comment(0)
E
0

The most easy option would probably be to create a program in Java that reads text files and if it finds something it would start writing a .java file and write what it basically means in Java. So for example my a file called main.boss could be:

write Hello world
write Bye!

And my java program would cycle through it and detect the word write and then go one space after it and read the text until the end of the line. Meaning it would get "Hello world" and "Bye!". Because says "write" it would then write a main.java file with this in it:

public class Main
{
System.out.println("Hello world");
System.out.println("Bye!");
}

Then you could have a .bat file that is executed by your java program and it would set classpath, compile the file, and turn it into a .jar file. It could possibly bundle a program like Launch4J with it so it could have an option to turn the .jar into a .exe.

Equal answered 27/6, 2015 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.