语言

Riicarus大约 3 分钟ProjectCompilerCompilerLangrage

语言

词法

letter[azAZ]digit[09]numberdigit+constIntnumbertruetruefalsefalseconstString"(chardigit)"constFloatnumber.numbervoidvoidreturnreturnintegerintegerififelseelseelseifelseifforforfunctionfunctionfuncfunc====!=!=<=<=<<>=>=>>++//&&!!:=:=(())[[]]{{}};;idletter(letterdigit) \begin{aligned} & letter \to [a-zA-Z] \\ & digit \to [0-9] \\ & number \to digit+ \\ & constInt \to number \\ & true \to true \\ & false \to false \\ & constString \to "(char | digit)*" \\ & constFloat \to number.number \\ & void \to void \\ & return \to return \\ & integer \to integer \\ & if \to if \\ & else \to else \\ & elseif \to elseif \\ & for \to for \\ & function \to function \\ & func \to func \\ & == \to == \\ & != \to != \\ & <= \to <= \\ & < \to < \\ & >= \to >= \\ & > \to > \\ & + \to + \\ & - \to - \\ & * \to * \\ & / \to / \\ & | \to | \\ & \And \to \And \\ & ! \to ! \\ & := \to := \\ & ( \to ( \\ & ) \to ) \\ & [ \to [ \\ & ] \to ] \\ & \left\{ \right. \to \left\{ \right. \\ & \left. \right\} \to \left. \right\} \\ & ; \to ; \\ & id \to letter(letter | digit)* \\ & \end{aligned}

语法

程序

程序开始ProgramBracedCodeBlockProgramϵ \begin{aligned} & 程序开始 \\ & Program \to BracedCodeBlock \\ & Program \to \epsilon \\ \end{aligned}

代码块

 大括号括起来的代码块BracedCodeBlock{StatementList}非空代码块NonNullCodeBlockStatement代码块中的单个语句StatementDefineStatementAssignOrFuncCallStatementControlStatementBracedCodeBlock赋值或函数调用语句AssignOrFuncCallId  AssignOrFuncCallSuf;AssignOrFuncCallSufAssignSufAssignOrFuncCallSufFuncCallSufAssignSuf:=ValueExpr代码块中的语句集合StatementListStatement  StatementListStatementListϵ \begin{aligned}\ & 大括号括起来的代码块 \\ & BracedCodeBlock \to \left\{ StatementList \right\} \\ \\ & 非空代码块 \\ & NonNullCodeBlock \to Statement \\ \\ & 代码块中的单个语句 \\ & Statement \to Define \\ & Statement \to AssignOrFuncCall \\ & Statement \to Control \\ & Statement \to BracedCodeBlock \\ \\ & 赋值或函数调用语句 \\ & AssignOrFuncCall \to Id \; AssignOrFuncCallSuf; \\ & AssignOrFuncCallSuf \to AssignSuf \\ & AssignOrFuncCallSuf \to FuncCallSuf \\ & AssignSuf \to := ValueExpr \\ \\ & 代码块中的语句集合 \\ & StatementList \to Statement \; StatementList \\ & StatementList \to \epsilon \\ \end{aligned}

定义语句

