Skip to content

Latest commit

 

History

History

ck-compiler

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

CK Compiler

This is a project I created for learning internals of compilers. The SimplerLang grammar is taken from this post.

To compile .ck to .class

  1. Open CompilerTest.java
  2. Run compile()
  3. Look for the output/CgSample.class generated under root.
  4. To see the source code of CgSample.class, use IntelliJ decompiler.

Grammar

grammar simplerlang;

program : statement+;
statement : let | show ;

let : VAR '=' INT ;
show : 'show' (INT | VAR) ;

VAR : [a-z]+ ;
INT : [0–9]+ ;
WS : [ \n\t]+ -> skip;

Features

  1. Used a simple grammar to focus more on compiler phases rather than language support.
  2. Took ANTLR generated code as a reference.
  3. Implemented Parse Tree & created Visitable Grammar Nodes.
  4. Implemented Visitor for writing business logic on Tree nodes.
  5. Implemented CodeGeneration using Java ASM.
  6. Implemented Interpreter & Semantic Analyzer.

TODO

  • Implement Semantic Analyser.
  • Implement custom filename.(Unsupported)
  • Implement Parse Tree.
  • Implement Visitor Pattern.
  • Implement byte code generation

Compiler Phases

  1. Lexical Analysis [Done]
  2. Syntactic Analysis (ie Parsing) [Done]
  3. Semantic analysis [Done] & Intermediate Code Generation [NA]
  4. Optimization (optional)
  5. Code Generation [Done]

Intermediate Code Generation: code gets converted to independent intermediate code. We are not doing this in ck-compiler. We could use LLVM as Backend for implementing this feature.

Reference:

  1. Java ASM
  2. Ops Code
  3. Lecture Note
  4. LLVM Backend

Do check out other modules in this project for better understanding.️