Giter Club home page Giter Club logo

bit-minicc's Introduction

BIT-MiniCC

BIT Mini C Compiler is a C compiler framework in Java for teaching.

Building & Running

Requirements

  • JDK 1.8 or higher
  • Eclipse Mars

Building & Running

  1. Import the project into Eclipse
  2. Set the input source file
  3. run as Java applications from class BitMiniCC

Supported targets

  1. ARM
  2. x86
  3. MIPS
  4. RISC-V

Lab. projects

  1. Lexical Analysis: input(C code), output(tokens in JSON)
  2. Syntactic Analysis: input(tokens in JSON), output(AST in JSON)
  3. Semantic Analysis: input(AST in JSON), output(errors)
  4. IR Generation: input(AST in JSON), output(Maple IR or LLVM IR)
  5. Target Code Generation: input(AST in JSON), output(x86/MIPS/RISC-V assembly)

Correspondence

Contributor

  • 2023 Yichen Yuan, Cheng Zhang, Jianhui Li
  • 2020: Hang Li
  • 2019: Chensheng Yu, Yueyan Zhao
  • 2017: Yu Hao
  • 2016: Shuofu Ning
  • 2015: YiFan Wu

bit-minicc's People

Contributors

jiweixing avatar marche147 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bit-minicc's Issues

PreProsser会向./input/目录下写文件(Windows下会报错)

老师您好
这学期学习编译原理课,发现了这样的错误:
看了反编译的结果,PreProcesser类里有一行向./input/目录下写文件,造成如下的错误:

1

为了不在运行时报错,就必须在run目录下设置一个input文件夹,这样会容易让使用者与项目主目录下的input文件夹产生混淆,影响框架上手速度。

解决方案:重新生成pp.jar 把多余的那一行代码去掉

broken support of external native binary tools

when using native binaries as external tools, the main compiler does not wait until the subprocess completes its job and continues, but bails out since the subprocess does not finish generating necessary files.

in the following code :

        private void run(String iFile, String oFile, String path) throws IOException{
                Runtime rt = Runtime.getRuntime();
                rt.exec(path + " " + iFile + " " + oFile); // this does not waits for the child
        }

a waitFor call should fix this.

定义的C语言子集改二乙

框架c语言子集没有明确,接口也很模糊,
继承分析有许多bug(比如预处理不能解决多行注释,词法分析无法识别数组,语法分析不全)

对要求不明确的,可以按照下列定义的子集去写(by misaki mc)
下面是我对c语言子集的建议

---6.12 改
有些产生式写得有些问题,现改动后的如下

---6.16 改二
将文法修改成LL(1)可识别的,有些个别歧义的认为规定规约式子(如if else)

---6.18 改二甲
修复了产生式的一些bug

---6.23 改二乙 FINAL
修复了表达式的bug,到此mmcc全部完成
并附上样例与程序
有bug或看不明白的请直接评论,或者发我邮箱

MMCC is a project to realize a simple subset c compiler relying on BITMiniCC.
It mainly include preprocessing(pp),scanning,parsing,semantic,intermediate code(icgen) parts.

The subset c have such features before:
(each module depends on different criterions
the next module may not realize all defined above)

1.words
(The "%%" mains don't consider now,others undefined)
(1)keywords:
int,long,float,double,char
void,const
%%(array),struct
%%(pointer)
if,else,do,while,for,goto
break,continue,return
(2)operator:

  • - ++ -- * / %

    < >= <= == != =
    && || ! | & ~
    (+= -= is not allowed)
    (3)boundary:
    ( ) { } [ ] ' " ; ,
    (4)
    const_i: 0332,0x223,0X332,3324
    const_c: 'D',
    const_s: "SASDF",
    const_f: 0.332,3332.3234
    (.332 00022 forbiden )

2.syntex

attentions(the sub c set):
1)The syntax analyze doesn't realize all the words resolved above.
a.keywords include only
int,float,if,else,while,break,continue,return
(that mains no struct,const type,for,do...while,void,#)
b.operator
+ - * / > < >= <= != =
c.no char or string
2)The declearation of the variable can't be assigned
int a;
int aa[2];
int b[2][6];
int a=3;//error
3)No global variables
4)No include,declearations,macros,typedef,define
5)No *(pointer) or &(get address) types
6)No lable or goto
7)The defined variable will be effect in the whole function

