CS 416 Compiler Design 1 Course Information Instructor

  • Slides: 31
Download presentation
CS 416 Compiler Design 1

CS 416 Compiler Design 1

Course Information • Instructor : Dr. Ilyas Cicekli – Office: EA 504, – Phone:

Course Information • Instructor : Dr. Ilyas Cicekli – Office: EA 504, – Phone: 2901589, – Email: ilyas@cs. bilkent. edu. tr • Course Web Page: http: //www. cs. bilkent. edu. tr/~ilyas/Courses/CS 416 Compiler Design 2

COMPILERS • A compiler is a program takes a program written in a source

COMPILERS • A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. source program COMPILER target program ( Normally the equivalent program in machine code – relocatable object file) ( Normally a program written in a high-level programming language) error messages CS 416 Compiler Design 3

Compilers • Compilers translate from a source language (typically a high level language) to

Compilers • Compilers translate from a source language (typically a high level language) to a functionally equivalent target language (typically the machine code of a particular machine or a machine-independent virtual machine). • Compilers for high level programming languages are among the larger and more complex pieces of software – Original languages included Fortran and Cobol • Often multi-pass compilers (to facilitate memory reuse) – Compiler development helped in better programming language design • Early development focused on syntactic analysis and optimization – Commercially, compilers are developed by very large software groups • Current focus is on optimization and smart use of resources for modern RISC (reduced instruction set computer) architectures. 4

Why Study Compilers? • General background information for good software engineer – Increases understanding

Why Study Compilers? • General background information for good software engineer – Increases understanding of language semantics – Seeing the machine code generated for language constructs helps understand performance issues for languages – Teaches good language design – New devices may need device-specific languages – New business fields may need domain-specific languages 5

Applications of Compiler Technology & Tools • • Processing XML/other to generate documents, code,

Applications of Compiler Technology & Tools • • Processing XML/other to generate documents, code, etc. Processing domain-specific and device-specific languages. Implementing a server that uses a protocol such as http or imap Natural language processing, for example, spam filter, search, document comprehension, summary generation Translating from a hardware description language to the schematic of a circuit Automatic graph layout (graphviz, for example) Extending an existing programming language Program analysis and improvement tools 6

Other Applications • In addition to the development of a compiler, the techniques used

Other Applications • In addition to the development of a compiler, the techniques used in compiler design can be applicable to many problems in computer science. – Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs. – Techniques used in a parser can be used in a query processing system such as SQL. – Many software having a complex front-end may need techniques used in compiler design. • A symbolic equation solver which takes an equation as input. That program should parse the given input equation. – Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems. CS 416 Compiler Design 7

Major Parts of Compilers • There are two major parts of a compiler: Analysis

Major Parts of Compilers • There are two major parts of a compiler: Analysis and Synthesis • In analysis phase, an intermediate representation is created from the given source program. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase. • In synthesis phase, the equivalent target program is created from this intermediate representation. – Intermediate Code Generator, and Code Optimizer are the parts of this phase. CS 416 Compiler Design 8

Phases of A Compiler Source Program Lexical Analyzer Syntax Semantic Analyzer Intermediate Code Generator

Phases of A Compiler Source Program Lexical Analyzer Syntax Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program • Each phase transforms the source program from one representation into another representation. • They communicate with error handlers. • They communicate with the symbol table. CS 416 Compiler Design 9

Lexical Analyzer • Lexical Analyzer reads the source program character by character and returns

Lexical Analyzer • Lexical Analyzer reads the source program character by character and returns the tokens of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, delimeters and so on) Ex: newval : = oldval + 12 => tokens: newval identifier : = oldval + 12 assignment operator identifier add operator a number • Puts information about identifiers into the symbol table. • Regular expressions are used to describe tokens (lexical constructs). • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer. CS 416 Compiler Design 10

Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree)

Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program. • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier : = newval expression • In a parse tree, all terminals are at leaves. expression + expression identifier • All inner nodes are non-terminals in a context free grammar. number oldval 12 CS 416 Compiler Design 11

Syntax Analyzer (CFG) • The syntax of a language is specified by a context

Syntax Analyzer (CFG) • The syntax of a language is specified by a context free grammar (CFG). • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies, the syntax analyzer creates a parse tree for the given program. • Ex: We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier : = expression -> identifier expression -> number expression -> expression + expression CS 416 Compiler Design 12

Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized

Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer? – Both of them do similar things; But the lexical analyzer deals with simple non-recursive constructs of the language. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The lexical analyzer recognizes the smallest meaningful units (tokens) in a source program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. CS 416 Compiler Design 13

Parsing Techniques • Depending on how the parse tree is created, there are different

