資工期中超渡團
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: Compiler chapter 1 --- # CH1 Introduction ## 1.1 Language Processors * a compiler is a program that can read a program in one language - the source language - and translate it into an equivalent program in another language - the target language ### Compiler * Source program $\rightarrow$ Compiler $\rightarrow$ Target program ### Target program * If the target program is an executable machine-language program, it can then be called by the user to process inputs and produce outputs; * input $\rightarrow$ Target program $\rightarrow$ output ### Interpreter * Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the use * ![](https://i.imgur.com/pxPuSY6.png) ## 1.2 The structure of a compiler ![](https://i.imgur.com/9EelKU3.png) * 96,100 , 101 考畫上圖 * 下面使用範例 position = initial + rate * 60 * symbol table | 1| position |float | | -------- | ---------- | -------- | | 2 | inital | float | | 3 | rate | float | ### 1.2.1 Lexical Analyzer * The first phase of a compiler is called lexical analysis or scanning. The lex- ical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexemes. * For each lexeme, the lexical analyzer produces as output a token of the form $(token-name, attribute-value)$ ![](https://i.imgur.com/0l9dPoj.png) ### 1.2.2 Syntax Analysis * The second phase of the compiler is syntax analysis or parsing. The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream. A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation. ![](https://i.imgur.com/aaegVjc.png) ### 1.2.3 Semantic Analysis * Uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition. * Gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation. * An important part of semantic analysis is type checking ![](https://i.imgur.com/mnWpJiM.png) ### 1.2.4 Intermediate Code Generation2 * An explicit low-level or machine like intermediate representation * intermediate representation should have two important properties: 1. easy to produce 2. easy to translate to into the target machine * Three-address code: consists of a sequence of assembly-like instructions with three operands per instruction ![](https://i.imgur.com/dT5lsKp.png) ### 1.2.5 Code Optimization * Attempts to improve the intermediate code 1. faster 2. shorter code 3. consumes less power ![](https://i.imgur.com/RtgzX5v.png) ### 1.2.6 Code Generation * takes intermediate representation of the source program as input and maps it into the target language * If the target language is machine code, registers or memory locations are selected for each of the variables used by the program. Then, the intermediate instructions are translated into sequences of machine instructions that perform the same task ![](https://i.imgur.com/G46AYrG.png) ### 1.2.7 Symbol-Table Management * The symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name ![](https://i.imgur.com/SiSrHp4.png) #### Extra example ![](https://i.imgur.com/BvP12wv.jpg) ### 1.2.8 The Grouping of Phases into Passes * Activities from several phases may be grouped together into a pass that reads an input file and writes an output file * We can produce compilers for different machines, by combining a front end with back ends for different target machines ### 1.2.9 Compiler-Construction Tools * Parser generators * Scanner generators * Syntax-directed translation engines * Code-generator generators * Data-flow analysis engine * Compiler-construction toolkits ## 1.3 The Evolution of Programming Languages ### 1.3.1 The Move to Higher-level Languages #### Generations * 1st machine languages * 2nd assembly languages * 3rd high level languages * 4th languages designed for specific applications. e.g. NOMAD, SQL * 5th logic and constraint-based languages. e.g. Prolog, OPS5 #### Types * Imperative: Program specifies how a computation is to be done. Uses statements that change a program's state. e.g. c, c++, c#, Java * Declarative: Program specifies what a computation is to be done. That expresses the logic of a computation without describing its control flow. e.g. SQL * Functional: constructed by applying and composing functions. e.g. Lisp, scheme * Object-oriented: one that supports object-oriented programming e.g. c++ * von Neumann language: computational model is based on the von Neumann computer archi- tecture. * Scripting languages: Interpreted languages with high-level operators designed for “gluing together” computations. e.g. Lisp, Lua, Bash ### 1.3.2 Impacts on Compilers * a computer system is dependent on compiler technology * compilers are used as a tool in evaluating architectural concepts before a computer is * Compiler writing is challenging * compiler must translate correctly the potentially finite set of programs that could be written in the source language ## 1.4 The Science of Building a Compiler ### 1.4.1 Modeling in Compiler Design and Implementation * The study of compilers is mainly a study of how we design the right mathematical models and choose the right algorithms, while balancing the need for generality and power against simplicity and efficiency. * Finite-state machines and regular expressions are useful for describing the lexical units of programs * Context-free grammars are used to describe the syntactic structure of programming languages ### 1.4.2 The Science of Code Optimization * The term "optimization" in compiler design refers to the attempts that a compiler makes to produce code that is more efficient than the obvious code. * Processor architectures have become more complex, yielding more opportunities to improve the way code executes * All compilers will have to face the problem of taking advantage of multiprocessor machines #### Objectives * optimization must be correct, that is, preserve the meaning of the compiled program * optimization must improve the performance of many programs * compilation time must be kept reasonable * engineering effort required must be manageable ## 1.5 Applications of Compiler Technology * Implementation of high-level programming languages * Optimizations for computer architectures * Design of new computer architectures * Program translations  Software productivity tools ###### tags: `Compiler` `CSnote`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully