bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

手動寫編譯器實現加減乘除運算-創新互聯

眾所周知?。ɑ蛟S并不hh),寫一個編譯器需要詞法、語法和代碼生成這些部分。
本迷你編譯器目的是接收一個算術表達式,生成匯編代碼,并在linux上運行,從而生成結果。
如果沒有Linux系統,可以把driver.c注釋掉,在main里直接復制匯編代碼文本,在虛擬機上跑。
為什么不寫一個能在Windows上能直接跑出來的呢?因為我不會。。(眾所周知啊,Linux和Windows所能接收的匯編語言是不一樣的)

成都網站設計、做網站的關注點不是能為您做些什么網站,而是怎么做網站,有沒有做好網站,給創新互聯公司一個展示的機會來證明自己,這并不會花費您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。

那么,什么是編譯器?

C語言代碼由固定的詞匯按照固定的格式組織起來,簡單直觀,程序員容易識別和理解,但是對于CPU,C語言代碼就是天書,根本不認識,CPU只認識幾百個二進制形式的指令。這就需要一個工具,將C語言代碼轉換成CPU能夠識別的二進制指令,也就是將代碼加工成 .exe 程序;這個工具是一個特殊的軟件,叫做編譯器(Compiler)。

編譯器能夠識別代碼中的詞匯、句子以及各種特定的格式,并將他們轉換成計算機能夠識別的二進制形式,這個過程稱為編譯(Compile)。

編譯也可以理解為“翻譯”,類似于將中文翻譯成英文、將英文翻譯成象形文字,它是一個復雜的過程,大致包括詞法分析、語法分析、語義分析、性能優化、生成可執行文件五個步驟,期間涉及到復雜的算法和硬件架構。對于學計算機或者軟件的大學生,“編譯原理”是一門專業課程,有興趣的讀者請自行閱讀《編譯原理》一書,這里我們不再展開講解。

C語言的編譯器有很多種,不同的平臺下有不同的編譯器,例如:

Windows 下常用的是微軟開發的 cl.exe,它被集成在 Visual Studio 或 Visual C++ 中,一般不單獨使用;

Linux 下常用的是 GUN 組織開發的 GCC,很多 Linux 發行版都自帶 GCC;

Mac 下常用的是 LLVM/Clang,它被集成在 Xcode 中(Xcode 以前集成的是 GCC,后來由于 GCC 的不配合才改為 LLVM/Clang,LLVM/Clang 的性能比 GCC 更加強大)。

你的代碼語法正確與否,編譯器說了才算,我們學習C語言,從某種意義上說就是學習如何使用編譯器,讓編譯器生成可執行程序(例如 Windows 下的 .exe 程序)。

編譯器可以 100% 保證你的代碼從語法上講是正確的,因為哪怕有一點小小的錯誤,編譯也不能通過,編譯器會告訴你哪里錯了,便于你的更改。

類的描述:Lexer提供詞法,Parser提供語法,AstNode定義語法樹為Parser服務,Print用來打印,CodeGen生成匯編代碼,driver接收代碼,main用來測試。
具體代碼如下:
Lexer.h:

//
// Created by xiaoyu ren on 2022/11/21.
//

#ifndef COMPILER_LEXER_H
#define COMPILER_LEXER_H
#include#includenamespace compiler {enum class TokenKind{Add,
        Sub,
        Mul,
        Div,
        Num,
        Eof,
        Null
    };

    class Token{public:
        TokenKind kind;
        int value;
        std::string_view content;
    };

    class Lexer {private:
        std::string_view sourceCode;
        std::shared_ptrcurrentToken;
        char currentChar{' '};
        int cursor{0};
    public:
        Lexer(const char *code){sourceCode = code;
        }
        void GetNextToken();
        void GetNextChar();
        std::shared_ptrGetCurrentToken();
    };
}


#endif //COMPILER_LEXER_H

Lexer.cpp:

//
// Created by xiaoyu ren on 2022/11/21.
//

#include "Lexer.h"

void compiler::Lexer::GetNextToken() {while (isspace(currentChar)){GetNextChar();
    }
    TokenKind kind = TokenKind::Null;
    int value = 0;
    int startPos = cursor -1;
    if (currentChar == '\0'){kind = TokenKind::Eof;
    }
    else if (currentChar == '+'){kind = TokenKind::Add;
        GetNextChar();
    }
    else if (currentChar == '-'){kind = TokenKind::Sub;
        GetNextChar();
    }
    else if (currentChar == '*'){kind = TokenKind::Mul;
        GetNextChar();
    }
    else if (currentChar == '/'){kind = TokenKind::Div;
        GetNextChar();
    }
    else if (std::isdigit(currentChar)){value = 0;
        kind = TokenKind::Num;
        do{value = value * 10 + currentChar - '0';
            GetNextChar();
        } while (isdigit(currentChar));
    } else {printf("error:invalid type\n");
    }
    currentToken = std::make_shared();
    currentToken->kind = kind;
    currentToken->value = value;
    currentToken->content = sourceCode.substr(startPos, cursor - startPos - 1);
}

