Download presentation
Presentation is loading. Please wait.
Published byAudra Lang Modified over 9 years ago
1
COMPSCI 322: Language and Compilers Class Hour: Hyer Hall 210: TThu 9:30am – 10:15am
2
A little bit about the instructor Graduated from the University of Connecticut (05 Class), Ph.D in Computer Science and Engineering Bachelor of Science from Hanoi University of Technology (86-91) Master of Computer Science from UW-Milwaukee (96-99) Assistant professor at UWW since August 2005
3
A little bit about the instructor Research Experience: –User Modeling, Information Retrieval, Decision Theory, Collaborative Filtering, Human Factors Teaching Experience: –MCS 220, COMPSCI 172, 181, 271, 381 at UWW –Introductory courses at UOP and Devry –TA for Computer Architecture, OO Design, Compiler, Artificial Intelligence
4
Contact information nguyenh@uww.edu (fastest way to contact me) Baker Hall 324 Office Hours: 9:50am – 10:50 am, 3-4pm, MWF or by appointment 262 472 5170
5
Course Objectives Understand the description and successfully design a scanner, parser, semantic checker and code generator for this language Implement successfully a scanner, parser, semantic checker and code generator for this given language. Test the implementation with all test cases for each component in a compiler.
6
Book Requirement Engineering a Compiler. 2004. Keith D. Cooper and Linda Torczon. Morgan Kaufmann Publisher (available in TextBook rental) Web site: http://www.cs.rice.edu/~keith/Errata.html http://www.cs.rice.edu/~keith/Errata.html
7
Course detail - Evaluation GRADABLEPOINTS 3 projects650 Final Exam150 Presentation100 In class exercises100 Total1000
8
Projects 3 projects: scanner, parser and semantic checker, code generator. Preferred language to develop them is Java, but C/C++ are welcomed too. Project 3 depends on Project 2, Project 2 depends on Project 1. ABSOLUTELY no LATE submission for Project 3 because of the time consuming to grade this project.
9
In class exercises Simple multiple choice questions and simple problems will be given in class weekly and graded. This requires students to read the assigned reading (partly also because this is a discussion course instead of lecture) –Not all material will be covered in class –Book complements the lectures
10
Presentation Each student will do research on a specific programming language of his choice. Please let the instructor know ahead of time which language do you choose Then present 15-20 minutes his research in front of class using powerpoint presentation. This will be followed by 10 minute questions.
11
Grade
12
Prerequisite Prerequisite: COMPSCI 271, and Data Structures Students are responsible for meeting these requirements.
13
Compilers What is a compiler? –A program that translates an executable program in one language into an executable program in another language –The compiler should improve the program, in some way What is an interpreter?
14
Compilers What is a compiler? –A program that translates an executable program in one language into an executable program in another language –The compiler should improve the program, in some way What is an interpreter? –A program that reads an executable program and produces the results of executing that program
15
Examples C is typically compiled, Basic is typically interpreted Java is compiled to bytecodes (code for the Java VM). –which are then interpreted –Or a hybrid strategy is used Just-in-time compilation
16
Taking a Broader View Compiler Technology = Off-Line Processing –Goals: improved performance and language usability Making it practical to use the full power of the language –Trade-off: preprocessing time versus execution time (or space) –Rule: performance of both compiler and application must be acceptable to the end user
17
Why study Compilation “ So even though I'd never actually want to write a compiler myself, knowing about compiler concepts would have made me a better programmer. It's one of those gaps that I regret, which is why I think I may actually try to struggle through a few chapters from this Engineering a Compiler book during the holidays, in between all the holiday activities like eating. And shopping. And listening to "Santa Got Run Over By a Reindeer" for the billionth time … “
18
Why Study Compilation? Compilers are important system software components –They are intimately interconnected with architecture, systems, programming methodology, and language design Compilers include many applications of theory to practice –Scanning, parsing, static analysis, instruction selection Many practical applications have embedded languages –Commands, macros, formatting tags …
19
Why Study Compilation? Many applications have input formats that look like languages, –Matlab, Mathematica Writing a compiler exposes practical algorithmic & engineering issues –Approximating hard problems; efficiency & scalability
20
Intrinsic interest Compiler construction involves ideas from many different parts of computer science Artificial intelligence Greedy algorithms Heuristic search techniques Algorithms Graph algorithms, union-find Dynamic programming Theory DFAs & PDAs, pattern matching Fixed-point algorithms Systems Allocation & naming, Synchronization, locality Architecture Pipeline & hierarchy management Instruction set use
21
Intrinsic merit Compiler construction poses challenging and interesting problems: –Compilers must do a lot but also run fast –Compilers have primary responsibility for run-time performance –Compilers are responsible for making it acceptable to use the full power of the programming language –Computer architects perpetually create new challenges for the compiler by building more complex machines –Compilers must hide that complexity from the programmer –Success requires mastery of complex interactions
22
Preparation for next class Review the materials for this class Read chapter 1 of the book
23
Overview of compilers
24
High-level View of a Compiler Source code Machine code Compiler Errors
25
High-level overview of a compiler Implications –Must recognize legal (and illegal) programs –Must generate correct code –Must manage storage of all variables (and code) –Must agree with OS & linker on format for object code Big step up from assembly language—use higher level notations
26
Traditional Two-pass Compiler Use an intermediate representation (IR) Front end maps legal source code into IR Back end maps IR into target machine code Admits multiple front ends & multiple passes Source code Front End Errors Machine code Back End IR
27
Responsibilities –Recognize legal (& illegal) programs –Report errors in a useful way –Produce IR & preliminary storage map –Shape the code for the back end –Much of front end construction can be automated The Front End Source code Scanner IR Parser Errors tokens
28
Scanner Maps character stream into words Produces pairs (token): x = x + y ; becomes = + ; –word lexeme, part of speech token type Typical tokens include number, identifier, +, –, new, while, if Scanner eliminates white space and comments Speed is important
29
Parser Recognizes context-free syntax & reports errors Guides context-sensitive (“semantic”) analysis (type checking) Builds IR for source program Hand-coded parsers are fairly easy to build Most books advocate using automatic parser generators
30
Parser Context-free syntax is specified with a grammar SheepNoise SheepNoise baa | baa SheepNoise -> nil This grammar defines the set of noises that a sheep makes under normal circumstances It is written in a variant of Backus–Naur Form (BNF)
31
Parser Formally, a grammar G = (S,N,T,P) S is the start symbol N is a set of non-terminal symbols T is a set of terminal symbols or words P is a set of productions or rewrite rules (P : N N T )
32
Parser 1. goal expr 2. expr expr op term 3. | term 4. term number 5. | id 6. op + 7. | - S = goal T = { number, id, +, - } N = { goal, expr, term, op } P = { 1, 2, 3, 4, 5, 6, 7}
33
Parser Context-free syntax can be put to better use This grammar defines simple expressions with addition & subtraction over “number” and “id”. This grammar, like many, falls in a class called “context-free grammars”, abbreviated CFG.
34
Parser Production Result goal 1 expr 2 expr op term 5 expr op y 7 expr - y 2 expr op term - y 4 expr op 2 - y 6 expr + 2 - y 3 term + 2 - y 5 x + 2 - y x + 2 - y
35
Parser A parse can be represented by a tree ( parse tree or syntax tree ) x + 2 - y termoptermexpr termexpr goal expr op + - 1. goal expr 2. expr expr op term 3. | term 4. term number 5. | id 6. op + 7. | -
36
Parser Compilers often use an abstract syntax tree + - The AST summarizes grammatical structure, without including detail about the derivation
37
The Back End Responsibilities Translate IR into target machine code Choose instructions to implement each IR operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been less successful in the back end Errors IR Register Allocation Instruction Selection Machine code Instruction Scheduling IR
38
The Back End Instruction Selection Produce fast, compact code Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem –ad hoc methods, pattern matching, dynamic programming Errors IR Register Allocation Instruction Selection Machine code Instruction Scheduling IR
39
The Back End Register Allocation Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs Optimal allocation is NP-Complete (1 or k registers) Compilers approximate solutions to NP-Complete problems Errors IR Register Allocation Instruction Selection Machine code Instruction Scheduling IR
40
The Back End Instruction Scheduling Avoid hardware stalls and interlocks Use all functional units productively Can increase lifetime of variables (changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Heuristic techniques are well developed Errors IR Register Allocation Instruction Selection Machine code Instruction Scheduling IR
41
Traditional Three-pass Compiler Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code –May also improve space, power consumption, … Must preserve “meaning” of the code –Measured by values of named variables Errors Source Code Middle End Front End Machine code Back End IR
42
The Optimizer (or Middle End) Typical Transformations Discover & propagate some constant value Move a computation to a less frequently executed place Specialize some computation based on context Discover a redundant computation & remove it Remove useless or unreachable code Encode an idiom in some particularly efficient form Errors Opt1Opt1 Opt3Opt3 Opt2Opt2 OptnOptn... IR Modern optimizers are structured as a series of passes
43
Modern Restructuring Compiler Typical Restructuring Transformations: Blocking for memory hierarchy and register reuse Vectorization Parallelization All based on dependence Also full and partial inlining Errors Source Code Restructure r Front End Machine code Opt + Back End HL AST IR HL AST IR Gen
44
Discussion Consider a simple web browser that takes as input a textual string in HTML format and displays the specified graphics on the screen. Is the display process of compilation or interpretation? Why?
45
Next class Lexical analysis Chapter 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.