8000 GitHub - wxy4869/Compiler: 三秋 编译 实验
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

wxy4869/Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

Compiler

三秋 编译 实验

文法定义

CompUnit		→ {Decl} {FuncDef} MainFuncDef
DeclConstDecl | VarDecl
ConstDecl		→ 'const' BType ConstDef { ',' ConstDef } ';'
BType'int'
ConstDefIdent { '[' ConstExp ']' } '=' ConstInitVal
ConstInitValConstExp | '{' [ ConstInitVal { ',' ConstInitVal } ] '}'
VarDeclBType VarDef { ',' VarDef } ';' 
VarDefIdent { '[' ConstExp ']' } | Ident { '[' ConstExp ']' } '=' InitVal
InitValExp | '{' [ InitVal { ',' InitVal } ] '}'
FuncDefFuncType Ident '(' [FuncFParams] ')' Block
MainFuncDef'int' 'main' '(' ')' Block
FuncType'void' | 'int'
FuncFParamsFuncFParam { ',' FuncFParam }
FuncFParamBType Ident ['[' ']' { '[' ConstExp ']' }]
Block			→ '{' { BlockItem } '}'
BlockItemDecl | Stmt
StmtLVal '=' Exp ';'
			| Block
			| 'if' '(' Cond ')' Stmt [ 'else' Stmt ]
			| 'break' ';' 
			| 'continue' ';'
			| 'return' [Exp] ';' 
			| LVal '=' 'getint''('')'';'
			| 'printf''('FormatString{','Exp}')'';'
ExpAddExp
CondLOrExp 
LValIdent {'[' Exp ']'}
PrimaryExp'(' Exp ')' | LVal | Number
NumberIntConst
UnaryExpPrimaryExp | Ident '(' [FuncRParams] ')' | UnaryOp UnaryExp
UnaryOp'+' | '−' | '!'
FuncRParamsExp { ',' Exp }
MulExpUnaryExp | MulExp ('*' | '/' | '%') UnaryExp
AddExpMulExp | 
59B3
AddExp ('+' | '−') MulExp
RelExpAddExp | RelExp ('<' | '>' | '<=' | '>=') AddExp
EqExpRelExp | EqExp ('==' | '!=') RelExp
LAndExpEqExp | LAndExp '&&' EqExp
LOrExpLAndExp | LOrExp '||' LAndExp
ConstExpAddExp

运行方式

  • 新建一个项目,将 src 文件夹复制到新项目中。

  • 新建 testfile.txt 文件,将要编译的源代码写到该文件中。

  • 程序入口在 src/Compiler.java,如需修改输出阶段,可修改该文件中的 op

  • 文件组织结构:

    .
    ├── src
    ├── testfile.txt	# 要编译的源代码 
    ├── output.txt		# 词法分析和语法分析的输出
    ├── llvm_ir.txt		# llvm 中间代码的输出
    └── error.txt		# 错误处理的输出

参考

About

三秋 编译 实验

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0