(1)
assign,
expression,(+-*/only)
if else (== > < >= <=)
while
%do while
%for,
%goto lable

(2)Grammer:

terminators:
TKN_INT int
TKN_FLOAT float
TKN_ID IDENTIFIER
TKN_CONSTF CONST_F
TKN_CONSTI CONST_I
TKN_RET return
TKN_IF if
TKN_ELSE else
TKN_WHILE while
TKN_BREAK break
TKN_CONTINUE continue
TKN_LP (
TKN_RP )
TKN_LB {
TKN_RB }
TKN_LB2 [
TKN_RB2 ]
TKN_COMMA ,
TKN_SEMI ;
TKN_ASIGN =
TKN_ADD +
TKN_SUB -
TKN_MUL *
TKN_DIV /
TKN_EQU ==
TKN_NEQU !=
TKN_G >
TKN_L <
TKN_GE >=
TKN_LE <=
TKN_END #
TKN_EMPTY $
$
(not consider of char or string)

non-terminators :
PROGRAM
FUNCTIONS
FUNCTION
FLIST function list
ARGS_DECL args declear
ARGS_NDECL args next declear
FUNC_BODY
STMTS
STMT syntex sentence
EXPR_STMT expression sentence
DEF_STMT defination
IF_STMT if sentence
RET_STMT
CODE_BLOAK {} | ;
ELSE_STMT else sentence
WHILE_STMT while
WHILE_BODY break,continue included while
WHILE_BLOAK
EXPR expression
ETERM expression terminator
FA_ARGS function or array args
ARGS_REF
ARGS_NREF
ARRAY_REF
ARRAY_NREF array reference next demention
ETLIST1 expression terminator list optp1
ETLIST1_C ETLIST1 closure
ETLIST2 expression terminator list optp2
ETLSIT2_C ETLIST2 closure
ETLIST3 expression terminator list optp3
ETLSIT3_C ETLIST3 closure
ETLIST4 expression terminator list optp4
ETLSIT4_C ETLIST4 closure
DEF defination
DTERM defination terminator
DTLIST defination terminator list
ARRAY_DECL
ARRAY_NDEM array declear next demention
TYPE
CONST
BRE_CONTI break or continue
OPTP1 =
OPTP2 == > < >= <= != (p=priority)
OPTP3 + -
OPTP4 * /

(all the non-terminators must be appeared in left)

