1 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical.

Slides:



Advertisements
Similar presentations
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
Advertisements

Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Compilers and Language Translation
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
 2005 Pearson Education, Inc. All rights reserved Introduction.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Chapter3: Language Translation issues
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
Louden, Programming Languages
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Computer Science 101 Introduction to Programming.
Introducing Java.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Chapter 1 - Introduction
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
ProgrammingLanguages Programming Languages Language Definition, Translation and Design.
CPS 506 Comparative Programming Languages Syntax Specification.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Programming Errors. Errors of different types Syntax errors – easiest to fix, found by compiler or interpreter Semantic errors – logic errors, found by.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
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.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
Introduction to Compiler Construction
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Compiler Construction (CS-636)
Representation, Syntax, Paradigms, Types
Revision Lecture
-by Nisarg Vasavada (Compiled*)
CSCE Fall 2013 Prof. Jennifer L. Welch.
COP4020 Programming Languages
Representation, Syntax, Paradigms, Types
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
CMP 131 Introduction to Computer Programming
Representation, Syntax, Paradigms, Types
CSCE Fall 2012 Prof. Jennifer L. Welch.
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Representation, Syntax, Paradigms, Types
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Presentation transcript:

1 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical structure - the structure of the words or tokens - uses regular expressions.) Semantics: the actual result of execution. Usually described in English, but can be specified mathematically.

2 Language translation Compiler: two-step process that 1. translates source code into executable code(typically in machine code) and then goes away.; 2. the system executes the executable code. compiler run sourceexecutable inputs outputs

3 Language translation Interpreter: one-step process in which the source code is executed directly. Hybrids are also possible (Java). interpreter source inputs outputs Java compiler sourcebytecode inputs outputs machine dependent interpreter Bytecode is composed of instructions that have been brought to the lowest level possible without making them machine dependent.

Analogy Compiling - Cooking with a precise recipe. You know all ingredients, utensils, amounts, oven temperatures, etc. Adapted for a specific kitchen and set of ingredients. Interpretation – Cooking without knowing ahead of time exactly what ingredients you have, what pans you will need, what cooking utensils/temperatures will be appropriate. Hybrid – The basic instructions and kitchen specifications are known, but there is room for last minute changes. 4

Parsing In compiling/interpretting, part of the work comes from parsing. NP: noun phrase VP: verb phrase N: noun A: article V: verb PP: Prepositional Phrase 5

What is Parsing in English? S  NP VP NP  A N NP  NP PP VP  V NP VP  VP PP PP  P NP NP  Papa N  caviar N  spoon V  spoon V  ate P  with A  the A  a S Papa NP VP V NP A N the caviar NP A N a spoon ate PP P with 6

Programming languages printf ("/charset [%s", (re_opcode_t) *(p - 1) == charset_not ? "^" : ""); assert (p + *p < pend); for (c = 0; c < 256; c++) if (c / 8 < *p && (p[1 + (c/8)] & (1 << (c % 8)))) { /* Are we starting a range? */ if (last + 1 == c && ! inrange) { putchar ('-'); inrange = 1; } /* Have we broken a range? */ else if (last + 1 != c && inrange) { putchar (last); inrange = 0; } if (! inrange) putchar (c); last = c; }  Easy to parse.  Designed that way! 7

Natural languages  No {} () [] to indicate scope & precedence  Lots of overloading (arity varies)  Grammar isn’t known in advance!  Context-free grammar not best formalism printf "/charset %s", re_opcode_t *p - 1 == charset_not ? "^" : ""; assert p + *p < pend; for c = 0; c < 256; c++ if c / 8 < *p && p1 + c/8 & 1 << c % 8 Are we starting a range? if last + 1 == c && ! inrange putchar '-'; inrange = 1; Have we broken a range? else if last + 1 != c && inrange putchar last; inrange = 0; if ! inrange putchar c; last = c; 8

Ambiguity S  NP VP NP  A N NP  NP PP VP  V NP VP  VP PP PP  P NP NP  Papa N  caviar N  spoon V  spoon V  ate P  with A  the A  a S Papa NP VP V NP A N the caviar NP A N a spoon ate PP P with 9