定义语句,  包含变量定义和函数定义,  可以在定义时初始化(赋值)DefineType  VarDef变量或者函数定义,  可以初始化VarDefId  VarInitVarDeffunc  Id(FuncArgListDef)  BracedCodeBlock标识符Ididentifier变量也可能是函数,  直接使用值表达式赋值VarInitAssignSuf;VarInit;函数参数列表定义FuncArgListDefFuncArgDef  FuncArgListDefSufFuncArgListDefϵFuncArgListDefSuf,  FuncArgDef  FuncArgListDefSufFuncArgListDefSufϵFuncArgDefType  Id数据类型定义,  包括基础类型,  数组类型和函数类型,  函数的返回类型可以为  voidTypeBaseType  TypeSufTypeVoidFuncType  TypeSufTypeSuffunction(FuncArgTypeDef)  TypeSufTypeSufϵ基础数据类型定义BaseTypeintBaseTypeboolBaseTypefloatBaseTypestring函数类型VoidFuncTypevoid  function(FuncArgTypeDef)函数参数类型定义,  参数可以没有或者有多个FuncArgTypeDefType  FuncArgTypeDefSufFuncArgTypeDefϵFuncArgTypeDefSuf,  Type  FuncArgTypeDefSufFuncArgTypeDefSufϵ \begin{aligned} & 定义语句, \; 包含变量定义和函数定义, \; 可以在定义时初始化(赋值) \\ & Define \to Type \; VarDef \\ \\ & 变量或者函数定义, \; 可以初始化 \\ & VarDef \to Id \; VarInit \\ & VarDef \to func \; Id (FuncArgListDef) \; BracedCodeBlock \\ \\ & 标识符 \\ & Id \to identifier \\ \\ & 变量也可能是函数, \; 直接使用值表达式赋值 \\ & VarInit \to AssignSuf; \\ & VarInit \to ; \\ \\ & 函数参数列表定义 \\ & FuncArgListDef \to FuncArgDef \; FuncArgListDefSuf \\ & FuncArgListDef \to \epsilon \\ & FuncArgListDefSuf \to , \; FuncArgDef \; FuncArgListDefSuf \\ & FuncArgListDefSuf \to \epsilon \\ & FuncArgDef \to Type \; Id \\ \\ & 数据类型定义, \; 包括基础类型, \; 数组类型和函数类型, \; 函数的返回类型可以为 \; void \\ & Type \to BaseType \; TypeSuf \\ & Type \to VoidFuncType \; TypeSuf \\ & TypeSuf \to function (FuncArgTypeDef) \; TypeSuf \\ & TypeSuf \to \epsilon \\ \\ & 基础数据类型定义 \\ & BaseType \to int \\ & BaseType \to bool \\ & BaseType \to float \\ & BaseType \to string \\ \\ & 函数类型 \\ & VoidFuncType \to void \; function (FuncArgTypeDef) \\ \\ & 函数参数类型定义, \; 参数可以没有或者有多个 \\ & FuncArgTypeDef \to Type \; FuncArgTypeDefSuf \\ & FuncArgTypeDef \to \epsilon \\ & FuncArgTypeDefSuf \to , \; Type \; FuncArgTypeDefSuf \\ & FuncArgTypeDefSuf \to \epsilon \\ \end{aligned}

值语句

值语句,第一层是逻辑计算ValueExprLogicItem  LogicExprSufLogicExprSuf  LogicItem  LogicExprSufLogicExprSufϵLogicItemLogicFactor  LogicItemSufLogicItemSuf&  LogicFactor  LogicItemSufLogicItemSufϵLogicFactor!RelExprLogicFactorRelExpr关系表达式RelExprArithExpr  RelExprSufRelExprSufRelOp  ArithExpr  RelExprSufRelExprSufϵ二元关系运算符RelOp<RelOp>RelOp<=RelOp>=RelOp==RelOp!=算术表达式ArithExprArithItem  ArithExprSufArithExprSuf  ArithItem  ArithExprSufArithExprSuf+  ArithItem  ArithExprSufArithExprSufϵArithItemPrimExpr  ArithItemSufArithItemSuf  PrimExpr  ArithItemSufArithItemSuf/  PrimExpr  ArithItemSufArithItemSufϵ基础数据表达式PrimExpr(ValueExpr)PrimExprFuncInlineDefinePrimExprConstPrimExprId  FuncCallSuf常量ConstconstIntConstconstFloatConstconstStringConsttrueConstfalseConstnull函数调用FuncCallSuf(FuncCallArgs)FuncCallSufepsFuncCallArgsValueExpr  FuncCallArgsSufFuncCallArgsϵFuncCallArgsSuf,  ValueExpr  FuncCallArgsSufFuncCallArgsSufϵ行内函数定义FuncInlineDefineType  (FuncArgListDef)=>BracedCodeBlock \begin{aligned} & 值语句, 第一层是逻辑计算 \\ & ValueExpr \to LogicItem \; LogicExprSuf \\ & LogicExprSuf \to | \; LogicItem \; LogicExprSuf \\ & LogicExprSuf \to \epsilon \\ & LogicItem \to LogicFactor \; LogicItemSuf \\ & LogicItemSuf \to \And \; LogicFactor \; LogicItemSuf \\ & LogicItemSuf \to \epsilon \\ & LogicFactor \to !RelExpr \\ & LogicFactor \to RelExpr \\ \\ & 关系表达式 \\ & RelExpr \to ArithExpr \; RelExprSuf \\ & RelExprSuf \to RelOp \; ArithExpr \; RelExprSuf \\ & RelExprSuf \to \epsilon \\ \\ & 二元关系运算符 \\ & RelOp \to < \\ & RelOp \to > \\ & RelOp \to <= \\ & RelOp \to >= \\ & RelOp \to == \\ & RelOp \to != \\ \\ & 算术表达式 \\ & ArithExpr \to ArithItem \; ArithExprSuf \\ & ArithExprSuf \to - \; ArithItem \; ArithExprSuf \\ & ArithExprSuf \to + \; ArithItem \; ArithExprSuf \\ & ArithExprSuf \to \epsilon \\ & ArithItem \to PrimExpr \; ArithItemSuf \\ & ArithItemSuf \to * \; PrimExpr \; ArithItemSuf \\ & ArithItemSuf \to / \; PrimExpr \; ArithItemSuf \\ & ArithItemSuf \to \epsilon \\ \\ & 基础数据表达式 \\ & PrimExpr \to (ValueExpr) \\ & PrimExpr \to FuncInlineDefine \\ & PrimExpr \to Const \\ & PrimExpr \to Id \; FuncCallSuf \\ \\ & 常量 \\ & Const \to constInt \\ & Const \to constFloat \\ & Const \to constString \\ & Const \to true \\ & Const \to false \\ & Const \to null \\ \\ & 函数调用 \\ & FuncCallSuf \to (FuncCallArgs) \\ & FuncCallSuf \to eps \\ & FuncCallArgs \to ValueExpr \; FuncCallArgsSuf \\ & FuncCallArgs \to \epsilon \\ & FuncCallArgsSuf \to , \; ValueExpr \; FuncCallArgsSuf \\ & FuncCallArgsSuf \to \epsilon \\ \\ & 行内函数定义 \\ & FuncInlineDefine \to Type \; (FuncArgListDef) => BracedCodeBlock \\ \\ \end{aligned}

