8000 GitHub - Meng-XuanYu/Compiler: BUAA compiler
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Meng-XuanYu/Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compiler

BUAA compiler BY Master XuanYu

文法

编译单元 CompUnit → {Decl} {FuncDef} MainFuncDef // 1.是否存在Decl 2.是否存在FuncDef
声明 DeclConstDecl | VarDecl // 覆盖两种声明
常量声明 ConstDecl → 'const' BType ConstDef { ',' ConstDef } ';' // 1.花括号内重复0次 2.花括号内重复多次
基本类型 BType'int' | 'char' // 覆盖两种数据类型的定义
常量定义 ConstDefIdent [ '[' ConstExp ']' ] '=' ConstInitVal // 包含普通变量、一维数组两种情况
常量初值 ConstInitValConstExp | '{' [ ConstExp { ',' ConstExp } ] '}' |StringConst // 1.常表达式初值 2.一维数组初值
变量声明 VarDeclBType VarDef { ',' VarDef } ';' // 1.花括号内重复0次 2.花括号内重复多次
变量定义 VarDefIdent [ '[' ConstExp ']' ] | Ident [ '[' ConstExp ']' ] '='InitVal // 包含普通常量、一维数组定义
变量初值 InitValExp | '{' [ Exp { ',' Exp } ] '}' | StringConst // 1.表达式初值 2.一维数组初值
函数定义 FuncDefFuncType Ident '(' [FuncFParams] ')' Block // 1.无形参 2.有形参
主函数定义 MainFuncDef → 'int' 'main' '(' ')' Block // 存在main函数
函数类型 FuncType'void' | 'int' | 'char'// 覆盖三种类型的函数
函数形参表 FuncFParamsFuncFParam { ',' FuncFParam } // 1.花括号内重复0次 2.花括号内重复多次
函数形参 FuncFParamBType Ident ['[' ']'] // 1.普通变量2.一维数组变量
语句块 Block → '{' { BlockItem } '}' // 1.花括号内重复0次 2.花括号内重复多次
语句块项 BlockItemDecl | Stmt // 覆盖两种语句块项
语句 StmtLVal '=' Exp ';' // 每种类型的语句都要覆盖
| [Exp] ';' //有无Exp两种情况
| Block
| 'if' '(' Cond ')' Stmt [ 'else' Stmt ] // 1.有else 2.无else
| 'for' '(' [ForStmt] ';' [Cond] ';' [ForStmt] ')' Stmt // 1. 无缺省,1种情况 2.ForStmt与Cond中缺省一个,3种情况 3. ForStmt与Cond中缺省两个,3种情况 4. ForStmt与Cond全部缺省,1种情况
| 'break' ';' | 'continue' ';'
| 'return' [Exp] ';' // 1.有Exp 2.无Exp
| LVal '=' 'getint''('')'';'
| LVal '=' 'getchar''('')'';'
| 'printf''('StringConst {','Exp}')'';' // 1.有Exp 2.无Exp
语句 ForStmtLVal '=' Exp // 存在即可
表达式 ExpAddExp // 存在即可
条件表达式 CondLOrExp // 存在即可
左值表达式 LValIdent ['[' Exp ']'] //1.普通变量、常量 2.一维数组
基本表达式 PrimaryExp'(' Exp ')' | LVal | Number | Character// 四种情况均需覆盖
数值 NumberIntConst // 存在即可,IntConst详细解释见下方 (3) 数值常量
字符 CharacterCharConst // CharConst详细解释见下方 (4) 字符常量
一元表达式 UnaryExpPrimaryExp | Ident '(' [FuncRParams] ')' | UnaryOp UnaryExp// 3种情况均需覆盖,函数调用也需要覆盖FuncRParams的不同情况
单目运算符 UnaryOp'+' | '−' | '!' :'!'仅出现在条件表达式中 // 三种均需覆盖
函数实参表 FuncRParamsExp { ',' Exp } // 1.花括号内重复0次 2.花括号内重复多次 3.Exp需要覆盖数组传参和部分数组传参
乘除模表达式 MulExpUnaryExp | MulExp ('*' | '/' | '%') UnaryExp // 1.UnaryExp 2.* 3./ 4.% 均需覆盖
加减表达式 AddExpMulExp | AddExp ('+' | '−') MulExp // 1.MulExp 2.+ 需覆盖 3.- 需覆盖
关系表达式 RelExpAddExp | RelExp ('<' | '>' | '<=' | '>=') AddExp // 1.AddExp 2. < 3.> 4.<= 5.>= 均需覆盖
相等性表达式 EqExpRelExp | EqExp ('==' | '!=') RelExp // 1.RelExp 2.== 3.!= 均需覆盖
逻辑与表达式 LAndExpEqExp | LAndExp '&&' EqExp // 1.EqExp 2.&& 均需覆盖
逻辑或表达式 LOrExpLAndExp | LOrExp '||' LAndExp // 1.LAndExp 2.|| 均需覆盖
常量表达式 ConstExpAddExp 使用的 Ident 必须是常量 // 存在即可

About

BUAA compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0