compiler construction tools libby rasnick christopher newport university cpsc 560 spring 2003

21
Compiler Compiler Construction Tools Construction Tools Libby Rasnick Libby Rasnick Christopher Newport Christopher Newport University University CPSC 560 CPSC 560 Spring 2003 Spring 2003

Upload: gael-markle

Post on 28-Mar-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Compiler Construction Compiler Construction ToolsTools

Libby RasnickLibby Rasnick

Christopher Newport UniversityChristopher Newport University

CPSC 560CPSC 560

Spring 2003Spring 2003

Page 2: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

ContentsContents Defining Compiler Construction Tools (aka CCTs)Defining Compiler Construction Tools (aka CCTs) Uses for CCTsUses for CCTs CCTs in the Compiler StructureCCTs in the Compiler Structure Lexical AnalyzerLexical Analyzer Syntax AnalyzerSyntax Analyzer Semantic AnalyzerSemantic Analyzer Intermediate Code GeneratorIntermediate Code Generator Code OptimizerCode Optimizer Code GeneratorCode Generator Compiler Construction Kit - CocktailCompiler Construction Kit - Cocktail ReferencesReferences

Page 3: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Defining CCTsDefining CCTs

programs or environments that assist programs or environments that assist in the creation of an entire compiler or in the creation of an entire compiler or its partsits parts

Page 4: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Uses for CCTsUses for CCTs

generate lexical analyzers, generate lexical analyzers,

syntax analyzers, syntax analyzers,

semantic analyzers, semantic analyzers,

intermediate code,intermediate code,

optimized target codeoptimized target code

Page 5: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

CCTs in the Compiler StructureCCTs in the Compiler Structure

Page 6: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Lexical AnalyzerLexical Analyzer

scanner generatorsscanner generators input: source programinput: source program output: lexical analyzeroutput: lexical analyzer task of reading characters from source task of reading characters from source

program and recognizing tokens or basic program and recognizing tokens or basic syntactic components [3]syntactic components [3]

maintains a list of reserved wordsmaintains a list of reserved words

Page 7: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Lexical AnalyzerLexical Analyzer

Flex (fast lexical analyzer generator)Flex (fast lexical analyzer generator)

Example which specifies a scanner which Example which specifies a scanner which replaces the string “username” with the replaces the string “username” with the user’s login nameuser’s login name

%%%%

username printf(“%s”, getlogin());username printf(“%s”, getlogin());

Page 8: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Syntax AnalyzerSyntax Analyzer

parser generatorsparser generators input: context-free grammarinput: context-free grammar output: syntax analyzeroutput: syntax analyzer the task of the syntax analyzer is to the task of the syntax analyzer is to produce a representation of the source produce a representation of the source program in a form directly representing its program in a form directly representing its syntax structure. This representation is syntax structure. This representation is usually in the form of a binary tree or similar usually in the form of a binary tree or similar data structure [3]data structure [3]

