Let’s start building our own Programming language.(Compiler of your own)

I’m here again after few days of time. 😊 Today, i’m going to teach you something very interesting. Everyone uses programming languages which are developed by someone. Let’s take C language, what C does, it enables programmers to implement various kind of applications using Cs powerful features. I’m trying to explain you this as simple as possible. 😌 We all know that every programming language consist of its own syntax and semantics. What we wrote on the IDE doesn’t mean to the computer. Therefore, those things has to be converted into machine understandable language which is called as machine language. Converting high level code into machine code is called as compilation step of a C Program. Actually, the compilation process isn’t a one step task as you see. If you go deeper then you’ll understand compilation consists of several steps. Study the below figure. (for my juniors-> Don’t worry about this much, if you get the subject Theory of Programming languages/Compiler Theory in your level 4. 😆😆)

I will explain those steps very clearly in my next posts. Let’s first see what we are going to implement.
I’m going to implement my own Compiler called “nim”. So what are the features my compiler has.
1. It can add numbers
2. It can subtract numbers
3. Our compiler allows only a expression which consists of numbers and also +,- operators that separates each number.(Ex: 32+5)
4. No whitespaces between numbers or operators is a must in our language too.

So i ‘m not going to explain all the concepts in this post. I will just show what my compiler does and how i implemented it.
Actually i am using Flex/Lex to implement my compiler’s lexical analyzing part and Bison(YACC) to implement my parser. This is easy to implement. Because first you have to write your lex program for lexical analysis part of the given source code. Lex file has the file extension “.l”. Then we have to write our grammar rules using Bison(YACC). Our, Bison(YACC) file contains “.y” extension. Below shows the code of those two files. Don’t try to understand the codes. Because you will get no idea about those still. Don’t worry just try to understand what we are going to do. Then my next posts will be helpful you to understand each line of the codes.

Lexical Analyzer. (nim.l)

Parser. (nim.y)

Okay now we got our two files. We should compile grammar file, lex file and then combine the generated C files and compile together to make an executable program for our “nim” compiler.

Since I’m using UBUNTU , following commands will do the job.

Make sure to execute those commands in the given order. Because, our lex file needs to have nim.tab.h header file which is generated when compiling our parser file.
In the last command we are compiling our generated C files together to make executable file which has the name “nimcompiler”

Now open your terminal, and execute the nimcompiler file. it will asks for the input source. So you need to give your source code as accepted by this compiler, if not compiler will say your source code got some syntax errors. We are done with our new programming language 😇😇😇
See the output below.

Look our language accepts only the defined syntax patterns. Our compiler accepts mathematical expressions in the following pattern only.
Below gives syntax errors.
12 + 4
11 -5
all the other types of expressions are considered as syntactically wrong. 😆😆

Originally published at http://printfnimesh.wordpress.com on June 13, 2017.

Senior Software Engineer - Technology Team | IFS R&D International (PVT) Ltd |Graduated from University of Moratuwa in Computer Engineering field