D Goforth COSC 31271 Translating High Level Languages.

Slides:



Advertisements
Similar presentations
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.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
Chapter 4 Lexical and Syntax Analysis Sections
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
A basis for computer theory and A means of specifying languages
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.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Lexical and Syntax Analysis
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
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
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.
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.
Lexical and Syntax Analysis
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
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.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
1 CS Programming Languages Class 04 September 5, 2000.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 – Describing Syntax
Lexical and Syntax Analysis
Chapter 3 – Describing Syntax
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax versus Semantics
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

D Goforth COSC Stages of translation  Lexical analysis  Syntactic analysis  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 Examples: tokens and lexemes  Some token categories contain only one lexeme: semi-colon ;  Some tokens categorize many lexemes: identifier count, maxCost,…

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 syntactic analyzer in translation 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)

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.113) = A | B | C // lexical + | * | ( ) | Assumes other rules for,

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 )

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

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  Putting features of languages into grammars:  expression any length  precedence - an extra non-terminal  associativity - order in recursive rules  nested if statements - “dangling else” problem: p. 119

D Goforth COSC Forms for grammars  Backus-Naur form (BNF)  Extended Backus-Naur fomr (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  e.g.: rules for using data types