Page 9: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Syntax AnalyzerSyntax Analyzer Bison (Yacc-compatible parser gen.) [5]Bison (Yacc-compatible parser gen.) [5] a general purpose parser generator that converts a general purpose parser generator that converts grammar description for an LALR(1) CFG into a C grammar description for an LALR(1) CFG into a C programprogram Bison grammar example (reverse polish notation)Bison grammar example (reverse polish notation) %{%{ #define YYSTYPE double#define YYSTYPE double #include <math.h>#include <math.h> %}%} %token NUM%token NUM

%% /* grammar rules and actions below */%% /* grammar rules and actions below */

Page 10: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Semantic AnalyzerSemantic Analyzer syntax-directed translatorssyntax-directed translators input: parse treeinput: parse tree output: routines to generate I-codeoutput: routines to generate I-code “ “The role of the semantic analyzer is to derive The role of the semantic analyzer is to derive methods by which the stuctures constructed by the methods by which the stuctures constructed by the syntax analyzer may be evaluate or executed.“ [3]syntax analyzer may be evaluate or executed.“ [3] type checker type checker two common tactics:two common tactics: ~ flatten the semantic analyzer’s parse tree~ flatten the semantic analyzer’s parse tree ~ embed semantic analyzer w/in syntax analyzer~ embed semantic analyzer w/in syntax analyzer (syntax-driven translation)(syntax-driven translation)

Page 11: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Intermediate Code GeneratorIntermediate Code Generator

Automatic code generatorsAutomatic code generators input: I-code rulesinput: I-code rules output: crude target machine programoutput: crude target machine program “ “The task of the code generator is to The task of the code generator is to traverse this tree, producing functionally traverse this tree, producing functionally equivalent object code.” [3]equivalent object code.” [3] three address code is one typethree address code is one type

Page 12: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Intermediate Code GeneratorIntermediate Code Generator Example 7 + (8 * y) / 2Example 7 + (8 * y) / 2

a := 8a := 8b := yb := yc := a * bc := a * ba := ca := cb := 2b := 2c := a / bc := a / ba := 7a := 7b := cb := cc := a + b c := a + b

expr

7 + expr

expr / 2

expr( )

8 * y

Page 13: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Code OptimizerCode Optimizer

Data flow enginesData flow engines input: I-codeinput: I-code output: transformed codeoutput: transformed code “ “This improvement is achieved by program This improvement is achieved by program transformations that are traditionally called transformations that are traditionally called optimizationsoptimizations, although the term , although the term ‘optimization’ is a misnomer because there ‘optimization’ is a misnomer because there is rarely a guarantee that the resulting code is rarely a guarantee that the resulting code is the best possible.” [1]is the best possible.” [1]

Page 14: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Code OptimizerCode Optimizer

Peephole OptimizationPeephole Optimizationmachine or assembly code is used along machine or assembly code is used along with knowledge of target machine’s with knowledge of target machine’s instruction set to replace I-code instructions instruction set to replace I-code instructions with shorter or more quickly executed with shorter or more quickly executed instructions - this is repeated as much as is instructions - this is repeated as much as is necessary [3]necessary [3]

Page 15: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Code OptimizerCode Optimizer

Common Optimizing Transformations [2]Common Optimizing Transformations [2] Optim. NameOptim. Name Required AnalysisRequired Analysis TransformationTransformation

constant foldingconstant folding simulated exec.simulated exec. elimination elimination dead code elim.dead code elim. simulated exec.simulated exec. eliminationelimination loop unrollingloop unrolling loop struct., stat.sloop struct., stat.s motion (replic.)motion (replic.) linearizing arrayslinearizing arrays loop structureloop structure eliminationelimination load/store optim.load/store optim. DFADFA motionmotion branch chainingbranch chaining statisticsstatistics selection (dec)selection (dec) math identitiesmath identities nonenone selection, eliminationselection, elimination common subexp.common subexp. simulated exec.simulated exec. eliminationelimination

Page 16: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Code OptimizerCode Optimizer Example 7 + (8 * y) / 2Example 7 + (8 * y) / 2

a := ya := ya := a * 8a := a * 8a := a / 2a := a / 2a := + 7a := + 7

expr

7 + expr

expr / 2

expr( )

8 * y

Page 17: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Code GeneratorCode Generator

Automatic code generatorsAutomatic code generators input: optimized (transformed) I-codeinput: optimized (transformed) I-code output: target machine program output: target machine program Example 7 + (8 * y) / 2Example 7 + (8 * y) / 2 Load a, yLoad a, y Mult a, 8Mult a, 8 Div a, 2Div a, 2 Add a, 7Add a, 7

Page 18: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Compiler Construction Tool KitsCompiler Construction Tool Kits

tool kits contain all (or almost all) phases of tool kits contain all (or almost all) phases of a compiler a compiler

Example: Cocktail [7]Example: Cocktail [7]Developed until 1993 at Karlsruhe research Developed until 1993 at Karlsruhe research lab, the German National Research lab, the German National Research Center for Information Technology.Center for Information Technology.

Page 19: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

Compiler Construction Tool KitsCompiler Construction Tool Kits Components:Components: RexRex (Regular EXpression tool) (Regular EXpression tool) - scanner generator- scanner generator LalrLalr - an LALR(1) parser generator accepting - an LALR(1) parser generator accepting grammars in extended BNFgrammars in extended BNF EllEll - an LL(1) parser generator accepting same - an LL(1) parser generator accepting same grammars as Lalr, but must obey LL(1) propertygrammars as Lalr, but must obey LL(1) property AstAst - generator for abstract syntax trees - generator for abstract syntax trees AgAg - attribute evaluator generator - attribute evaluator generator PumaPuma - transformation tool based on pattern - transformation tool based on pattern matchingmatching

Page 20: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

ReferencesReferences

1 1 Aho, Alfred V., Sethi, Ravi and Ullman, Jeffrey D. Compilers: principles, techniques, and

tools. (1986). Reading: Addison-Wesley.

2 2 Peters, James, Pittman, Thomas. The art of compiler design: theory and practice.

(1992). Englewood Cliffs: Prentice Hall.3 Watson, Des. High-level languages and their

compilers. (1989). Wokingham: Addison- Wesley.

Page 21: Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003

ReferencesReferences

4 Heng, Christopher. (2003). Free Compiler Construction Tools. http://www.thefreecountry.com/programming/compilercontructiontools (01 March 2003).

5 The Lex & Yacc Page. http://dinosaur.compilertools.net (01 March 2003).

6 Compiler Construction Kits. http://catalog.compilertools.net (01 March 2003).

7 The Cocktail Compiler Toolbox. http://www.first.gmd.de/cocktail/ (01 March 2003).