这是我们在 doraHack 上用 C++ 编写的一个 miniC 编译器。
- 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
Program → FunClosure
FunClosure → Function FunClosure | ε
Function → Type Variable(ParameterDecl){FunBody}
Function → void Variable(ParameterDecl){FunBody}
Type → int | char
Variable → ID
Factor → (Expression)
Factor → VarOrFunCall
Factor → NUM | CHAR
VarOrFunCall → Variable FunSuffix
FunSuffix → (VariableList) | ε
Expression → FactorCombine Term
FactorCombine → Factor FactorRecur
FactorRecur → * Factor FactorRecur
FactorRecur → / Factor FactorRecur
FactorRecur → % Factor FactorRecur
FactorRecur → ε
Term → + FactorCombine Term
Term → - FactorCombine Term
Term → ε
ParameterDecl → Decl DeclClosure | void | ε
Decl → Type Variable Assign
Assign → RightVal | ε
RightVal → Expression
DeclClosure → , Decl DeclClosure | ε
FunBody → DeclStmtClosure FunClosure
DeclStmtClosure → DeclStmt DeclStmtClosure | ε
DeclStmt → Decl ;
FunClosure → AssignStmt FunClosure
FunClosure → ForStmt FunClosure
FunClosure → Condition FunClosure
FunClosure → ReturnStmt FunClosure
FunClosure → ScanStmt FunClosure
FunClosure → PrintStmt FunClosure
FunClosure → ε
AssignStmt → Variable AssignOrFunCall
AssignOrFunCall → RightVal ;
AssignOrFunCall → (ParameterList) ;
ParameterList → Parameter ParameterClosure
ParameterClosure → , Parameter ParameterClosure
ParameterClosure → ε
Parameter → ID | NUM | CHAR
ForStmt → for (AssignStmt LogicStmt; SuffixStmt){FunctionBody}
MultLogicExpr → LogiExpr MultLogiExprRecur
MultLogiExprRecur → AdvanLogiOp MultLogicExpr | ε
AdvanLogiOp → AND | OR
LogiExpr → Expr LogiExprRecur
LogiExprRecur → LogicOp LogiExpr | ε
LogicOp → LESS | LESSEQUAL | NOTEQUAL | EQUAL | GREATER | GREATEREQUAL
SuffixStmt → Variable SuffixOp
SuffixOp → PLUS2 | MINUS2
IfStmt → IF ( MultLogicExpr ) { FunctionBody } ElseStmt
ElseStmt → ELSE { FunctionBody } | ε
ReturnStmt → RETURN Factor ;
ScanStmt → SCAN ( Variable ) ;
PrintStmt → PRINT ( Variable ) ; | PRINT ( STRING ) ;
Lexer 读取字符,并组成符合要求的标记(Token)。
Paser 从 Lexer 中读取 Token ,并转换为中间表达式。