Advertisement

《Compilers: Principles, Techniques, & Tools》读书笔记

阅读量:

Compiler

Programming languages are notations for describing computations to people and to machines.

Before a program can be run, it first must be translated into a form in which it can be executed by a computer.

The software systems that do this translation are called compilers.

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.

An important role of the compiler is to report any errors in the source program that it detects during the tranlation process.
在这里插入图片描述

Interpreter

An interpreter is another common kind of language processor.

Instead of producing a target program as a translation, an interpreter appears to directly execute the operations sepcified in the source prorgam on input supplied by the user.
在这里插入图片描述

Compiler vs. Interpreter

The machine-language target program produced by a compiler is ususally much faster than an interpreter at mapping inputs to outputs.

An interpreter, however, can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.

Language Processing System

In addition to a compiler, several other programs may be required to create an executable target program:

Preprocessor

The task of collecting the source program which maybe divided into modules stored in separate files, is sometimes entrusted to a separate program, called a preprocessor ;

The preprocessor may also expand shorthands, called macros , into source language statement;

Compiler

The compiler may produce an assembly-language program as its output, because assembly language is easier to produce as output and is easier to debug;

Assembler

The assembly language is then processed by a program called an assembler that produces relocatable machine code as its output;

Large programs are often compiled in pieces, so the relocatable machine code may have to be linked together with other relocatable object files and library files into the code that actually runs on the machine;

Linker

The linker resolves external memory addresses, where the code in one file may refer to a location in another file;

Loader

The loader then puts together all of the executable object files into memory for execution;
在这里插入图片描述

Structure of a Compiler

We treat a compiler as a single box that maps a source program into a semantically equivalent target program, if we open up this box a little, we see that there are two parts to this mapping:

Analysis(the front end of the compiler)

The analysis part breaks up the source program into constituent pieces and imposes a grammatical structure on them;

It also collects information about the source program and stores it in a data stucture called symbol table ;

Synthesis(back end of the compiler)

The synthesis part constructs the desired target grogram from the intermediate representation and the information in the symbol table ;
在这里插入图片描述

Phases of a compiler

理解lexeme, syntax, semantic in Compiler

Lexical analysis (scanning)

The lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexemes ;

Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator

理解three-address code

Code Optimization
Code Generator

P11

全部评论 (0)

还没有任何评论哟~