Parsing Techniques • Depending on how the parse tree is created, there are different parsing techniques. • These parsing techniques are categorized into two groups: – Top-Down Parsing, – Bottom-Up Parsing • Top-Down Parsing: – Construction of the parse tree starts at the root, and proceeds towards the leaves. – Efficient top-down parsers can be easily constructed by hand. – Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). • Bottom-Up Parsing: – – – Construction of the parse tree starts at the leaves, and proceeds towards the root. Normally efficient bottom-up parsers are created with the help of some software tools. Bottom-up parsing is also known as shift-reduce parsing. Operator-Precedence Parsing – simple, restrictive, easy to implement LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR CS 416 Compiler Design 14

Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and

Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. • Type-checking is an important part of semantic analyzer. • Normally semantic information cannot be represented by a context-free language used in syntax analyzers. • Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules) – the result is a syntax-directed translation, – Attribute grammars • Ex: newval : = oldval + 12 • The type of the identifier newval must match with type of the expression (oldval+12) CS 416 Compiler Design 15

Intermediate Code Generation • A compiler may produce an explicit intermediate codes representing the

Intermediate Code Generation • A compiler may produce an explicit intermediate codes representing the source program. • These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the level of machine codes. • Ex: newval : = oldval * fact + 1 id 1 : = id 2 * id 3 + 1 MULT id 2, id 3, temp 1 ADD temp 1, #1, temp 2 MOV temp 2, , id 1 Intermediates Codes (Quadraples) CS 416 Compiler Design 16

Code Optimizer (for Intermediate Code Generator) • The code optimizer optimizes the code produced

Code Optimizer (for Intermediate Code Generator) • The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space. • Ex: MULT id 2, id 3, temp 1 ADD temp 1, #1, id 1 CS 416 Compiler Design 17

Code Generator • Produces the target language in a specific architecture. • The target

Code Generator • Produces the target language in a specific architecture. • The target program is normally is a relocatable object file containing the machine codes. • Ex: ( assume that we have an architecture with instructions whose at least one of its operands is a machine register) MOVE MULT ADD MOVE id 2, R 1 id 3, R 1 #1, R 1, id 1 CS 416 Compiler Design 18

Compiler / Translator Design Decisions • Choose a source language – Large enough to

Compiler / Translator Design Decisions • Choose a source language – Large enough to have many interesting language features – Small enough to implement in a reasonable amount of time – Examples for us: Micro. Java, Decaf, Mini. Java • Choose a target language – Either a real assembly language for a machine with an assembler – Or a virtual machine language with an interpreter – Examples for us: Micro. Java VM (μJVM), MIPS (a popular RISC architecture, for which there is a “SPIM” simulator) • Choose an approach for implementation: – Either use an existing scanner and parser / compiler generator • lex/flex, yacc/bison/byacc, Antlr/Java. CC/Sable. CC/byaccj/Coco /R. – Or implement these yourself (limits the language somewhat) 19

Example Micro. Java Program program P main program; no separate compilation final int size

Example Micro. Java Program program P main program; no separate compilation final int size = 10; class Table { classes (without methods) int[] pos; int[] neg; } global variables Table val; { void main() int x, i; local variables { //----- initialize val -----val = new Table; val. pos = new int[size]; val. neg = new int[size]; i = 0; while (i < size) { val. pos[i] = 0; val. neg[i] = 0; i = i + 1; } //----- read values -----read(x); while (x != 0) { if (x > 0) val. pos[x] = val. pos[x] + 1; else if (x < 0) val. neg[-x] = val. neg[-x] + 1; read(x); } } } 20

Analysis of specific compilers Programs to be discussed: • lex – Programming utility that

Analysis of specific compilers Programs to be discussed: • lex – Programming utility that generates a lexical analyzer • yacc – Parser generator • lcc - ANSI C compiler Platforms: • All three programs designed for use on Unix • lcc runs under DOS and Unix

lex Programming Utility General Information: • • • Input is stored in a file

lex Programming Utility General Information: • • • Input is stored in a file with *. l extension File consists of three main sections lex generates C function stored in lex. yy. c Using lex: 1) Specify words to be used as tokens (Extension of regular expressions) 2) Run the lex utility on the source file to generate yylex( ), a C function 3) Declares global variables char* yytext and int yyleng

lex Programming Utility Three sections of a lex input file: /* C declarations and

lex Programming Utility Three sections of a lex input file: /* C declarations and #includes lex definitions */ %{ #include “header. c” int i; }% %% /* lex patterns and actions */ {INT} {sscanf (yytext, “%d”, &i); printf(“INTEGERn”); } %% /* C functions called by the above actions */ { yylex(): }

yacc Parser Generator General Information: • • Input is specification of a language Output

yacc Parser Generator General Information: • • Input is specification of a language Output is a compiler for that language yacc generates C function stored in y. tab. c bison Public domain version available Using yacc: 1) Generates a C function called yyparse() 2) yyparse() may include calls to yylex() 3) Compile this function to obtain the compiler

