Code Monkey home page Code Monkey logo

minic-compiler's Introduction

Mini-C compiler

Overview

Mini C is complete subset of ANSI C

Mini C was devised by reducing several language structures from the existing ANSI C grammar. The overall program structure is similar, but there is only an integer type in the data type, and no structure such as multiple arrangements exists. In addition, bit-related operators were excluded. However, in general, it is thought that there is a great educational effect in designing experimental compilers.

Code structure of the MiniC compiler

drawing

Documentation

How to run

# build
./gradlew clean
./gradlew build

Scanner

This scanner read a character stream from a MiniC source file and translate it to a sequence of MiniC tokens. This is known as the lexical analysis part of a compiler.

# running and testing scanner
java -jar build/libs/MiniC-Scanner.jar {input-path}

# example
java -jar build/libs/MiniC-Scanner.jar MiniC/Scanner/tst/base/testcases/c1.txt

# redirect the output of scanner to a file
java –jar build/libs/MiniC-Scanner.jar {input-path} > {output-path}

Parser

A recursive descent parser (RD parser) for MiniC. (only check the syntactic correctness of the input program)

# build for parser(Recommended)
./gradlew jarNoScanner

# running and testing parser
java -jar {input-path}

# example
java -jar build/libs/MiniC-Parser.jar MiniC/Parser/tst/base/testcases/c1.txt
  • jarNoScanner: sacnning with perfect scanner on library
  • jar: scanning with custom scanner

AST Generation

Extend recursive descent parser to produce Abstract Syntax Trees (ASTs) for MiniC. If a program is syntactically legal, then the parser must build the AST for the program. Otherwise the parser can print any error message and exit without completing the AST for the illegal input program.

The MiniC compiler accepts a set of command-line switches:

  • -ast to display the AST on-screen.
  • -astp to display the AST, including source positions.
  • -t prints the AST in file , using the TreePrinter.
  • -u unparses the AST into file .
# build for AST generation
./gradlew jarNoScanner

# running and testing parser(ast-generation)
java -jar build/libs/MiniC-AstGen.jar -option {input-path} {solution-path}

# check difference between desirable
diff {mytest.mc.u} {mytest.mc.u.u}

# examples
java -jar build/libs/MiniC-AstGen.jar -t MiniC/Parser/tst/base/AST_testcases/c3.mc.u MiniC/Parser/tst/base/AST_testcases/c3.mc

java -jar build/libs/MiniC-AstGen.jar -u MiniC/Parser/tst/base/AST_testcases/c3.mc.u MiniC/Parser/tst/base/AST_testcases/c3.mc

diff MiniC/Parser/tst/base/AST_testcases/c4.mc.u MiniC/Parser/tst/base/AST_testcases/c4.mc.u.u

# drawing AST 
java -jar build/libs/MiniC-AstGen.jar -astp MiniC/Parser/tst/base/AST_testcases/c3.mc.u

AST Drawing Example

drawing

Static Semantic Analysis

MiniC static semantic analysis ensures two types of constraints, namely scope rules and type rules. (check detail consist of two subphases by read Description)

The build tasks "jar" and "jarNoScanner" are the same as with AST generation. Build task "jarNoScannerNoParser" has been added

  • -ast1 to display the AST from the parser on-screen.
  • -ast2 to display the AST from semantic analysis on-screen.
  • -envast to display the AST of the MiniC standard environment on-screen.
  • -t prints the AST from the parser in file , using the TreePrinter.
  • -u unparses the AST from the parser into file .
# build for Static Semantic Analysis
./gradlew jarNoScannerNoParser

# running and test commands are similar with AST commands

Code Generation

Code generator will take our decorated ASTs as input. We will generate code for the Java Virtual Machine (JVM). Specifically, we will generate assembly instructions that will be assembled to Java class files by the Jasmin bytecode assembler. This class files can then be executed on the JVM.

# build for Static Semantic Analysis
./gradlew jarNoScannerNoParser

# opening file
java -jar build/libs/MiniC-CodeGen.jar {file_name.mc}

# make class file from assembly
jasmin {jarfile_name.j}

# access jarfile
java -jar resources/jasmin_2.4/jasmin.jar {jarfile_name.j}

Environment

OS: Mac Ventura
Language: Java (JDK 17)
Build: gradle
Assembler: Jasmin

minic-compiler's People

Contributors

yeong-hwan avatar

Watchers

 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.