A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://blog.klipse.tech/javascript/2017/02/08/tiny-compiler-compiler.html below:

Write your own compiler - Last station

The plan

Our journey is made of 4 stations - each of them depending on the previous ones:

  1. The tokenizer (aka “Lexical Analysis”): converting an input code - in LISP syntax - into an array of tokens.
  2. The parser (aka “Syntactic Analysis”): transforming an array of tokens into an Abstract Syntax Tree (AST).
  3. The emitter (aka “Code Generation”): string-ifying an AST into C-like code.
  4. The compiler (aka “You made it”): combining all the pieces together.

(The interactive code snippets are powered by a tool of mine named KLIPSE.)

The last station: the compiler

The last station is only fun - lot of fun!!!

Let’s write our last piece of code - by assembling the tokenizer, the parser and the emitter into a single my_compiler function:


1+2
my_compiler = input => {
  let tokens = tokenizer(input);
  let ast    = parser(tokens);
  let output = emitter(ast);

  return output;
}

And let’s test it…

my_compiler("(add 1 2 (mult 3 4))")

Enjoy the moment, play with your compiler…

Congratulations - I mean you did it. Give yourself a huge hug, buy yourself a gift. I don’t know… Find the most appropriate way to celebrate your success… You truly deserve it!

One last thing: the whole code is accessible as a single file on github: it’s around 150 line of codes.

Subscribe to Yehonathan Sharvit newsletter

Get the latest and greatest from Yehonathan Sharvit delivered straight to your inbox every week.

Read Next Tags

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4