void compiler::Lexer::GetNextChar() {if (cursor >= sourceCode.size()){currentChar = '\0';
        cursor++;
    } else {currentChar = sourceCode[cursor++];
    }
}

std::shared_ptrcompiler::Lexer::GetCurrentToken() {return currentToken;
}

Parser.h:

//
// Created by xiaoyu ren on 2022/11/21.
//

#ifndef COMPILER_PARSER_H
#define COMPILER_PARSER_H
#include "Lexer.h"
#include "AstNode.h"

namespace compiler {class Parser {private:
        Lexer &lexer;
        std::shared_ptrParseExpr();
        std::shared_ptrParseAddExpr();
        std::shared_ptrParseMulExpr();
        std::shared_ptrParsePrimaryExpr();
    public:
        Parser(Lexer &lexer):lexer(lexer){}
        std::shared_ptrParse();
    };
}


#endif //COMPILER_PARSER_H

Parser.cpp

//
// Created by xiaoyu ren on 2022/11/21.
//

#include "Parser.h"

std::shared_ptrcompiler::Parser::Parse() {auto node = std::make_shared();
    node->Lhs = ParseExpr();
    return node;
}

std::shared_ptrcompiler::Parser::ParseExpr() {return ParseAddExpr();
}

std::shared_ptrcompiler::Parser::ParseAddExpr() {std::shared_ptrleft = ParseMulExpr();
    while (lexer.GetCurrentToken()->kind == TokenKind::Add || lexer.GetCurrentToken()->kind == TokenKind::Sub) {BinaryOperator binaryOperator = BinaryOperator::Add;
        if (lexer.GetCurrentToken()->kind == TokenKind::Sub){binaryOperator = BinaryOperator::Sub;
        }
        lexer.GetNextToken();
        auto node = std::make_shared();
        node->binaryOperator = binaryOperator;
        node->Lhs = left;
        node->Rhs = ParseMulExpr();
        left = node;
    }
    return left;
}

std::shared_ptrcompiler::Parser::ParseMulExpr() {std::shared_ptrleft = ParsePrimaryExpr();
    while (lexer.GetCurrentToken()->kind == TokenKind::Mul || lexer.GetCurrentToken()->kind == TokenKind::Div) {BinaryOperator binaryOperator = BinaryOperator::Mul;
        if (lexer.GetCurrentToken()->kind == TokenKind::Div){binaryOperator = BinaryOperator::Div;
        }
        lexer.GetNextToken();
        auto node = std::make_shared();
        node->binaryOperator = binaryOperator;
        node->Lhs = left;
        node->Rhs = ParsePrimaryExpr();
        left = node;
    }
    return left;
}

std::shared_ptrcompiler::Parser::ParsePrimaryExpr() {auto node = std::make_shared();
    node->value = lexer.GetCurrentToken()->value;
    lexer.GetNextToken();
    return node;
}

AstNode.h:

//
// Created by xiaoyu ren on 2022/11/21.
//

#ifndef COMPILER_ASTNODE_H
#define COMPILER_ASTNODE_H
#includenamespace compiler {class AstVisitor;

    class AstNode {public:
        virtual ~AstNode(){};
        virtual void Accept(AstVisitor *visitor) {};
    };

    class ProgramNode : public AstNode{public:
        std::shared_ptrLhs;
        void Accept(AstVisitor *visitor) override;
    };

    enum class BinaryOperator{Add,
        Sub,
        Mul,
        Div
    };

    class BinaryNode : public AstNode{public:
        BinaryOperator binaryOperator;
        std::shared_ptrLhs;
        std::shared_ptrRhs;
        void Accept(AstVisitor *visitor) override;
    };

    class ConstantNode : public AstNode{public:
        int value;
        void Accept(AstVisitor *visitor) override;
    };

    class AstVisitor{public:
        virtual void VisitorProgramNode(ProgramNode *node) {};
        virtual void VisitorBinaryNode(BinaryNode *node) {};
        virtual void VisitorConstantNode(ConstantNode *node) {};
    };
}


#endif //COMPILER_ASTNODE_H

AstNode.cpp:

//
// Created by xiaoyu ren on 2022/11/21.
//

#include "AstNode.h"

void compiler::BinaryNode::Accept(compiler::AstVisitor *visitor) {visitor->VisitorBinaryNode(this);
}

void compiler::ConstantNode::Accept(compiler::AstVisitor *visitor) {visitor->VisitorConstantNode(this);
}

void compiler::ProgramNode::Accept(compiler::AstVisitor *visitor) {visitor->VisitorProgramNode(this);
}

Print.h:

//
// Created by xiaoyu ren on 2022/11/22.
//

#ifndef COMPILER_PRINT_H
#define COMPILER_PRINT_H
#include "AstNode.h"

namespace compiler {class Print : public AstVisitor {private:
        void VisitorBinaryNode(BinaryNode *node) override;
        void VisitorConstantNode(ConstantNode *node) override;
    public:
        void VisitorProgramNode(ProgramNode *node) override;
    };
}


