UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Names and Bindings.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Final Lecture of the Semester Spring 2010 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2007 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.8: Interpretation Spring 2008 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2008 Marco Valtorta.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.2: Language Processors Spring 2010 Marco.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.2 Spring 2007 Marco Valtorta
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Languages and Compilers (SProg og Oversættere)
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
C H A P T E R TWO Syntax and Semantic.
Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University.
Introduction to Language Processing Technology Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
LESSON 04.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., D.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Constructing Precedence Table
8. Symbol Table Chih-Hung Wang
Chapter 3 – Describing Syntax
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Course supervisor: Lubna Siddiqui
Compiler Design 4. Language Grammars
Introduction CI612 Compiler Design CI612 Compiler Design.
CSCE 531 Compiler Construction Ch.2
CSE401 Introduction to Compiler Construction
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
CS416 Compiler Design lec00-outline February 23, 2019
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Acknowledgment The slides are based on the textbook and other sources, including slides from Bent Thomsen’s course at the University of Aalborg in Denmark and several other fine textbooks The three main other compiler textbooks I considered are: –Aho, Alfred V., Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, & Tools, 2 nd ed. Addison-Welsey, (The “dragon book”) –Appel, Andrew W. Modern Compiler Implementation in Java, 2 nd ed. Cambridge, (Editions in ML and C also available; the “tiger books”) –Grune, Dick, Henri E. Bal, Ceriel J.H. Jacobs, and Koen G. Langendoen. Modern Compiler Design. Wiley, 2000

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Levels of Programming Languages High-level program class Triangle {... float surface() return b*h/2; } class Triangle {... float surface() return b*h/2; } Low-level program (in an assembly language) LOAD r1,b LOAD r2,h MUL r1,r2 DIV r1,#2 RET LOAD r1,b LOAD r2,h MUL r1,r2 DIV r1,#2 RET Executable machine code ( a string of bits)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Machine Code and Assembly A machine code program (or just “machine code”) is a sequence of instructions, where each instruction is a bit string that is interpreted by the machine to perform an operation The process of translating each instruction into machine code is called assembly Machine languages and assembly languages are low-level languages However, the notion of low- and high-level is relative

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Features of High-Level Languages Expressions Data types Control structures Declarations Control abstraction (via routines) Data abstraction (via encapsulation)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Declarations, Expressions, Commands A command is executed to update variables and perform I/O An expression is evaluated to yield a value A declaration is elaborated (at compile time) to produce bindings. It may also have the side effect of allocating and initializing variables

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Language Translation A source program in some source language is translated into an object program in some target language An assembler translates from assembly language to machine language A compiler translates from a high-level language into a low-level language –the compiler is written in its implementation language An interpreter is a program that accepts a source program and runs it immediately An interpretive compiler translates a source program into an intermediate language, and the resulting object program is then executed by an interpreter

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Programming in the Large

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax Specification Syntax is specified using Context Free Grammars: –A finite set of terminal symbols –A finite set of non-terminal symbols –A start symbol –A finite set of production rules Usually CFG are written in Backus-Naur Form (or Backus Normal Form) or BNF notation. A production rule in BNF notation is written as: N ::=  where N is a non terminal and  a sequence of terminals and non-terminals N ::=  is an abbreviation for several rules with N on the left-hand side.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax Specification A CFG defines a set of strings. This is called the language of the CFG. Example: Start ::= Letter | Start Letter | Start Digit Letter ::= a | b | c | d |... | z Digit ::= 0 | 1 | 2 |... | 9 Q: What is the “language” defined by this grammar? Note: see the first correction on the errata sheet for the textbook concerning the set of letters in Mini-Triangle.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax of Mini Triangle Mini triangle is a very simple Pascal-like programming language. An example program: !This is a comment. let const m ~ 7; var n in begin n := 2 * m * m ; putint(n) end !This is a comment. let const m ~ 7; var n in begin n := 2 * m * m ; putint(n) end Declarations Command Expression

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax of Mini Triangle Program ::= single-Command single-Command ::= V-name := Expression | Identifier ( Expression ) | if Expression then single-Command else single-Command | while Expression do single-Command | let Declaration in single-Command | begin Command end Command ::= single-Command | Command ; single-Command... Program ::= single-Command single-Command ::= V-name := Expression | Identifier ( Expression ) | if Expression then single-Command else single-Command | while Expression do single-Command | let Declaration in single-Command | begin Command end Command ::= single-Command | Command ; single-Command...

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax of Mini Triangle (continued) Expression ::= primary-Expression | Expression Operator primary-Expression primary-Expression ::= Integer-Literal | V-name | Operator primary-Expression | ( Expression ) V-name ::= Identifier Identifier ::= Letter | Identifier Letter | Identifier Digit Integer-Literal ::= Digit | Integer-Literal Digit Operator ::= + | - | * | / | | = Expression ::= primary-Expression | Expression Operator primary-Expression primary-Expression ::= Integer-Literal | V-name | Operator primary-Expression | ( Expression ) V-name ::= Identifier Identifier ::= Letter | Identifier Letter | Identifier Digit Integer-Literal ::= Digit | Integer-Literal Digit Operator ::= + | - | * | / | | =

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax of Mini Triangle Declaration ::= single-Declaration | Declaration ; single-Declaration single-Declaration ::= const Identifier ~ Expression | var Identifier : Type-denoter Type-denoter ::= Identifier Declaration ::= single-Declaration | Declaration ; single-Declaration single-Declaration ::= const Identifier ~ Expression | var Identifier : Type-denoter Type-denoter ::= Identifier Comment ::= ! CommentLine eol CommentLine ::= Graphic CommentLine Graphic ::= any printable character or space Comment ::= ! CommentLine eol CommentLine ::= Graphic CommentLine Graphic ::= any printable character or space

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax Trees A syntax tree is an ordered labeled tree such that: a) terminal nodes (leaf nodes) are labeled by terminal symbols b) non-terminal nodes (internal nodes) are labeled by non terminal symbols. c) each non-terminal node labeled by N has children X 1,X 2,...X n (in this order) such that N := X 1,X 2,...X n is a production.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax Trees Example: Expression V-name primary-Exp. Expression Ident d + primary-Exp OpInt-Lit 10* Op V-name primary-Exp. Ident d Expression ::= Expression Op primary-Exp

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Contextual Constraints Syntax rules alone are not enough to specify the format of well-formed programs. Example 1: let const m~2 in m + x Example 2: let const m~2 ; var n:Boolean in begin n := m<4; n := n+1 end Undefined! Scope Rules Type error! Type Rules

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Scope Rules Scope rules regulate visibility of identifiers. They relate every applied occurrence of an identifier to a binding occurrence Example 1 let const m~2; var r:Integer in r := 10*m Binding occurence Applied occurence Terminology: Static binding vs. dynamic binding Example 2: let const m~2 in m + x ?

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rules Type rules regulate the expected types of arguments and types of returned values for the operations of a language. Examples Terminology: Static typing vs. dynamic typing Type rule of < : E1 < E2 is type correct and of type Boolean if E1 and E2 are type correct and of type Integer Type rule of while : while E do C is type correct if E is of type Boolean and C is type correct

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semantics Specification of semantics is concerned with specifying the “meaning” of well-formed programs. Terminology: Expressions are evaluated and yield values (and may or may not perform side effects) Commands are executed and perform side effects. Declarations are elaborated to produce bindings Side effects: change the values of variables perform input/output

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semantics Example: The (informally specified) semantics of commands in mini Triangle. Commands are executed to update variables and/or perform input/output. The assignment command V := E is executed as follows: first the expression E is evaluated to yield a value v then v is assigned to the variable named V The sequential command C1;C2 is executed as follows: first the command C1 is executed then the command C2 is executed, Etc.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semantics Example: The semantics of expressions. An expression is evaluated to yield a value. An (integer literal expression) IL yields the integer value of IL The (variable or constant name) expression V yields the value of the variable or constant named V The (binary operation) expression E1 O E2 yields the value obtained by applying the binary operation O to the values yielded by (the evaluation of) expressions E1 and E2 etc.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semantics Example: The semantics of declarations. A declaration is elaborated to produce bindings. It may also have the side effect of allocating (memory for) variables. The constant declaration const I~E is elaborated by binding the identifier value I to the value yielded by E The constant declaration var I:T is elaborated by binding I to a newly allocated variable, whose initial value is undefined. The variable will be deallocated on exit from the let block containing the declaration. The sequential declaration D1;D2 is elaborated by elaborating D1 followed by D2 combining the bindings produced by both. D2 is elaborated in the environment of the sequential declaration overlaid by the bindings produced by D1