Giter Site home page Giter Site logo

melvinmo / xlang-compiler Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 377 KB

This is a compiler for the Xlang programming language, developed as the final project of my undergraduate compiler course.

Lex 7.70% Yacc 92.30%
compiler-design xlang mips bison-yacc flex

xlang-compiler's Introduction

Xlang-Compiler

This is a compiler for the Xlang programming language, developed as the final project of my undergraduate compiler course.

Overview

The Xlang compiler follows a typical compiler design, with the following phases:

  • Lexical analysis - Using flex, this phase breaks the input code into tokens 
  • Syntax analysis - Using bison, this phase parses the tokens and generates a syntax tree
  • Semantic analysis - This phase performs semantic checking and builds a symbol table
  • Intermediate code generation - The syntax tree is traversed to generate intermediate code
  • Code optimization - The intermediate code is optimized to improve performance
  • Code generation - The optimized intermediate code is translated to target assembly code

The compiler targets the MIPS R2000/R3000 architecture and can generate executable code on MIPS processors.

Language Features

Xlang is an educational programming language with C/Pascal-like syntax. Key features include:

Syntax

  • Xlang syntax is similar to Pascal/C
  • Code is organized into classes and methods
  • Supports nested block structures using { }
  • ; required to terminate statements

Data Types

  • Built-in types are integer and boolean
  • Arrays are supported for these types
  • No support for strings, floating point, etc.

Variables

  • Variables must be declared before use
  • Local variables within methods
  • Global variables at class scope
  • Call-by-value parameter passing

Expressions

  • Arithmetic operators: +, -, *, /, %
  • Logical operators: &&, ||, !
  • Comparison operators: ==, !=, <, >, <=, >=

Control Flow

  • if-else conditional statements 
  • for loops with 3 parts: init, check, increment
  • break and continue keywords

Functions

  • Functions defined with return type
  • void return for procedures
  • Recursion is permitted
  • Parameters passed by value

Classes

  • Single inheritance model
  • Program entry point is main() method
  • Fields and methods defined in class

Comments 

  • // indicates line comment
  • /* */ delimits block comment

Limitations

  • No object-oriented features besides classes
  • No strings, floats, chars, enums, structs
  • Basic type checking but no full type safety
  • Limited compile-time error checking
  • Small subset of features compared to C or C++

Implementation Details

Lexical Analysis

  • Input file is broken into a stream of tokens
  • Each token has a type (keyword, identifier, operator etc.) and attribute value
  • Regular expressions match token patterns
  • Handling of comments, whitespace, preprocessor directives

Syntax Analysis

  • Context-free grammar defines allowable syntax
  • Recursive descent parsing constructs syntax tree
  • Syntax tree nodes reflect grammar productions 
  • Tree represents program structure based on language grammar

Semantic Analysis

  • Check types of expressions and assignments
  • Resolve variable references using symbol table
  • Check function arguments and return types
  • Detect undeclared and redefined identifiers
  • Insert implicit type conversions where needed

Intermediate Representation

  • 3-address code with temporary variables 
  • Static single assignment form for optimization
  • Quadruples represent operations on variables
  • Symbols refer to variables and constants

Code Optimization

  • Common subexpression elimination
  • Copy propagation
  • Dead code elimination
  • Redundant load/store elimination
  • Basic block optimizations

Code Generation

  • Tree traversal generates MIPS instructions
  • Register allocation using graph coloring
  • Peephole optimizations combine instructions
  • Add directives for data/text segments
  • Insert stack management instructions

The information provided above is an English translation of the original project description, which was written in Persian. The original Persian project description PDF can be found in this repository, along with my Yacc and Lex codes.

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.