#endif //COMPILER_PRINT_H

Print.cpp:

//
// Created by xiaoyu ren on 2022/11/22.
//

#include#include "Print.h"

void compiler::Print::VisitorProgramNode(compiler::ProgramNode *node) {node->Lhs->Accept(this);
    printf("\n");
}

void compiler::Print::VisitorBinaryNode(compiler::BinaryNode *node) {node->Rhs->Accept(this);
    node->Lhs->Accept(this);
    switch (node->binaryOperator) {case BinaryOperator::Add:
            printf("+ ");
            break;
        case BinaryOperator::Sub:
            printf("- ");
            break;
        case BinaryOperator::Mul:
            printf("* ");
            break;
        case BinaryOperator::Div:
            printf("/ ");
            break;
        default:
            assert(0);
    }
}

void compiler::Print::VisitorConstantNode(compiler::ConstantNode *node) {printf("%d ", node->value);
}

CodeGen.h:

//
// Created by xiaoyu ren on 2022/11/21.
//

#ifndef COMPILER_CODEGEN_H
#define COMPILER_CODEGEN_H
#include "AstNode.h"

namespace compiler {class CodeGen : public AstVisitor {private:
        int top{0};
        void VisitorBinaryNode(BinaryNode *node) override;
        void VisitorConstantNode(ConstantNode *node) override;
        void Push();
        void Pop(const char *reg);
    public:
        CodeGen(){}
        void VisitorProgramNode(ProgramNode *node) override;
    };
}


#endif //COMPILER_CODEGEN_H

CodeGen.cpp:

//
// Created by xiaoyu ren on 2022/11/21.
//

#include "CodeGen.h"
#includevoid compiler::CodeGen::VisitorProgramNode(compiler::ProgramNode *node) {printf("\t.text\n");
#ifdef __linux__
    printf("\t.global prog\n");
    printf("_prog\n");
#endif
    printf("\tpush %%rbp\n");
    printf("\tmove %%rsp, %%rbp\n");
    printf("\tsub $32, %%rsp\n");

    node->Lhs->Accept(this);
    assert(top == 0);

    printf("\tmove %%rbp, %%rsp\n");
    printf("\tpop %%rbp\n");
    printf("\tret\n");
}

void compiler::CodeGen::VisitorBinaryNode(compiler::BinaryNode *node) {node->Rhs->Accept(this);
    Push();
    node->Lhs->Accept(this);
    Pop("%rdi");
    switch (node->binaryOperator) {case BinaryOperator::Add:
            printf("\tadd %%rdi, %%rax\n");
            break;
        case BinaryOperator::Sub:
            printf("\tsub %%rdi, %%rax\n");
            break;
        case BinaryOperator::Mul:
            printf("\timul %%rdi, %%rax\n");
            break;
        case BinaryOperator::Div:
            printf("\tcqo\n");
            printf("\tdiv %%rdi\n");
            break;
        default:
            assert(0);
    }
}

void compiler::CodeGen::VisitorConstantNode(compiler::ConstantNode *node) {printf("\tmove $%d, %%rax\n", node->value);
}

void compiler::CodeGen::Push() {printf("\tpush %%rax\n");
    top++;
}

void compiler::CodeGen::Pop(const char *reg) {printf("\tpop %s\n", reg);
    top--;
}

driver.c:

//
// Created by xiaoyu ren on 2022/11/23.
//
#includeint prog();

int main() {printf("%d\n", prog());
    return 0;
}

main.cpp:

#include#include "Lexer.h"
#include "Parser.h"
#include "Print.h"
#include "CodeGen.h"

int main() {const char *code = " 5 + 1 - 3 * 4 / 2";
    compiler::Lexer lexer(code);
    //test lexer
//    do{//        lexer.GetNextToken();
//        std::cout<< lexer.GetCurrentToken()->content<< std::endl;
//    } while (lexer.GetCurrentToken()->kind != compiler::TokenKind::Eof);
    //test parser,后序遍歷
    lexer.GetNextToken();
    compiler::Parser parser(lexer);
//    compiler::Print visitor;
//    auto root = parser.Parse();
//    root->Accept(&visitor);
    //test codeGen
    compiler::CodeGen codeGen;
    auto root = parser.Parse();
    root->Accept(&codeGen);
    //execute
//    make
//    ./compiler "5+1-3*4/2">tmp.s
//    clang tmp.s ../driver.c -o tmp.out
//    ./tmp.out
//    0
    return 0;
}

如果你懶得自己復制,可以去走下面鏈接下載壓縮包(為什么要1積分?因為你懶哈哈)
https://download.csdn.net/download/r643064456/87156158

你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧

文章標題:手動寫編譯器實現加減乘除運算-創新互聯
URL鏈接:http://vcdvsql.cn/article40/ichho.html

成都網站建設公司_創新互聯,為您提供搜索引擎優化、軟件開發網站排名網站設計外貿網站建設、電子商務

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

h5響應式網站建設