Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

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

1 Languages and Compilers (SProg og Oversættere) Code Generation.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
1 Languages and Compilers (SProg og Oversættere) Lecture 2 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
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.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter3: Language Translation issues
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Introduction and Syntax. Course objectives Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
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.
Language processors (Chapter 2) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
PART I: overview material
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.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
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.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Review (Chapter 9) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
CONCEPTS OF PROGRAMMING LANGUAGES
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.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Chapter 1 Introduction.
Describing Syntax and Semantics
CS 326 Programming Languages, Concepts and Implementation
System Software Unit-1 (Language Processors) A TOY Compiler
CSCI-235 Micro-Computer Applications
Chapter 3 – Describing Syntax
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Programming Languages 2nd edition Tucker and Noonan
Semantic Analysis Chapter 6.
Course Overview PART I: overview material PART II: inside a compiler
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside the compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Introduction (Chapter 1) 2 Chapter 1: Introduction GOAL this lecture: What is this course about... a high-level perspective. OVERVIEW –Levels of programming languages –Language processors –Specification of a programming language

Introduction (Chapter 1) 3 Levels of Programming Languages High-level program class Triangle {... float area() { return b*h/2; } class Triangle {... float area() { return b*h/2; } Low-level program 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

Introduction (Chapter 1) 4 Levels of Programming Languages Some high-level languages: –C, C++, Java, Pascal, Ada, Fortran, Cobol, Scheme, Prolog, Smalltalk,... Some low-level languages: –x86 assembly language, PowerPC assembly language, SPARC assembly language, MIPS assembly language, ARM assembly language,...

Introduction (Chapter 1) 5 Levels of Programming Languages What makes a high-level language different from a low- level language? Things found in HL languages but typically not in LL languages - Expressions - control structures/abstractions: while, repeat-until, if-then-else procedures - data types - distinguish several different types of data - composite data types - user defined data types - encapsulation modules, procedures, objects

Introduction (Chapter 1) 6 Abstraction A high-level language is more abstract than a low-level language. More abstract? What does that mean? Abstraction: Separate the ‘how’ from the ‘what’. Or what is implemented from how is it implemented. e.g. procedural abstraction = separate ‘what does it do’ from ‘how does it do it’ HL languages abstract away from the underlying machine => much more portable

Introduction (Chapter 1) 7 Levels of Programming Languages Q: How do the following make a HL language more abstract? - Expressions - control structures: while, repeat-until, if-then-else procedures - data types - encapsulation modules, procedures, objects

Introduction (Chapter 1) 8 Language Processors: What are they? A programming language processor is any system (software or hardware) that manipulates programs. Examples: –Editors –Translators (e.g. compiler, assembler, disassembler) –Interpreters

Introduction (Chapter 1) 9 Language Processors: Why do we need them? Hardware Programmer X86 Processor JVM Binary code JVM Assembly code Java Program JVM Interpreter Concepts and Ideas Hardware Programmer How to bridge the “semantic gap” ? Compute surface area of a triangle?

Introduction (Chapter 1) 10 Programming Language Specification Why? –A communication device between people who need to have a common understanding of the PL: language designer, language implementer, user What to specify? –Specify what is a ‘well formed’ program syntax contextual constraints (also called static semantics): –scoping rules –type rules –Specify what is the meaning of (well formed) programs semantics (also called runtime semantics)

Introduction (Chapter 1) 11 Programming Language Specification Why? What to specify? How to specify ? –Formal specification: use some kind of precisely defined formalism –Informal specification: description in English. –Usually a mix of both (e.g. Java specification) Syntax => formal specification using CFG/BNF Contextual constraints and semantics => informal

Introduction (Chapter 1) 12 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 “Bachus Naur 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 as left-hand side.

Introduction (Chapter 1) 13 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?

Introduction (Chapter 1) 14 Example: 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: Integer in begin n := 2 * m * m ; putint(n) end !This is a comment. let const m ~ 7; var n: Integer in begin n := 2 * m * m ; putint(n) end Declarations Command Expression

Introduction (Chapter 1) 15 Example: 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...

Introduction (Chapter 1) 16 Example: 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 ::= + | - | * | / | | =

Introduction (Chapter 1) 17 Example: Syntax of “Mini-Triangle” (continued) 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 | Empty Graphic ::= any printable character or space Comment ::= ! CommentLine eol CommentLine ::= Graphic CommentLine | Empty Graphic ::= any printable character or space

Introduction (Chapter 1) 18 Syntax Trees A syntax tree or parse 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.

Introduction (Chapter 1) 19 Syntax Trees Example: Expression V-name primary-Exp. Expression Ident d + primary-Exp OpInt-Lit 10* Op V-name primary-Exp. Ident n Expression := Expression Op primary-Exp

Introduction (Chapter 1) 20 Concrete and Abstract Syntax The previous grammar specified the concrete syntax of Mini-Triangle. The concrete syntax is important for the programmer who needs to know exactly how to write syntactically well- formed programs. The abstract syntax omits irrelevant syntactic details and only specifies the essential structure of programs. Example: different concrete syntaxes for an assignment v := e (set! v e) e -> v v = e

Introduction (Chapter 1) 21 Example: Concrete/Abstract Syntax of Commands 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 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 Concrete Syntax

Introduction (Chapter 1) 22 Example: Concrete/Abstract Syntax of Commands Command ::= V-name := Expression AssignCmd | Identifier ( Expression ) CallCmd | if Expression then Command else Command IfCmd | while Expression do Command WhileCmd | let Declaration in Command LetCmd | Command ; Command SequentialCmd Command ::= V-name := Expression AssignCmd | Identifier ( Expression ) CallCmd | if Expression then Command else Command IfCmd | while Expression do Command WhileCmd | let Declaration in Command LetCmd | Command ; Command SequentialCmd Abstract Syntax

Introduction (Chapter 1) 23 Example: Concrete Syntax of Expressions Expression ::= primary-Expression | Expression Operator primary-Expression primary-Expression ::= Integer-Literal | V-name | Operator primary-Expression | ( Expression ) V-name ::= Identifier Expression ::= primary-Expression | Expression Operator primary-Expression primary-Expression ::= Integer-Literal | V-name | Operator primary-Expression | ( Expression ) V-name ::= Identifier

Introduction (Chapter 1) 24 Example: Abstract Syntax of Expressions Expression ::= Integer-Literal IntegerExp | V-name VNameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp V-name ::= Identifier SimpleVName Expression ::= Integer-Literal IntegerExp | V-name VNameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp V-name ::= Identifier SimpleVName

Introduction (Chapter 1) 25 Abstract Syntax Trees Abstract Syntax Tree for: d:=d+10*n BinaryExpression VNameExp BinaryExpression Ident d + Op Int-Lit 10 * Op SimpleVName IntegerExpVNameExp Ident n SimpleVName AssignmentCmd d Ident VName SimpleVName

Introduction (Chapter 1) 26 Contextual Constraints Syntax rules alone are not enough to specify the format of well-formed programs. Example 1: let const m~2 in putint(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

Introduction (Chapter 1) 27 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 occurrence Applied occurrence Terminology: Static binding vs. dynamic binding Example 2 let const m~2 in putint (m + x) ?

Introduction (Chapter 1) 28 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 has type Boolean if E1 and E2 are both type - correct and have type Integer Type rule of while : while E do C is type - correct if E is type - correct and has type Boolean and C is type - correct

Introduction (Chapter 1) 29 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

Introduction (Chapter 1) 30 Semantics Example: The (informally specified) semantics of commands in Mini-Triangle. Commands are executed to update variables and/or to perform input/output. The assignment command V := E is executed as follows: first the expression E is evaluated to yield a value x then x 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.

Introduction (Chapter 1) 31 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.

Introduction (Chapter 1) 32 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 variable declaration var I:T is elaborated by binding I to a newly allocated variable, whose initial value is undefined. The variable will be de-allocated upon exit from the let-command that contains the declaration. The sequential declaration D1;D2 is elaborated by elaborating D1 followed by D2 and combining the bindings produced by both. D2 is elaborated in the environment of the sequential declaration overlaid by the bindings produced by D1.

Introduction (Chapter 1) 33 Conclusion / Summary This course is about compilers Compilers are language processors –translate high-level language into low-level language –help bridge the semantic gap Language specification –needed for communication between language designers, implementers, and users –Three “parts” we will study during this course Syntax of the language: usually formal: Extended BNF Contextual constraints: usually informal: scope rules and type rules (written in English) Semantics: usually informal: descriptions in English