30
CSC 415: Translators and Compilers Spring 2009 Dr. Chuck Lillie

CSC 415: Translators and Compilers Spring 2009

  • Upload
    sal

  • View
    38

  • Download
    1

Embed Size (px)

DESCRIPTION

CSC 415: Translators and Compilers Spring 2009. Dr. Chuck Lillie. Course Overview. Translators and Compilers Textbook Programming Language Processors in Java , Authors: David A. Watts & Deryck F. Brown, 2000, Prentice Hall Syllabus http://www.uncp.edu/home/lilliec Homework & Project - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 415:  Translators and Compilers Spring 2009

CSC 415: Translators and CompilersSpring 2009

Dr. Chuck Lillie

Page 2: CSC 415:  Translators and Compilers Spring 2009

Chart 2

Course Overview

Translators and Compilers Textbook

– Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000, Prentice Hall

Syllabus– http://www.uncp.edu/home/lilliec

Homework & Project– First half of semester

Problems

– Second half of semester Modify Triangle compiler

Page 3: CSC 415:  Translators and Compilers Spring 2009

Chart 3

Course Outline

Translators and Compilers– Language Processors– Compilation– Syntactic Analysis– Contextual Analysis– Run-Time Organization– Code Generation– Interpretation

Major Programming Project– Project Definition and

Planning– Implementation

Weekly Status Reports

– Project Presentation

Page 4: CSC 415:  Translators and Compilers Spring 2009

Chart 4

Project

Modify a compiler for the programming language Triangle– Appendix B: Informal Specification of the Programming

Language Triangle– Appendix D: Class Diagrams for the Triangle Compiler

Present Project Plan– What and How

Weekly Status Reports– Work accomplished during the reporting period– Deliverable progress, as a percentage of completion– Problem areas– Planned activities for the next reporting period

Page 5: CSC 415:  Translators and Compilers Spring 2009

CSC 415: Translators and CompilersSpring 2009

Chapter 1Introduction to Programming Languages

Page 6: CSC 415:  Translators and Compilers Spring 2009

Chart 6

Chapter 1: Introduction to Programming Languages

Programming Language: A formal notation for expressing algorithms.

Programming Language Processors: Tools to enter, edit, translate, and interpret programs on machines.

Machine Code: Basic machine instructions– Keep track of exact address of each data item and

each instruction– Encode each instruction as a bit string

Assembly Language: Symbolic names for operations, registers, and addresses.

Page 7: CSC 415:  Translators and Compilers Spring 2009

Chart 7

Programming Languages

High Level Languages: Notation similar to familiar mathematical notation– Expressions: +, -, *, /– Data Types: truth variables, characters, integers,

records, arrays– Control Structures: if, case, while, for– Declarations: constant values, variables, procedures,

functions, types– Abstraction: separates what is to be performed from

how it is to be performed– Encapsulation (or data abstraction): group together

related declarations and selectively hide some

Page 8: CSC 415:  Translators and Compilers Spring 2009

Chart 8

Programming Languages

Any system that manipulates programs expressed in some particular programming language– Editors: enter, modify, and save program text– Translators and Compilers: Translates text from

one language to another. Compiler translates a program from a high-level language to a low-level language, preparing it to be run on a machine

Checks program for syntactic and contextual errors– Interpreters: Runs program without compilation

Command languagesDatabase query languages

Page 9: CSC 415:  Translators and Compilers Spring 2009

Chart 9

Programming Languages Specifications

Syntax– Form of the program– Defines symbols– How phrases are composed

Contextual constraints– Scope: determine scope of each declaration– Type: ensures each operation is supplied with

operands of the correct type Semantics

– Meaning of the program – behavior when run on a machine

Page 10: CSC 415:  Translators and Compilers Spring 2009

Chart 10

Representation

Syntax– Backus-Naur Form (BNF): context-free grammar

Terminal symbols (>=, while, ;) Non-terminal symbols (Program, Command, Expression,

Declaration) Start symbol (Program) Production rules (defines how phrases are composed from

terminals and sub-phrases)– N::=|….

