Planning a compiler Source Language –A subset of C Target Language –Assembler Performance...
-
Upload
hector-simpson -
Category
Documents
-
view
223 -
download
0
Transcript of Planning a compiler Source Language –A subset of C Target Language –Assembler Performance...
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
Approaches to compiler development
• Bootstrapping
– Using the facilities offered by a language to compile itself is the essence of bootstrapping
• Automatic generator
– Lex
– Yacc
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
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
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.
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
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
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)
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
A Yacc/Lex Example
Yacc/Lex for Pascal