Click here to Skip to main content
Click here to Skip to main content

Compiler and language

, 3 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Design your own language and write a compiler/ interpreter for it


Design a computer language ,a compiler and an interpreter around it, needless to say that we are scratching the surface (and I hope to scratch deep).


You should have some knowledge of C/C++, a basic understanding of algorithms (esp. : Trees), x86 assembly (MASM) and Turing completeness (

We design the simplest of language which supports one type (to avoid context sensitive grammar) and is better than a Turing tarpit (, its best you keep your language specification to context free to avoid resolving unnecessary ambiguity. e.g. :- C/C++ generates different assembly code for float and int type (hence code generation is sensitive to the type). since this is my first article towards creating a computer language, lets keep it simple, also grammar checks for this language are missing and can easily be added once you understand the generation of the syntax tree related to expression handling.

This article also introduces to the reader a glimpse of commercial compiler implementation and language evaluation without the thrills of syntax/type checking.

Also this article does not expose you to optimizations (all commercial compilers spend a considerable compilation time towards this) and I do hope to add the following in our compiler (may be in my next article):

Using the code

As with my previous articles, the code must be referred to at all times and debugged to get a better understanding of how it works.

The basis of Turing completeness are
  • Writing/reading arbitrary values to/from arbitrary memory location e.g. : a=10, b=b*2+a,c=b+a.
  • JUMPS , allow execution of jumps to perform looping and static flow control of program (static flow does not depend on runtime values of variables)
    • #ifdef, #else , #endif. if(false) etc can perform static flow control in C.
  • Flow control e.g. : if a isEqualTo 2 Then Goto End1, here flow is controlled at runtime, (depends on runtime value of variable(s)).

With flow control, we can create loops, function recursion (feel free to build them using your language).

No language is complete without an expression evaluator

We are supporting the following operators in precedence (), *,/,% (mod),+. We do not support '-' since we are going to replace all expressions of the type a-b to a-1*b and handle -1*b as an independent sub expression (you may handle it differently by placing the value of -b as a complement of b e.g. : -1 will be represented as 0xffffffff and then generate out put as a+b).

First we start by evaluating the bracket '('

we must perform a search for '(' by using     if(strstr(str,"("))

if we find it, we call string EvaluateBrackets(const char *ex_str,bool bBuildCode=false)

We start by evaluating the innermost bracket by looking for the first closing bracket: ')', we then work our way backwards to the first opening bracket (found in back search which means the last opening bracket before we meet the first closing bracket), refer to code.

string EvaluateBrackets(const char *ex_str,bool bBuildCode=false)  //will evaluate one set of inner most brackets only
    string ret="";
    stack<char,vector<char>> bracket_check;
    for(UINT i=0;i<strlen(ex_str);++i)
            int j=i;
            string bracketed;
                    static UINT tempVarCnt=0;
                    char tempVar[STR_SIZE]={};


                    variableList[tempVar]=expression(stringReplace(stringReplace(bracketed.c_str(),"(","").c_str(),")","").c_str(),tempVar,bBuildCode);  //add the variable in the map

                    return ret;
    return ret;
Notice that we replace the innermost bracket with a temporary variable for evaluating an expression, something that C/C++ and other compilers must do: please refer

Now comes the fun part, expression evaluation

This is best done building a tree and is simple (provided you know data structure:Tree)

struct Tree
    Tree():r(0),l(0),m_expression(" "),m_data(" "){}
    string m_expression;
    string m_data;
    Tree *l,*r; //as with all binary trees, a 'left' and 'right' pointer

    void AddSubExpressions(string expression)

        bool bFound=false;
        PairString pa;
        //follow operator precendance + has least precendance, * has most precedance
        if(Findoperator(expression,"+",pa))  // we look for '+' since it has the least precedance
        else if(Findoperator(expression,"%",pa))
        else if(Findoperator(expression,"/",pa))
        else if(Findoperator(expression,"*",pa))
            this->l=new Tree;
            this->r=new Tree;

for an expression a=10+3*5 the following tree is generated, you can provide more complicated expression to see the tree being generated, also traversal of this tree in the pre-order , in-order , post order will provide you with a prefix, infix and post fix expression.

Lets compute this expression

For this we will call the function static int compute(Tree *tmp,bool &bFoundVariable)

a DFS traversal does the trick (, using function recursion.

     return compute(tmp->l,bFoundVariable)+compute(tmp->r,bFoundVariable);
  return ((int)compute(tmp->l,bFoundVariable))%((int)compute(tmp->r,bFoundVariable));

The instruction pointer :-

For Interpreter:

We will use the file pointer itself to point to the next line for execution, being an interpreter we will read and evaluate each line independently using only the variable / label table (with their values) generated while executing the previous lines (C/C++ also evaluates each sentence independently and uses the tables related to function and variables generated while evaluating the previous lines).

For Compiler:

Since we have to generate an executable, we do not have the source code on which we can run the file pointer as an instruction pointer, the executable must be sufficient to execute without the source file, here we will have to generate the complete code itself (we will generate x86 32-bit assembly code, to be consumed by MASM).

We need to evaluate all the variables as they appear in the source code, we do this by calling void BuildASMCode(Tree *tmp,string Temp) to generate x86 code.

Optimization used here:-

static int compute(Tree *tmp,bool &bFoundVariable) is used to find the value of the variable, inputs are the expression tree (we generated it earlier) and bFoundVariable, if bFoundVariable returns as false, the variable is optimized and code generated is as

int y=compute(...)


MOV variable,EAX

e.g. : a=b+1  , a's value is dependent on b's and hence will not be optimized (bFoundVariable will return true) , but a=2+3 can be optimized to a=5 (bFoundVariable will return false hence the optimization)

When bFoundVariable  returns true, we will traverse the expression and build assembly code to compute its value:

BuildASMCode(Tree tmp, string Variable variableName)
//Create 2 new variables templ (left) and tempr (right),
//evaluate these 2 new variables first using DFS


   MOV eax,templ  //for temporary variable on left
   MOV ebx,tempr  //for temporary variable on right
   MOV variableName, EAX

We also have to define all the variables (including the temporary ones) in a data region of the ASM file


for(map<string,int>::iterator it=variableList.begin();;++it)
  if(it==variableList.end()) break;
  fprintf(m_fp,"    %s DD 0 \n",it->first.c_str());

Now that we have looked at expression evaluation, lets look at flow control

Remember Turing completeness requires flow control : branching through which we can perform controlled looping

Many languages that choose performance over Turing completeness due to performance reason choose not to provide flow control, this makes sense due to the pipeline nature of high performance (simple) processors that don't need elaborate branch prediction and not decrease high performance through put due to branch miss, e.g. :- older shader languages (DX shader 1.0):

uncontrolled jump: GOTO, controlled flows are implemented the same way

For Interpreter:

We must generate the table for labels available , hence we must parse the entire source file once

            char *str=strstr(Buffer,"LABEL:");
            if(str)  //found label
                stringArray strArr=tokenizer(Buffer,":");  
                strArr[1].at(strArr[1].length()-1)=NULL;  //we expect 2 strings

The gotoLabelList will maintain the position of the line number where the Labels are found, when a GOTO statement is found , we will increment/decrement the file pointer to that line number in the source code.

int iCurrentLine=0;
#define JMP(j) {fclose(fp);\
    for(int i=0;i<j+1;i++)\

for conditional jumps, we only allow jumps when our condition is matched.

For Compiler:

This is simple for a compiler since .asm files already have provisions for labels and jmp (with condition) instructions, we only have to write them to .asm file when encountered in the source code (while reading the source code line wise).

Function calling

For Interpreter:

We use the existing tables for labels, and implementation is similar to GOTO, but we must maintain the stack to keep our return address, funcRet.push_back(iCurrentLine), maintains the line number to which it must return when RETURN is encountered.

For Compiler:

This is simple (like the GOTO syntax discussed before) since .asm files already have provisions for labels and call/return instructions, x86 maintains the call stack.

Array support

I am currently not supporting arrays and their respective index based array access. We will look into it in another article.

Last bit

BREAK: this tells the interpreter /compiler that the end of program has been reached must exit execution

Output assembly

generated by the compiler can be assembled using MASM (32-bit ) , ships with VS2012, please refer to attached code for instructions related to MASM (ml.exe).

A Break point Int 3 is added forcing the executable to break into a debugger, this will allow you to see the output using the processes memory viewer (part of the debugger).

Points of Interest

I hope this article demystifies the defining of a programming language and construction of a compiler/interpreter. I do plan to write more articles on this subject and add more features to our language.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Asif Bahrainwala
Instructor / Trainer
India India
I have been working with computers since my eight grade, programming the ZX Spectrum. I have always had an interest in assembly language and computer theory (and is still the reason for taking tons of online courses), actively code using C/C++ on Windows (using VS) and Linux (using QT).
I also provide training on data structures, algorithms, parallel patterns library , Graphics (DX11), GPGPUs (DX11-CS,AMP) and programming for performance on x86.
Feel free to call me at 0091-9823018914 (UTC +5:30)
Follow on   LinkedIn

Comments and Discussions

QuestionCan someone port BuildASMCode for 64-bit? [modified] Pinmemberehaerim7-Dec-14 14:14 
AnswerRe: Can someone port BuildASMCode for 64-bit? PinmemberAsif Bahrainwala14-Jan-15 0:50 
Questiontwo glitches Pinmemberehaerim7-Dec-14 13:56 
Question[My vote of 2] A bit over simplistic PinmemberRob Grainger18-Nov-14 1:03 
QuestionI just make a language the other day. PinmemberRobert Planas6-Aug-14 23:16 
QuestionCool article, typo? PinmemberMember 109929295-Aug-14 11:22 
AnswerRe: Cool article, typo? PinmemberAsif Bahrainwala5-Aug-14 20:51 
QuestionGreat Job, Asif! Pinprofessionalkoothkeeper5-Aug-14 8:07 
Generalmy vote of 5 PinmemberSouthmountain4-Aug-14 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150301.1 | Last Updated 4 Aug 2014
Article Copyright 2014 by Asif Bahrainwala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid