CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 5.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Context-Free Grammars Lecture 7
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
TES3111 October 2001 Artificial Intelligence LISP.
CSE S. Tanimoto Introduction 1 LISP Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Functional Programming in Scheme
CS 152: Programming Language Paradigms February 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing G Programming Languages May 24, 2012 New York University Chanseok Oh
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Input and Output in Scheme CS 480/680 – Comparative Languages.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
CMSC 330: Organization of Programming Languages
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING LANGUAGES
CSE S. Tanimoto Introduction
CS 3304 Comparative Languages
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Programming Language Syntax 5
CSE S. Tanimoto Introduction
CSE S. Tanimoto Introduction
Streams, Delayed Evaluation and a Normal Order Interpreter
Lisp and Scheme I.
CSE S. Tanimoto Introduction
6.001 SICP Variations on a Scheme
CSE S. Tanimoto Introduction
Syntax Analysis - 3 Chapter 4.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 5

22/69 Parsing Top-downBottom-up Grammar:Input: A, B, C;

33/69 Parsing The example grammar is not very well suited for bottom-up parsing. Why? Because it needs to shift all input tokens until it finds the ; Here is a more suitable one: id_list → id_list_prefix ; id_list_prefix → id_list_prefix, id → id Other classes of grammars: −Subclasses of LR: SLR, LALR, … −Subclasses of LL: SLL, … Notation for number of tokens to look ahead: −LL(k), LR(k) −In general, only LL(1) and LR(1) are used

44/69 Parsing Implementing a parser: −Recursive descent parser (top-down) – section from textbook −Automatic parser generators – in Unix: yacc / bison −Take a grammar as input −Produce a C program as output, that implements the parser

55/69 The Scheme Programming Language 1950s-1960s: Lisp developed at MIT (John McCarthy), based on lambda calculus (Alonzo Church) Lisp - the first functional programming language No single standard evolved for Lisp Two major Lisp dialects - Common Lisp and Scheme Scheme - developed at MIT in 1970s Member of functional class of programming languages Actually - has a functional core plus some imperative features Main application - symbolic computing, artificial intelligence

66/69 The Structure of a Scheme Program All programs and data are expressions Expressions can be atoms or lists Atom: number, string, identifier, character, boolean List: sequence of expressions separated by spaces, between parentheses Syntax: expression  atom | list atom  number | string | identifier | character | boolean list  ( expr_seq ) expr_seq  expression expr_seq | expression

77/69 Interacting with Scheme Interpretor: "read-eval-print" loop > 1 1 Reads 1, evaluates it (1 evaluates to itself), then prints its value > (+ 2 3) 5 + => function + 2 => 2 3 => 3 Applies function + on operands 2 and 3 => 5

88/69 Evaluation Constant atoms - evaluate to themselves 42- a number another number "hello"- a string #/a- character 'a' #t- boolean value "true" > "hello world" "hello world"

99/69 Evaluation Identifiers (symbols) - evaluate to the value bound to them (define a 7) > a 7 > + #

1010/69 Evaluation Lists - evaluate as "function calls": (function arg1 arg2 arg3...) First element must evaluate to a function Recursively evaluate each argument Apply the function on the evaluated arguments > (- 7 1) 6 > (* (+ 2 3) (/ 6 2)) 15

1111/69 Operators - More Examples Prefix notation Any number of arguments > (+) 0 > (+ 2) 2 > (+ 2 3) 5 > ( ) 9 > ( ) 1 > (/ ) 2

1212/69 Preventing Evaluation (quote) Evaluate the following: > (1 2 3) Error: attempt to apply non-procedure 1. Use the quote to prevent evaluation: > (quote (1 2 3)) (1 2 3) Short-hand notation for quote: > '(1 2 3) (1 2 3)

1313/69 More Examples (define a 7) a=> 7 'a=> a (+ 2 3)=> 5 '(+ 2 3)=> ((+ 2 3))=> (+ 2 3) Error: attempt to apply non-procedure 5. '(her 3 "sons")=> (her 3 "sons") Make a list: (list 'her (+ 2 1) "sons")=> (her 3 "sons") (list '(+ 2 1) (+ 2 1))=> ((+ 2 1) 3)

1414/69 (list + 1 2)=> (+ 1 2) (eval (list + 1 2))=> Forcing Evaluation (eval) ( )=> 6 '( )=> ( ) (eval '( ))=> 6 Eval evaluates its single argument Eval is implicitly called by the interpretor to evaluate each expression entered: “read-eval-print” loop 3

1515/69 Special Forms Have different rules regarding whether/how arguments are evaluated (define a 5); binds a to 5, does not evaluate a (quote (1 2 3)); does not evaluate (1 2 3) There are a few other special forms – discussed later 5 a

1616/69 Using Scheme Petite Chez Scheme −installed on ECC computers running Linux and on CS network (banyan.cs.unr.edu) −free downloads for Windows, Linux, Unix The essentials: scheme; to start Scheme from the Unix prompt ^C; to interrupt a running Scheme program (exit); to exit Scheme Documentation available on Chez Scheme webpage

1717/69 Announcements Readings −May look at Scheme books or on-line references