CS 330 Programming Languages 09 / 11 / 2007 Instructor: Michael Eckmann.

Slides:



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

CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
CS 330 Programming Languages 09 / 07 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 12 / 2006 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Reasons to study concepts of PL
ISBN Chapter 1 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter 1 Topics Motivation Programming Domains.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
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.
CS 330 Programming Languages 09 / 06 / 2007 Instructor: Michael Eckmann.
Programming Languages Structure
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
ISBN Chapter 1 Topics Motivation Programming Domains Language Evaluation Criteria Influences on Language Design Language Categories Language.
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.
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 355 – Programming Languages
CS 363 Comparative Programming Languages
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
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.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Advanced programming language theory. week 2. Attribute grammars and semantics.
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
Concepts of Programming Languages
What does it mean? Notes from Robert Sebesta Programming Languages
Chapter 1 Preliminaries.
Chapter 3 Describing Syntax and Semantics.
Presentation transcript:

CS 330 Programming Languages 09 / 11 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2004 Today’s Topics Continue the discussion of Language Evaluation von Neumann architecture / Imperitive languages The major language groups Language design tradeoffs Compilation vs. Interpretation vs. Hybrids Syntax & Semantics

Language Evaluation Writability –Orthogonality if small set and all combinations make sense + if large set and all combinations exist but some don't make sense to ever use or would be rarely used – (could cause undetected errors) –Abstraction (functional (methods/functions) & data (classes)) don't need to know/be reminded of/replicate the implementation details, just need to use what has already been written/tested –Expressivity (powerful operators or many ways to do the same things e.g. count++, for vs. while...) Reliability –(+)Type checking (at run-time vs. compile-time vs. neither) when is better? –(+)Exception Handling –(-)Aliasing (why?) Michael Eckmann - Skidmore College - CS Fall 2007

Language Evaluation Cost (time == $) –training programmers in the language –writing --- IDEs –compiling, executing --- compiler optimization tradeoff –Implementation system (e.g. The compiler and/or interpreter available for free?) –Poor reliability –Maintenance (up to 4 times cost of developing) Can you think of others? Michael Eckmann - Skidmore College - CS Fall 2007

A few Imperative Languages C, Pascal and many others C++ is imperative-based but it is OO Java is also imperative-based but it is OO

von Neumann Architecture Michael Eckmann - Skidmore College - CS Fall 2007

von Neumann bottleneck transmission/piping between memory and CPU takes longer than executing instructions within the CPU. Michael Eckmann - Skidmore College - CS Fall 2007

Imperative Language Features Imperative languages are built FOR the von Neumann architecture –Data and programs stored in same memory –Memory is separate from CPU –Instructions and data are transmitted to CPU from memory, results are transmitted back to memory Variables model memory cells Assignment statements model transmission Iteration is efficient (b/c instructions are in adjacent memory cells) – recursion is inefficient (b/c save state of each call). Michael Eckmann - Skidmore College - CS Fall 2007

Functional vs. Imperative Languages Functional (e.g. Lisp, Scheme, et al.) –Apply functions to parameters –different use of variables –No assignment statements –No iteration –Many programmers feel that there are extreme benefits to computing using functional languages. So, why aren't they used more? Michael Eckmann - Skidmore College - CS Fall 2007

Functional vs. Imperative Languages Because of von Neumann, that's why! What might be a drawback to using a functional language on a von Neumann machine? Michael Eckmann - Skidmore College - CS Fall 2007

Imperative Languages Von Neumann => imperative languages But many programmers do not realize this and think that imperative languages are the natural way to program. Michael Eckmann - Skidmore College - CS Fall 2007

The major Language Groups Imperative –And visual langs (drag/drop code) Functional Logic –Rule based, no order of execution Object-Oriented –provide support for abstract data types inheritance dynamic binding of method calls to methods Michael Eckmann - Skidmore College - CS Fall 2007

Some Language Design Trade-offs Reliability vs. cost of execution –e.g. Run-time array range checking --- Java vs. C Readability vs. writability (expressivity) –Compactness Flexibility vs. safety –e.g. Multiple types allowed in same memory location Michael Eckmann - Skidmore College - CS Fall 2007

Let's think about some stuff Any other language evaluation criteria you can think of? What are the pros and cons of case sensitivity in user defined names? Michael Eckmann - Skidmore College - CS Fall 2007

Compilation, Interpretation, Hybrids What is a compiler? What is an interpreter? What is a hybrid of these? –Java, Perl –Compiler generates intermediate code that is then interpreted when executed –Any advantages/disadvantages? Michael Eckmann - Skidmore College - CS Fall 2007

Compilation Process Michael Eckmann - Skidmore College - CS Fall 2007

