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:
id <letter>(<letter> j <digit>)*
keyword SELECT | FROM | WHERE | AND
operator = j < j >
The second step is to create a Token class to store these values and their category.
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.
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.