《Chapter 1 Addison WesleyCompilersPrinciples, Techniques and Tools2001( Laxxuss)fixed》由会员分享,可在线阅读,更多相关《Chapter 1 Addison WesleyCompilersPrinciples, Techniques and Tools2001( Laxxuss)fixed(25页珍藏版)》请在金锄头文库上搜索。
1、Chapter 1: Introduction to Compiling1 of 25 CHAPTER 1Introduccin to Compiling.The principles and techniques of compiler writing are so pervasive that the ideas found in this book will be used many times in the career of a computer scientist. Compiler writing spans programming languages, machine arch
2、itecture, language theory, algorithms and software engineering.Fortunately, a few basic compiler-writing techniques can be used to construct translators for a wide variety of languages and machines. In this chapter we introduce the subject of compiling by describing the components of a compiler the
3、environment in which compilers do their job, and some software tools that make it easier to build compilers.1.1 COMPILERS Simply stated, a compiler is a program that reads a program written in one language -the source language -and translates it into an equivalent program in another language -the ta
4、rget language (see Fig. 1.1). As an important part of this translation process, the compiler reports to its user the presence of errors in the source program.targetAt first glance, the variety of compilers may appear overwhelming. There are thousands of source languages, ranging from traditional pro
5、gramming languages such as Fortran and Pascal to specialized languages that have arisen in virtually every area of computer application. Target languages are equally as varied; a target language may be another programming language, or the machine language of any computer between a microprocessor and
6、 a supercomputer.Compilers arc sometimes classified as single-pass. multi-pass, Load-and-go, debugging, or optimizing. depending on how they have been constructed or on what function they arc supposed to perform. Despite this apparent complexity. the basic (asks that any compiler must perform are es
7、sentially the same. By understanding these tasks, we can construct compilers for a wide variety or source languages and target machines using the same basic techniquesOur knowledge about how to organize and write compilers has increased vastly since the first compilers started to appear in the early
8、 I950s It is difficult to give an exact dale for the first compiler because initially a great deal of experimentation and implementation was done independently by several groups. Much of the early work. on compiling dealt with the translation or arithmetic formulas into machine code. Throughout the
9、1950s, compilers were considered notoriously difficult programs to write. Tile first Fortran compiler, for example. took 18 staff-years to implement (Backus et al. 1957). We have since discovered systematic techniques for handling many of the important tasks that occur during compilation. Good imple
10、mentation languages. programming environments, and software tools have also been developed. With these advances. a substantial compiler call be implemented even as a student project in a one-semester compiler-design course. Tb! Analysis Synthesis Model of Compilation There lire two parts to compilat
11、ion: analysis and synthesis. The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program. The synthesis part constructs the desired target program from the intermediate representation. Of the two parts. synthesis requires th
12、e most specialized techniques. We shall consider analysis informally ill Section 1.2 and outline the way target code is synthesized in a standard compiler in Section 1.3. During analysis, the operations implied by the source program are determined and recorded in a hierarchical structure called a tr
13、ee. Often, a special kind of tree called a syntax tree is used. in which each node represents an operation and the children of a node represent the arguments of the operation. For example. a syntax tree for an assignment statement is shown in Fig. 1.2. .-/, position + -: , initial. . /, ra.te 60 . 1
14、.2. Syntax tree for position:. initial . rate. 60. Many software tools that manipulate source programs first perform some kind of analysis. Some examples of such tools include: l, Structure editors. A structure editor takes as input a sequence of commands to build a source program. The structure edi
15、tor not only performs the text-creation and modification functions of an ordinary text editor. but it also analyzes the program text, putting an appropriate hierarchical structure on the source program. Thus, the structure editor can perform additional tasks that are useful in the preparation of pro
16、grams. For example, it can check. that the input is correctly formed, can supply keywords automatically (e.g. when the user types while. the editor supplies the matching do and reminds the user that a conditional must come between them). and can jump from a begin or left parenthesis to its matching end or right parenthesis. Further. the output of such an editor is often similar to the output o