Skip to content

fipped/MiniC

Repository files navigation

MiniC 编译器

这是我们在 doraHack 上用 C++ 编写的一个 miniC 编译器。

标记(Token)

  • keywords = if|else|while|for|switch|case|deafult|break|return|void|int|char|scan|print
  • letter = a | ... | z | A | ... | Z
  • digit = 0 | ... | 9
  • space = '\t',' ','\n'
  • NUM = digit+
  • ID = letter(letter|digit)*
  • STRING = "最多 1024 个字符. " 可以被 \ 转义."
  • CHAR = '任何字符'
  • PLUS = +
  • MINUS = -
  • DIV = /
  • MULT = *
  • MOD = %
  • PLUS2 = ++
  • MINUS2 = --
  • ASSIGN = =
  • EQUAL = ==
  • GREATER = >
  • GREATEREQUAL = >=
  • LESS = <
  • LESSEQUAL = <=
  • NOTEQUAL = !=
  • LBRAC = {
  • RBRAC = }
  • LPAREN = (
  • RPAREN = )
  • LBRACE = [
  • RBRACE = ]
  • COLON = :
  • COMMA = ,
  • SEMICOLON = ;
  • END_OF_FILE = EOF

文法(Grammar)

ProgramFunClosure

FunClosureFunction FunClosure | ε

FunctionType Variable(ParameterDecl){FunBody}

Function → void Variable(ParameterDecl){FunBody}

Type → int | char

Variable → ID

Factor → (Expression)

FactorVarOrFunCall

Factor → NUM | CHAR

VarOrFunCallVariable FunSuffix

FunSuffix → (VariableList) | ε

ExpressionFactorCombine Term

FactorCombineFactor FactorRecur

FactorRecur → * Factor FactorRecur

FactorRecur → / Factor FactorRecur

FactorRecur → % Factor FactorRecur

FactorRecur → ε

Term → + FactorCombine Term

Term → - FactorCombine Term

Term → ε

ParameterDeclDecl DeclClosure | void | ε

DeclType Variable Assign

AssignRightVal | ε

RightValExpression

DeclClosure → , Decl DeclClosure | ε

FunBodyDeclStmtClosure FunClosure

DeclStmtClosureDeclStmt DeclStmtClosure | ε

DeclStmtDecl ;

FunClosureAssignStmt FunClosure

FunClosureForStmt FunClosure

FunClosureCondition FunClosure

FunClosureReturnStmt FunClosure

FunClosureScanStmt FunClosure

FunClosurePrintStmt FunClosure

FunClosure → ε

AssignStmtVariable AssignOrFunCall

AssignOrFunCallRightVal ;

AssignOrFunCall → (ParameterList) ;

ParameterListParameter ParameterClosure

ParameterClosure → , Parameter ParameterClosure

ParameterClosure → ε

Parameter → ID | NUM | CHAR

ForStmt → for (AssignStmt LogicStmt; SuffixStmt){FunctionBody}

MultLogicExprLogiExpr MultLogiExprRecur

MultLogiExprRecurAdvanLogiOp MultLogicExpr | ε

AdvanLogiOp → AND | OR

LogiExprExpr LogiExprRecur

LogiExprRecurLogicOp LogiExpr | ε

LogicOp → LESS | LESSEQUAL | NOTEQUAL | EQUAL | GREATER | GREATEREQUAL

SuffixStmtVariable SuffixOp

SuffixOp → PLUS2 | MINUS2

IfStmt → IF ( MultLogicExpr ) { FunctionBody } ElseStmt

ElseStmt → ELSE { FunctionBody } | ε

ReturnStmt → RETURN Factor ;

ScanStmt → SCAN ( Variable ) ;

PrintStmt → PRINT ( Variable ) ; | PRINT ( STRING ) ;

模块(Modules)

词法分析器 Lexer

Lexer 读取字符,并组成符合要求的标记(Token)。

语法分析器 Paser

Paser 从 Lexer 中读取 Token ,并转换为中间表达式。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors