Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."— Presentation transcript:

1 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

2 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

3 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 0001001001000101 0010010011101100 10101101001...

4 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,...

5 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

6 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

7 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

8 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

9 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? 0101001001...

10 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)

11 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

12 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.

13 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?

14 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

15 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...

16 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 ::= + | - | * | / | | =

17 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

18 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.

19 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 123 1 2 3

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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) ?

28 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

29 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

30 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.

31 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.

32 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.

33 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


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

Similar presentations


Ads by Google