csc 415 translators and compilers spring 2009
Download
Skip this Video
Download Presentation
CSC 415: Translators and Compilers Spring 2009

Loading in 2 Seconds...

play fullscreen
1 / 30

CSC 415: Translators and Compilers Spring 2009 - PowerPoint PPT Presentation


  • 392 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CSC 415: Translators and Compilers Spring 2009' - Pat_Xavi


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
course overview
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
course outline
Course Outline
  • Major Programming Project
    • Project Definition and Planning
    • Implementation
      • Weekly Status Reports
    • Project Presentation
  • Translators and Compilers
    • Language Processors
    • Compilation
    • Syntactic Analysis
    • Contextual Analysis
    • Run-Time Organization
    • Code Generation
    • Interpretation
project
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
csc 415 translators and compilers spring 20095

CSC 415: Translators and CompilersSpring 2009

Chapter 1

Introduction to Programming Languages

chapter 1 introduction to programming languages
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.
programming languages
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
programming languages8
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 languages
      • Database query languages
programming languages specifications
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
representation
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::=a|b|….
    • Syntax Tree
      • Used to define language in terms of strings and terminal symbols
representation11
Representation
  • Semantics
    • Abstract Syntax
      • Concentrate on phrase structure alone
    • Abstract Syntax Tree
contextual constraints
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

semantics
Semantics
  • Concerned with meaning of program
    • Behavior when run
  • Usually specified informally
    • Declarative sentences
    • Could include side effects
    • Correspond to production rules
structure of a compiler
Structure of a Compiler

Lexical Analyzer

Source code

Symbol

Table

tokens

Parser & Semantic Analyzer

parse tree

Intermediate Code Generation

intermediate representation

Optimization

intermediate representation

Assembly Code Generation

Assembly code

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

Mini-Triangle Syntax
mini triangle syntax16
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

Mini-Triangle Syntax
syntax tree let var y integer in y y 1
Syntax Tree – let var y: Integer in y := y + 1

Program

single-Command

single-Command

Expression

Declaration

Expression

primary-Expression

primary-Expression

single-Declaration

Type-denoter

V-name

V-name

Integer-Literal

Identifier

Operator

Identifier

Identifier

Identifier

y

var

y

y

:

Integer

in

let

1

:=

+

representation18
Representation
  • Semantics
    • Abstract Syntax
      • Concentrate on phrase structure alone
    • Abstract Syntax Tree
mini triangle program
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

mini triangle terminal symbols
Mini-Triangle Terminal Symbols

begin const do else end if

in let then var while

; : := ~ ( )

+ - * / < > =

\

mini triangle non terminals
Mini-Triangle Non-Terminals

Program (start symbol)

Command single-Command

Expression primary-Expression

V-name

Declaration single-Declaration

Type-denoter

Operator identifier

Integer-Literal

mini triangle production rules
Program

Command

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 )

Mini-Triangle Production Rules
mini triangle production rules23
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

Mini-Triangle Production Rules
mini triangle abstract syntax
Program

Command

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

Mini-Triangle Abstract Syntax

Label

Program

AssignCommand

CallCommand

SequentialCommand

IfCommand

WhileCommand

LetCommand

IntegerExpression

VnameExpression

UnaryExpression

BinaryExpression

mini triangle abstract syntax25
V-name

Declaration

Type-Denoter

::= Identifier

::= const Identifier ~ Expression

| var Identifier : Type-denoter

| Declaration ; Declaration

::= Identifier

Mini-Triangle Abstract Syntax

Label

SimpleVname

ConstDeclaration

VarDeclaration

SequentialDeclaration

SimpleTypeDenoter

abstract syntax tree let var y integer in y y 1
Abstract Syntax Tree – let var y: Integer in y := y + 1

Program

LetCommand

AssignmentCommand

BinaryExpression

VarDeclaration

Expression

IntegerExpression

VnameExpression

SimpleTypeDenoter

SimpleVname

SimpleVname

Integer-Literal

Identifier

Operator

Identifier

Identifier

Identifier

y

y

y

Integer

1

+

semantics27
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
mini triangle semantics
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.
mini triangle semantics cont
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.
mini triangle semantics cont30
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.
ad