Skip to content

mahdasjs/IUSTCompiler

 
 

Repository files navigation

Compiler course code sniptes

This repository contains course materials and code snippets that I developed to teach the compiler at Iran University of Science and Technology (UST). Various language grammars have been written in ANTLR v4 format. For each grammar, the source code of generated Lexer and Parser is available in Python 3.8.x The repository is assumed to be updated regularly. It would be appreciated if you use our repository by forking it. For any question please contact me m-zakeri@live.com or read documentation.

Examples

The following outputs could be generated by code snippets in this repository.

Three Addresses Codes

Figure 1 shows how a single pass compiler can generate three address code for assignment statements with minimum numbers of temporary variable, started with T:

three_addresses_code_generation

Fig 1: Examples of three addresses codes generated by ANTLR for AssignmentStatement grammar.

Abstract Syntax Tree (AST)

Figure 2 demonstrates how a single pass compiler can generate abstract syntax three (AST) for assignment statements.

abstract_syntax_three

Fig 2: Examples of abstract syntax tree (AST) generated by ANTLR for AssignmentStatement grammar.

Structure

The following describes the structure of the repository:

grammars

gram1: ANTRL hello world grammar.

Expr1: Simple grammar for handling mathematical expressions without any attribute and action.

Expr2: Simple attributed grammar for handling mathematical expressions with code() attribute.

Expr3: Currently, it is the same Expr2 grammar.

AssignmentStatement1.g4: The grammar to handle multiple assignment statements and mathematical expressions in programming languages like Pascal and C/C++.

AssignmentStatement2.g4: It is the same AssignmentStatement1.g4 grammar with attributes for holding rule code and rule type.

AssignmentStatement3.g4: The grammar to handle multiple assignment statements and mathematical expressions in programming languages like Pascal and C/C++. It provides semantic rules to perform type checking and semantic routines to generate intermediate representation.

AssignmentStatement4.g4: The grammar to handle multiple assignment statements and mathematical expressions in programming languages like Pascal and C/C++. It provides semantic rules to perform type checking and semantic routines to generate intermediate representation. It has been implemented to generate intermediate representation (three addresses codes) with minimum number of "temp" variables.

CPP14_v2: ANTLR grammar for C++14 forked from the official ANTRL website. Some bugs have been fixed and also the rule identifiers have been added to the grammar rules.

EMail.g4: Lexical grammar to validate email addresses.

EMail2.g4: Lexical grammar to validate email addresses, fixing bugs in EMail.g4

language_apps

The language_apps package currently contains Lexer and Parser codes for each grammar in directory grammars, with a main driver script to demonstrate the type checking and intermediate code generation based on semantic rules and semantic routines.

terminal_batch_scripts

The termina_batch_script directory contains several batch script to run ANTLR in terminal (Windows) to generate target code in JAVA language. The code snippets in this directory belong to my first experiences with ANTLR.

Read more

IUST compiler course official webpage

ANTLR slides: PART 1: Introduction

ANTLR slides: PART 2: Getting started in Java

ANTLR slides: PART 3: Getting started in C#

About

IUST Compiler Design: Principles, Techniques, and Tools

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 90.7%
  • ANTLR 5.2%
  • Java 4.0%
  • Other 0.1%