Compiler course Book Compilers Principles Techniques Tools Aho

Compiler course Book: Compilers Principles, Techniques, & Tools Aho, Lam, Sethi, and Ullman Prof. Dr. Md Abdul Masud Chairman Dept. of Computer Science and Information Technology(CSIT) Patuakhali Science and Technology University E-mail: masud@pstu. ac. bd Mobile: 01887826804

Introduction to Compiler Next study Chapter 5: Syntax-Directed Translation Chapter 6: Intermediate-Code Generation

What is a Compiler? • A compiler is a program that translates a source program written in some high-level programming language (such as Java) into machine code for some computer architecture (such as the Intel Pentium architecture). • The generated machine code can be later executed many times against different data each time.

What is a Compiler? • An interpreter reads an executable source program written in a high-level programming language as well as data for this program, and it runs the program against the data to produce some results. • One example is the Unix shell interpreter, which runs operating system commands interactively. • An interpreter is generally slower than a compiler because it processes and interprets each statement in a program as many times as the number of the evaluations of this statement.

What is a Compiler? • Some languages, such as Java and Lisp, come with both an interpreter and a compiler. • Java source programs (Java classes with. java extension) are translated by the javac compiler into byte-code files (with. class extension). • The Java interpreter, called the Java Virtual Machine (JVM), may actually interpret byte codes directly or may internally compile them to machine code and then execute that code (JIT: just-in-time compilation).

What is a Compiler? • Compilers and interpreters are not the only examples of translators. Here are few more:

What is the Challenge? Many variations: • many programming languages (eg, Java, C++, Scala) • many programming paradigms (eg, object-oriented, functional, logic) • many computer architectures (eg, x 86, MIPS, ARM, alpha, Power. PC) • many operating systems (eg, Linux, OS X, Microsoft Windows, Android, i. OS)

What is the Challenge? Qualities of a compiler (in order of importance): the compiler itself must be bug-free • it must generate correct machine code • the generated machine code must run fast • the compiler itself must run fast (compilation time must be proportional to program size) • the compiler must be portable (ie, modular, supporting separate compilation) • it must print good diagnostics and error messages • the generated code must work well with existing debuggers • must have consistent and predictable optimization.

What is the Challenge? Building a compiler requires knowledge of programming languages (parameter passing, variable scoping, memory allocation, etc) • theory (automata, context-free languages, etc) • algorithms and data structures (hash tables, graph algorithms, dynamic programming, etc) • computer architecture (assembly programming) • software engineering.

Compiler

Model of a Compiler front end

Syntax Definition • Such rules is called Production. • Terminals: Lexical elements like keyword if, parentheses. • Nonterminals: Variables like expr, stmt.

Context-free-grammar A context-free grammar has four components: 1. 2. 3. 4. A set of terminals A set of nonterminals A set of production A designation of one of the nonterminals as the start symbol.

Syntax-Directed Translation • A grammar-oriented compiling technique known as syntax-directed translation. • SDT is basically used to construct the parse tree with Grammar and Semantic action. Application of Syntax Directed Translation : • • • SDT is used for Executing Arithmetic Expression. In the conversion from infix to postfix expression. In the conversion from infix to prefix expression. It is also used for Binary to decimal conversion. In counting number of Reduction. In creating a Syntax tree. SDT is used to generate intermediate code. In storing information into symbol table. SDT is commonly used for type checking also. 14

Syntax-Directed Translation • Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. • For example, consider an expression expr generated by the production: • We can translate expr by exploiting its structure, as in the following pseudo-code: 15

Concepts of syntax-directed translation • Attributes. – An attribute is any quantity associated with a programming construct. Examples of attributes are data types of expressions, the number of instructions in the generated code, or the location of the first instruction in the generated code for a construct , among many other possibilities. • (Syntax- directed) translation schemes. – A translation scheme is a notation for attaching program fragments to the productions of a grammar. The program fragments are executed when the production is used during syntax analysis. 16
- Slides: 16