Ambiguity -eating two things? S  NP VP NP  A N NP  NP PP VP  V NP VP  VP PP PP  P NP NP  Papa N  caviar N  spoon V  spoon V  ate P  with A  the A  a S Papa NP VP NP V A N the caviar NP A N a spoon ate PP P with 10

11 Compiling: we would create a file of machine instructions that would only work for one architecture. Interpreting: our interpreter would have to be able to understand the high level code AND would repeatedly parse it (if the code was in a loop or called multiple times). Parsing is the process of analyzing a text, made of a sequence of tokens (for example, words), to determine its grammatical structure with respect to a given grammar.tokens When we use the hybrid approach of Java, we produce the file Test.class, which is a binary file that's not readable by most humans.

12 Language Translation Steps Compilation: lexical analysis: characters grouped into logical chunks (keywords, constants, etc) syntax analysis: figure out what it means - usually use parse trees (grammars to define). Like diagraming sentences. I have a picture of John at my home – what is meant? What does “at my home” modify? code generation optimization - to make smaller or faster linking: supplying missing addresses to system code load module: user code augmented with system code

13 Language Implementation Steps Pure Interpretation: no translation phase - fetch, decode, and execute the source code (not machine code) Advantages/Disadvantages 1. easy to write debugger - as source lines are unchanged (no cryptic messages from debugger) 2. execution is times slower; statement decoding is bottleneck 3. better for language with simple structure - as not so slow to decode 4. natural for some kinds of features - like dynamic binding of type. Ex: c = c+b If c may be integer, string, or a set, how can we know what code to generate? 5. Nice for executing user produced code at runtime Ex. boolean expression is easy to evaluate if known at compile time, but NOT if produced at run time.

14 What is meant by dynamic binding? Girls choice dance: –Will you go with Sofie? (early binding) –Will you go with Sofie/Ann/Betty (whoever shows up at your door)? (delayed binding) –No specific partner assigned, but will change throughout the night. (changing binding) Lots of the interesting issues involve binding times.

15 Rectangle Triangle Polygon class Polygon{ int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); String display(String s) { return “POLYGON” + s; float area(); }; class Rectangle: public Polygon{ public: float area(); String display(String s) { return “RECTANGLE” + s … ; }; class Triangle: public Polygon{ public: float area() {…} String display(String s) { return “TRIANGE” + s …; }; Passing in a polygon, actual type can be any descendant.

16 Class binding (C++) Suppose I have Polygon * p = new Rectangle() p->display(); Which display is called? virtual – function call is resolved at run time

Dictionary Moment Lexical: of or relating to words or the vocabulary of a language as distinguished from its grammar and construction Lexical Analysis: finding the individual words (tokens) of the code. 17

18 Error classification Lexical: character-level error, such as illegal character (hard to distinguish from syntax error). Syntax: error in structure (e.g., missing semicolon or keyword). Static semantic: non-syntax error prior to execution (e.g., undefined vars, type errors). Dynamic semantic: non-syntax error during execution (e.g., division by 0). Logic: programmer error, program not at fault.

19 Notes on error reporting A compiler will report lexical, syntax, and static semantic errors. It cannot report dynamic semantic errors; which is job of runtime system. An interpreter will often only report lexical and syntax errors when loading the program. Static semantic errors may not be reported until just prior to execution. Indeed, most interpreted languages (e.g. Lisp, Smalltalk) do not define any static semantic errors. No translator can report a logic error.

20 Find examples of (possible) syntax, semantic and logic errors. public int gcd ( int v# ) { int z = value y = v; while ( y >= 0 ) { int t = y; y = z % y; z = t; } return y; }

21 Sample Errors (Java): public int gcd ( int v# ) // lexical bad # { int z = value // syntax - missing ; y = v; // static semantic - y undeclared while ( y >= 0 ) { int t = y; y = z % y; z = t; // dynamic semantic - division by zero } return y; // logic - should return z }