L1 Intro to Compilers

Intro to Compilers

Compilers

Interpreters

FORTRAN 1

  • The first Compiler
    • Huge impact o computer science.
  • Led to an enormous body of theoretical work
  • Modern compilers preserve the outline of FORTRAN 1

Structure of FORTRAN 1

  1. Lexical Analysis(词法分析)
  2. Parsing (语法分析)
  3. Semantic Analysis (语义分析)
  4. Optimization(优化)
  5. Code Generation

First step: recognize words.

smallest unit above letters

    This is a sentence.
  • Lexical analysics divides program text into "words" or "tokens"

      if x == y then z = 1;
      
      else z = 2;
    
  • Parsing = Diagrmming Sentences.

    • The diagram is a tree.
  • Once sentence structure is understood, we can try to understand "meaning"
    • This is too hard
  • Compilers perform limited semantic analysis to catch inconsistencies.

  • Expmple: Jack said Jerry left his assignment at home.

  • Even worse: Jack said Jack left his assignment at home?

  • Question? Which one "his" is referring to whether it's jack or it's Jerry.

  • Programming languages define strict rules to avoid such ambiguities.

      int main {
          int jack = 3;
          {
              int jack = 4;
              cout << jack;
          }
      }
    

Compilers perform many semantic checks besides variable bindings.

  • Example: Jack left her homework at home.
  • A "type mismatch" between her and jack; we know they are different people.

Optimization has no strong counterpart in English.

  • But a little bit like editing.

Automatically modify programs so that they

  • Run faster
  • Use less memory

X = Y * 0 is the same as X = 0

  • NO!
  • valid for integers
  • invalid for floating point (NaN)
  • NaN, Standing for Not a Number, is a numeric data type value representing an undefined or unrepresentable value.
  • NaN * 0 = NaN

Produces assembly code (usually)

A translation into another language

  • Analogous to human translation

  • The overall structure of almost every compiler adheres to our outline

  • The proportions has changed since FORTRAN

Compare Compiler
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本书作者: 李富生,诗协会员。 鸟鸣院林花郁香,痴情才俊心荡漾;驻步凝神勾文章,不得佳句断愁肠。
    小启明星阅读 1,417评论 3 5
  • 落笔的瞬间总是希望能文思如泉涌,但如果你不开始,就算灵感再多,也不知如何收拾这大开的脑洞。没有如期,KK29期/ ...
    白兰白小兔阅读 5,045评论 4 8
  • 题记:顾皖梓曦,等待,在夜晚中绽放的栀子花。 1.序章(夜清雅篇) 夜清雅,是她的名字。 ...
    幽涟薇月阅读 1,449评论 0 0