Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003."— Presentation transcript:

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

2 Contents Defining Compiler Construction Tools (aka CCTs) Defining Compiler Construction Tools (aka CCTs) Uses for CCTs Uses for CCTs CCTs in the Compiler Structure CCTs in the Compiler Structure Lexical Analyzer Lexical Analyzer Syntax Analyzer Syntax Analyzer Semantic Analyzer Semantic Analyzer Intermediate Code Generator Intermediate Code Generator Code Optimizer Code Optimizer Code Generator Code Generator Compiler Construction Kit - Cocktail Compiler Construction Kit - Cocktail References References

3 Defining CCTs programs or environments that assist in the creation of an entire compiler or its parts programs or environments that assist in the creation of an entire compiler or its parts

4 Uses for CCTs generate lexical analyzers, generate lexical analyzers, syntax analyzers, syntax analyzers, semantic analyzers, semantic analyzers, intermediate code, intermediate code, optimized target code optimized target code

5 CCTs in the Compiler Structure

6 Lexical Analyzer scanner generators scanner generators input: source program input: source program output: lexical analyzer output: lexical analyzer task of reading characters from source program and recognizing tokens or basic syntactic components [3] task of reading characters from source program and recognizing tokens or basic syntactic components [3] maintains a list of reserved words maintains a list of reserved words

7 Lexical Analyzer Flex (fast lexical analyzer generator) Flex (fast lexical analyzer generator) Example which specifies a scanner which replaces the string username with the users login name Example which specifies a scanner which replaces the string username with the users login name % % username printf(%s, getlogin()); username printf(%s, getlogin());

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

9 Syntax Analyzer Bison (Yacc-compatible parser gen.) [5] Bison (Yacc-compatible parser gen.) [5] a general purpose parser generator that converts grammar description for an LALR(1) CFG into a C program a general purpose parser generator that converts grammar description for an LALR(1) CFG into a C program Bison grammar example (reverse polish notation) Bison grammar example (reverse polish notation) %{ %{ #define YYSTYPE double #define YYSTYPE double #include #include %} %} %token NUM %token NUM % /* grammar rules and actions below */ % /* grammar rules and actions below */

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

11 Intermediate Code Generator Automatic code generators Automatic code generators input: I-code rules input: I-code rules output: crude target machine program output: crude target machine program The task of the code generator is to traverse this tree, producing functionally equivalent object code. [3] The task of the code generator is to traverse this tree, producing functionally equivalent object code. [3] three address code is one type three address code is one type

12 Intermediate Code Generator Example 7 + (8 * y) / 2 Example 7 + (8 * y) / 2 a := 8 b := y c := a * b a := c b := 2 c := a / b a := 7 b := c c := a + b expr 7+ / 2 () 8 * y

13 Code Optimizer Data flow engines Data flow engines input: I-code input: I-code output: transformed code output: transformed code This improvement is achieved by program transformations that are traditionally called optimizations, although the term optimization is a misnomer because there is rarely a guarantee that the resulting code is the best possible. [1] This improvement is achieved by program transformations that are traditionally called optimizations, although the term optimization is a misnomer because there is rarely a guarantee that the resulting code is the best possible. [1]

14 Code Optimizer Peephole Optimization Peephole Optimization machine or assembly code is used along with knowledge of target machines instruction set to replace I-code instructions with shorter or more quickly executed instructions - this is repeated as much as is necessary [3]

15 Code Optimizer Common Optimizing Transformations [2] Common Optimizing Transformations [2] Optim. NameRequired AnalysisTransformation Optim. NameRequired AnalysisTransformation constant foldingsimulated exec.elimination constant foldingsimulated exec.elimination dead code elim.simulated exec.elimination dead code elim.simulated exec.elimination loop unrollingloop struct., stat.smotion (replic.) loop unrollingloop struct., stat.smotion (replic.) linearizing arraysloop structureelimination linearizing arraysloop structureelimination load/store optim.DFAmotion load/store optim.DFAmotion branch chainingstatisticsselection (dec) branch chainingstatisticsselection (dec) math identitiesnoneselection, elimination math identitiesnoneselection, elimination common subexp.simulated exec.elimination common subexp.simulated exec.elimination

16 Code Optimizer Example 7 + (8 * y) / 2 Example 7 + (8 * y) / 2 a := y a := a * 8 a := a / 2 a := + 7 expr 7+ / 2 () 8 * y

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

18 Compiler Construction Tool Kits tool kits contain all (or almost all) phases of a compiler tool kits contain all (or almost all) phases of a compiler Example: Cocktail [7] Example: Cocktail [7] Developed until 1993 at Karlsruhe research lab, the German National Research Center for Information Technology.

19 Compiler Construction Tool Kits Components: Components: Rex (Regular EXpression tool) Rex (Regular EXpression tool) - scanner generator - scanner generator Lalr - an LALR(1) parser generator accepting grammars in extended BNF Lalr - an LALR(1) parser generator accepting grammars in extended BNF Ell - an LL(1) parser generator accepting same grammars as Lalr, but must obey LL(1) property Ell - an LL(1) parser generator accepting same grammars as Lalr, but must obey LL(1) property Ast - generator for abstract syntax trees Ast - generator for abstract syntax trees Ag - attribute evaluator generator Ag - attribute evaluator generator Puma - transformation tool based on pattern matching Puma - transformation tool based on pattern matching

20 References 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.

21 References http://www.thefreecountry.com/programming/comp ilercontructiontools http://www.thefreecountry.com/programming/comp ilercontructiontools 4 Heng, Christopher. (2003). Free Compiler Construction Tools. http://www.thefreecountry.com/programming/comp ilercontructiontools (01 March 2003). http://www.thefreecountry.com/programming/comp ilercontructiontools http://dinosaur.compilertools.net http://dinosaur.compilertools.net 5 The Lex & Yacc Page. http://dinosaur.compilertools.net (01 March 2003). http://dinosaur.compilertools.net http://catalog.compilertools.net http://catalog.compilertools.net 6 Compiler Construction Kits. http://catalog.compilertools.net (01 March 2003). http://catalog.compilertools.net http://www.first.gmd.de/cocktail/ http://www.first.gmd.de/cocktail/ 7 The Cocktail Compiler Toolbox. http://www.first.gmd.de/cocktail/ (01 March 2003). http://www.first.gmd.de/cocktail/


Download ppt "Compiler Construction Tools Libby Rasnick Christopher Newport University CPSC 560 Spring 2003."

Similar presentations


Ads by Google