编译原理Review

本文详细介绍了编程语言的生成、编译过程,包括词法分析、语法分析、语义分析、中间代码生成、机器独立优化以及运行时环境管理。讨论了如何设计语法、不同阶段的任务和工具,如正则表达式、递归下降解析和LR算法。
摘要由CSDN通过智能技术生成

1 Background knowledge

1.1 Programing language 5 generation

在这里插入图片描述

1.2 Programing language Processors 3 type

  • Compiler
    在这里插入图片描述

  • Interpreter
    在这里插入图片描述

  • Hybrid Processor
    在这里插入图片描述

1.3 Whole procedure of compilation

[Task] Role Function Input Output

  1. Analysis Stage

    • Lexical Analysis
    • Syntax Analysis
    • Semantic Analysis
  2. Synthesis Stage

    • Intermediate Code Generation TAC
    • Machine-Independent Code Optimizer
    • Code Generation 汇编代码
    • Machine-Dependent Code Optimizer
    • Reload & Link 加载链接形成可执行程序

    img

1.4 Concepts

  1. Language

    • Natural language
    • Artificial language (Formal language)
      • Vocabularies Table
      • Sentence: a sequence of lexemes from the alphabet table by some laws
      • Language: a set of all sentences
  2. Grammar

    G = ( V T , V N , S , P ) L ( G ) = { α ∈ V T ∗ ∣ S ⇒ α } G = (V_{T},V_{N},S,P) \\ L(G) = \{{\alpha} \in V_{T}^{*} | S \Rightarrow \alpha \} G=(VT,VN,S,P)L(G)={αVTSα}
    在这里插入图片描述

    4 types of grammars
    在这里插入图片描述

1.5 Design grammar for a given language

how to design a grammar

ε \varepsilon ε - free grammar

S -> ε \varepsilon ε S is start symbol and S should not appear in right-side of any production

how to design a ε \varepsilon ε - free grammar

Ambiguous grammar
在这里插入图片描述

2 Inside of Compilation

2.1 Lexical Analyzer/Scanner

  • Role

    在这里插入图片描述

  • Tasks

    • Scanning/Buffering Buffer pair

    • Lexical Analysis Tokens

      • Recognize Tokens

        • Lexical Patterns

          Regular expression

          Regular grammar

          Finite automata (FA) RE -> NFA -> DFA -> mDFA

      • Message Error or Warning

  • Input/Output

    Input: Source Program

    Output: Sequence of tokens/Symbol table

2.2 Syntax Analyzer/Parser

  • Role

    在这里插入图片描述

  • Tasks

    • Syntax Analysis

      • Top-down(Derivation) Pushdown Automata

        • Derivation & Backmatching

        • Basic 递归下降

          在这里插入图片描述

          Left Recursion / Never halt

          Back Tracking / Time consuming / Inefficient

          Can’t Identify errors

        • LL(1) Algorithm

          Eliminate left recursion

          Lift left max common factors

          Construct prediction table

          在这里插入图片描述

          FIRST & FOLLOW

      • Bottom-Up(Reduction)

        • Basic Algorithm

          在这里插入图片描述

          Identify Handling

          Back Tracking / Inefficient

          Can’t identify errors exactly

        • LR Algorithm family

          SLR

          在这里插入图片描述

          LR(1)

          在这里插入图片描述

          在这里插入图片描述

          LALR

          在这里插入图片描述

          在这里插入图片描述

    • messages

    • Identify error position

  • Input/Output

    Input: Sequence of tokens/Symbol table

    Output: Parsing tree

2.3 Semantics Analysis

  • Role

    Source Program

    Scanner

    Sequence of tokens/Symbol table

    Parser C、C++、Java

    Parsing tree

    semantics Analyzer

    Annotated parse tree

  • Tasks

    • Evaluating the value of each Attributions of the grammar symbol in the Parse tree. (By semantics rules to do this)

      1. Type checking
      2. value
      3. code (TAC generator)

      Syntax directed Semantic Analysis 语法制导的语义分析

    • Handling errors.

  • Input/Output

    Input: Parse Tree

    Output: Annotated Parse tree

设计语义规则

产生式

属性 Inherited / Synthesis 继承性 综合性

Synthesized, whose value is defined only in terms of attributes values at the children and itself. E.g., val

Inherited, whose value is defined only in terms of attributes values at the parents, siblings and itself. E.g., type

2.4 Intermediate Generation (TAC generation)

  • Role

    在这里插入图片描述

  • Forms of three address code

  • Storage Forms of three address code

在这里插入图片描述

  • Short-curcuit(Jumping) code generation method

    在这里插入图片描述

    在这里插入图片描述

2.5 Machine Independent Optimization

  • Role

    在这里插入图片描述

  • Tasks

    在这里插入图片描述

    • Collecting Information

      • Control Flow Analysis
        • Blocks

          (1) The first statement is a leader.

          (2) Any statement that is the target of a conditional or unconditional goto is a leader.

          (3) Any statement that immediately follows a goto or conditional goto statement is a leader.

        • Loops

      • Data Flow Analysis
        • Dead Position
        • Live Assignment 有用的赋值
    • Transformation

      • Block/Local Optimization
      • Global Optimization
        • Loop 循环不变代码外提
        • 循环变量i优化

      Directed acyclic graph 有向无环图 (DAG)

      在这里插入图片描述

  • Input/Output

2.6 Run-time Environment Management

  • Role

    Some Program

    Compilation

    .exe

    code area

    data area

    运行时环境管理包

  • Task

    在这里插入图片描述

    为什么用栈 栈好 可以用栈

    • 存储空集利用连续

    • 每次只用Top (一个size大小)

    Heap浪费空间和时间 但是灵活

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值