GithubHelp home page GithubHelp logo

mini-c-compiler's Introduction

// Copyright (c) 2014 Jihyeok Park
// KAIST CS420 Project

// NAME         : Jihyeok Park
// STUDENT NO   : 20120484

// This is the parser for mini-c.
// It follows below mini-c grammar.



######################### HOW TO RUN ##########################


environment setting :   make init
build               :   make all
test                :   make run < [filename].c
clean               :   make clean


###################### mini-C GRAMMAR  ########################


//  \(a\)\+ : one or more repetition of a
//  \(a\)\* : zero or more repetition of a
//  \(a\)\? : a optionally exists
//  unop    : -
//  binop   : *, /, +, -, <, >, <=, >=, ==, !=
//  id      : \[A-Za-z\]\[A-Za-z0-9_\]\*
//  intnum  : \[0-9\]+
//  floatnum: \[0-9\]\+.\[0-9\]+

Program         := \(DeclList\)\? \(FuncList\)\?
DeclList        := \(Declaration\)\+
FuncList        := \(Function\)\+
Declaration     := Type IdentList ;
IdentList       := Identifier\(, Identifier\)*
identifier      := id \| id [ intnum ]
Function        := Type id (\(ParamList\)\?) CompoundStmt
ParamList       := Type Identifier \(, Type Identifier\)\*
Type            := int \| float
CompoundStmt    := { \(DeclList\)? StmtList }
StmtList        := \(Stmt\)\*
Stmt            := OpenStmt \| ClosedStmt
OpenStmt        := IfStmt \| IfOpenElseStmt \| ForOpenStmt \| WhileOpenStmt
ClosedStmt      := AssignStmt \| CallStmt \| RetStmt \| SwitchStmt \| CompStmt \| IfClosedElseStmt \| ForClosedStmt \| WhileClosedStmt \| DoWhileStmt \| ;
AssignStmt      := Assign ;
Assign          := id = Expr \| id [ Expr ] = Expr
CallStmt        := Call ;
Call            := id ( \(ArgList\)\? )
RetStmt         := return \(Expr\)\? ;
WhileOpenStmt   := while ( Expr ) OpenStmt
WhileClosedStmt := whlie ( Expr ) ClosedStmt
ForOpenStmt     := for ( Expr ) OpenStmt
ForOpenStmt     := for ( Expr ) ClosedStmt
IfStmt          := if ( Expr ) Stmt
IfOpenElseStmt  := if ( Expr ) ClosedStmt else OpenStmt
IfClosedElseStmt:= if ( Expr ) ClosedStmt else ClosedStmt
SwitchStmt      := switch ( Identifier ) { CaseList \(default : StmtList \(break ;\)\?\)\? }
CaseList        := \(case intnum : StmtList \(break ;\)\?\)\*
Expr            := unop Expr \| Expr binop Expr \| Call \| intnum \| floatnum \| id \| id [ Expr ] \| ( Expr )
ArgList         := Expr \(, Expr\)\*



*** I modified the condition and loop production rule for removing dangling else problem with loop
*** I modified the switch case rule for representing the default case

########################    SEMANTIC CHECK    #######################

    - Warning (Type Conversion) -

        int to float : __int2float__
        float to int : __float2int__

##########################     RESULT     ###########################

    Production Rule :   prod.txt
    C-style AST     :   tree.txt
    Symbol Table    :   table.txt
    Final result    :   out.T

#####################################################################

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.