– Syntax Tree Used to define language in terms of strings and terminal

symbols

Page 11: CSC 415:  Translators and Compilers Spring 2009

Chart 11

Representation

Semantics– Abstract Syntax

Concentrate on phrase structure alone

– Abstract Syntax Tree

Page 12: CSC 415:  Translators and Compilers Spring 2009

Chart 12

Contextual Constraints

Scope– Binding

Static: determined by language processor Dynamic: determined at run-time

– Type Statically: language processor can detect all errors Dynamically: type errors cannot be detected until run-time

Will assume static binding and statically typed

Page 13: CSC 415:  Translators and Compilers Spring 2009

Chart 13

Semantics

Concerned with meaning of program– Behavior when run

Usually specified informally– Declarative sentences– Could include side effects– Correspond to production rules

Page 14: CSC 415:  Translators and Compilers Spring 2009

Chart 14

Structure of a Compiler

Lexical Analyzer

Parser & Semantic Analyzer

Intermediate Code Generation

Optimization

Assembly Code Generation

Symbol Table

Source code

Assembly code

tokens

parse tree

intermediate representation

intermediate representation

Page 15: CSC 415:  Translators and Compilers Spring 2009

Chart 15

Mini-Triangle Syntax

Program

Command

Single-Command

Expression

::= single-Command

::= single-Command

| 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

::= primary-Expression

| Expression Operator primary-Expression

Page 16: CSC 415:  Translators and Compilers Spring 2009

Chart 16

Mini-Triangle Syntax

Primary-Expression

V-name

Declaration

Single-Declaration

Type-Denoter

Operator

Identifier

Integer-Literal

Comment

Digit

Letter

::= Integer-Literal| V-name| Operator primary-Expression| ( Expression )::= Identifier::= single-Declaration| Declaration ; single-Declaration::= const Identifier ~ Expression| var Identifier : Type-denoter::= Identifier::= + | - | * | / | < | > | = | \::= Letter | Identifier Letter | Identifier Digit::= Digit | Integer-Literal Digit::= ! Graphic* eol::= 0|1|2|3|4|5|6|7|8|9::= a|b|c|d|…|z|A|B|C|…|Z

Page 17: CSC 415:  Translators and Compilers Spring 2009

Chart 17

Syntax Tree – let var y: Integer in y := y + 1

let var y : Integer in y := y + 1

Identifier Identifier Identifier Identifier

Type-denoter V-name V-name

single-Declaration primary-Expression primary-Expression

Declaration Expression

Operator Integer-Literal

Expression

single-Command

single-Command

Program

Page 18: CSC 415:  Translators and Compilers Spring 2009

Chart 18

Representation

Semantics– Abstract Syntax

Concentrate on phrase structure alone

– Abstract Syntax Tree

Page 19: CSC 415:  Translators and Compilers Spring 2009

Chart 19

Mini-Triangle Program

! This is a comment. It continues to the end-of-line

let

const m ~ 7;

var n: Integer

in

begin

n := 2 * m * m;

putint (n)

end

Page 20: CSC 415:  Translators and Compilers Spring 2009

Chart 20

Mini-Triangle Terminal Symbols

begin const do else end if

in let then var while

; : := ~ ( )

+ - * / < > =

\

Page 21: CSC 415:  Translators and Compilers Spring 2009

Chart 21

Mini-Triangle Non-Terminals

Program (start symbol)

Command single-Command

Expression primary-Expression

V-name

Declaration single-Declaration

Type-denoter

Operator identifier

Integer-Literal

Page 22: CSC 415:  Translators and Compilers Spring 2009

Chart 22

Mini-Triangle Production Rules

ProgramCommand

single-Command

Expression

Primary-Expression

::= single-Command::= single-Command| 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:= primary-Expression| Expression Operator primary-Expression::= Integer-Literal| V-name| Operator primary-Expression| ( Expression )

Page 23: CSC 415:  Translators and Compilers Spring 2009

