This is the 20th day of my participation in the Gwen Challenge.More article challenges

Chapter one: Introduction

1. Compilers vs. interpreters

  • Common ground: Translation
  • Differences: Interpreters translate item by item; The compiler is the whole translation

2. Compilation process and tasks at each stage

2.1 Compilation Process:

  • Lexical analysis
  • Syntax analysis
  • Semantic analysis
  • Intermediate code generation
  • Code optimization
  • Object code generation

2.2 Lexical analysis tasks

Reading a source program character by character from left to right, scanning and breaking up the stream of characters that make up the source program to identify individual words (or word symbols or symbols)

2.3 Syntax analysis tasks

On the basis of lexical analysis, the word sequence is decomposed into various grammatical phrases, such as “program”, “statement”, “expression” and so on

2.4 Semantic analysis tasks

Review the source program for semantic errors and gather type information for the code generation phase

2.5 * Intermediate code generation

  • Three forms

Inverse Polish notation (can be understood as a postfix expression) :

Reverse polish type
a+b ab+
a+b*c abc*+
(a+b)*c ab+c*
a:=b* c+d*e abc *de *+ :=

Three yuan type:

a:=b* c+d*e

(1) (*, b, c)

(2) (*, d, e)

(3) (+, (1), (2))

(4) (:=, (3), a)

Four type:

a:=b*c+d *e

(1) (*, b, C, T1)

(2) (*, d, e, t2)

(3) (+, T1, T2, t3)

(2) (:=, t3, __, a)

  • A tree structure

3. Compiler structure

3.1 Compiler structure diagram

3.2 Error handlers

Which errors: lexical, grammatical, semantic

3.3 Front-end and back-end

  • Front-end: dependent on the source and independent of the target

    Lexical analysis, grammar analysis, semantic analysis, intermediate code generation;

    Some optimization can also be done on the front end, including error handling and intermediate code generation phases associated with each phase of the front end

  • Back end: Depends on the target rather than the source

    Those phases of work that are only related to intermediate code, that is, object code generation, and associated error handling and symbol table manipulation