grammer production :
PROGRAM -> FUNCTIONS
FUNCTIONS -> FUNCTION·FLIST
FLIST -> FUNCTION·FLIST | $
FUNCTION -> TYPE·TKN_ID·TKN_LP·ARGS_DECL·TKN_RP·FUNC_BODY
ARGS_DECL -> TYPE·TKN_ID·ARGS_NDECL | $
ARGS_NDECL -> TKN_COMMA·TYPE·TKN_ID·ARGS_NDECL | $
FUNC_BODY -> TKN_LB·STMTS·TKN_RB
STMTS -> STMT·STMTS | $
STMT -> EXPR_STMT | DEF_STMT | IF_STMT | WHILE_STMT | RET_STMT
RET_STMT -> TKN_RET·EXPR_STMT
EXPR_STMT -> EXPR·TKN_SEMI
EXPR -> ETLIST1
ETLIST1 -> ETLIST2·ETLIST1_C
ETLIST1_C -> OPTP1·ETLIST2·ETLIST1_C | $
ETLIST2 -> ETLIST3·ETLIST3_C
ETLIST2_C -> OPTP2·ETLIST3·ETLIST2_C | $
ETLIST3 -> ETLIST4·ETLIST3_C
ETLIST3_C -> OPTP3·ETLIST4·ETLIST3_C | $
ETLIST4 -> ETERM·ETLIST4_C
ETLIST4_C -> OPTP4·ETERM·ETLIST4_C | $
ETERM -> TKN_ID·FA_ARGS | CONST | TKN_LP·EXPR·TKN_RP
FA_ARGS -> TKN_LP·ARGS_REF·TKN_RP | ARRAY_REF | $
ARGS_REF -> EXPR·ARGS_NREF| $
ARGS_NREF -> TKN_COMMA·EXPR·ARGS_NREF | $
ARRAY_REF -> TKN_LB2·EXPR·TKN_RB2·ARRAY_NREF
ARRAY_NREF -> TKN_LB2·EXPR·TKN_RB2·ARRAY_NREF |$
DEF_STMT -> DEF·TKN_SEMI
DEF -> TYPE·DTERM·DTLIST
DTERM -> IDENTIFIER·ARRAY_DECL
ARRAY_DECL -> TKN_LB2·TKN_CONSTI·TKN_RB2·ARRAY_NDEM | $
ARRAY_NDEM -> TKN_LB2·TKN_CONSTI·TKN_RB2·ARRAY_NDEM | $
DTLIST -> TKN_COMMA·DTERM·DTLIST | $
IF_STMT -> TKN_IF·TKN_LP·EXPR·TKN_RP·CODE_BLOAK·ELSE_STMT
CODE_BLOAK -> STMT | FUNC_BODY
ELSE_STMT -> TKN_ELSE·CODE_BLOAK | $
WHILE_STMT -> TKN_WHILE·TKN_LP·EXPR·TKN_RP·WHILE_BODY
WHILE_BODY -> EXPR_STMT | TKN_LB·WHILE_BLOAK·TKN_RB
WHILE_BLOAK -> STMTS·BRE_CONTI·WHILE_BLOAK | $
CONST -> TKN_CONSTF | TKN_CONSTI
TYPE -> TKN_INT | TKN_FLOAT
OPTP1 -> TKN_ASIGN
OPTP2 -> TKN_EQU | TKN_NEQU | TKN_G | TKN_L | TKN_GE | TKN_LE
OPTP3 -> TKN_ADD | TKN_SUB
OPTP4 -> TKN_MUL | TKN_DIV
BRE_CONTI -> TKN_BREAK·TKN_SEMI | TKN_CONTINUE·TKN_SEMI | $

(3)LL(1) analyze
The number is the productions sequence number
When ambiguous production occurs,chose one to make it ll(1) grammer
The ll(1) analyze may not solve the problem of expression's priority

the ambiguous:

//WHILE_BLOAK -> STMTS·BRE_CONTI·WHILE_BLOAK | $
<WHILE_BLOAK,}>= 0 1 //1

//BRE_CONTI -> TKN_BREAK·TKN_SEMI | TKN_CONTINUE·TKN_SEMI | $
<BRE_CONTI,break>= 0 2 //0
<BRE_CONTI,continue>= 1 2 //1

3.semantic
using recursive decendence

struct ARGNODE
{
std::string name;
union
{
int index;
float varf;
int vari;
};
int type;
std::vector arr;//the array position coordinate or function args (the number of the temp var)
ARGNODE()
{
type=TYPE_EMPTY;
}
static const int TYPE_INT=TERMSET::TKN_INT;
static const int TYPE_FLOAT=TERMSET::TKN_FLOAT;
static const int TYPE_ARRAY=NTERMSET::ARRAY_REF;
static const int TYPE_TMP=NTERMSET::EXPR;
static const int TYPE_VAR=TERMSET::TKN_ID;
static const int TYPE_FUNC=NTERMSET::FUNCTION;
static const int TYPE_EMPTY=TERMSET::TKN_EMPTY;
};
struct SYMBOLNODE
{
int type;
std::string name;
std::vector dims;//demention size;if null mains virables else mains arrays
SYMBOLNODE(){}
SYMBOLNODE(int type,std::string name)
{
this->type=type;
this->name=name;
}
};
struct QUATERNION
{
int op;
struct ARGNODE arg1;
struct ARGNODE arg2;
struct ARGNODE result;
int addr;
};
struct FUNCIC//function intermediate code
{
std::string func_name;
int ret_type;
std::vector args;
std::vector vars;
std::vector quats;//quaternions

//intermediate values
int tmp_num;//temp var number
struct ARGNODE last_result;
FUNCIC()
{
    tmp_num=0;
}

};