Chart 23

Mini-Triangle Production Rules

V-name

Declaration

single-Declaration

Type-denoter

Operator

Identifier

Integer-Literal

Comment

::= Identifier

::= single-Declaration

| Declaration ; single-Declaratiion

::= const Identifier ~ Expression

| var Identifier : Type-denoter

::= Identifier

::= + | - | * | / | < | > | = | \

::= Letter | Identifier Letter | Identifier Digit

::= Digit |Integer-Literal Digit

::= ! Graphic* eol

Page 24: CSC 415:  Translators and Compilers Spring 2009

Chart 24

Mini-Triangle Abstract Syntax

ProgramCommand

Expression

::= Command::= V-name := Expression| Identifier ( Expression )| Command ; Command| if Expression then Command else Command| while Expression do

Command| let Declaration in Command::= Integer-Literal| V-name| Operator Expression| Expression Operator

Expression

Label

Program

AssignCommand

CallCommand

SequentialCommand

IfCommand

WhileCommand

LetCommand

IntegerExpression

VnameExpression

UnaryExpression

BinaryExpression

Page 25: CSC 415:  Translators and Compilers Spring 2009

Chart 25

Mini-Triangle Abstract Syntax

V-name

Declaration

Type-Denoter

::= Identifier

::= const Identifier ~ Expression

| var Identifier : Type-denoter

| Declaration ; Declaration

::= Identifier

Label

SimpleVname

ConstDeclaration

VarDeclaration

SequentialDeclaration

SimpleTypeDenoter

Page 26: CSC 415:  Translators and Compilers Spring 2009

Chart 26

Abstract Syntax Tree – let var y: Integer in y := y + 1

y Integer y y + 1

Identifier Identifier Identifier Identifier

SimpleTypeDenoter SimpleVname SimpleVname

VnameExpression IntegerExpression

VarDeclaration Expression

Operator Integer-Literal

BinaryExpression

AssignmentCommand

LetCommand

Program

Page 27: CSC 415:  Translators and Compilers Spring 2009

Chart 27

Semantics

Concerned with the meaning of the program– Their behavior when run

Specifying semantics– Specify in general terms what will be the semantics of each class

of phrase in the language– Semantics of commands, expressions, and declarations

A command is executed to update variables– May also have side effect of performing input-output

An expression is evaluated to yield a value– May also have side effect of updating variables

A declaration is elaborated to produce bindings– May also have the side effect of allocating and initializing variables

– Specify the semantics of each specific form of command, expression, declaration, and so on

One clause for each form of phrase

Page 28: CSC 415:  Translators and Compilers Spring 2009

Chart 28

Mini-Triangle Semantics

A command C is executed in order to update variables (this includes input and output)– The assignment statement V := E is executed as

follows. The expression E is evaluated to yield a value v; then v is assigned to the value-or-variable-name V.

– The call-command I (E) is executed as follows. The expression E is evaluated to yield a value v; then the procedure bound to I is called with v as its argument.

– The sequence command C1 ; C2 is executed as follows. First C1 is executed; then C2 is executed.

Page 29: CSC 415:  Translators and Compilers Spring 2009

Chart 29

Mini-Triangle Semantics (cont)

A command C is executed in order to update variables (this includes input and output) cont…– The if-command if E then C1 else C2 is executed

as follows. The expression E is evaluated to yield a truth-value t; If t is true, C1 is executed; if t is false, C2 is executed.

– The while-command while E do C is executed as follows. The expression E is evaluated to yield a truth-value t; if t is true, C is executed, and then the while-command is executed again; if t is false, execution of the while-command is completed.

Page 30: CSC 415:  Translators and Compilers Spring 2009

Chart 30

Mini-Triangle Semantics (cont)

A command C is executed in order to update variables (this includes input and output) cont…– The let-command let D in C is executed as follows.

The declaration D is elaborated to produce bindings b; C is executed, in the environment of the let-command overlaid by the bindings b. The bindings b have no effect outside the let-command.