ååž°çéäžã«ãã 解æãšæŒç®åã®åªäœæ§ã®è§£æ ïŒåŸè ã¯ãã€ããªåŒçšã§ãæåã¯ä»ã®ãã¹ãŠçšïŒã®çµã¿åããã䜿çšããŠãäžè¯é¡èšèªçšã®ããŒãµãŒãéçºããŸãã 解æèªäœã«é²ãåã«ãåºåã§åŸããããã®ã«ã€ããŠè©±ããŸãããïŒAbstract Syntax Treeã
æœè±¡æ§æããªãŒïŒASTïŒ
ASTã¯ãã³ã³ãã€ã©ãŒã®åŸã®æ®µéïŒã³ãŒãçæãªã©ïŒã§ç°¡åã«è§£éã§ããããã«ããã°ã©ã ã衚瀺ããŸãã èšèªæ§æããšã«1ã€ã®ãªããžã§ã¯ããå¿ èŠã§ãã ã«ã¬ã€ãã¹ã³ãŒãã«ã¯ãåŒããããã¿ã€ããããã³é¢æ°ããããŸãã åŒããå§ããŸãããïŒ
/// ExprAST - . class ExprAST { public: virtual ~ExprAST() {} }; /// NumberExprAST - (, "1.0"). class NumberExprAST : public ExprAST { double Val; public: NumberExprAST(double val) : Val(val) {} };
äžèšã®ã³ãŒãã¯ãæ°å€ãªãã©ã«ã«äœ¿çšããExprASTåºæ¬ã¯ã©ã¹ãšãã®ãµãã¯ã©ã¹ã®å®çŸ©ã瀺ããŠããŸãã
ããã§ãããŸããŸãªäŸ¿å©ãªæäœæ¹æ³ãªãã§ASTã®ã¿ãäœæããŸãã å¿ èŠã«å¿ããŠãããšãã°ããã©ãŒããããããã³ãŒãåºåçšã®ä»®æ³ã¡ãœãããè¿œå ããã®ã¯éåžžã«ç°¡åã§ãã ã«ã¬ã€ãã¹ã³ãŒãã§äœ¿çšããä»ã®ASTåŒã®ããŒãå®çŸ©ã¯æ¬¡ã®ãšããã§ãã
/// VariableExprAST - (, "a"). class VariableExprAST : public ExprAST { std::string Name; public: VariableExprAST(const std::string &name) : Name(name) {} }; /// BinaryExprAST - . class BinaryExprAST : public ExprAST { char Op; ExprAST *LHS, *RHS; public: BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) : Op(op), LHS(lhs), RHS(rhs) {} }; /// CallExprAST - . class CallExprAST : public ExprAST { std::string Callee; std::vector<ExprAST*> Args; public: CallExprAST(const std::string &callee, std::vector<ExprAST*> &args) : Callee(callee), Args(args) {} };
ç°¡åã§ããå€æ°ã«ã¯å€æ°åãå«ãŸãããã€ããªæŒç®åã«ã¯ãã®ãªãã³ãŒãïŒããšãã°ã+ãïŒããã³å·Šå³åŒïŒASTããŒãïŒãå«ãŸããé¢æ°åŒã³åºãã«ã¯é¢æ°åãšãã¹ãŠã®åŒæ°ã®ãªã¹ããå«ãŸããŸãã ASTã®çŽ æŽãããç¹ã®1ã€ã¯ãããã°ã©ãã³ã°èšèªã®æ§æã«é¢ä¿ãªãèšèªæ©èœãã«ããŒã§ããããšã§ãã äºé æŒç®åãåå¥æ§é ãªã©ã®åªå 床ã«ã€ããŠã¯ãè«äºã®ããåé¡ã¯ãªãããšã«æ³šæããŠãã ããã
èšèªã®è¡šçŸã®ãã¹ãŠã®ããŒããç¹å®ããŸããã ãã¥ãŒãªã³ã°å®å šã§ã¯ãããŸãããæ¡ä»¶ä»ãå¶åŸ¡ãããŒããªãããã次ã®éšåã§ä¿®æ£ããŸãã 次ã®2ã€ã®å¿ èŠãªãã®ã¯ãé¢æ°ã€ã³ã¿ãŒãã§ã€ã¹ãšé¢æ°èªäœã§ãã
/// PrototypeAST - "" , /// (, , /// ). class PrototypeAST { std::string Name; std::vector<std::string> Args; public: PrototypeAST(const std::string &name, const std::vector<std::string> &args) : Name(name), Args(args) {} }; /// FunctionAST - class FunctionAST { PrototypeAST *Proto; ExprAST *Body; public: FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {} };
Kaleidoscopeã§ã¯ãé¢æ°ã¯åŒæ°ã®æ°ã«ãã£ãŠã®ã¿å ¥åãããŸãã ãã¹ãŠã®å€ã¯å粟床ã®å®æ°ã§ãããããååŒæ°ã®åãã©ãã«ã§ãæ ŒçŽããããšã¯æå³ããããŸããã å®éã®ããã°ã©ãã³ã°èšèªã§ã¯ãããããExprASTã¯ã©ã¹ã«ã¯å¥ã®åãã£ãŒã«ããå«ãŸããŸãã
ããã§ãã«ã¬ã€ãã¹ã³ãŒãã§ã®åŒãšé¢æ°ã®è§£æã«ã€ããŠæçµçã«èª¬æã§ããŸãã
ããŒãµãŒããŒã¹
ASTèŠçŽ ãã§ããã®ã§ãã³ãŒãããŒãµãŒãå®çŸ©ããŠãã«ãããå¿ èŠããããŸãã ããã§ã®èãæ¹ã¯ããx + yãïŒåå¥è§£æäžã«3ã€ã®ããŒã¯ã³ã®åœ¢åŒã§è¿ãããïŒã®ãããªãã®ãã次ã®ãããªãã®ã«ãã£ãŠçæå¯èœãªASTã«è§£æããããšã§ãã
ExprAST *X = new VariableExprAST("x"); ExprAST *Y = new VariableExprAST("y"); ExprAST *Result = new BinaryExprAST('+', X, Y);
ãããè¡ãåã«ãããã€ãã®è£å©æé ãå®çŸ©ããŸãã
/// CurTok/getNextToken - . CurTok - /// , . getNextToken /// CurTok. static int CurTok; static int getNextToken() { return CurTok = gettok(); }
ãã®ã³ãŒãã¯ãåå¥ã¢ãã©ã€ã¶ãŒã®äžã«æãåçŽãªããŒã¯ã³ãããã¡ãŒãå®è£ ããŸãã ããã«ãããåå¥è§£æããã°ã©ã ã«ãã£ãŠè¿ããã1ã€ã®ããŒã¯ã³ãæåŸ ã§ããŸãã ããŒãµãŒã®åé¢æ°ã¯ãCurTokãæ§æ解æã®çŸåšã®ããŒã¯ã³ã§ãããšèŠãªããŸãã
/// Error* - . ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
ãããã¯ãããŒãµãŒããšã©ãŒãåŠçããããã«äœ¿çšããè£å©ããã·ãŒãžã£ã§ãã ããŒãµãŒã§ã®ãšã©ãŒã®ä¿®æ£ã¯æé«ãšã¯ã»ã©é ãããŠãŒã¶ãŒã«ãšã£ãŠç¹ã«äŸ¿å©ã§ã¯ãããŸãããããã®ãã¥ãŒããªã¢ã«ã®ãã¬ãŒã ã¯ãŒã¯å ã§ã¯ååã§ãã
ãããã®ãã«ããŒé¢æ°ã䜿çšããŠãææ³ã®æåã®éšåã§ããæ°å€ãªãã©ã«ãå®è£ ã§ããŸãã
åŒã®è§£æ
æ°å€ãªãã©ã«ããå§ããŸããããæãç°¡åãªæ¹æ³ã§ãã ææ³èŠåããšã«ããã®èŠåãåæããé¢æ°ãå®çŸ©ããŸãã æ°å€ãªãã©ã«ã®å Žåã次ã®ãã®ããããŸãã
/// numberexpr ::= number static ExprAST *ParseNumberExpr() { ExprAST *Result = new NumberExprAST(NumVal); getNextToken(); // return Result; }
ãã®é¢æ°ã¯éåžžã«åçŽã§ããåŒã³åºããããšãçŸåšã®ããŒã¯ã³ãtok_numberã§ãããšæ³å®ãããŸãã NumberExprASTããŒããäœæããçŸåšã®æ°å€ãæž¡ããåå¥è§£æããã°ã©ã ã次ã®ããŒã¯ã³ã«ç§»åããŠãäœæãããããŒããè¿ããŸãã
ããã€ãã®èå³æ·±ãåŽé¢ããããŸãã æãéèŠãªããšã¯ããã®é¢æ°ã¯ãææ³èŠåã«äžèŽããããŒã¯ã³ãåãåãã次ã®ããŒã¯ã³ïŒææ³èŠåã®äžéšã§ã¯ãªãïŒãåŠçå¯èœãªã¬ãã·ã«ã«ã¢ãã©ã€ã¶ãŒãããã¡ãŒãè¿ãããšã§ãã ããã¯ãååž°éäžã«ãã解æã®æšæºçãªæ¹æ³ã§ãã ãããããããç解ããããã«ãæ¬åŒ§å ã®åŒã®è§£æãæ€èšããŠãã ããã
/// parenexpr ::= '(' expression ')' static ExprAST *ParseParenExpr() { getNextToken(); // (. ExprAST *V = ParseExpression(); if (!V) return 0; if (CurTok != ')') return Error("expected ')'"); getNextToken(); // ). return V; }
ãã®é¢æ°ã¯ãããŒãµãŒã«é¢ããããã€ãã®èå³æ·±ãããšã瀺ããŠããŸãã
- ãšã©ãŒæé ã®äœ¿çšæ¹æ³ã瀺ããŸãã åŒã³åºããããšãé¢æ°ã¯çŸåšã®ããŒã¯ã³ãèšå· 'ïŒ'ã§ããããšãäºæããŸãããéšååŒã解æããåŸãäºæããã 'ïŒ'ãååšããªãå¯èœæ§ããããŸãã ããšãã°ããŠãŒã¶ãŒããïŒ4ïŒãã§ã¯ãªããïŒ4 xããšå ¥åããå Žåãã¢ãã©ã€ã¶ãŒã¯ãšã©ãŒã衚瀺ããå¿ èŠããããŸãããã®ãããªãšã©ãŒãçºçããå¯èœæ§ããããããããŒãµãŒã¯ãšã©ãŒãçºçããããšã瀺ãæ¹æ³ãå¿ èŠãšããŸãã
- ãã®é¢æ°ã®ãã1ã€ã®èå³æ·±ãåŽé¢ã¯ã ParseExpressionãåŒã³åºããšãã«ååž°ã䜿çšããããšã§ã ïŒ ParseExpressionãParseParenExprãåŒã³åºãããšãã§ããããšãããã«ããããŸãïŒã ããã¯åŒ·åãªã¡ã«ããºã ã§ããååž°çãªææ³ãåŠçãããã®ãããªææ³ã®ã«ãŒã«ã«éåžžã«ç°¡åã«å¯ŸåŠã§ããããã§ãã æ¬åŒ§çšã®ASTããŒãã¯äœæãããªãããšã«æ³šæããŠãã ããã æ¬åŒ§ã®éèŠãªåœ¹å²ã¯ãããŒãµãŒã«ã°ã«ãŒãåæ©èœãæäŸããããšã§ãã ããŒãµãŒãASTãæ§ç¯ãããšãæ¬åŒ§ã¯äžèŠã«ãªããŸãã
/// identifierexpr /// ::= identifier /// ::= identifier '(' expression* ')' static ExprAST *ParseIdentifierExpr() { std::string IdName = IdentifierStr; getNextToken(); // . if (CurTok != '(') // . return new VariableExprAST(IdName); // . getNextToken(); // ( std::vector<ExprAST*> Args; if (CurTok != ')') { while (1) { ExprAST *Arg = ParseExpression(); if (!Arg) return 0; Args.push_back(Arg); if (CurTok == ')') break; if (CurTok != ',') return Error("Expected ')' or ',' in argument list"); getNextToken(); } } // ')'. getNextToken(); return new CallExprAST(IdName, Args); }
ãã®é¢æ°ã¯ãåã®ãã®ãšåãããã«æ©èœããŸãã ïŒåŒã³åºããããšãçŸåšã®ããŒã¯ã³tok_identifierãå¿ èŠã§ãïŒã ãŸããååž°ãšãšã©ãŒåŠçã䜿çšããŸãã èå³æ·±ãç¹ã®1ã€ã¯ãçŸåšã®èå¥åãå€æ°ãžã®åç §ãé¢æ°åŒã³åºãããå€æããããã«å èªã¿ããªããã°ãªããªãããšã§ãã ããã¯ãèå¥åã«ç¶ãããŒã¯ã³ãèšå· 'ïŒ'ã§ãããã©ããã«å¿ããŠããã§ãã¯ã§ããVariableExprASTãŸãã¯CallExprASTãåŒã³åºããŸãã
æãåçŽãªåŒã解æããããã®ãã¹ãŠã®ããžãã¯ãæã£ãã®ã§ã1ã€ã®ãšã³ããªãã€ã³ãã«ã©ããããè£å©é¢æ°ãå®çŸ©ã§ããŸãã ãã®åŒã¯ã©ã¹ããprimaryãïŒprimaryïŒãšåŒã³ãŸããçç±ã¯ããã¥ãŒããªã¢ã«ã®çµããã«åãã£ãŠæããã«ãªãã§ãããã ä»»æã®äžæ¬¡åŒã解æããã«ã¯ãã©ã®ãããªåŒã§ããããå€æããå¿ èŠããããŸãã
/// primary /// ::= identifierexpr /// ::= numberexpr /// ::= parenexpr static ExprAST *ParsePrimary() { switch (CurTok) { default: return Error("unknown token when expecting an expression"); case tok_identifier: return ParseIdentifierExpr(); case tok_number: return ParseNumberExpr(); case '(': return ParseParenExpr(); } }
ãã®é¢æ°ã®å®çŸ©ã確èªã§ããã®ã§ãããŸããŸãªé¢æ°ã§æå¹ãªCurTokç¶æ ãæ³å®ã§ããçç±ãããæ確ã«ãªããŸãã ãã®å Žåãå èªã¿ã䜿çšããŠãã©ã®çš®é¡ã®åŒããã§ãã¯ããå¿ èŠãããããå€æãããã®åŸãåŒã³åºããã察å¿ããé¢æ°ã§åŒèªäœãåæããŸãã
åºæ¬çãªåŒãåŠçã§ããããã«ãªã£ãã®ã§ããã€ããªåŒãæäœã§ããŸãã ãããã¯ã¯ããã«è€éã§ãã
ãã€ããªåŒã®è§£æ
ãã€ããªåŒã¯ãããŸãã«ãªãããšãå€ãããã解æãã¯ããã«å°é£ã§ãã ããšãã°ãæååãx + y * zãã®å ŽåãããŒãµãŒã¯ãïŒx + yïŒ* zããŸãã¯ãx +ïŒy * zïŒããšããŠè§£æã§ããŸãã æ°åŠã®åºæ¬ãç¥ã£ãŠããã°ãã*ãïŒä¹ç®ïŒã®åªå é äœãã+ãïŒå ç®ïŒãããé«ãããã2çªç®ã®ãªãã·ã§ã³ãå¿ èŠã§ãã
å€ãã®å¯èœãªè§£æ±ºçããããŸãããæããšã¬ã¬ã³ãã§å¹ççãªæ¹æ³ã¯ãæŒç®åã®åªäœæ§ã解æããããšã§ãã ãã®è§£æææ³ã§ã¯ãååž°ã䜿çšããŠæäœã®åªå é äœã決å®ããŸãã ãŸããåªå 床ããŒãã«ãå¿ èŠã§ãã
/// BinopPrecedence - static std::map<char, int> BinopPrecedence; /// GetTokPrecedence - . static int GetTokPrecedence() { if (!isascii(CurTok)) return -1; // , . int TokPrec = BinopPrecedence[CurTok]; if (TokPrec <= 0) return -1; return TokPrec; } int main() { // // 1 - . BinopPrecedence['<'] = 10; BinopPrecedence['+'] = 20; BinopPrecedence['-'] = 20; BinopPrecedence['*'] = 40; // . ... }
Kaleidoscopeã¯4ã€ã®äºé æŒç®åã®ã¿ããµããŒãããŸãïŒãã ããåæ¢ã§å€§èäžæµãªèªè ã«ãã£ãŠæããã«æ¡åŒµã§ããŸãïŒã GetTokPrecedenceé¢æ°ã¯ãçŸåšã®ããŒã¯ã³ã®åªå é äœãè¿ããŸããããŒã¯ã³ããã€ããªæŒç®åã§ãªãå Žåã¯-1ãè¿ããŸãã ãã®ãããªããŒãã«ãååšãããšãæ°ããæŒç®åãç°¡åã«è¿œå ã§ããŸãããã¡ãããã¢ã«ãŽãªãºã ã¯ç¹å®ã®æŒç®åã«äŸåããŸããã åªå é äœããŒãã«ãç°¡åã«åé€ãã GetTokPrecedenceé¢æ°ã§æ¯èŒãè¡ãããšãã§ããŸãã
ããã§ããã€ããªåŒã®è§£æãéå§ã§ããŸãã æŒç®åã®åªäœæ§ã解æããäž»ãªã¢ã€ãã¢ã¯ãæœåšçã«ææ§ãªãã€ããªæŒç®åãå«ãåŒãéšåã«åå²ããããšã§ãã ããšãã°ããa + b +ïŒc + dïŒ* e * f + gããšããåŒãèããŠã¿ãŸãããã æŒç®ååªå é äœããŒãµãŒã¯ãããã2é æŒç®åã§åºåãããäžæ¬¡åŒã®ã¹ããªãŒã ãšèŠãªããŸãã ãããã£ãŠãäžæ¬¡è§£æäžã«ãå é ã®äžæ¬¡åŒãaããæåã«è§£æããã[+ãb] [+ãïŒc + dïŒ] [*ã] [*ãf]ããã³[+ãg ]ã æ¬åŒ§ã«æ³šæããŠãã ããïŒããŒãµãŒã¯ïŒc + dïŒã®ãããªãã¹ããããåŒã«ã€ããŠå¿é ããå¿ èŠã¯ãããŸããã
ãŸããåŒã¯äžæ¬¡åŒã§ããã[binopãprimaryexpr]ãã¢ã®ã·ãŒã±ã³ã¹ãåŸã«ç¶ãå¯èœæ§ããããŸãã
/// expression /// ::= primary binoprhs /// static ExprAST *ParseExpression() { ExprAST *LHS = ParsePrimary(); if (!LHS) return 0; return ParseBinOpRHS(0, LHS); }
ParseBinOpRHSã¯ããã¢ã®ã·ãŒã±ã³ã¹ãåæããé¢æ°ã§ãã åªå ãããåæçšã®åŒãžã®ãã€ã³ã¿ã䜿çšãããŸãã ãxãã¯çµ¶å¯Ÿã«æå¹ãªåŒã§ããããšã«æ³šæããŠãã ããããããã£ãŠããbinoprhsãã¯ç©ºã®å Žåãããããã®å Žåãé¢æ°ã¯æž¡ãããåŒãè¿ããŸãã äžèšã®äŸã§ã¯ãã³ãŒãã¯åŒãaããšçŸåšã®ããŒã¯ã³ã+ããParseBinOpRHSã«æž¡ããŸãã
ParseBinOpRHSã«æž¡ãããåªå 床ã®å€ã¯ããããåãå ¥ããããã«å¿ èŠãªãªãã¬ãŒã¿ãŒã®æå°åªå 床ã瀺ããŸãã ããšãã°ãã¹ããªãŒã [+ãx]ããã³ParseBinOpRHSã®çŸåšã®ãã¢ãåªå 床40ã«è»¢éãããå ŽåãããŒã¯ã³ã¯åãå ¥ããããŸããïŒåªå 床 "+"ã¯20ã®ã¿ã§ããããïŒã ãããã£ãŠãParseBinOpRHSã¯æ¬¡ã§å§ãŸããŸãã
/// binoprhs /// ::= ('+' primary)* static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { // , while (1) { int TokPrec = GetTokPrecedence(); // , // , if (TokPrec < ExprPrec) return LHS;
ãã®ã³ãŒãã¯çŸåšã®ããŒã¯ã³ãåªå ãããããã©ãã ãäœããããã§ãã¯ããŸãã -1ã®åªå 床ãæã€ããŒã¯ã³ã¯ç¡å¹ã§ãããšå€æããããããã¢ã®ã¹ããªãŒã ãçµäºããã¿ã€ãã³ã°ã決å®ããæé»ã®ãã§ãã¯ãçºçããŸãã ãã®ãã§ãã¯ã«æåããå ŽåãããŒã¯ã³ã¯ééããªãäºé æŒç®åã§ãããåŒã«å«ãŸããããšãããããŸãã
// , , . int BinOp = CurTok; getNextToken(); // // ExprAST *RHS = ParsePrimary(); if (!RHS) return 0;
ãããã£ãŠããã®ã³ãŒãã¯äºé æŒç®åãåãåãïŒãããŠèšæ¶ãïŒããã®åŸã«ç¶ãäžæ¬¡åŒã解æããŸãã ãã¢ãäœæããŸãããã®äŸã§ã¯ãæåã®ãã¢ã¯[+ãb]ã§ãã
åŒã®å·ŠåŽãšRHSã·ãŒã±ã³ã¹ã®1ã€ã®ãã¢ãåæããã®ã§ãåŒããã€ã³ãããæ¹æ³ã決å®ããå¿ èŠããããŸãã ç¹ã«ããïŒa + bïŒbinop unparsedããšãa +ïŒb binop unparsedïŒãã®äž¡æ¹ã䜿çšã§ããŸãïŒbinopã¯ãã€ããªæŒç®åãunparsedã¯æªã¢ã»ã³ãã«éšåã§ãïŒã 決å®ããã«ã¯ãå¥ã®ãbinopãïŒäºé æŒç®åïŒã䜿çšããŠãã®åªå 床ã決å®ãããããBinOpã®åªå 床ïŒãã®å Žåã¯ã+ãïŒãšæ¯èŒããŸãã
// BinOp RHS , RHS, // RHS LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) {
ãRHSãã®å³åŽã®äºé æŒç®åã®åªå 床ãçŸåšã®æŒç®åã®åªå 床以äžã§ããå ŽåããïŒa + bïŒbinop ...ããšããã±ãŒã¹ãããããšãããããŸãã ãã®äŸã§ã¯ãçŸåšã®ã+ãæŒç®åãšæ¬¡ã®ã+ãæŒç®åã®åªå é äœã¯åãã§ãã ãããã£ãŠããa + bãã®ASTããŒããäœæãã解æãç¶è¡ããŸãã
... ... } // LHS/RHS. LHS = new BinaryExprAST(BinOp, LHS, RHS); } }
ãã®äŸã§ã¯ããa + b +ãã¯ãïŒa + bïŒããšããŠè¿ãããã«ãŒãã®æ¬¡ã®å埩ãå®è¡ãããã+ããçŸåšã®ããŒã¯ã³ã«ãªããŸãã ãã®éšåã¯åãå ¥ããããããã«æãåºãããã«ããïŒc + dïŒãã¯äžæ¬¡åŒãšããŠè§£æãããå¿ èŠããããŸããã€ãŸããçŸåšã®ãã¢ã¯[+ãïŒc + dïŒ]ã«ãªããŸãã 次ã«ããã€ããªæŒç®åãšããŠã*ããå«ãåŒãåä¿¡ãããŸãã ãã®å Žåãåªå 床ã*ãã¯åªå 床ã+ããããé«ããããäžèšã®æ¡ä»¶ä»ãæ§ç¯ãå®è¡ãããŸãã
ããã§æãéèŠãªå·ŠåŽã®è³ªåã¯ãããã®ç¶æ ã§å³åŽãå®å šã«å解ããæ¹æ³ãã§ãã ç¹ã«ããã®äŸã§ASTãæ£ããæ§ç¯ããã«ã¯ãRHSåŒå€æ°ãšããŠå®å šãªåŒãïŒc + dïŒ* e * fããååŸããå¿ èŠããããŸãã ãã®ã³ãŒãã¯é©ãã»ã©åçŽã§ãïŒäžèšã®2ã€ã®ãããã¯ã®ã³ãŒãã¯ã³ã³ããã¹ãã®ããã«è€è£œãããŠããŸãïŒïŒ
// BinOp RHS , RHS, // RHS LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) {
RHS = ParseBinOpRHS(TokPrec+1, RHS); if (RHS == 0) return 0;
} // LHS/RHS. LHS = new BinaryExprAST(BinOp, LHS, RHS); } }
çŸæç¹ã§ã¯ãäžæ¬¡åŒã®å³åŽã®äºé æŒç®åã¯ãçŸåšè§£æãããŠããäºé æŒç®åãããåªå 床ãé«ãããšãããã£ãŠããŸãã ãããã£ãŠãã+ããããé«ãåªå 床ãæã€æŒç®åã®ãã¢ã®ã·ãŒã±ã³ã¹ãå解ãããRHSããšããŠè¿ãå¿ èŠãããããšãããããŸãã ãããè¡ãã«ã¯ãç¶è¡ããããã«å¿ èŠãªæå°åªå 床ãšããŠãã©ã¡ãŒã¿ãŒãTokPrec + 1ããæå®ããŠParseBinOpRHSé¢æ°ãååž°çã«åŒã³åºããŸãã é·ãéèŠåŽããŠããäŸã§ã¯ãããã«ããASTããŒãã¯RHSãšããŠãïŒc + dïŒ* e * fããè¿ãããããã+ããæã€åŒã®å³åŽã«ãªããŸãã
æåŸã«ãwhileã«ãŒãã®æ¬¡ã®å埩ã§ã+ géšåã解æãããASTã«è¿œå ãããŸãã ãã®å°ããªã³ãŒãïŒ14è¡ïŒã®å©ããåããŠããšã¬ã¬ã³ããªè§£ææ¹æ³ã䜿çšããŠãã€ããªåŒãæ£ããåŠçããŸãã ããã¯ã³ãŒãã®ç°¡åãªæŠèŠã§ããã®ã§ãããã€ãã®äŸã䜿çšããŠå®è¡ããŠãã©ã®ããã«æ©èœãããã確èªããããšããå§ãããŸãã
ããã§ãåŒã®åŠçãå®äºããŸãã çŸæç¹ã§ã¯ãã¢ãã©ã€ã¶ãŒã«ããŒã¯ã³ã®ä»»æã®ã¹ããªãŒã ãæ¢ã«ç€ºããããããåŒãäœæããåŒã®äžéšã§ã¯ãªãæåã®ããŒã¯ã³ã§åæ¢ã§ããŸãã ããã§ãé¢æ°å®£èšãªã©ã«å¯ŸåŠããå¿ èŠããããŸãã
æ®ãã®è§£æ
次ã«æ¬ ããŠããã®ã¯ããããã¿ã€ãé¢æ°ã®åŠçã§ãã ã«ã¬ã€ãã¹ã³ãŒãã§ã¯ãå€éšé¢æ°ã®å®£èšãšé¢æ°ã®æ¬äœã®å®£èšã®äž¡æ¹ã«äœ¿çšãããŸãã 解æã®ãã®éšåã®ã³ãŒãã¯åçŽã§ãããŸãèå³æ·±ããã®ã§ã¯ãããŸããïŒåŒã®è§£æãçã延ã³ãåŸïŒã
/// prototype /// ::= id '(' id* ')' static PrototypeAST *ParsePrototype() { if (CurTok != tok_identifier) return ErrorP("Expected function name in prototype"); std::string FnName = IdentifierStr; getNextToken(); if (CurTok != '(') return ErrorP("Expected '(' in prototype"); // . std::vector<std::string> ArgNames; while (getNextToken() == tok_identifier) ArgNames.push_back(IdentifierStr); if (CurTok != ')') return ErrorP("Expected ')' in prototype"); // . getNextToken(); // ')'. return new PrototypeAST(FnName, ArgNames); }
é¢æ°ã宣èšããåçŽããèãããšãé¢æ°ã®æ¬äœã«ã¯ãããã¿ã€ã+åŒãå«ãŸããŸãã
/// definition ::= 'def' prototype expression static FunctionAST *ParseDefinition() { getNextToken(); // def. PrototypeAST *Proto = ParsePrototype(); if (Proto == 0) return 0; if (ExprAST *E = ParseExpression()) return new FunctionAST(Proto, E); return 0; }
ããã«ããsinãããcosããªã©ã®é¢æ°ã宣èšãããŠãŒã¶ãŒå®çŸ©é¢æ°ã®æ©æ宣èšããµããŒãããã«ã¯ããå€éšããµããŒããå¿ èŠã§ãã ãexternãã¯ãæ¬äœã®ãªããããã¿ã€ãã®ã¿ã§æ§æãããŸãã
/// external ::= 'extern' prototype static PrototypeAST *ParseExtern() { getNextToken(); // extern. return ParsePrototype(); }
æåŸã«ããŠãŒã¶ãŒãä»»æã®ãããã¬ãã«ã®åŒãå ¥åãããã®å Žã§è©äŸ¡ã§ããããã«ããå¿ èŠããããŸãã å¿åã®ãã«åŒæ°ïŒåŒæ°ãªãïŒé¢æ°ãå®çŸ©ããŠåŠçããŸãïŒ
/// toplevelexpr ::= expression static FunctionAST *ParseTopLevelExpr() { if (ExprAST *E = ParseExpression()) { // . PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>()); return new FunctionAST(Proto, E); } return 0; }
ãã¹ãŠã®éšåãæã£ãã®ã§ãäœæãããã¹ãŠã®ã³ãŒããè©Šãããšãã§ããå°ããªå¶åŸ¡ããã°ã©ã ãäœæããŸãããïŒ
管çããã°ã©ã
å¶åŸ¡ããã°ã©ã ã¯ãåçŽã«åšæçã«è§£æãåŒã³åºããŸãã ããã§ã¯äœãããããããã®ã¯ãããŸããã以äžã®å®å šãªã³ãŒãã®ããããã¬ãã«è§£æãã»ã¯ã·ã§ã³ãã芧ãã ããã
/// top ::= definition | external | expression | ';' static void MainLoop() { while (1) { fprintf(stderr, "ready> "); switch (CurTok) { case tok_eof: return; case ';': getNextToken(); break; // ';' . case tok_def: HandleDefinition(); break; case tok_extern: HandleExtern(); break; default: HandleTopLevelExpression(); break; } } }
ãã®ã³ãŒãã§æãèå³æ·±ãéšåã¯ããããã¬ãã«ã®ã»ãã³ãã³ãç¡èŠããããšã§ãã ãªãã ããïŒäž»ãªçç±ã¯ãã³ãã³ãã©ã€ã³ã§ã4 + 5ããšå ¥åããŠããããŒãµãŒã¯ãããçµäºã§ããããšãèªèããªããããããã¹ããå ¥åãç¶ããããšã§ããããšãã°ã次ã®è¡ã«ãdef foo ...ããšå ¥åã§ããŸãããã®å Žåã4 +5ããããã¬ãã«ã®åŒã®æåŸã«ãªããŸãããŸãã¯ãã* 6ããšå ¥åããŠåŒãç¶è¡ã§ããŸããäžäœã¬ãã«ã®ã»ãã³ãã³ãååšãããããã4 + 5;ããšå°å·ã§ããããŒãµãŒã¯ãã®æå³ãèªèããŸãã
çµè«
ã³ã¡ã³ãä»ãã³ãŒããããã400è¡ïŒã³ã¡ã³ããŸãã¯ç©ºçœè¡ã®ãªã240è¡ïŒã§ãã¬ãã·ã«ã«ããã³ããŒãµãŒãšASTæ§ç¯ãå«ãæå°éã®ããã°ã©ãã³ã°èšèªãå®å šã«å®çŸ©ããŸãããããããã¹ãŠã«ãããKaleidoscopeã¯å®è¡æã«ã³ãŒããæ€èšŒããææ³çã«ééã£ãŠãããã©ããããç¥ããããŸããã€ã³ã¿ã©ã¯ãã£ãã»ãã·ã§ã³ã®äŸã次ã«ç€ºããŸãã
$ ./a.out
ready> def foo(xy) x+foo(y, 4.0);
Parsed a function definition.
ready> def foo(xy) x+yy;
Parsed a function definition.
Parsed a top-level expr
ready> def foo(xy) x+y );
Parsed a function definition.
Error: unknown token when expecting an expression
ready> extern sin(a);
ready> Parsed an extern
ready> ^D
$
æ¹åããæ¹æ³ã¯ãããããããŸããæ°ããASTããŒããå®çŸ©ããããããŸããŸãªæ¹æ³ã§èšèªãæ¡åŒµãããã§ããŸãã次ã®ããŒãã§ã¯ãASTããLLVMäžéè¡šçŸïŒIRïŒãçæããæ¹æ³ãèŠãŠãããŸãã
å®å šãªã³ãŒããªã¹ã
ããã¯ããã®ç« ãšåã®ç« ã®ã³ãŒãã®å®å šãªãªã¹ãã§ããã³ãŒãã¯å®å šã«ç¬ç«ããŠããããšã«æ³šæããŠãã ãããLLVMãå€éšã©ã€ãã©ãªã¯å¿ èŠãããŸãããïŒãã¡ãããæšæºã®Cããã³C ++ã©ã€ãã©ãªãé€ããŸãïŒããã®ã³ãŒãã¯æ¬¡ã®ããã«ã³ã³ãã€ã«ã§ããŸãã
#
g++ -g -O3 toy.cpp
#
./a.out
ããŠãã³ãŒãèªäœïŒ
#include <cstdio> #include <cstdlib> #include <string> #include <map> #include <vector> //===----------------------------------------------------------------------===// // Lexer ( ) //===----------------------------------------------------------------------===// // [0-255], , // enum Token { tok_eof = -1, // ( ) tok_def = -2, tok_extern = -3, // ( : , ) tok_identifier = -4, tok_number = -5 }; static std::string IdentifierStr; // , tok_identifier static double NumVal; // , tok_number /// gettok - . static int gettok() { static int LastChar = ' '; // . while (isspace(LastChar)) LastChar = getchar(); if (isalpha(LastChar)) { // : [a-zA-Z][a-zA-Z0-9]* IdentifierStr = LastChar; while (isalnum((LastChar = getchar()))) IdentifierStr += LastChar; if (IdentifierStr == "def") return tok_def; if (IdentifierStr == "extern") return tok_extern; return tok_identifier; } if (isdigit(LastChar) || LastChar == '.') { // : [0-9.]+ std::string NumStr; do { NumStr += LastChar; LastChar = getchar(); } while (isdigit(LastChar) || LastChar == '.'); NumVal = strtod(NumStr.c_str(), 0); return tok_number; } if (LastChar == '#') { // do LastChar = getchar(); while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); if (LastChar != EOF) return gettok(); } // . if (LastChar == EOF) return tok_eof; // ASCII int ThisChar = LastChar; LastChar = getchar(); return ThisChar; } //===----------------------------------------------------------------------===// // Abstract Syntax Tree ( ) //===----------------------------------------------------------------------===// /// ExprAST - . class ExprAST { public: virtual ~ExprAST() {} }; /// NumberExprAST - (, "1.0"). class NumberExprAST : public ExprAST { double Val; public: NumberExprAST(double val) : Val(val) {} }; /// VariableExprAST - (, "a"). class VariableExprAST : public ExprAST { std::string Name; public: VariableExprAST(const std::string &name) : Name(name) {} }; /// BinaryExprAST - . class BinaryExprAST : public ExprAST { char Op; ExprAST *LHS, *RHS; public: BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) : Op(op), LHS(lhs), RHS(rhs) {} }; /// CallExprAST - . class CallExprAST : public ExprAST { std::string Callee; std::vector<ExprAST*> Args; public: CallExprAST(const std::string &callee, std::vector<ExprAST*> &args) : Callee(callee), Args(args) {} }; /// PrototypeAST - "" , /// (, , /// ). class PrototypeAST { std::string Name; std::vector<std::string> Args; public: PrototypeAST(const std::string &name, const std::vector<std::string> &args) : Name(name), Args(args) {} }; /// FunctionAST - class FunctionAST { PrototypeAST *Proto; ExprAST *Body; public: FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {} }; //===----------------------------------------------------------------------===// // Parser ( ) //===----------------------------------------------------------------------===// /// CurTok/getNextToken - . CurTok - /// , . getNextToken /// CurTok. static int CurTok; static int getNextToken() { return CurTok = gettok(); } /// BinopPrecedence - static std::map<char, int> BinopPrecedence; /// GetTokPrecedence - . static int GetTokPrecedence() { if (!isascii(CurTok)) return -1; // , . int TokPrec = BinopPrecedence[CurTok]; if (TokPrec <= 0) return -1; return TokPrec; } /// Error* - . ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; } static ExprAST *ParseExpression(); /// identifierexpr /// ::= identifier /// ::= identifier '(' expression* ')' static ExprAST *ParseIdentifierExpr() { std::string IdName = IdentifierStr; getNextToken(); // . if (CurTok != '(') // . return new VariableExprAST(IdName); // . getNextToken(); // ( std::vector<ExprAST*> Args; if (CurTok != ')') { while (1) { ExprAST *Arg = ParseExpression(); if (!Arg) return 0; Args.push_back(Arg); if (CurTok == ')') break; if (CurTok != ',') return Error("Expected ')' or ',' in argument list"); getNextToken(); } } // ')'. getNextToken(); return new CallExprAST(IdName, Args); } /// numberexpr ::= number static ExprAST *ParseNumberExpr() { ExprAST *Result = new NumberExprAST(NumVal); getNextToken(); // return Result; } /// parenexpr ::= '(' expression ')' static ExprAST *ParseParenExpr() { getNextToken(); // (. ExprAST *V = ParseExpression(); if (!V) return 0; if (CurTok != ')') return Error("expected ')'"); getNextToken(); // ). return V; } /// primary /// ::= identifierexpr /// ::= numberexpr /// ::= parenexpr static ExprAST *ParsePrimary() { switch (CurTok) { default: return Error("unknown token when expecting an expression"); case tok_identifier: return ParseIdentifierExpr(); case tok_number: return ParseNumberExpr(); case '(': return ParseParenExpr(); } } /// binoprhs /// ::= ('+' primary)* static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { // , while (1) { int TokPrec = GetTokPrecedence(); // , // , if (TokPrec < ExprPrec) return LHS; // , , . int BinOp = CurTok; getNextToken(); // eat binop // ExprAST *RHS = ParsePrimary(); if (!RHS) return 0; // BinOp RHS , RHS, // RHS LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec+1, RHS); if (RHS == 0) return 0; } // LHS/RHS. LHS = new BinaryExprAST(BinOp, LHS, RHS); } } /// expression /// ::= primary binoprhs /// static ExprAST *ParseExpression() { ExprAST *LHS = ParsePrimary(); if (!LHS) return 0; return ParseBinOpRHS(0, LHS); } /// prototype /// ::= id '(' id* ')' static PrototypeAST *ParsePrototype() { if (CurTok != tok_identifier) return ErrorP("Expected function name in prototype"); std::string FnName = IdentifierStr; getNextToken(); if (CurTok != '(') return ErrorP("Expected '(' in prototype"); // . std::vector<std::string> ArgNames; while (getNextToken() == tok_identifier) ArgNames.push_back(IdentifierStr); if (CurTok != ')') return ErrorP("Expected ')' in prototype"); // . getNextToken(); // ')'. return new PrototypeAST(FnName, ArgNames); } /// definition ::= 'def' prototype expression static FunctionAST *ParseDefinition() { getNextToken(); // def. PrototypeAST *Proto = ParsePrototype(); if (Proto == 0) return 0; if (ExprAST *E = ParseExpression()) return new FunctionAST(Proto, E); return 0; } /// toplevelexpr ::= expression static FunctionAST *ParseTopLevelExpr() { if (ExprAST *E = ParseExpression()) { // . PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>()); return new FunctionAST(Proto, E); } return 0; } /// external ::= 'extern' prototype static PrototypeAST *ParseExtern() { getNextToken(); // extern. return ParsePrototype(); } //===----------------------------------------------------------------------===// // Top-Level parsing ( ) //===----------------------------------------------------------------------===// static void HandleDefinition() { if (ParseDefinition()) { fprintf(stderr, "Parsed a function definition.\n"); } else { // . getNextToken(); } } static void HandleExtern() { if (ParseExtern()) { fprintf(stderr, "Parsed an extern\n"); } else { // . getNextToken(); } } static void HandleTopLevelExpression() { // . if (ParseTopLevelExpr()) { fprintf(stderr, "Parsed a top-level expr\n"); } else { // . getNextToken(); } } /// top ::= definition | external | expression | ';' static void MainLoop() { while (1) { fprintf(stderr, "ready> "); switch (CurTok) { case tok_eof: return; case ';': getNextToken(); break; // . case tok_def: HandleDefinition(); break; case tok_extern: HandleExtern(); break; default: HandleTopLevelExpression(); break; } } } //===----------------------------------------------------------------------===// // Main driver code ( ) //===----------------------------------------------------------------------===// int main() { // . // 1 - . BinopPrecedence['<'] = 10; BinopPrecedence['+'] = 20; BinopPrecedence['-'] = 20; BinopPrecedence['*'] = 40; // highest. fprintf(stderr, "ready> "); getNextToken(); // " ". MainLoop(); return 0; }
PS
ãã®ç« ã®ç¿»èš³ã¯éåžžã«é£ããã£ãã®ã§ãããŸã質ã®æªã翻蚳ãšå°ãã®ãéãã·ã¢èªã®ã¿ãŒã³ãããaã³ããŸãã次ã®ç« ã§èšæ£ããããšããçŽæããŸãïŒç°¡åã§ãïŒããã€ãã®ããã«ãç§ã¯åãã§PMã®ä¿®æ£ã説æãåžæãè åšãåãå ¥ããŸã:)