How to Make a Compiler

One of my first programming assignment this year was creating a simple compiler to understand the mechanism behind compilation of code. Essentially, all the compiler for all the languages use the similar mechanism when compiling. The general process do not change but the syntax does. For CSE 262 – Programming Languages, the first programming assignment was creating a compiler using Java. Over the summer, I toke CSE 109 and the first programming assignment was creating a compiler but using C++. Here is a brief overview on making a compiler with Java.

The first step is analyzing the grammar of the language. This goes from figuring out what kind of symbols are allowed in the language. This was the grammar for the compiler we were coding for:

digit [0-9]
letter [a-zA-Z]
int <digit>+
oat <digit>+.<digit>+
id <letter>(<letter> j <digit>)*
keyword SELECT | FROM | WHERE | AND
operator = j < j >
comma ,

The second step is to create a Token class to store these values and their category.

Token

The third step is to create a Lexer class for the lexical analyze. You start by drawing the finite state automaton so that you can recognize the tokens and then covert the automaton into code.

Lexer

The final step is to create a Parser class which is the syntax analyzer. The class uses the recursive descent technique to parse through all the token.

Parser

Advertisements

About lunur16

I am undergraduate student at Lehigh University. I am from Allentown, Pennsylvania. My intended major is Computer Engineering and possibly a minor in Business.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s