examples:
(T means temp,L means lable)
。。。

关于Python和C/C++的Bug报告和建议解决方案

Bug report


兰天 1120151828

徐恒达 1120151811

  1. 关于 Python / C / C++ 报的 java.lang.IllegalMonitorStateException 异常
    • 原因
      scan / parser / semantic / intermediate code generate / optimization / code generate / simulate
      对应行数
      131, 151, 171, 191, 211, 231
      判读语句中
      if (pp.type.equals("python")) // 应该改成对应的scan, parser, ...
    • 上述的原因所有的 Python 程序进入了 MiniCCompile.java: run 函数中运行,并没有进入 runPy 函数中运行
    • 在 run 函数中, 创建运行时环境等待子进程结束的语句使用抛出异常 java.lang.IllegalMonitorStateException
      修改为
      p.waitFor();
  2. 关于 Python 的版本的问题
    1. Python 版本使用的 Jython 版本号是 2.6 , 大部分同学使用的是 Python3 环境开发
    2. 解决方案
      • 建议删除 MiniCCompiler.java 中的 runPy 函数,全部采用 run 函数在运行时环境中执
        行,并且建议删除 Jython 环境
      • 只需要学生在自己电脑上安装最新的 Python 环境即可
      • 并且对于 run 函数需要针对 Python 进行修改
        Process p = rt.exec("python " + path + " " + iFile + " " + oFile);
        // 或者在 config.xml 文件中,在 path 字段中写入 python path,前提是Python必须加入用户电脑的环境变量中,Linux 用户只需要对 Python 脚本加上可执行权限即可

Bug Report

bug1:

时间:2018.4.9 15:32

当输入目标文件不存在时应该直接报错停止运行,而当前版本还进行后续的操作,在run文件夹下生成空的token。

bug2:

时间:2018.4.9 15:54

当输入文件路径正确时,只是文件不在当前目录下的 input 文件夹中的时候,可以在输入文件所在目录下生成相关的文件,但是在编译过程中会显示文件未找到错误,不妨大碍,但是不太完美。
解决办法

1.在源文件中将输入路径作为后续的处理路径而非预设 ./input 为默认路径[根本方法]

或者可以:
2.将 源文件中的预设 ./input 修改为: ../input [简单方法]

Preprocessor incorrect behaviour

Consider the following case:

int main() {
  int c = 1000;
  char d = '\'';
  return c;
}

The builtin preprocessor throws exception when parsing the testcase. Also when the preprocessor parsing the testcase without the line defining variable d, it works normally.

wrong configuration variable used in supporting external python scripts

As pr #5 pointed out, there used to be an old bug related to wrong variable usage causing the external python script malfunction , according to the code below in the latest commit:

                // step 3: parser
                String pOutFile = scOutFile.replace(MiniCCCfg.MINICC_SCANNER_OUTPUT_EXT, MiniCCCfg.MINICC_PARSER_OUTPUT_EXT);

                if(parsing.skip.equals("false")){
                        if(parsing.type.equals("java")){
                                if(parsing.path != ""){
                                        Class<?> c = Class.forName(parsing.path);
                                        Method method = c.getMethod("run", String.class, String.class);
                                        method.invoke(c.newInstance(), scOutFile, pOutFile);
                                }else{
                                        MiniCCParser p = new MiniCCParser();
                                        p.run(pOutFile);
                                }
                        }else if(pp.type.equals("python")){ // <------- 'pp' was used instead of 'parsing'
                                this.runPy(scOutFile, pOutFile, parsing.path);
                        }else{
                                this.run(scOutFile, pOutFile, parsing.path);
                        }
                }

please fix this bug asap.

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.