Write your own programming language and compiler with Python (2023)

Marcelo Andrade


stay together

(Video) Making My Own Programming Language and Coding a Game in It

7 minutes reading


June 28, 2018


Write your own programming language and compiler with Python (2)
(Video) Create your Own IDE || Build a Python Compiler || Best IDE for Python || Python Project

After studying compilers and programming languages, I felt that the online tutorials and guides are too complicated for beginners or that some important parts about these topics are missing.

My goal with this post is to help people looking for a way to start developing their first programming language/compiler.

Want to read this story later?Save it in the diary.

In this guide I will usePLYas a lexer and parser, andLLVMliteas a low-level intermediate language for generating code with optimizations (if you don't know what I'm talking about, don't worry, I'll explain later).

Therefore, the requirements for this project are:

  • anaconda(much easier to install LLVMlite viacondafimcore)
  • LLVMlite
$ conda install --channel=numba llvmlite
  • RPLY (same as PLY but with a better API)
$ conda install -c conda-forge rply
  • LLC(LLVM static compiler)
  • GCC(or another login tool)

"Where to start?".This is the most common question when trying to build your programming language.

I'll start by defining my own language. let's call itGAME, here is a simple exampleGAMEprogram:

there is;
x:= 4 + 4 * 2;
if (x > 3) {
x:= 2;
} other things {
x:= 5;

While this example is really simple, it's not as easy to implement as a programming language. So let's start with a simpler example:

print(4 + 4 - 2);

But how do you formally describe a language grammar? It's very difficult to create every possible example of a language to show everything it can do.

To do this, we do what is called aEBNF. It is a metalanguage for defining all possible grammatical structures in a language with a document. You can easily find most EBNF programming languages.

To better understand how an EBNF grammar works, I suggest you readTo bePosition.

Let's create an EBNF that describes its minimal possible functionalityGAME, just an addition operation.It will describe the following example:

4 + 2;

Your EBNF can be described as:

expression = number, "+", number, "?";
number = digit+;
digit = [0-9];

This example is too simple to be useful as a programming language, so let's add some features. The first is being able to add as many numbers as you want and the second is being able to subtract numbers.

Here is an example of our new programming language:

4 + 4 - 2;

And your EBNF can be described as:

expression = number, { ("+"|"-"), number }, "?";
number = digit+;
digit = [0-9];

And finally he addsPrintfor our programming language:

print(4 + 4 - 2);

We arrive at this EBNF:

program = "print", "(", expression, ")", "?";
expression = number, { ("+"|"-"), number } ;
number = digit +
digit = [0-9];

Now that we've defined our grammar, how do we translate it into code? So, can we validate and understand a program? And after that, how can we compile it into a binary executable?

A compiler is a program that converts a programming language into machine language or other languages. In this guide I will compile our programming language toIR LLVMand then in machine language.

Write your own programming language and compiler with Python (3)
(Video) I MADE an UwU Programming Language!

With the help of LLVM it is possible to optimize the compilation without learning compilation optimization and LLVM has a very good library for working with compilers.

Our compiler can be divided into three components:

  • Lexer
  • Analyst
  • generator code

ForLexereAnalystwe needresponderit really seemsPLY: a Python library with dictionaries and parsing tools, but with a better API. Is forgenerator code, we useLLVMlite, a Python library for linking LLVM components.

The first component of our compiler isLexer. Its job is to take the program as input and split it intotestimonials.

Write your own programming language and compiler with Python (4)

We use the minimal structures of our EBNF to define our tokens. For example with the following entry:

print(4 + 4 - 2);

Our Lexer will split this string into this list of tokens:

Token('PRINT', 'print')
Token('OPEN_PAIRS', '(')
Symbol('NUMBER', '4')
Token('SOMA', '+')
Symbol('NUMBER', '4')
Token ('SUB', '-')
Symbol('NUMBER', '2')
Token('CLOSE_PAREN', ')')
Token('SEMI_COLON', ';')

So let's start coding our compiler. First, create a file calledlexer.py. We define our tokens in this file. we only useLexerGeneratorclass from RPLY to create our Lexer.

After that create your main file calledmain.py. We have combined all three build components into this file.

if you run$ python main.py, the token output will be the same as described above. You can change the name of your tokens if you like, but I recommend keeping it the same to stay consistent.Analyst.

The second component of our compiler isAnalyst. Its function is to perform a program syntax check. It takes the list of tokens as input and creates aASTas output. This concept is more complex than a list of tokens, so I recommend doing a little research on parsers and ASTs.

Write your own programming language and compiler with Python (5)

To implement our parser, we use the structure generated without EBNF as a model. Fortunately, RPLY's parser uses a very similar format to EBNF to build its parser, so it's very straightforward.

The hardest part is connecting the analyzer to the AST, but once you get the hang of it, it becomes really mechanic.

First, create a new file calledast.py. It will contain all the classes that will be called in the parser and will create the AST.

Second, we need to create the parser. This is what we useGenerator Analyzerfrom RPLY. Create a file nameparser.py:

And finally we update our filemain.pyto combineAnalystcomLexer.

now if you run$ python main.py, you will see that the output is the result ofprint (4 + 4 - 2), which is the same as print 6.

With these two elements, we have a working compiler that interpretsGAMElanguage with Python. However, it still does not generate machine language code and is not well optimized. To do this, let's move on to the more complicated part of the guide, generating code with LLVM.

(Video) Make YOUR OWN Programming Language - EP 1 - Lexer

The third and final component of the output compiler isgenerator code. Its function is to convert the AST generated by the parser into machine language or IR. In that case it will convert AST to LLVM IR.

This item is the main reason I am writing this post. There are no good guides on how to implement code generation with LLVM in Python.

LLVM can be very complicated to understand, so if you want to fully understand what's going on, I recommend reading onLLVMlite Documents.

LLVMlite has no implementation for aPrintmode, so you need to set your own.

To start, let's create a file calledcodegen.pywhich will contain the classcode generation. This class is responsible for configuring LLVM and generating and storing the IR code. We also declare the Print function in it.

After that, let's update ourmain.pyfile to callcode generationmethods:

As you can see, I removed the import program from this file and created a new file calledinput.gamesto simulate an external program. Its content is the same as the described entry.

print(4 + 4 - 2);

Another change made is passing byunit of measurement,Buildingeprintfobject in the parser. This was done so that we could pass these objects to the AST where the LLVM AST is created. so we changeparser.pyto receive these items and forward them to AST.

And finally, and most importantly, we changeast.pyto get those objects and build the LLVM AST using LLVMlite methods.

With these changes, our compiler is ready to transform aGAMEprogram to an LLVM IR fileexit.ll. to compile this.llfile to an executable file, we use LLC to create an object fileleave.the, and finally GCC (you can use other linkers) to build the final executable.

$ llc -filetype=obj output.ll
$ output gcc.o -o output

And you can finally run the compiled executable of the original program.

$ ./output

After this guide, I hope you understand an EBNF and the three basic concepts of a compiler. With that knowledge, you can now create your own programming language and write an optimized compiler for it with Python. I encourage you to go ahead and add new elements to your language and compiler, here are some ideas:

  • Declaration
  • Variables
  • New binary operators (multiplication, division)
  • unary operators
  • if declaration
  • male statement

Feel free to send me compiler projects. I'm happy to help you with anything.

You can contact me atmarceloga1@al.insper.edu.br

We hope you enjoyed this post and have a little love for programming languages ​​and compilers!

You can seefinal code not GitHub.

More from the Journal

There are many black creators doing amazing work in technology. This collection of resources enlightens some of us:


Can you make your own programming language in Python? ›

PLY stands for Python Lex Yacc. It is a library you can use to make your own programming language with python. Lex is a well known library for writing lexers. Yacc stands for "Yet Another Compiler Compiler" which means it compiles new languages, which are compilers themself.

How to make your own programming language compiler? ›

Steps to create a compiler for a programming language
  1. Lexical Analysis. Recognize language keywords, operators, constant and every token that the grammar defines.
  2. Parsing. ...
  3. Semantic Analysis. ...
  4. Optimization. ...
  5. Code Generation.

How to create a compiler in Python? ›

In CPython, the compilation from source code to bytecode involves several steps:
  1. Tokenize the source code ( Parser/tokenizer. ...
  2. Parse the stream of tokens into an Abstract Syntax Tree ( Parser/parser. ...
  3. Transform AST into a Control Flow Graph ( Python/compile. ...
  4. Emit bytecode based on the Control Flow Graph ( Python/compile.

What is an example of a programming language using a compiler? ›

Examples of pure compiled languages are C, C++, Erlang, Haskell, Rust, and Go.

Can I make my own AI with Python? ›

Python is commonly used to develop AI applications, such as improving human to computer interactions, identifying trends, and making predictions. One way that Python is used for human to computer interactions is through chatbots.

Can I master in Python my own? ›

Yes, it's absolutely possible to learn Python on your own. Although it might affect the amount of time you need to take to learn Python, there are plenty of free online courses, video tips, and other interactive resources to help anyone learn to program with Python.

Can I build my own compiler? ›

If languages each have a set of grammar rules, and those rules are all the legal expressions, then there are primarily two parts to building a compiler. Be able to read a file, parse it, then build an validate an Abstract Syntax Tree from that grammar.

Can I design my own programming language? ›

Creating a programming language is a process that seems mysterious to many developers. In this article we tried to show that it is just a process. It is fascinating and not easy, but it can be done. You may want to build a programming language for a variety of reasons.

Is it hard to write your own programming language? ›

Programming has a reputation for being one of the most difficult disciplines to master. Considering how different it is from traditional forms of education, including college degrees in computer science, it's not hard to see why some people have difficulty learning how to code.

What is Python compiler written in? ›

To answer the question, In which language is Python written? The complete script of Python is written in the C Programming Language. When we write a Python program, the program is executed by the Python interpreter. This interpreter is written in the C language.

How do I start Python compiler? ›

A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .

What are the 4 types of compilers? ›

Types of Compiler
  • Cross Compilers. They produce an executable machine code for a platform but, this platform is not the one on which the compiler is running.
  • Bootstrap Compilers. These compilers are written in a programming language that they have to compile.
  • Source to source/transcompiler. ...
  • Decompiler.

What is compiler in Python with example? ›

A compiler is a special program that translates a programming language's source code into machine code, bytecode or another programming language.

What is Python used for? ›

Python is often used as a support language for software developers, for build control and management, testing, and in many other ways. SCons for build control. Buildbot and Apache Gump for automated continuous compilation and testing.

Is it hard to learn AI with Python? ›

If you're going to pursue machine learning, it's a good idea to start with these key mathematical concepts and move onto the coding aspects from there. Many of the languages associated with artificial intelligence such as Python are considered relatively easy.

Which Python is best for AI? ›

Let's jump in.
  • Keras. Keras is a deep learning framework in Python. ...
  • Pytorch. Pytorch is an AI Framework created by Facebook in 2016. ...
  • Scikit-Learn. It was developed by David Cournapeau as a Google summer project in 2007. ...
  • Apache Spark. Apache Spark was developed by UC Berkeley in 2009.
Oct 5, 2022

How many hours a day to learn Python? ›

Time devoted to learning:

The answer to how much time it takes to learn python depends on the time you spent learning. Ask yourself how much time you can dedicate to learning and practicing Python. Generally, it is recommended to dedicate one hour every day to Python learning.

Can I master Python in 3 months? ›

In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.

How many hours a day to master Python? ›

From Awareness to Ability
GoalLearn Python's syntax and fundamental programming and software development concepts
Time RequirementApproximately four months of four hours each day
WorkloadApproximately ten large projects
1 more row

How hard is it to write a compiler? ›

Writing a compiler requires knowledge of a lot of areas of computer science - regular expressions, context-free grammars, syntax trees, graphs, etc. It can help you see how to apply the theory of computer science to real-world problems.

Which language is easiest to write compiler for? ›

Any assembly language would be easy to write a compiler for, since each statement translates directly into a machine instruction. (Though it would more accurately be called an assembler, rather than a compiler.)

How difficult it is to write your own compiler? ›

Writing a simple compiler is easy. I wrote a compiler for a toy language as part of an undergraduate CS class. There are even tools, like YACC (Yet Another Compiler Compiler), to do some of the heavy lifting for you. YACC creates the parser—the part of the compiler that “understands” the source code.

Can you be a self-taught computer programmer? ›

The path of a self-taught developer is arduous and rich with exhausting unpredictability. There is no one-track-fits-all from coding newbie to a career developer. With this, there are so many unique stories that have been told by self-taught programmers who went on to be full-time developers.

Can you make it as a self-taught programmer? ›

Programmers come in many forms, performing different tasks for businesses and clients, and requiring various coding basics. All these roles can be self-taught using research skills, short courses, and practice on coding projects.

Can you be an independent programmer? ›

Freelance programmers are their own bosses, meaning they pay their taxes, choose their clients, set their hours and manage their workspace. As a freelance programmer, you can write code for websites, software, mobile apps and any other type of computing application.

How long does it take to become a fluent coder? ›

It typically takes 6-12 months to get a firm grasp on 3-4 programming languages. Traditional Degree: It takes about four years to complete a bachelor's degree in computer programming or computer science in a traditional college or university setting.

How long does it take to self teach programming? ›

If you go the self-taught route, you may spend between six and 12 months learning to code. If you are concerned about how hard it is to learn coding, you may want to choose a structured program over teaching yourself.

How long does it take to learn coding by yourself? ›

Generally, most people can learn basic coding skills in as little as three to four months. Developing more profound programming knowledge takes most people between six months and a year. The process of learning to program requires you to learn new concepts and languages, such as HTML, Java, or Python.

Is Python a compiler or not? ›

Python is Both Compiled as well as Interpreted

While running the code, Python generates a byte code internally, this byte code is then converted using a python virtual machine (p.v.m) to generate the output.

Which compiler is best for Python? ›

Top 10 Most Popular Python Compilers
  • PyCharm.
  • Jupyter Notebook.
  • Atom.
  • Spyder.
  • IDLE.
  • Sublime Text.
  • Vim.
  • Visual Studio Code.
May 20, 2023

Is Python an interpreter or compiler? ›

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

How do I write my first Python script? ›

To create your first Python file, navigate to the folder that you would like to create your file in and create a file called test.py. Next, open this file up in a text editor and type in the following code: print("Hello, World!") Save your file, and in the terminal, navigate to the file's location.

How to write a script in Python? ›

Be A Python Dev
  1. Install Python3. Important: Python2. ...
  2. Setup your code editor. ...
  3. Create a python sandbox folder. ...
  4. Open the folder in your IDE. ...
  5. Create a HelloWorld.py file. ...
  6. Extend your script to use variables, built-in functions, and operators. ...
  7. Reusing code by creating functions. ...
  8. Using a while loop to continually display messages.
Dec 23, 2019

How do you write a Python script and run it? ›

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard, and that's it.

What is the best way to start Python? ›

Codecademy. One of the best places on the internet to learn Python for free is Codecademy. This e-learning platform offers lots of courses in Python, both free and paid. Python 2 is a free course they provide, which is a helpful introduction to basic programming concepts and Python.

How to install a Python compiler? ›

How to install Python in Windows?
  1. Step 1 − Select Version of Python to Install. ...
  2. Step 2 − Download Python Executable Installer. ...
  3. Step 3 − Run Executable Installer. ...
  4. Step 4 − Verify Python is installed on Windows. ...
  5. Step 5 − Verify Pip was installed.
Mar 10, 2021

How to create a Python file? ›

Create a Python file
  1. In the Project tool window, select the project root (typically, it is the root node in the project tree), right-click it, and select File | New ....
  2. Select the option Python File from the context menu, and then type the new filename. PyCharm creates a new Python file and opens it for editing.
May 3, 2023

What are the basics of compiler? ›

Compiler design principles provide an in-depth view of translation and optimization process. Compiler design covers basic translation mechanism and error detection & recovery. It includes lexical, syntax, and semantic analysis as front end, and code generation and optimization as back-end.

What is the 5 most popular programming language? ›

Discover the most popular and in-demand programming languages with our list of the 10 best options to learn.
  • C++ ...
  • JavaScript. ...
  • PHP. ...
  • Swift. ...
  • Java. ...
  • Go. ...
  • SQL. ...
  • Ruby. Ruby is another popular open-source programming language.
May 5, 2023

What type of language is Python? ›

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics developed by Guido van Rossum. It was originally released in 1991. Designed to be easy as well as fun, the name "Python" is a nod to the British comedy group Monty Python.

What are the 5 basic programming languages? ›

Here are five basic programming languages to explore:
  • Python. This is a high-level and general-purpose language that focuses on code readability. ...
  • Java. ...
  • JavaScript. ...
  • C and C++ ...
  • SQL.
Jan 26, 2023

What type of compiler is Python? ›

Answer: Python is an interpreted programming language i.e. the software present in the computer reads the Python code and gives the instructions to the machine. That's why it has no compiler.

What are two examples of compiler? ›

Examples of compiled languages are, – C, C++, C#, CLEO, COBOL, etc. A compiler is a language processor that reads the entire source program written in the high-level language in one go and converts it into an equivalent program in machine code. C, C++, C#, and Java are a few examples.

How to create your own language? ›

How to Create Your Own Language
  1. Name Your Language. ...
  2. Build Grammar Rules. ...
  3. Consider Basing Your Artificial Language on an Existing Language. ...
  4. Combine Words to Make New Ones. ...
  5. Get Inspiration from Existing Alphabets. ...
  6. Record Everything. ...
  7. Practice Your Language.
Mar 2, 2021

What is the Python compiler written in? ›

The complete script of Python is written in the C Programming Language. When we write a Python program, the program is executed by the Python interpreter. This interpreter is written in the C language.

Which language program is most difficult to write? ›

Malbolge. This language is so hard that it has to be set aside in its own paragraph. Malbolge is by far the hardest programming language to learn, which can be seen from the fact that it took no less than two years to finish writing the first Malbolge code.

How do people write compilers? ›

A very simple compiler can be written from an assembler and machine code. Once you have a software that is able to translate something into binary instructions, you can use the original compiler to write a more sophisticated one (then use a second further refined one to write a third and so on).

What is the most simplistic language? ›

Riau Indonesian is different from most other languages in how simple it is. There are no endings of any substance, no tones, no articles, and no word order. There is only a little bit of indicating things in time.

What is the easiest language to learn? ›

We've used data from the Foreign Service Institute (FSI) to rank them from the easier to the somewhat more challenging.
  • Frisian. ...
  • Dutch. ...
  • Norwegian. ...
  • Spanish. ...
  • Portuguese. ...
  • Italian. ...
  • French. ...
  • Swedish.
Oct 24, 2021

How to master a language by yourself? ›

Top tips from experts to learn a language quickly
  1. Take risks and speak the language whenever you can.
  2. Read children's books and comic books in the foreign language.
  3. Consume foreign language media.
  4. Immerse yourself in the local culture.
  5. Make use of free foreign language podcasts and apps.
Mar 3, 2015

Does Python use compiler or interpreter? ›

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

Does Python run on a compiler? ›

Python is both compiled as well as an interpreted language, which means when we run a python code, it is first compiled and then interpreted line by line. The compile part gets deleted as soon as the code gets executed in Python so that the programmer doesn't get onto unnecessary complexity.

Why isn t Python compiled? ›

Python does not need a compiler because it relies on an application (called an interpreter) that compiles and runs the code without storing the machine code being created in a form that you can easily access or distribute.

Which compiler is the fastest? ›

The Zapcc compiler is the fastest compiler in this test, handily beating the nearest competitor by a factor of more than 1.6x. The PGI compiler is the slowest compiler in the test. According to the Portland Group website, they are working on an LLVM-based update to the PGI compiler, which may improve the compile time.

Do all languages need a compiler? ›

In principle, any language can be implemented with a compiler or with an interpreter. A combination of both solutions is also common: a compiler can translate the source code into some intermediate form (often called p-code or bytecode), which is then passed to an interpreter which executes it.


1. So I Made My Own Programming Language...
2. Create your own programming language // Compilers Demystified
3. Creating a Programming Language (Part 001)
4. Making a COMPILER FROM SCRATCH in C | 001
5. How To Build A Programming Language From Scratch
6. How do I create a Programming Language? #1


Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated: 26/07/2023

Views: 5710

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.