控制语句

控制语句ControlBreak;ControlContinue;ControlReturn;ControlIfControlLoop退出循环Breakbreak下一次Continuecontinue返回Returnreturn  RetValue返回值RetValueValueExprRetValueϵ \begin{aligned} & 控制语句 \\ & Control \to Break; \\ & Control \to Continue; \\ & Control \to Return; \\ & Control \to If \\ & Control \to Loop \\ \\ & 退出循环 \\ & Break \to break \\ \\ & 下一次 \\ & Continue \to continue \\ \\ & 返回 \\ & Return \to return \; RetValue \\ \\ & 返回值 \\ & RetValue \to ValueExpr \\ & RetValue \to \epsilon \\ \end{aligned}

If 语句

if  语句Ifif  (ValueExpr)  BracedCodeBlock  Elseif  条件为假时执行的语句ElseElseIfList  EndElseelse  if  语句列表ElseIfListElseIf  ElseIfListElseIfListϵelse  if  语句ElseIfelseif  (ValueExpr)  BracedCodeBlockelse  语句EndElseelse  BracedCodeBlockEndElseϵ \begin{aligned} & if \; 语句 \\ & If \to if \; (ValueExpr) \; BracedCodeBlock \; Else \\ \\ & if \; 条件为假时执行的语句 \\ & Else \to ElseIfList \; EndElse \\ \\ & else \; if \; 语句列表 \\ & ElseIfList \to ElseIf \; ElseIfList \\ & ElseIfList \to \epsilon \\ \\ & else \; if \; 语句 \\ & ElseIf \to elseif \; (ValueExpr) \; BracedCodeBlock \\ \\ & else \; 语句 \\ & EndElse \to else \; BracedCodeBlock \\ & EndElse \to \epsilon \\ \end{aligned}

循环语句

循环语句LoopForLoop \begin{aligned} & 循环语句 \\ & Loop \to ForLoop \\ \\ \end{aligned}

For 循环

For  循环ForLoopfor  (ForInit;  ForCondition;  ForUpdate)  ForBodyFor  初始化ForInitForInitListForInitϵ变量列表初始化ForInitListForAssignOrDefine  ForInitListSufForInitListSuf,  ForAssignOrDefine  ForInitListSufForInitListSufϵForAssignOrDefineType  Id  AssignSufForAssignOrDefineId  AssignSufFor  执行条件ForConditionValueExprForConditionϵFor  更新ForUpdateForInitListForUpdateϵFor循环体ForBodyBracedCodeBlock \begin{aligned} & For \; 循环 \\ & ForLoop \to for \; (ForInit; \; ForCondition; \; ForUpdate) \; ForBody \\ \\ & For \; 初始化 \\ & ForInit \to ForInitList \\ & ForInit \to \epsilon \\ \\ & 变量列表初始化 \\ & ForInitList \to ForAssignOrDefine \; ForInitListSuf \\ & ForInitListSuf \to , \; ForAssignOrDefine \; ForInitListSuf \\ & ForInitListSuf \to \epsilon \\ & ForAssignOrDefine \to Type \; Id \; AssignSuf \\ & ForAssignOrDefine \to Id \; AssignSuf \\ \\ & For \; 执行条件 \\ & ForCondition \to ValueExpr \\ & ForCondition \to \epsilon \\ \\ & For \; 更新 \\ & ForUpdate \to ForInitList \\ & ForUpdate \to \epsilon \\ \\ & For 循环体 \\ & ForBody \to BracedCodeBlock \\ \end{aligned}