Skip to content

mtnrzna/TSLANG-Compiler

Repository files navigation

TSLANG Compiler

Front end of a compiler for a phony language called TSLANG, (The Simple Language), using PLY. ref.: https://www.dabeaz.com/ply/ply.html Front end of this compiler contains: lexical analysis, syntax analysis and semantic analysis.

Lexical Analysis

Here is done with lex.py from PLY package. Tokens and reserved tokens were defined using Regex and handed over to the lex.py

Syntax Analysis

This part ontains defining TSLANG grammar using BNF as described in PLY documantation and finally handing this grammar over to yacc.py. I also used syntax error handling using error token. After reduction of each rule, the syntax tree and abstract synta tree, AST, of that node is created. abstract syntax tree is printed in the console as a tree, usuing "anytree" library and AST is being used for the next section.

Semantic Analysis

I traversed AST and first fill the symbol table with function and variable definitions in preprocess.py and then with traversing the AST for the second time I filled the symbol table using rest of the code. Traversing the AST is done using visitor design pattern. Also the preprocess action results in the "forward referencing" feature.

Generate IR Code

The last part is generating the Intermediate Representation code. Once again I traversed the AST and make the TSLANG IR-code for each node.

Run The IR

The IR-code generated by this project, gets run by a virtual machine, created by Prof. Ali Gholami Rudi. ref: https://github.com/aligrudi/tsvm

BNF sample used in parser: "func : FUNCTION iden LPARANT flist RPARANT RETURNS type COLON body END"

Syntax tree sample created in parser: prog └── func ├── function ├── iden │ └── find ├── ( ├── flist │ ├── type │ │ └── Array │ └── iden │ └── A . . .


Run The Project

first run pip install -r 'requirements.txt', then run py main.py


-This project was my assignment from "Compiler Design" course in university. Uni.: Babol Noshirvani University of Technology (BNUT), Prof.:Dr.Ali Gholami Rudi

-Matin Rezania Jan of 2022