Compiler Glossary
Language Translator
- Internal Architecture
flowchart LR S[Source Code/HLL Code] P[Preprocessor] C[Compiler] A[Assembler/Assembly Language] L[Linker/Loader] M[Machine Code] S --> P --> C --> A --> L --> M
HLL ==> Preprocessor ==> Pure HLLPure HLL ==> Compiler ==> Assembly LanguageAssembly Language ==> Assembler ==> Relocatable Machine Code
Different Phases in Compiler
- Front-End :
LANCECompiler
flowchart TD LA[Lexical Analysis] StA[Syntax Analysis] SmA[Semantic Analysis] ICG[Intermediate Code Generation] CO[Code Optimization] TCG[Target Code Generation] LA ---> StA ---> SmA ---> ICG ---> CO ---> TCG
Lexical Analyzer
- Tool:
LEX
Function: The first phase of compilation involves breaking the source code into tokens, which are the smallest units of meaning in a programming language (keywords, identifiers, operators, etc.).
Purpose: This step helps in recognizing the basic building blocks and structure of the code. It removes white spaces and comments and produces a stream of tokens as output.
Syntax Analysis
- Tool:
YACC
Function: This phase involves parsing the stream of tokens generated by the lexical analysis to create a hierarchical structure (syntax tree) that represents the grammatical structure of the source code.
Purpose: It checks whether the source code adheres to the specified programming language syntax rules. If there are syntax errors, they are identified during this phase.
Semantic Analysis
Function: The semantic analysis phase focuses on understanding the meaning of the code. It checks for semantic errors that may not be apparent during lexical and syntax analysis.
Purpose: This step ensures that the code has a meaningful interpretation and detects issues such as undeclared variables, type mismatches, and other violations of semantic rules.
Intermediate Code Generation
Function: The compiler generates an intermediate representation of the source code, known as intermediate code. This code is independent of the target machine and serves as a bridge between the high-level source code and the machine code.
Purpose: Intermediate code simplifies the process of code optimization and facilitates the generation of code for different target machines.
Code Optimization
Function: Code optimization involves improving the intermediate code to make it more efficient in terms of execution speed and memory usage.
Purpose: The goal is to enhance the performance of the generated code while maintaining its correctness. Various optimization techniques are applied to achieve this, such as loop optimization, constant folding, and dead code elimination.
Target Code Generation
Function: In this phase, the compiler translates the optimized intermediate code into the machine code of the target architecture.
Purpose: The final output of the compilation process is the machine code that can be executed on the target hardware. This phase is responsible for generating efficient and correct machine code based on the optimizations performed during the previous steps.
Directive
一個 編譯器指引 (compiler directive) 可以告知編譯器在查核陣列索引時的範疇,或者信任程式師尚未編譯的程式碼,以免導致編譯錯誤。
C: 預處理器指導 - preprocessor directive- e.g. :
#include
- e.g. :
Inline Function
Preprocessor
Its main purpose is to perform text substitutions and include header files, creating an expanded version of the source code.