# WLP4 Compiler WLP4 is a subset of the C++ language made by the University of Waterloo. During CS 241 (Sequential Programs), I worked on building a compiler for WLP4 thorughout the semester. A compiler/translator has two stages; Analysis and Synthesis. Analysis checks the input lexically, syntactically, and semantically to ensure proper language grammar and creates a parse tree. The generated parse tree is used in Synthesis to produce the output. In our WLP4 Compiler, the compiler takes a `.wlp4` file and produces a `.asm` file which is in Assembly code. **Structure of the compiler:** - Scanner - The first part of the analysis stage. Reads the input file and checks the input lexically for any invalid characters or strings (i.e using unkown characters 🌱). Produces a vector of tokens; each strings in the input are turned into tokens for parsing - Parser - Checks the vector of tokens for syntax errors (i.e missing semicolons or parentheses) and produces a parse tree which is useful in checking for context. - Context Sensitive Analysis - Checks the parse tree for any semantic errors (i.e assigning the wrong type). Context Sensitive Analysis produces a pruned parse tree where some nodes are removed since it is not needed for code generation, and also a symbol table. The symbol table contains the variable and function names in the input. - Code Generation - Takes a parse tree and symbol table and produces the generated assembly code. Code Generation is the only part of the Synthesis stage. **How WLP4 differs from C++?** - First of all, the 'main' function of WLP4 is 'wain' and it can only take two parameters; either two ints, or an int and an array of ints - Another significant difference is that all variables can only be declared once at the very top of a procedure. There are only two types of variables; int or int\* - [UWaterloo CS241 - WLP4](https://student.cs.uwaterloo.ca/~cs241/wlp4/WLP4.html) **What the Compiler produces?** The compiler generates an assembly code which takes the parameters of 'wain' in register $1 and $2 and enters the returned value of the procedure in register $3 **Usage** ``` make ./compiler < ./sample/test.wlp4 ```