1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections 3.1 - 3.3.1.

Slides:



Advertisements
Similar presentations
Programming Languages Third Edition Chapter 6 Syntax.
Advertisements

Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02B - Regular grammars Programming Language Design.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
PZ02B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02B - Regular grammars Programming Language Design.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Grammars CPSC 5135.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Organization of Programming Languages Meeting 3 January 15, 2016.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
1 Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
CS 326 Programming Languages, Concepts and Implementation
Chapter 3 – Describing Syntax
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
What does it mean? Notes from Robert Sebesta Programming Languages
Automata and Languages What do these have in common?
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Syntax versus Semantics
CSE 3302 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
High-Level Programming Language
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Discrete Maths 13. Grammars Objectives
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Programming Languages 2nd edition Tucker and Noonan
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
COMPILER CONSTRUCTION
PZ02B - Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ02B.
Presentation transcript:

1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections

2 Program structure  Syntax  What a program looks like  BNF (John Bacus and Peter Naur) – context free grammars) – a useful notation for describing syntax  FORTRAN 은 문법이 formal 하게 정의되지 않았다. –Blank 무시 ::: Do10I=110  Do10I=1,10

3 Program structure  Semantics  Execution behavior  Static semantics - Semantics determined at compile time: –var A: integer; Type and storage for A –int B[10]; Type and storage for array B –float MyProcC(float x;float y){...}; Function attributes  Dynamic semantics - Semantics determined during execution: –X = ``ABC'' SNOBOL4 example: X a string –X = 1 + 2; X an integer –:(X) X an address; Go to label X

4 Aspects of a program  Declarations - Information for compiler –var A: integer; –typedef struct { int A; float B } C;  Control - Changes to state of the machine –if (A<B) {... } –while (C>D) {... }  Structure often defined by a Backus Naur Form (BNF) grammar (First used in description of Algol in Peter Naur was chair of Algol committee, and John Backus was secretary of committee, who wrote report.)  We will see later - BNF turns out to be same as context free grammars developed by Noam Chomsky, a linguist)

5 Stages in translating a program

6 Major stages  Lexical analysis (Scanner): Breaking a program into primitive components, called tokens (identifiers, numbers, keywords,...) We will see that regular grammars and finite state automata are formal models of this.  Syntactic analysis (Parsing): Creating a syntax tree of the program. We will see that context free grammars and pushdown automata are formal models of this.  Symbol table: Storing information about declared objects (identifiers, procedure names,...)  Semantic analysis: Understanding the relationship among the tokens in the program.  Optimization: Rewriting the syntax tree to create a more efficient program.  Code generation: Converting the parsed program into an executable form.  We will briefly look at scanning and parsing. A full treatment of compiling is beyond scope of this course.

7 Translation environments

8 BNF grammars  Nonterminal: A finite set of symbols:  Terminal: A finite set of symbols: the, boy, girl, ran, ate, cake  Start symbol: One of the nonterminals:  Rules (productions): A finite set of replacement rules:  ::=  ::= ran | ate  ::= the  ::= boy | girl | cake  Replacement Operator: Replace any nonterminal by a right hand side value using any rule (written  )

9 Example BNF sentences   First rule   Second rule   the Fifth rule ...  the boy ate the cake  Also from you can derive   the cake ate the boy  Syntax does not imply correct semantics  Note:  Rule ::=  This BNF rule also written with equivalent syntax:  A  BC

10 Languages  Any string derived from the start symbol is a sentential form.  Sentence: String of terminals derived from start symbol by repeated application of replacement operator  A language generated by grammar G (written L(G)) is the set of all strings over the terminal alphabet (i.e., sentences) derived from start symbol.  That is, a language is the set of sentential forms containing only terminal symbols.

11 Derivations  A derivation is a sequence of sentential forms starting from start symbol.  Derivation trees:  Grammar: B  0B | 1B | 0 | 1  Derivation: B  0B  01B  010  From derivation get parse tree  But derivations may not be unique  S  SS | (S) | ()  S  SS  (S)S  (())S  (())()  S  SS  S()  (S)()  (())()  Different derivations but get the same parse tree

12 Ambiguity  But from some grammars you can get 2 different parse trees for the same string: ()()()  Each corresponds to a unique derivation:  S  SS  SSS  ()SS  ()()S  ()()()  A grammar is ambiguous if some sentence has 2 distinct parse trees.  We desire unambiguous grammars to understand semantics.

13 Role of  How to characterize strings of length 0? – Semantically it makes sense to consider such strings.  1. In BNF,  -productions: S  SS | (S) | () |   Can always delete them in grammar. For example:  X  abYc  Y    Delete  -production and add production without  :  X  abYc  X  abc  2. In fsa - moves means that  in initial state, without input  you can move to final state.

14 Syntax can be used to determine some semantics  During Algol era, thought that BNF could be used for semantics of a program:  What is the value of: 2 * * 5?  (a) 26  (b) 70  (c) 50  All are reasonable answers? Why?

15 Usual grammar for expressions  E  E + T | T  T  T * P | P  P  i | ( E )  “Natural” value of expression  is 26  Multiply 2 * 3 = 6  Multiply 4 * 5 = 20  Add = 26

16 But the “ precedence ” of operations is only a convention  Grammar for 70  E  E * T | T  T  T + P | P  P  i | ( E )  Grammar for 50  E  E + T | E * T | T  T  i | ( E ) Draw parse tree of expression 2*3+4*5 for each grammar All 3 grammars generate exactly the same language, but each has a different semantics (i.e., expression value) for most expressions.

17 Classes of grammars  BNF: Backus-Naur Form - Context free - Type 2 - Already described  Regular grammars: subclass of BNF - Type 3:  BNF rules are restricted: A  t N | t  where: N = nonterminal, t = terminal  Examples:  Binary numbers: B  0 B | 1 B | 0 | 1  Identifiers:  I  a L | b L | c L |...| z L | a |...| y | z  L  1 L | 2 L |...| 9 L | 0 L | 1 |...| 9 | 0 | a L | b L | c L |...| z L | a |...| y | z ab7d

18 Other classes of grammars  The context free and regular grammars are important for programming language design. We study these in detail.  Other classes have theoretical importance, but not in this course:  Context sensitive grammar: Type 1 - Rules:    where |  |  |  | [That is, length of   length of , i.e., all sentential forms are length non- decreasing]  Unrestricted, recursively enumerable: Type 0 -  Rules:   . No restrictions on  and .

19 The Big Picture Chomsky Hierarchy of Language Grammars (1956)