We Implemented this as a part of the Complier Design Lab Course at PES University.The following Details are taken from PESU's Compiler Lab Mini Project Guidelines.
This repo contains an implementation of a Compiler for Ruby written in Python by making use of the PLY (i.e Pythonic Version of Lex & Yacc Tools) package. A thing to note is that this is just a Front End Compiler i.e it doesn't implement the Machine Dependent Code optimization (MDCO) Phase out of the seven standard phases of Compilation. Also it is implemented to compile a few basic constructs of Ruby only & can be extended to include more.
- Remove Comments.
- Generate tokens.
- Preload keywords into the Symbol Table.
- Make an entry for the identifiers into the Symbol Table (if there exists an identifier with the same name in different scopes then construct Symbol Table per scope)
- Symbol Table must contain entries for predefined routines like printf, scanf etc.
- Write CFG for the entire program using appropriate Semantic rules if you are using PLY.
- Else if you are implementing Parser by hand use Recursive Descent Parser (RDP) with Backtracking & Perform translation at required places in the code for each non-terminal.
- Take care of the primitive types and array types.
- Take care of coersions.
- Take care of Arithemetic Expressions.
- Concetrate on the looping construct choosen.
- Update type and storage information into the symbol table.
- Show Abstract Syntax tree (AST).
- Do a Three address code generation.
- Perform Constant folding.
- Perform Constant Propogation.
- Perform Common subexpression elimination.
- Perform Dead code elimination.
These are some of the basic optimizations that are implemented in this Project.You can extend the same to add more optimizations like Reducing temporaries, Loop optimizations etc.
The remaining 2 phases that form the Backend of the Compiler are Machine Dependent Code Generation (MDCO) & Target Code Generation which are not implmented here.
Link to the PLY Package: https://www.dabeaz.com/ply/ply.html
To Understand more about the Compier's phases you can checkout the Compliers: Principles, Techniques & Tools Book (more famously known as the Dragon Book) by Aho et al. Link- https://www.amazon.in/Compilers-Alfred-V-Aho/dp/9332542457?tag=googinhydr18418-21&tag=googinkenshoo-21&ascsubtag=_k_CjwKCAjwstfkBRBoEiwADTmnEHm7nMy2Ma_kjQqmHPMuiqdM2SX5V8V11nI4GnC7hVKgrKfsH4yOnxoCJzoQAvD_BwE_k_&gclid=CjwKCAjwstfkBRBoEiwADTmnEHm7nMy2Ma_kjQqmHPMuiqdM2SX5V8V11nI4GnC7hVKgrKfsH4yOnxoCJzoQAvD_BwE