--- title: Adv Compiler(09/22)W02#2 -Introduction tags: 111-1, AdvCompiler --- [TOC] <style> .blue { color: blue; } .red { color: red; } .orange { color: orange; } .green { color: green; } .purple { color: purple; } </style> # Language Processors ## Compiler ![](https://i.imgur.com/Hrz5SZM.png) - ### 3 Languages Involved 1. <span class="red">Source language</span> 2. <span class="green">Target language</span> 3. <span class="orange">Implementation language</span> - example:A compiler written in <span class="orange">Java(3.)</span> compiles a <span class="red">Pascal(1.)</span> program into a <span class="green">C(2.)</span> program ### Compilers v.s. Interpreters - 兩個的差別在<span class="red">有沒有生成 Target Program(target code)</span> 1. Compiler(編譯器) - 會先將 source code 轉成 target code(Target Program) 後,再將 input 讀入 target program。 - Compiler scans the entire program and translates the **whole of it** into machine code at once. - cons and pros - advantages: 編譯器會把所有編譯型語言**先轉換成電腦語言**,再轉換成執行檔,**程式碼執行速度上會比直譯型別語言來的快** - disadvantages:在編譯流程中,必須和你所使用的CPU架構對應,這樣才能在電腦上運行,所以編譯型語言的缺點就是**不適合跨平台**。 - ex: C compiler - Interpreter(直譯器) - 同時讀入 source code 和 input,每轉譯一行 code 就立刻執行(轉乘 machine code),然後再轉譯下一行。每次都要先轉成另一種語言再作執行,因此直譯器的程式運行速度比較緩慢。(不會一次把整個程式轉譯出來。) - Interpreter translates just **one statement of the program at a time** into machine code. - cons and pros - advantages: 可以跨平台執行並且消除編譯整個的負擔 - disadvantages: 程式執行效率不好 - ex: JavaScript interpreter - [java interpreter? compiler?, JVM 是compiler還是interpreter?](https://blog.csdn.net/weixin_34456422/article/details/118831764) ### A LanguageProcessing System :star2: 必考 :star2: :star2: :star2: :star2: - ![](https://i.imgur.com/MeUkwEH.png) - 參考資料在這,自己用簡短的描述將每個階段在做的事情寫出。 - ref: [一點都不深入的了解 Compiler、 Interpreter 和 VM](https://www.spreered.com/compiler_for_dummies/) - ![](https://i.imgur.com/nT7Jyyd.png) # The Structure of a Compiler ## Overall Structure of a Compiler ![](https://i.imgur.com/LkydYqR.png) - **front end**: 主要關心 source language,並作分析的行為。 - analysis part: 將 source code 拆解成一個個的 token,並轉成 IR(creates an intermediate representation of source program.) - synthesis part: 將 IR 轉成 target program。 - intermediate representation: - 通常和 source program 及 target language 無關的就是 IR。 - VScode 和 gcc 有兩層 IR 1. 因為 compiler 通常從高階的語言翻成低階的語言,所以 IR 相對於 low-level language 的層級再高一些些。 2. 一層 IR 為 high-level IR,能根據 source language 設計 IR 格式;另一層 IR 為 low-level IR,能根據 target language 設計的 IR 格式。 3. 在現在大型的 compiler 這種設計的技術是很常見的。 4. 所以,IR的複雜度介於 source language 和 target language 之間,及介於高階(target) &低階語言(source) 之間 > - 三層理論上可以,實務上沒必要 - **back end**: 主要關心 target language,大多動作在做找到相對應的 data 或相對應的指令,把 target program 需要的指令「組合」出。 - 通常和 source language 無關的就放在 backend。 - 小結論: - source code 進入 compiler 後,先進入 front end 做 Analysis 的過程(lexical_字母組成token - syntax_文法分析 - sematic_語意檢查),都分析好後就會形成 IR,再使用 IR 進入 back end,透過 Synthesis 的過程(透過前面分析後產生的資訊),產生(組合) target code # Building a Compiler ## Phases of a Compiler :star2: 必考 :star2: :star2: :star2: :star2: :star2: ![](https://i.imgur.com/pnzU8X8.png) 1. lexical: 字母組成單字的分析。 2. Syntax: 這些 words(token) 放在一起有沒有符合文法規則 3. Sematic: 不在上方這兩者分析的統稱。 - 也可以說 ≒ type checking > 1. & 2. 為較能公式化的簡易分析,故較容易區分開實作。 # Applications of Compiler Technology ###### compiler 技術可以應用的地方 ## Implementation of High-Level Programming Language ###### 高等程式語言的應用 ## Optimizations for Computer Architectures ## Design of New Computer Architectures ## Program Translations :::info 下面這個有興趣的自己看一看,老師上課沒有解釋到 ::: ## Software Productivity Tools