Semantic Analysis. Lecture 9. February 7, 2018
|
|
- Earl Rose
- 5 years ago
- Views:
Transcription
1 Semantic Analysis Lecture 9 February 7, 2018
2 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average 92 Compiler Construction 2/48
3 Formal Languages Wrap-up You made it! Takeaways from lexical analysis and parsing Regular languages define tokens Context-free languages define parse trees Classes of languages require different levels of expressive power Compiler Construction 3/48
4 Compilers Reminder 1. Lexical Analysis Source code Tokens 2. Syntactic Analysis Tokens Abstract Syntax Tree 3. Semantic Analysis (later today) 4. Code Generation 5. Optimization Compiler Construction 4/48
5 Compilers Reminder Remember, we want to support developers effort to write software They give our compiler source code Our job is to turn it into a program Automatic conversion requires various levels of analysis On the front end, we want to extract structure from the string of source code Compiler Construction 5/48
6 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Is there a structural difference between these strings? x 1 ; Compiler Construction 6/48
7 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Is there a structural difference between these strings? y 1 ; Compiler Construction 6/48
8 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Is there a structural difference between these strings? z 42 ; Compiler Construction 6/48
9 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Is there a structural difference between these strings? y 100 ; Assign x = 1; y = 1; z = 42; y = 100;... Compiler Construction 6/48
10 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Is there a structural difference between these strings? y 100 ; Assign x = 1; y = 1; z = 42; y = 100;... Compiler Construction 6/48
11 Parsing to Extract Structure Consider two input strings: x = 1; y = 1; And what about: z = 42; y = 100; = Assign ID = VALUE ; Is there a structural difference between these strings? ID VALUE ; Compiler Construction 6/48
12 Parsing to Extract Structure We want a concise grammar Don t want to depend on specific values! S ID = VAL ;, not S x = 42 ; Well, we need an earlier lexical analysis step Bin lexemes into tokens x, y, z all the same thing: identifiers 1, 42, 100 all the same thing: integer constants Compiler Construction 7/48
13 Wrapping Up Formal Languages Lexical Analysis Regular Language = DFA = NFA = Regular Expression Syntactic Analysis / Parsing Context-free Language = Context-free Grammar = Nondeterministic Pushdown Automaton = Parsing Automaton Compiler Construction 8/48
14 Formal Languages Highlights Regular Languages are easier to recognize than CFLs! Need a stack with CFLs As compiler designers, we restrict our choices of CFL LL and LR are less expressive, but easier to parse Parsing DFA is kind of a lie! Technically, CFLs are recognized with NPDAs This is not a formal languages course Compiler Construction 9/48
15 Parse Tree vs. Abstract Syntax Tree Almost the same Input: 6 + ( ( 5 ) ) Parse Tree: EXP EXP EXP EXP int EXP 6 + int ( ( 5 ) ) Compiler Construction 10/48
16 Parse Tree vs. Abstract Syntax Tree Almost the same Input: 6 + ( ( 5 ) ) Abstract Syntax Tree: ADD_EXP int:6 int:5 Compiler Construction 10/48
17 Parsing Highlights You should understand that LL and LR are different restricted subsets of CFLs Trade language expressiveness for parsing ease You should know top-down and bottom-up parsing Real parser generators build bottom-up parsers We talked about LL(1), recursive descent, and LR(1) parsing There are many other parsing algorithms: LALR (lookahead LR) SLR (simple) GLR (generalized) IELR (inadequacy elimination) Earley Compiler Construction 11/48
18 Compiler Construction 12/48
19 The Front End Lexical Analysis Detects inputs with illegal tokens Syntactic Analysis Detects inputs with ill-formed parse trees Semantic Analysis Last front end phase Detects more invalid inputs Compiler Construction 12/48
20 Intro to Type Checking We have just parsed our program It followed the rules of a context-free grammar We know it is a structurally valid program Compiler Construction 13/48
21 Intro to Type Checking We have just parsed our program It followed the rules of a context-free grammar We know it is a structurally valid program Ultimately, programming languages are not context-free Need a rigorous analysis to tell us about semantic validity of the program Basically, a laundry list of tasks Compiler Construction 13/48
22 Type Checking Summary Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values. A type system specifies which operations are valid for which types. Type checking can be done statically (at compile time) or dynamically (at run time). Compiler Construction 14/48
23 What s Wrong? Example 1: 1 l e t y : I n t in 2 x + 3 Example 2: 1 l e t y : S t r i n g < " abc " in 2 y + 3 Compiler Construction 15/48
24 Semantic Analysis: Why? Parsing cannot catch some errors Some language constructs are not context-free. All used variables must have been declared (scoping) All methods must be invoked with arguments of the proper type (i.e. typing) Compiler Construction 16/48
25 What does semantic analysis do? Lots of things! In Cool: All identifiers are declared Static types Inheritance relationships Classes defined only once Methods in a class defined only once Reserved identifiers are not misused several others... The requirements are language-dependent. Which of these are checked by Python? C++? Compiler Construction 17/48
26 Scope Scoping rules match identifier uses with identifier declarations Critical analysis in most languages (including Cool) Compiler Construction 18/48
27 Scope The scope of an identifier is the portion of a program in which that identifier is accessible The same identifier may refer to different things in different parts of the program Different scopes for the same name do not overlap An identifier may have restricted scope Compiler Construction 19/48
28 Static vs. Dynamic Scope Most languages have static scope Scope depends only on the program text, not the run-time behavior Cool is statically scoped A few languages are dynamically scoped L A TEX, SNOBOL Compiler Construction 20/48
29 Static vs. Dynamic Scope 1 int x; 2 3 int main () { 4 x = 2; 5 f(); 6 g(); 7 } 8 void f () { 9 int x = 3; 10 h(); 11 } 12 void g () { 13 int x = 4; 14 h(); 15 } 16 void h() { 17 printf ("%d\n", x); 18 } Compiler Construction 21/48
30 Static vs. Dynamic Scope 1 int x; 2 3 int main () { 4 x = 2; 5 f(); 6 g(); 7 } 8 void f () { 9 int x = 3; 10 h(); 11 } 12 void g () { 13 int x = 4; 14 h(); 15 } 16 void h() { 17 printf ("%d\n", x); 18 } Static scoping output: 2 2 Dynamic scoping output: 3 4 Compiler Construction 21/48
31 Static Scoping Example 1 let x : Int <- 0 in 2 { 3 x; 4 { let x : Int <- 1 in 5 x ; 6 }; 7 x; 8 } Compiler Construction 22/48
32 Static Scoping Example 9 let x : Int <- 0 in 10 { 11 x; 12 { let x : Int <- 1 in 13 x ; 14 }; 15 x; 16 } Uses of x refer to closest enclosing definition. Compiler Construction 22/48
33 Scope in Cool Cool identifier bindings are introduced by Class declarations (class names) Method definitions (method names) Let expressions (object id s) Formal parameters (object id s) Attribute definitions in a class (object id s) Case expressions (object id s) Compiler Construction 23/48
34 Implementing the Most-Closely Nested Rule Much of semantic analysis can be expressed as a recursive descent of an AST! Process an AST node n Process the children of n Finish processing the node n Compiler Construction 24/48
35 Implementing... (2) Example: the scope of let bindings is one subtree 1 l e t x : I n t < 0 in expr x can be used in subtree expr Compiler Construction 24/48
36 Symbol Tables Recall: let x : Int <- 0 in expr Idea: Before processing expr, add definition of x to current definitions (override any previous definition) After processing expr, remove definition of x and restore old definition of x (if any) A symbol table is a data structure that tracks the current bindings of identifiers Part of PA4 How might we implement this? Compiler Construction 25/48
37 Exceptions to Most-closely Nested Not all kinds of identifiers follow the most-closely nested rule Class definitions in Cool are globally visible throughout all of the program Implication: class names can be used before it is defined Compiler Construction 26/48
38 Example: Use before define 1 class Foo { 2... let y : Test in... 3 }; 4 class Test { }; Compiler Construction 27/48
39 More Scope in Cool Attribute names are global within the class in which they are defined 1 class Foo { 2 f() : Int { tm }; 3 tm : Int <- 0; 4 }; Compiler Construction 28/48
40 More Scope in Cool Method and attribute names have complex rules A method need not be defined in the class in which it is used, but in some parent class that s inheritance Methods may also be redefined (overridden) Which languages allow this? Cool does Compiler Construction 29/48
41 Class Definitions Class names can be used before being defined We can t check this property in one pass :( Solution Pass 1: Collect all the class names Pass 2: Do the checking? Pass 4: Profit! Semantic analysis requires multiple passes Compiler Construction 30/48
42 Types What is a Type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type Compiler Construction 31/48
43 Why do we need types? Consider this assembly: addi, $r1, $r2, $r3 What are the types of r1, r2, and r3? Compiler Construction 32/48
44 Types and Operations Cretain operations are legal or valid for values of each type It doesn t make sense to add a function pointer and an integer in C It does make sense to add two integers Both are the same in assembly! Compiler Construction 33/48
45 Type Systems A language s type system specifies which operations are valid for which types The goal of type checking is to ensure that operations are used with the correct types Enforces intended interpretation of values, because nothing else will! Our last, best hope... for victorious execution Type systems provide a concise formalization of the semantic checking rules Compiler Construction 34/48
46 What Can Types Do? Can detect certain kinds of errors Memory errors: Reading from an invalid pointer, etc. Violation of abstraction boundaries class FileSystem { open ( x : String ) : File {... }; }; The f method can t see data inside fdesc! class Client { f( fs : FileSystem ) { File fdesc <- fs. open (" foo ")... } }; Compiler Construction 35/48
47 Type Checking Overview Three kinds of languages: Statically typed: All or almost all checking of types is done as part of compilation (C, Java, Cool) Dynamically typed: Almost all checking of types is done during execution (Python, PHP, Ruby) Untyped: No type checking (machine code) Compiler Construction 36/48
48 The Type Wars Competing views on static vs. dynamic typing Static typing proponents say: Static checking catches many programming errors at compile time Avoids overhead of runtime type checks Dynamic typing proponents say: Static type systems are restrictive Rapid prototyping is easier in a dynamic type system Compiler Construction 37/48
49 The Type Wars (2) In practice, most code is written in statically-typed languages with an escape mechanism. Unsafe casts in C, native methods in Java,... Dynamic typing (a.k.a. duck typing) is big in the scripting world Compiler Construction 38/48
50 Cool Types In Cool, types are Class names SELF_TYPE Technically, there are no native types Everything is boxed as an object Java Integer int The use declares types for all identifiers The compiler infers types for expressions every expression Compiler Construction 39/48
51 Type checking and Type Inference Type Checking is the process of verifying fully-typed programs Type Inference is the process of filling in missing type information Sometimes used interchangably Compiler Construction 40/48
52 Rules of Inference You have survived formal languages in specifying parts ot he compiler Regular expressions (lexer) Context-free grammars (parser) We use logical rules of inference as a formalism for type inference Compiler Construction 41/48
53 Rules of Inference? Inference rules have the form If Hypothesis is true, then Conclusion is true Type checking computes via reasoning: If E 1 and E 2 have certain types, then E 3 has a certain type Rules of inference are a compact notation for If-then statements Compiler Construction 42/48
54 From English to Inference Rules Start with a simplified system and gradually add features... Building blocks Symbol is and Symbol is if-then x:t is x has type T Compiler Construction 43/48
55 English to Inference Rules (2) If e 1 has type Int and e 2 has type Int, then e 1 + e 2 has type Int (e 1 has type Int e 2 has type Int) e 1 + e 2 has type Int (e 1 : Int e 2 : Int) e 1 + e 2 : Int This is an inference rule! e 1 : Int e 2 : Int are two hypotheses, e 1 + e 2 : Int is a conclusion! Compiler Construction 44/48
56 Notation for Inference Rules By tradition, inference rules are written Hypothesis 1... Hypothesis n Conclusion Cool type rules have hypotheses and conclusions of the form: e : T means we can prove that... Compiler Construction 45/48
57 Examples (i is an Integer) i : Int Int e 1 : Int e 2 : Int e 1 + e 2 : Int Add (the result of adding to Ints is an Int) Compiler Construction 46/48
58 Examples (2) e 1 : Int e 2 : Int e 1 + e 2 : Int Add These rules give templates describing how to type integers and + expressions... We can fill in the templates, producing a complete typing for any expression! false : Int true : Int true + false : Int Compiler Construction 47/48
59 Example: : Int 2 : Int : Int Compiler Construction 48/48
Scoping and Type Checking
#1 Scoping and Type Checking #2 #3 Recall: Review Sets Exist Ask me to look at your answers! Derivations and parse trees are closely related, but if we ask you to draw a parse tree you must draw the parse
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Statically vs. Dynamically typed languages
More informationThe role of semantic analysis in a compiler
Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationThe Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis
The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More informationTypes. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus
Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type
More informationOverview of Semantic Analysis. Lecture 9
Overview of Semantic Analysis Lecture 9 1 Midterm Thursday In class SCPD students come to campus for the exam Material through lecture 8 Open note Laptops OK, but no internet or computation 2 Outline The
More informationThe Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.
The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees
More informationAnnouncements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types
Announcements Types Working on requirements this week Work on design, implementation Lecture 17 CS 169 Prof. Brewer CS 169 Lecture 16 1 Prof. Brewer CS 169 Lecture 16 2 Outline Type concepts Where do types
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationCSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking
CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax
More informationCS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).
CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Parser Generators: ANTLR History of ANTLR ANother Tool for Language Recognition Terence Parr's dissertation: Obtaining Practical Variants of LL(k) and LR(k) for k > 1 PCCTS:
More informationOperational Semantics. One-Slide Summary. Lecture Outline
Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:
More informationMIDTERM EXAM (Solutions)
MIDTERM EXAM (Solutions) Total Score: 100, Max. Score: 83, Min. Score: 26, Avg. Score: 57.3 1. (10 pts.) List all major categories of programming languages, outline their definitive characteristics and
More informationCSE 401 Midterm Exam Sample Solution 2/11/15
Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationTopics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited
CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation
More informationLecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1
Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery Last modified: Mon Feb 23 10:05:56 2015 CS164: Lecture #14 1 Shift/Reduce Conflicts If a DFA state contains both [X: α aβ, b] and [Y: γ, a],
More informationOperational Semantics of Cool
Operational Semantics of Cool Key Concepts semantics: the meaning of a program, what does program do? how the code is executed? operational semantics: high level code generation steps of calculating values
More informationCOMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table
COMPILER CONSTRUCTION Lab 2 Symbol table LABS Lab 3 LR parsing and abstract syntax tree construction using ''bison' Lab 4 Semantic analysis (type checking) PHASES OF A COMPILER Source Program Lab 2 Symtab
More informationSemantic Analysis and Type Checking
Semantic Analysis and Type Checking The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on
More informationProject Compiler. CS031 TA Help Session November 28, 2011
Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level
More informationStatic Semantics. Winter /3/ Hal Perkins & UW CSE I-1
CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationContext Analysis. Mooly Sagiv. html://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html
Context Analysis Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html 1 Short Decaf Program Interface not declared class MyClass implements MyInterface { string myinteger; void dosomething()
More informationSyntax Errors; Static Semantics
Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to
More informationCSCE 314 Programming Languages. Type System
CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.
More informationLecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far
Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify
More informationLECTURE NOTES ON COMPILER DESIGN P a g e 2
LECTURE NOTES ON COMPILER DESIGN P a g e 1 (PCCS4305) COMPILER DESIGN KISHORE KUMAR SAHU SR. LECTURER, DEPARTMENT OF INFORMATION TECHNOLOGY ROLAND INSTITUTE OF TECHNOLOGY, BERHAMPUR LECTURE NOTES ON COMPILER
More informationType Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.
Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationEDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:
EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)
More informationFirst Midterm Exam CS164, Fall 2007 Oct 2, 2007
P a g e 1 First Midterm Exam CS164, Fall 2007 Oct 2, 2007 Please read all instructions (including these) carefully. Write your name, login, and SID. No electronic devices are allowed, including cell phones
More informationOutline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference
Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationIntroduction to Lexical Analysis
Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexical analyzers (lexers) Regular
More informationCMSC 330: Organization of Programming Languages. Operational Semantics
CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged
More informationCompiler Theory. (Semantic Analysis and Run-Time Environments)
Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful
More informationMidterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1
Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)
More informationCOMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking
Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)
More informationInformal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility
Informal Semantics of Data semantic specification names (identifiers) attributes binding declarations scope rules visibility 1 Ways to Specify Semantics Standards Documents (Language Definition) Language
More informationLexical Analysis. Chapter 2
Lexical Analysis Chapter 2 1 Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples
More informationWhat is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done
What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g., C++) to low-level assembly language that can be executed by hardware int a,
More informationUNIVERSITY OF CALIFORNIA
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS164 Fall 1997 P. N. Hilfinger CS 164: Midterm Name: Please do not discuss the contents of
More informationLexical Analysis. Lecture 3-4
Lexical Analysis Lecture 3-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 3-4 1 Administrivia I suggest you start looking at Python (see link on class home page). Please
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False
More informationSemantic Analysis. Compiler Architecture
Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis
More informationLECTURE 3. Compiler Phases
LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent
More informationLecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.
Lecture Outline Operational Semantics of Cool COOL operational semantics Motivation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Notation The rules CS781(Prasad) L24CG 1 CS781(Prasad)
More informationClosures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming
Closures Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming Summary 1. Predictive Parsing 2. Large Step Operational Semantics (Natural) 3. Small Step Operational Semantics
More informationBriefly describe the purpose of the lexical and syntax analysis phases in a compiler.
Name: Midterm Exam PID: This is a closed-book exam; you may not use any tools besides a pen. You have 75 minutes to answer all questions. There are a total of 75 points available. Please write legibly;
More informationProgramming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3
Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components
More informationCompilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:
More information9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation
Language Implementation Methods The Design and Implementation of Programming Languages Compilation Interpretation Hybrid In Text: Chapter 1 2 Compilation Interpretation Translate high-level programs to
More informationCS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)
CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going
More informationClosures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming
Closures Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming t ::= x x. t t t Call-by-value big-step Operational Semantics terms variable v ::= values abstraction x.
More informationLexical Analysis. Lecture 2-4
Lexical Analysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 2 1 Administrivia Moving to 60 Evans on Wednesday HW1 available Pyth manual available on line.
More informationCS164: Midterm I. Fall 2003
CS164: Midterm I Fall 2003 Please read all instructions (including these) carefully. Write your name, login, and circle the time of your section. Read each question carefully and think about what s being
More informationTime : 1 Hour Max Marks : 30
Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with
More informationCS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria
CS152 Programming Language Paradigms Prof. Tom Austin, Fall 2014 Syntax & Semantics, and Language Design Criteria Lab 1 solution (in class) Formally defining a language When we define a language, we need
More informationCSE P 501 Exam 11/17/05 Sample Solution
1. (8 points) Write a regular expression or set of regular expressions that generate the following sets of strings. You can use abbreviations (i.e., name = regular expression) if it helps to make your
More informationWhat is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573
What is a compiler? Xiaokang Qiu Purdue University ECE 573 August 21, 2017 What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g.,
More informationOutline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context
Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of
More informationCSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)
CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent
More informationG Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University
G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 4a Andrew Tolmach Portland State University 1994-2017 Semantics and Erroneous Programs Important part of language specification is distinguishing valid from
More informationCOMPILER CONSTRUCTION Seminar 02 TDDB44
COMPILER CONSTRUCTION Seminar 02 TDDB44 Martin Sjölund (martin.sjolund@liu.se) Adrian Horga (adrian.horga@liu.se) Department of Computer and Information Science Linköping University LABS Lab 3 LR parsing
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationProgram Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.
Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008. Outline. Abstractions for telling a computer how to do things. Computational Paradigms. Language Definition, Translation.
More informationThe compilation process is driven by the syntactic structure of the program as discovered by the parser
Semantic Analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationECE251 Midterm practice questions, Fall 2010
ECE251 Midterm practice questions, Fall 2010 Patrick Lam October 20, 2010 Bootstrapping In particular, say you have a compiler from C to Pascal which runs on x86, and you want to write a self-hosting Java
More informationCMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics
Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End
More informationJavaCC Parser. The Compilation Task. Automated? JavaCC Parser
JavaCC Parser The Compilation Task Input character stream Lexer stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1 CC&P 2003 2 Automated? JavaCC Parser The initial
More informationSyntax-Directed Translation. Lecture 14
Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,
More informationThe Environment Model. Nate Foster Spring 2018
The Environment Model Nate Foster Spring 2018 Review Previously in 3110: Interpreters: ASTs, evaluation, parsing Formal syntax: BNF Formal semantics: dynamic: small-step substitution model static semantics
More informationTopics Covered Thus Far CMSC 330: Organization of Programming Languages
Topics Covered Thus Far CMSC 330: Organization of Programming Languages Names & Binding, Type Systems Programming languages Ruby Ocaml Lambda calculus Syntax specification Regular expressions Context free
More informationCSE 401 Midterm Exam Sample Solution 11/4/11
Question 1. (12 points, 2 each) The front end of a compiler consists of three parts: scanner, parser, and (static) semantics. Collectively these need to analyze the input program and decide if it is correctly
More informationOperational Semantics of Cool
Operational Semantics of Cool Lecture 22 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week are Wed at 4pm and
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationCrafting a Compiler with C (II) Compiler V. S. Interpreter
Crafting a Compiler with C (II) 資科系 林偉川 Compiler V S Interpreter Compilation - Translate high-level program to machine code Lexical Analyzer, Syntax Analyzer, Intermediate code generator(semantics Analyzer),
More informationFormal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1
Formal Semantics Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1 Formal Semantics At the beginning of the book we saw formal definitions of syntax with BNF And how to make a BNF that generates
More informationType Checking in COOL (II) Lecture 10
Type Checking in COOL (II) Lecture 10 1 Lecture Outline Type systems and their expressiveness Type checking with SELF_TYPE in COOL Error recovery in semantic analysis 2 Expressiveness of Static Type Systems
More informationIn Our Last Exciting Episode
In Our Last Exciting Episode #1 Lessons From Model Checking To find bugs, we need specifications What are some good specifications? To convert a program into a model, we need predicates/invariants and
More informationAnnouncements. Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions!
Type-Checking Announcements Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions! Stop by office hours! Email the staff list! Ask on Piazza!
More informationThe Environment Model
The Environment Model Prof. Clarkson Fall 2017 Today s music: Selections from Doctor Who soundtracks by Murray Gold Review Previously in 3110: Interpreters: ASTs, evaluation, parsing Formal syntax: BNF
More informationCMSC 350: COMPILER DESIGN
Lecture 11 CMSC 350: COMPILER DESIGN see HW3 LLVMLITE SPECIFICATION Eisenberg CMSC 350: Compilers 2 Discussion: Defining a Language Premise: programming languages are purely formal objects We (as language
More informationMore Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE
More Static Semantics #1 One-Slide Summary Typing rules formalize the semantics checks necessary to validate a program. Well-typed programs do not go wrong. Subtyping relations ( ) and least-upper-bounds
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationCOS 320. Compiling Techniques
Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly
More informationScoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.
#1 Type Checking Previously, in PL Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values. A type system specifies which
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter
More informationAbstract Syntax Trees & Top-Down Parsing
Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationStatic Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1
Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing
More informationCOMPILER DESIGN. For COMPUTER SCIENCE
COMPILER DESIGN For COMPUTER SCIENCE . COMPILER DESIGN SYLLABUS Lexical analysis, parsing, syntax-directed translation. Runtime environments. Intermediate code generation. ANALYSIS OF GATE PAPERS Exam
More information