Coder Social home page Coder Social logo

arisaghafari / iustcompiler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from m-zakeri/iustcompiler

0.0 0.0 0.0 2 MB

IUST Compiler Design: Principles, Techniques, and Tools

License: MIT License

PureBasic 0.02% Python 91.89% Java 2.88% ANTLR 5.15% Batchfile 0.06%

iustcompiler's Introduction

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 [email protected] 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#

iustcompiler's People

Contributors

m-zakeri avatar pragma-once avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.