Download presentation
Presentation is loading. Please wait.
Published byBarry Newton Modified over 8 years ago
1
©SoftMoore ConsultingSlide 1 Structure of Compilers
2
©SoftMoore ConsultingSlide 2 Structure of a Compiler Lexical Analyzer (a.k.a. Scanner) Syntax Analyzer (a.k.a. Parser) Constraint Analyzer Optimizer Code Generator Identifier Table Error Handler Source Program Object Program Front End Back End
3
©SoftMoore ConsultingSlide 3 Comments on Compiler Structure Not every phase is separated out as a distinct collection of code modules; e.g., syntax analysis and constraint analysis might be intertwined. Many compilers use intermediate code during the compilation process –abstract syntax tree (high-level intermediate code representing the basic structure of the program) –low-level intermediate code similar to machine code (but machine independent) Some optimization can be performed on the intermediate code.
4
©SoftMoore ConsultingSlide 4 Compiler Front End Analysis –lexical analysis –syntax analysis –constraint analysis Determine the intended effect of the program Heavily dependent on the source language Relatively independent of target machine Can include some high-level optimizations
5
©SoftMoore ConsultingSlide 5 Compiler Back End Synthesis –optimization –code generation Generating an equivalent machine code program Heavily dependent on the target machine Relatively independent of source language
6
©SoftMoore ConsultingSlide 6 Scanner (Lexical Analysis) Identifies basic lexical units of the language –called tokens or symbols –based on regular expressions Removes extraneous white space and comments Reports any errors Scanner y := x + 1 id [“y”, (1, 1)]:= [(1, 3)]id [“x”, (1, 6)]+ [(1, 8)]literal [(“1”, (1, 10)]
7
©SoftMoore ConsultingSlide 7 Parser (Syntax Analysis) Verifies that the grammatical rules of the language are satisfied Based on context-free grammars (a.k.a. BNF) Parser := id [“y”, (1, 1)]:= [(1, 3)]id [“x”, (1, 6)]+ [(1, 8)]literal [(“1”, (1, 10)] id y id x + 1
8
©SoftMoore ConsultingSlide 8 Constraint Analyzer Handles features that can’t be expressed in a context free grammar (e.g., a variable must be declared exactly once if referenced in an assignment statement) Scope and type analysis Usually just checks for validity with little or no modification of current representation Constraint analysis is sometimes referred to as semantic analysis.
9
©SoftMoore ConsultingSlide 9 Optimizer Time/space performance of the code Register allocation Move invariant computations outside of a loop Compile time arithmetic Both intermediate and object code optimizations Local versus global optimization Optimizing compilers
10
©SoftMoore ConsultingSlide 10 Optimizer (continued) Optimizer := id y id x + 1 := id y id x inc
11
©SoftMoore ConsultingSlide 11 Code Generator Translates optimized intermediate code into machine code or assembly language Highly machine dependent
12
©SoftMoore ConsultingSlide 12 Code Generator (continued) := id y id x inc Code Generator LDLADDR 16 LDLADDR 12 LOADW INC STOREW
13
©SoftMoore ConsultingSlide 13 Tables and Maps Often drive the compilation process (table driven parsers) Record information (attributes) about identifiers –identifier table/map –symbol table/map –type table/map
14
©SoftMoore ConsultingSlide 14 Error Handler Reports nature and location of errors (error messages) Most of the time, a compiler is used to compile incorrect programs! Error recovery –Turbo Pascal versus traditional approach –All errors reported after the first error are suspect.
15
©SoftMoore ConsultingSlide 15 Compiler Construction Tools Scanner/Parser generators; e.g., Antlr, Coco/R, Flex/Bison, Lex/Yacc, JavaCC Syntax directed translation engines Code generators for common low-level intermediate code; e.g., LLVM Optimizers (DEC Ada PL/I story)
16
©SoftMoore ConsultingSlide 16 Passes Pass – a complete traversal of the source program or an equivalent intermediate representation. Often involves disk I/O (i.e., reading and/or writing a file to disk), but the intermediate representation can be in memory. Note: Some authors restrict the definition of compiler to a traversal that involves disk I/O, but we will use a more general definition. A single-pass compiler makes only one traversal of the source program. (early Pascal compilers) A multi-pass compiler makes several traversals.
17
©SoftMoore ConsultingSlide 17 Single-pass Versus Multi-pass Compilers Advantages of multi-pass compilers –increased modularity –improved ability to perform global analysis (optimization) –usually less memory required at run-time (passes overlaid) –ideal for multiprocessor systems –some languages require more than one pass (e.g., if the defining occurrence of an identifier can follow an applied occurrence) Disadvantages of multi-pass compilers –can be slower, especially if extra disk I/O is involved –usually larger (in terms of SLOCs) and more complex –requires design of intermediate language(s)/representation(s)
18
©SoftMoore ConsultingSlide 18 Passes in the Compiler Project Pass 1: Reads/analyzes source text and produces intermediate representation (AST’s) Pass 2: Performs constraint analysis Pass 3: Generates assembly language for the CPRLVM (Note: Some optimizations are performed by the assembler.) For the project, all intermediate passes use in-memory data structures called abstract syntax trees. The only I/O to disk occurs when reading the source file and generating the object code.
19
©SoftMoore ConsultingSlide 19 Possible Compiler Design Goals (Conflicts and Tradeoffs) Reliability (Rule #1: A compiler must be error free.) Modularity/maintenance Fast object programs Small object programs Fast compilation times Small compiler size Good diagnostic and error recovery capabilities Minimize compiler development time
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.