Download presentation
Presentation is loading. Please wait.
Published byGeorge McCormick Modified over 9 years ago
1
Fall 2013
2
Chart 2 Translators and Compilers Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000, Prentice Hall Syllabus o http://www.uncp.edu/home/lilliec http://www.uncp.edu/home/lilliec Homework & Project o First half of semester Problems o Second half of semester Modify Triangle compiler
3
Chart 3 Translators and Compilers o Language Processors o Compilation o Syntactic Analysis o Contextual Analysis o Run-Time Organization o Code Generation o Interpretation Major Programming Project – Project Definition and Planning – Implementation Weekly Status Reports – Project Presentation
4
Chart 4 Modify a compiler for the programming language Triangle o Appendix B: Informal Specification of the Programming Language Triangle o Appendix D: Class Diagrams for the Triangle Compiler Present Project Plan o What and How Weekly Status Reports o Work accomplished during the reporting period o Deliverable progress, as a percentage of completion o Problem areas o Planned activities for the next reporting period
5
Chapter 1 Introduction to Programming Languages
6
Chart 6 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 o Keep track of exact address of each data item and each instruction o Encode each instruction as a bit string Assembly Language: Symbolic names for operations, registers, and addresses.
7
Chart 7 High Level Languages: Notation similar to familiar mathematical notation o Expressions: +, -, *, / o Data Types: truth variables, characters, integers, records, arrays o Control Structures: if, case, while, for o Declarations: constant values, variables, procedures, functions, types o Abstraction: separates what is to be performed from how it is to be performed o Encapsulation (or data abstraction): group together related declarations and selectively hide some
8
Chart 8 Any system that manipulates programs expressed in some particular programming language o Editors: enter, modify, and save program text o 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 o Interpreters: Runs program without compilation Command languages Database query languages
9
Chart 9 Syntax o Form of the program o Defines symbols o How phrases are composed Contextual constraints o Scope: determine scope of each declaration o Type: ensures each operation is supplied with operands of the correct type Semantics o Meaning of the program – behavior when run on a machine
10
Chart 10 Syntax o 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::= | …. o Syntax Tree Used to define language in terms of strings and terminal symbols
11
Chart 11 Semantics o Abstract Syntax Concentrate on phrase structure alone o Abstract Syntax Tree
12
Chart 12 Scope o Binding Static: determined by language processor Dynamic: determined at run-time o Type Statically: language processor can detect all errors Dynamically: type errors cannot be detected until run-time Will assume static binding and statically typed
13
Chart 13 Concerned with meaning of program o Behavior when run Usually specified informally o Declarative sentences o Could include side effects o Correspond to production rules
14
Chart 14 Lexical Analyzer Parser & Semantic Analyzer Intermediate Code Generation Optimization Assembly Code Generation Symbol Table Source code Assembly code tokens parse tree intermediate representation
15
Chart 15 Program Command Single-Command Expression ::= 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
16
Chart 16 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
17
Chart 17 let var y :Integerin y := y + 1 Identifier Type-denoter V-name single-Declaration primary-Expression Declaration Expression Operator Integer-Literal Expression single-Command Program
18
Chart 18 Semantics o Abstract Syntax Concentrate on phrase structure alone o Abstract Syntax Tree
19
Chart 19 ! 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
20
Chart 20 beginconstdoelseendif inletthenvarwhile ;::=~() +-*/<>= \
21
Chart 21 Program (start symbol) Commandsingle-Command Expressionprimary-Expression V-name Declarationsingle-Declaration Type-denoter Operatoridentifier Integer-Literal
22
Chart 22 Program Command single-Command Expression Primary-Expression ::= 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 )
23
Chart 23 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
24
Chart 24 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 Label Program AssignCommand CallCommand SequentialCommand IfCommand WhileCommand LetCommand IntegerExpression VnameExpression UnaryExpression BinaryExpression
25
Chart 25 V-name Declaration Type-Denoter ::= Identifier ::= const Identifier ~ Expression | var Identifier : Type-denoter | Declaration ; Declaration ::= Identifier Label SimpleVname ConstDeclaration VarDeclaration SequentialDeclaration SimpleTypeDenoter
26
Chart 26 y Integer y y + 1 Identifier SimpleTypeDenoter SimpleVname VnameExpression IntegerExpression VarDeclaration Expression Operator Integer-Literal BinaryExpression AssignmentCommand LetCommand Program
27
Chart 27 Concerned with the meaning of the program o Their behavior when run Specifying semantics o Specify in general terms what will be the semantics of each class of phrase in the language o 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 o Specify the semantics of each specific form of command, expression, declaration, and so on One clause for each form of phrase
28
Chart 28 A command C is executed in order to update variables (this includes input and output) o 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. o 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. o The sequence command C 1 ; C 2 is executed as follows. First C 1 is executed; then C 2 is executed.
29
Chart 29 A command C is executed in order to update variables (this includes input and output) cont… o The if-command if E then C 1 else C 2 is executed as follows. The expression E is evaluated to yield a truth-value t; If t is true, C 1 is executed; if t is false, C 2 is executed. o 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.
30
Chart 30 A command C is executed in order to update variables (this includes input and output) cont… o 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.