D Goforth COSC 31271 Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Describing Syntax and Semantics
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Software II: Principles of Programming Languages
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
ISBN Chapter 3 Describing Syntax and Semantics.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
Chapter 3 Part I Describing Syntax and Semantics.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Describing Syntax and Semantics
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Syntax and Grammars.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
CS 363 Comparative Programming Languages
R.Rajkumar Asst.Professor CSE
Chapter 3 Describing Syntax and Semantics.
Presentation transcript:

D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126

D Goforth COSC Stages of translation  Lexical analysis - the lexer or scanner  Syntactic analysis - the parser  Code generation  Linking Before  Execution

D Goforth COSC Lexical analysis  Translate stream of characters into lexemes  Lexemes belong to categories called tokens  Token identity of lexemes is used at the next stage of syntactic analysis

D Goforth COSC From characters to lexemes yVal = x – min ( 100, 4xVal ));

D Goforth COSC Examples: tokens and lexemes  Some token categories contain only one lexeme: semi-colon ;  Some tokens categorize many lexemes: identifier count, maxCost,… based on a rule for legal identifier strings

D Goforth COSC Tokens and Lexemes yVal = x – min ( 100, 4xVal )); Lexical analysis identifies lexemes and their token type recognizes illegal lexemes (4xVal) does NOT identify syntax error: ) ) identifier illegal lexeme left_paren equal_sign

D Goforth COSC Syntax or Grammar of Language rules for  generating (used by programmer) or  Recognizing (used by parser) a valid sequence of lexemes

D Goforth COSC Grammars  4 categories of grammars (Chomsky)  Two categories are important in computing:  Regular expressions (pattern matching)  Context-free grammars (programming languages)

D Goforth COSC Context-free grammar  Meta-language for describing languages  States rules or productions for what lexeme sequences are correct in the language  Written in Backus-Naur Form (BNF) or EBNF Syntax graphs

D Goforth COSC Example of BNF rule PROBLEM: how to recognize all these as correct? y = x f = rVec.length + 1 button[4].label = “Exit” RULE for defining assignment statement: = Assumes other rules for,

D Goforth COSC BNF rules Non-terminal and terminal symbols:  Non-terminals are defined by at least one rule =  Terminals are tokens (or lexemes)

D Goforth COSC Simple sample grammar(p.123) = A | B | C // lexical + | * | ( ) | terminals terminals

D Goforth COSC Simple sample production = <- apply one rule at each step B = to leftmost non-terminal B = * B = A * B = A * ( ) B = A * ( + ) B = A * ( C + ) B = A * ( C + C ) = A | B | C + | * | ( ) | = A | B | C + | * | ( ) |

D Goforth COSC Sample parse tree = + * B A ( ) C C Leaves represent the sentence of lexemes Rule application = A | B | C + | * | ( ) | = A | B | C + | * | ( ) |

D Goforth COSC extended sample grammar | if ( ) then | if ( ) then else | = | == | ~= How to add compound condition?

D Goforth COSC Ambiguous grammar  Different parse trees for same sentence  Different translations for same sentence  Different machine code for same source code!

D Goforth COSC Grammars for ‘human’ conventions without ambiguity  Putting features of languages into grammars:  expression any length: lists, p. 121  precedence - an extra non-terminal: p. 125  associativity - order in recursive rules: p. 128  nested if statements - “dangling else” problem: p. 130

D Goforth COSC Forms for grammars  Backus-Naur form (BNF)  Extended Backus-Naur form (EBNF) -shortens set of rules  Syntax graphs -easier to read for learning language

D Goforth COSC EBNF  optional zero or one occurrence [..] -> [ + ]  optional zero or more occurrences {..} -> { + }  ‘or’ choice of alternative symbols | -> [ (*|/) ]

Syntax Graph - basic structures expr term factor * / expr term + - factor * / term

BNF (p. 121)EBNF Syntax Graph -> + | - | -> * | / | -> [ (+|-)] -> [ (*|/)] -> {(+|-) } -> {(*|/) } expr term + - factor * /

D Goforth COSC Attribute grammars  Problem: context-free grammars cannot describe some features needed in programming - “static semantics” e.g.: rules for using data types *Can’t assign real to integer (clumsy in BNF) *Can’t access variable before assigning (impossible in BNF)

D Goforth COSC Attributes  Symbols in the grammar can have attributes (properties)  Productions can have functions of some of the attributes of their symbols that compute the attributes of other symbols  Predicates (boolean functions) inspect the attributes of non- terminals to see if they are legitimate

D Goforth COSC Using attributes 1)Apply productions to create parse tree (symbols have some intrinsic attributes) 2)Apply functions to determine remaining attributes 3)Apply predicates to test correctness of parse tree

D Goforth COSC Sebesta’s example = + | A | B | C Add attributes for type checking Expected_type Actual_type

D Goforth COSC Sebesta’s example = + | A | B | C expected_type actual_type expected_type actual_type expected_type actual_type expected_type actual_type

D Goforth COSC Sebesta’s example = + | A | B | C actual_type Determined from string (A,B,C) Which has been declared actual_type Determined from string (A,B,C) Which has been declared

D Goforth COSC Sebesta’s example = + | A | B | C actual_type Determined from Actual types actual_type Determined from Actual types

D Goforth COSC Sebesta’s example = + | A | B | C expected type Determined from Actual types expected type Determined from Actual types

D Goforth COSC Sebesta’s type rules p.138

D Goforth COSC Sebesta’s example

D Goforth COSC Sebesta’s example

D Goforth COSC Axiomatic semantics  Assertions about statements Preconditions Postconditions  like JUnit testing  Purpose Define meaning of statement Test for validity of computation (does it do what it is supposed to do?)

D Goforth COSC Example for assignment  What the statement should do is expressed as a postcondition  Based on the syntax of the assignment, a precondition is inferred  When statement is executed, conditions can be verified before and after

D Goforth COSC Example assignment statement y = 25 + x * 2 postcondition: y>40 y>40 25+x*2>40 x*2>15 x>7.5precondition