Compilation Process Lexical analyzer – groups the code into variable names, reserved words, punctuation, etc. Syntax analyzer – constructs parse trees to determine if the lexical units in order are syntactically correct Both create/update items in the symbol table to refer to these units Intermediate code generator (and semantic analyzer) generates assembly-like code from the symbol table and parse trees. Michael Eckmann - Skidmore College - CS Fall 2007

Compilation Process Optimization may occur at this stage What might the compiler try to optimize? Code generator takes the intermediate code and the symbol table to create the machine code for the particular architecture on which the code is compiled. Before execution, the machine code must be linked to any necessary programmed libraries and/or systems programs. Michael Eckmann - Skidmore College - CS Fall 2007

Execution Process Fetch-execute cycle on a von-Neumann machine –Uses a program counter to know which instruction is next to be fetched. while (true) { Fetch instruction, increment program counter, decode instruction, execute instruction } Michael Eckmann - Skidmore College - CS Fall 2007

Now Chapter 3... Describing a language –How to give it a clear and precise definition so that implementers (compiler writers) will get it right –How to describe it to users (programmers) of the language Michael Eckmann - Skidmore College - CS Fall 2007

Syntax and Semantics Describing a language involves both it's syntax and semantics Syntax is the form, semantics is the meaning –e.g. English language example: Time flies like an arrow. –Syntactically correct but has 3 different meanings (semantics) Easier to describe syntax formally than it is to describe semantics formally Michael Eckmann - Skidmore College - CS Fall 2007

Syntax A language is a set of strings (or sentences or statements) of characters from an alphabet. Lexemes vs. tokens –tokens (more general) are categories of lexemes (more specific) –e.g. Some tokens might be: identifier, int_literal, plus_op –e.g. Some lexemes might be: idx, 42, + Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Lexemes idx = 42 + Count ; Michael Eckmann - Skidmore College - CS Fall 2007 Tokens identifier equal_sign int_literal plus_op identifier semicolon idx = 42 + count;

Syntax Recognizers and generators are used to define languages. Generators generate valid programs in a language. Recognizers determine whether or not a program is in the language (valid syntactically.) Generators are studied in Chapter 3 (stuff coming up next in this lecture) and recognizers (parsers) in Chapter 4. How many valid programs are there for some particular language, say Java? Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Context Free Grammars (CFGs) developed by Noam Chomsky are essentially equal to Backus-Naur Form (BNF) by Backus and Naur. They are used to describe syntax. These are metalanguages (languages used to describe languages.) Michael Eckmann - Skidmore College - CS Fall 2007

Syntax A Context Free Grammar is a four-tuple (T, N, S, P) where –T is the set of terminal symbols –N is the set of non-terminal symbols –S is the start symbol (which is one of the non- terminals) –P is the set of productions of the form: A -> X 1... X M where –A element of N –X i element of N U T, 1 =0 Michael Eckmann - Skidmore College - CS Fall 2007

Syntax How are CFGs used to describe the syntax of a programming language? –The nonterminals are abstractions –The terminals are tokens and lexemes –The productions are used to describe programs, individual statements, expressions etc. Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Example production:  while ( ) Everything to the left of the arrow is considered the left-hand side, LHS, and to the right the RHS. The only thing that can appear on the LHS is one nonterminal. Multiple RHS's for a LHS are separated by the | or symbol, e.g.  ; | { } Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Recursion is allowed in productions, e.g.  ident | ident, Michael Eckmann - Skidmore College - CS Fall 2007

Syntax An example grammar:   | ;  =  a | b | c | d  + | -  | const Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Derivations are repeated applications of production rules. An example derivation: => => => = => a = => a = + => a = b + => a = b + const Michael Eckmann - Skidmore College - CS Fall 2007

Syntax Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded in each step of the derivation. Michael Eckmann - Skidmore College - CS Fall 2007

Syntax / Parse Trees Michael Eckmann - Skidmore College - CS Fall 2007 A hierarchical representation of a derivation (parse trees also hold some semantic information) const a = b +

An Ambiguous Expression Grammar  | const  / | - const --//

This one is now unambiguous Ambiguity is bad for compilers, so the language description should be unambiguous.  - |  / const | const Compiler examines parse tree to determine the code to generate. Two parse trees for the same syntax causes the meaning (semantics) of the code to not be unique. Michael Eckmann - Skidmore College - CS Fall 2007

Ambiguous? Look at the if statement rules below  if then | if then else  |... Do you think this is ambiguous? That is, can more than one parse tree be generated from the same code? if (a==b) then if (c==d) then print_something() else print_something_else() Michael Eckmann - Skidmore College - CS Fall 2007

Ambiguous? if (a==b) then if (c==d) then print_something() else print_something_else() if (a==b) then if (c==d) then print_something() else print_something_else() Michael Eckmann - Skidmore College - CS Fall 2007 if (a==b) then if (c==d) then print_something() else print_something_else()

Ambiguous? To make it unambiguous take a look at page 131 in our text. Michael Eckmann - Skidmore College - CS Fall 2007