23
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Embed Size (px)

Citation preview

Page 1: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Chapter 1

Introduction

SamuelCollege of Computer Science &

TechnologyHarbin Engineering University

Page 2: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 2

Objectives

• by the end of the lesson students will be able to

– make sure the mainframe of the compiler construction

– tell the features of the problem computer can solve

Page 3: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 3

Compilers

• Compilers are computer programs that translate one language to another .– Very complex program from 10,000 to 1,000,000 lines of

code.

• Its input is a program written in its source language.– Usually, the source language is a high-level language (C,

C++, etc).

• It produces an equivalent program written in its target language.– The target language is object code (machine code) for the

target machine.

Page 4: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 4

Bilingual

• A person who knows and uses two languages.

• In everyday use the word bilingual usually means a person who speaks, reads, or understands two languages equally well (a balanced bilingual), but a bilingual person usually has a better knowledge of one language than of the other.

English

Source language

Page 5: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 5

Compilers

Page 6: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 6

双语• 一个能运用两种语言的人。

• 在他的日常生活中能将一门外语和本族语基本等同地运用于听、说、读、写,当然他的母语语言知识和能力通常是大于第二语言的。

Chinese

Target language

Page 7: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 7

Interpreters

• An interpreter is a language translator like a compiler.• The difference: the source program is executed

immediately, not after translation is complete.

Page 8: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 8

Bilingual education• The use of a second or foreign language in school for the

teaching of content subjects.

• The use of the child’s home language when the child enters school

• but later a gradual change to the use of the school language for teaching some subjects

• and the home language for teaching others.

• This is sometimes called maintenance bilingual education.

English

Source language

Page 9: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 9

Bilingual education• The use of a second or foreign language in school for the

teaching of content subjects.

• The use of the child’s home language when the child enters school

• but later a gradual change to the use of the school language for teaching some subjects

• and the home language for teaching others.

• This is sometimes called maintenance bilingual education.

Page 10: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 10

Bilingual education• The use of a second or foreign language in school for the

teaching of content subjects.• 能在学校里使用第二语言或外语进行各门学科的教学。• The use of the child’s home language when the child enters

school • 学生刚进入学校时使用本族语• but later a gradual change to the use of the school language for

teaching some subjects • 然后逐渐地使用第二语言进行部分学科的教学• and the home language for teaching others. • 其它学科仍使用母语教学• This is sometimes called maintenance bilingual education.• 这种模式称之为:保持型双语教学。

Chinese?

Target language?

Page 11: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 11

Interpreters

• An interpreter is a language translator like a compiler.• The difference: the source program is executed

immediately, not after translation is complete.• Programming language can be either interpreted or

compiled.• Interpreted languages: BASIC, LISP, Java• Compiled languages: FORTRAN, C, C++.• Interpreters share many operations with compilers.

Page 12: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 12

Machine Language

• Initially, the programs were written in machine language – numeric codes that represented the actual machine operations to be performed.

• C7 06 0000 0002 moves number 2 to the location 0000.

• Writing such codes is time consuming and tedious.

Page 13: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 13

Assembly Language• In assembly language instructions and memory

locations are given symbolic forms.MOV X, 2

• An assembler translates the symbolic codes and memory locations into corresponding numeric codes.

• Advantages:– Great improvement in speed and accuracy of writing

programs.– Still used today.

• Disadvantages:– Still not easy to write and difficult to read and understand.– Machine dependent.

Page 14: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 14

High-level Languages

• Nearly resembles mathematical notation or natural language.

• Independent on any particular machine.• Capable of being translated into executable code

X = 2

• Need a program that performs translation.• Is it possible? Is generated object code efficient?

Page 15: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 15

Theoretical Foundations• Noam Chomsky’s study of the structure of natural

languages.– Classification of languages according to the complexity of

their grammars and the algorithms to recognize them.• Context-free grammars are the most useful for programming

languages

– Study of the parsing problem, which become a standard part of compiler theory.

• Study of finite automata and regular expressions.– Led to symbolic methods for expressing the structure of

words of a programming language

• Optimization techniques (code improvement techniques).

Page 16: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 16

A Code Optimization Example

• What machine-independent optimizations are applicable to the following example?

Page 17: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 17

A Code Optimization Example: Result

Page 18: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 18

Compilers

• Compilers are computer programs that translate one language to another .– Very complex program from 10,000 to 1,000,000

lines of code.

• Its input is a program written in its source language.

• It produces an equivalent program written in its target language.

Page 19: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 19

Discussion 1

• If you wanna translate English into Chinese, how many steps will you follow? And what are they? The left side

• On the other hand, when you translate a Chinese sentence into an English one, what are you going to do? The right side

• Step by step, tell me the steps.

Page 20: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 20

Push the elephant into the refrigerator

GO!

Page 21: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 21

Translation ProcessThis is a book.Step 1: lexical analysisThis / is / a / book / .Step 2: syntax analysisThis subjectis predicatea quantifierbook object. endStep 3: semantic analysisThis pronoun 这is copula 是a numeral 一book noun 书. period 。Step 4: This is a book.Step 5: 这是一书。Step 6: 这是一本书。

Page 22: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 22

the Problem Computer can Solve

• Discussion 2: In your opinion, what kind of problem can be solved by computer? Tell me the features of the problem which can be solved by computer. The right side

• And what can not be? Just provide the reasons that some problems can not be processed through a computer. The left side

• Go!

Page 23: Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compiler [email protected] 23

Conclusion

• The features of the problem computer can solve are just as follows:

1. The problem can be specified by computer

2. Be able to find the relevant algorithm

3. The algorithm has reasonable temporal complexity and spatial complexity