Professional Documents
Culture Documents
History of Compilation
Compilers are fundamental to modern computing.
programming languages like Java, C and C++ into executable machine-language instructions
Compiler technology is more broadly applicable and has
been employed in rather unexpected areas. Tex, LaTex PostScript Verilag and VHDL (address the creation of VLSI)
3
What Compiler Do
Compilers may be distinguished in two ways
By the kind of machine code they generate By the format of target code they generate
Interpreters
Another kind of language processor, called an
interpreter. differs from a compiler in that it executes programs without explicitly performing a translation. Interpreters provide a number of capabilities not
proceeds. Languages in which the type of an object is developed dynamically (e.g., Lisp and Scheme) are easily supported in an interpreter. Provide a significant degree of machine independence, since no machine code is generated.
Scanner
Scanner
The scanner begins the analysis of the source
program by reading the input, character by character, and grouping characters into individual words and symbols (tokens) such as identifiers, integers, reserved words, and delimiters. The tokens are encoded (often as integers) and then are fed to the parser for syntactic analysis.
Regular expressions
See Chapter 3
Parser
Parser The parser is based on a formal syntax specification such as CFGs (context-free grammars) It reads tokens and groups them into phrases as the syntax specification Grammars . The parser verifies correct syntax. If a syntax error is found, it issues a suitable error message. As syntactic structure is recognized, the parser usually builds a syntax tree (AST) as a concise representation of program structure.
10
AST node. If an AST node is semantically correct, it can be translated into IR code that correctly implements the meaning of the AST node. A symbol table is a mechanism that allows information to be associated with identifiers and share among compiler phases. The IR code generated by the translator is analyzed and transformed into functionally equivalent but improved IR code by the optimizer. The IR code produced by the translator is mapped into target machine code by the code generator.
A well-known compiler: GCC [GUN] is a heavily
11
optimizing compiler that can target over thirty computer architectures (Intel, Sparc, PowerPC, ) and has at least six front ends (C, C++, Fortran, Ada and Java).
Supplementary
From Modern Compiler Design
12
Compiler Compilers
To obtain the compiler, we run another
compiler whose input consists of compiler source text and which will produce executable code for it, as it would for any program source text. When the source language is also the implementation language and the source text to be compiled is actually a new version if the compiler itself, the process is called bootstrapping.
13
Compiler Compilers
Magic Work
The compiler can work its magic because of two
factors:
The input is in a language and consequently has a
structure, which is described in the language manual. The semantics of the input is described in terms of and is attached to that structure.
15
Conceptual Structure
Conceptual structure of a compiler
16
Phases of a compiler
From [ASU]
17
18
Demo Compiler
Structure of the demo compiler
19
From [ASU]
20
From [ASU]
21
Notations
Parsing Parse Tree Syntax Analysis Abstract Syntax Tree (AST) Annotated Abstract Syntax Tree
The annotations in a node are also called the
22
Parsing
Syntax trees are also called parse tree
23
Parse Tree
b*b 4*a*c
24
25
Annotated AST
Examples of annotations are type information and
optimization information. The annotations in a node are also called the attributes of that node.
26
Annotated AST
27
28
29
Lexical analyzer
30
31
32
P -> A1 A2 An | B1 B2 |
33
ADD Replaces the topmost two elements by their sum MULT Replaces the topmost two elements by their product PRINT Pops the top element and prints its value
34
performed by a machine at a later time. The interpreter performs the actions right away.
36
37