Planning a compiler Source Language –A subset of C Target Language –Assembler Performance...

10
Planning a compiler Source Language – A subset of C Target Language – Assembler Performance Criteria – Compiler speed – Code quality – Error diagnostics – Portability •Re-target-ability (for a new target language) •Re-host-ability (run on a new machine) – Maintainability

Transcript of Planning a compiler Source Language –A subset of C Target Language –Assembler Performance...

Page 1: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

Planning a compiler

• Source Language – A subset of C

• Target Language– Assembler

• Performance Criteria– Compiler speed– Code quality– Error diagnostics– Portability

• Re-target-ability (for a new target language)• Re-host-ability (run on a new machine)

– Maintainability

Page 2: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

Approaches to compiler development

• Bootstrapping

– Using the facilities offered by a language to compile itself is the essence of bootstrapping

• Automatic generator

– Lex

– Yacc

Page 3: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A look at the C Compilers

• Three-quarters of the code in PCC(Portable C Compiler) is independent of the target machine.

• All these compilers are essentially two-pass.

– The PDP-11comipler has an optional third pass that does optimization ( peephole optimization ) on the assembly-language output

Page 4: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

Lexical and syntax analysis intermediate code generation

Code generation

Post optimization

Postfix or prefix form for expressions assembly code otherwise

Assembly language

Source code

Assembly language

Page 5: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Programming Project

• Main tasks

– Translate the source program in C into an intermediate representation such as quadruples

– Interpret the intermediate representation

– Implement a basic components of a compiler for a subset of C.

Page 6: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Programming Project

• Suggested exercises– Design a symbol-table mechanism

(Required)• Search the symbol table for a given

name• Create a new entry for that name if

none is present• Delete from the symbol table all names

local to a given function

Page 7: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Programming Project• Suggested exercises

– Write an interpreter for quadruples(Optional)– Write the lexical analyzer(Required)

• Select internal codes for the tokens• Decide the representation of constants• Count lines for later use by an error-message handler• Produce a listing of the source program if desired• Write a program to enter the reserved words into the symbol

table• Design the lexical analyzer to be a function called by the

parser• Error handling

Page 8: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Programming Project

• Suggested exercises

– Write the semantic actions(Required)

• Do semantic analysis at this time. Converting integers to real numbers when necessary

– Write the parser(Required)

• LALR parser generator (Yacc)

– Write the error-handling routines (Optional)

Page 9: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Programming Project

• Important Website about Lex/YACC– http://dinosaur.compilertools.net/ (Lex/Yacc P

ages) – http://www.cs.princeton.edu/software/lcc/pkg/– http://www.thefreecountry.com/compilers/cpp.

shtml– http://www.thefreecountry.com/programming/

compilerconstruction.shtml– http://www.gnu.org/software/gcc/gcc.html– http://gcc.gnu.org/install/index.html

Page 10: Planning a compiler Source Language –A subset of C Target Language –Assembler Performance Criteria –Compiler speed –Code quality –Error diagnostics –Portability.

A Yacc/Lex Example

Yacc/Lex for Pascal