yacc Parser Generator yacc source lex yacc y. tab. c #include “lex. yy. c”

yacc Parser Generator yacc source lex yacc y. tab. c #include “lex. yy. c” lex. yy. c cc a. out • Input source file – similar to lex input file • Declarations, Rules, Support routines • Four parts of output atom: (Operation, Left Operand, Right Operand, Result)

Cross Compilation A cross compiler is a compiler capable of creating executable code for

Cross Compilation A cross compiler is a compiler capable of creating executable code for a platform other than the on which the compiler is run. Cross compiler tools are used to generate executables for embedded system or multiple platforms. It is used to compile for a platform upon which it is not feasible to do the compiling, like microcontrollers that don't support an operating system.

Uses of cross compilers • The fundamental use of a cross compiler is to

Uses of cross compilers • The fundamental use of a cross compiler is to separate the build environment from target environment. This is useful in a number of situations: • Embedded computers where a device has extremely limited resources. • For example, a microwave oven will have an extremely small computer to read its touchpad and door sensor, provide output to a digital display and speaker, and to control the machinery for cooking food. This computer will not be powerful enough to run a compiler, a file system, or a development environment. • Since debugging and testing may also require more resources than are available on an embedded system, cross-compilation can be less involved and less prone to errors than native compilation. CS 416 Compiler Design 27

 • Compiling for multiple machines. • For example, a company may wish to

• Compiling for multiple machines. • For example, a company may wish to support several different versions of an operating system or to support several different operating systems. By using a cross compiler, a single build environment can be set up to compile for each of these targets. • Compiling on a server farm. Similar to compiling for multiple machines, a complicated build that involves many compile operations can be executed across any machine that is free, regardless of its underlying hardware or the operating system version that it is running. CS 416 Compiler Design 28

 • GCC and cross compilation • GCC, a free software collection of compilers,

• GCC and cross compilation • GCC, a free software collection of compilers, can be set up to cross compile. It supports many platforms and languages. CS 416 Compiler Design 29

C 4 droid compiler for Android (Rs 192/-) https: //play. google. com/store/apps/details? id=com. n

C 4 droid compiler for Android (Rs 192/-) https: //play. google. com/store/apps/details? id=com. n 0 n 3 m 4. droidc&hl=en • C 4 droid is a user-friendly (but powerful) C/C++ IDE + C/C++ compiler for Android. Note that C 4 droid supports devices with ARM processors only (not devices with Intel x 86 or MIPS processor). Basic features: - Offline compiler: create your own applications on Android device and run them even without Internet access - No root required (but C 4 droid can use it for your programs if you want) - Full ANSI C and ISO C 99 support with TCC + u. Clibc - Source code editor with syntax highlighting, tabs, code completion, code formatting, file association and infinite undo/redo - Export & share your programs as APKs or native executables (for terminal apps) More features with a free GCC plugin: - Full C++ and almost complete C++11 support with GCC + Bionic libc - Native Activity, Qt, SDL and SDL 2 support for GUI - The most recent version of GCC always available - Makefile support: use the same build scripts as on your PC (Busy. Box is included) - Semi-automatic open-source library porting feature for enhanced programming & education CS 416 Compiler Design 30

AIDE - Android IDE - Java, C++ http: //android. appstorm. net/roundups/developer/15 -apps-for-programming-on-android/ • •

AIDE - Android IDE - Java, C++ http: //android. appstorm. net/roundups/developer/15 -apps-for-programming-on-android/ • • AIDE is an integrated development environment (IDE) for developing real Android Apps directly on Android devices. AIDE supports the full edit-compile-run cycle: write code with the feature rich editor offering advanced features like code completion, real-time error checking, refactoring and smart code navigation, and run your App with a single click. AIDE will turn your Android tablet with keyboard into a real development box. We use the Transformer Prime running Ice Cream Sandwich to code with AIDE will turn your Android Phone into a small development computer to browse and touch your code on the go. AIDE supports building Apps with Java/Xml and the Android SDK as well as Apps with C/C++ and the Android NDK. AIDE is fully compatible with Eclipse projects. You can just copy the sourcecode to your device and open the Eclipse project in AIDE to start coding. Alternatively you can keep your sourcecode on your Dropbox - AIDE integrates with Dropbox and allows to easily download from your Dropbox and sync back your changes. AIDE can also open Android Studio projects, which follow the default folder structure. AIDE supports GIT for professional development CS 416 Compiler Design 31