Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635 www.cs.njit.edu/~elsa/635.

Slides:



Advertisements
Similar presentations
Programming Language Concepts
Advertisements

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.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter3: Language Translation issues
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 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
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.
Chapter 3: Formal Translation Models
(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.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
10/3/20151 Programming Languages and Compilers (CS 421) Choonghwan Lee Based in part on slides by Mattox Beckman,
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
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.
Definition A string is a sequence of symbols. Examples “Hi, Mom.” “YAK” “abbababba” Question In what ways do programmers use strings?
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
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)
Grammars CPSC 5135.
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.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
CPS 506 Comparative Programming Languages Syntax Specification.
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.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
ISBN Chapter 3 Describing Syntax and Semantics.
LESSON 04.
Syntax and Grammars.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
2/1/20161 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based in.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
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.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
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.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Chapter 3 – Describing Syntax
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Chapter 3 Context-Free Grammar and Parsing
Syntax (1).
Programming Languages and Compilers (CS 421)
Compiler Construction (CS-636)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE 3302 Programming Languages
Programming Languages and Compilers (CS 421)
Context–free Grammar CFG is also called BNF (Backus-Naur Form) grammar
Programming Languages and Compilers (CS 421)
COMPILER CONSTRUCTION
Programming Languages and Compilers (CS 421)
Presentation transcript:

Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,

Copyright 2002 Elsa L. Gunter More Datatypes - datatype Int_Bin_Tree = = Leaf of int | Node of (Int_Bin_Tree * Int_Bin_Tree); datatype Int_Bin_Tree = Leaf of int | Node of Int_Bin_Tree * Int_Bin_Tree - datatype 'a option = NONE | SOME of 'a; datatype 'a option = NONE | SOME of 'a

Copyright 2002 Elsa L. Gunter Datatype Values - val bin_tree = Node(Node(Leaf 3, Leaf 6),Leaf ~7); val bin_tree = Node (Node (Leaf #,Leaf #),Leaf ~7) : Int_Bin_Tree

Copyright 2002 Elsa L. Gunter Functions over Datatypes - fun first_leaf_value (Leaf n) = n = | first_leaf_value (Node (left_tree, right_tree)) = = first_leaf_value left_tree; val first_leaf_value = fn : Int_Bin_Tree -> int - val first = first_leaf_value bin_tree; val first = 3 : int

Copyright 2002 Elsa L. Gunter Functions over Datatypes - fun is_neg n = n < 0; val is_neg = fn : int -> bool - fun first_neg (Leaf n) = = if is_neg n then SOME n else NONE = | first_neg (Node(t1,t2)) = = (case first_neg t1 = of SOME n => SOME n = | NONE => first_neg t2);

Copyright 2002 Elsa L. Gunter Functions over Datatypes val first_neg = fn : Int_Bin_Tree -> int option - val n = first_neg bin_tree; val n = SOME ~7 : int option

Copyright 2002 Elsa L. Gunter Mutually Recursive Datatypes - datatype 'a Tree = TreeLeaf of 'a = | TreeNode of 'a TreeList = and 'a TreeList = Last of 'a Tree | More of ('a Tree * 'a TreeList); datatype 'a Tree = TreeLeaf of 'a | TreeNode of 'a TreeList datatype 'a TreeList = Last of 'a Tree | More of 'a Tree * 'a TreeList

Copyright 2002 Elsa L. Gunter Mutually Recursive Datatypes - val tree = = TreeNode = (More (TreeLeaf 5, = (More (TreeNode (More (TreeLeaf 3, Last (TreeLeaf 2))), = Last (TreeLeaf 7))))); val tree = TreeNode (More (TreeLeaf 5,More (#,#))) : int Tree

Copyright 2002 Elsa L. Gunter Mutually Recursive Functions - fun fringe (TreeLeaf x) = [x] = | fringe (TreeNode list) = list_fringe list = and list_fringe (Last tree) = fringe tree = | list_fringe (More (tree,list)) = = (fringe (list_fringe list); val fringe = fn : 'a Tree -> 'a list val list_fringe = fn : 'a TreeList -> 'a list - fringe tree; val it = [5,3,2,7] : int list

Copyright 2002 Elsa L. Gunter Language Syntax Syntax is the description of which strings of symbols are meaningful expressions in a language It takes more than syntax to understand a language; need meaning (semantics) too Syntax is the entry point

Copyright 2002 Elsa L. Gunter Features of a Good Syntax Readable Writeable Lack of ambiguity Suggestive of correct meaning Ease of translation

Copyright 2002 Elsa L. Gunter Elements of Syntax Character set – previously typically ASCII, now often 64 character sets (UNICODE) Keywords – usually reserved Special constants – cannot be assigned to Identifiers – can be assigned to Operator symbols Delimiters (parenthesis, braces,brackets,) Blanks (aka white space)

Copyright 2002 Elsa L. Gunter Elements of Syntax Expressions Type expressions Declarations Statements (in imperative languages) Subprograms (subroutines)

Copyright 2002 Elsa L. Gunter Elements of Syntax Modules Interfaces Classes (for object-oriented languages) Libraries

Copyright 2002 Elsa L. Gunter Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs

Copyright 2002 Elsa L. Gunter Types of Formal Language Descriptions Regular expressions, regular grammars Context-free grammars, BNF grammars, syntax diagrams Finite state automata Whole family more of grammars and automata – covered in automata theory

Copyright 2002 Elsa L. Gunter Sample Grammar Language: Parenthesized sums of 0’s and 1’s ::= 0 ::= 1 ::= + ::= ( )

Copyright 2002 Elsa L. Gunter BNF Grammars Start with a set of characters, a,b,c,… –We call these terminals Add a set of different characters, X,Y,Z,… –We call these nonterminals One special nonterminal S called start symbol

Copyright 2002 Elsa L. Gunter BNF Grammars BNF rules (aka productions) have form X ::= y where X is any nonterminal and y is a string of terminals and nonterminals BNF grammar is a set of BNF rules such that every nonterminal appears on the left of some rule

Copyright 2002 Elsa L. Gunter Sample Grammar Terminals: ( ) Nonterminals: Start symbol = ::= 0 ::= 1 ::= + ::= ( ) Can be abreviated as ::= 0 | 1 | + | ( )

Copyright 2002 Elsa L. Gunter BNF Deriviations Given rules X::= yZw and Z::=v we may replace Z by v to say X => yZw => yvw From example: => + => ( ) + => ( + ) + => (0 + ) + => (0 + 1) + => (0 + 1) + 0

Copyright 2002 Elsa L. Gunter BNF Semantics The meaning of a BNF grammar is the set of all strings consisting only of terminals that can be derived from the Start symbol

Copyright 2002 Elsa L. Gunter Extended BNF Grammars Alternatives: allow rules of from X::=y|z –Abbreviates X::= y, X::= z Options: X::=y[v]z –Abbreviates X::=yvz, X::=yz Repetition: X::=y{v}*z –Can be eliminated by adding new nonterminal V and rules X::=yz, X::=yVz, V::=v, V::=vV

Copyright 2002 Elsa L. Gunter Regular Grammars Subclass of BNF Only rules of form ::= or ::= Defines same class of languages as regular expressions Important for writing lexers (programs that convert strings of characters into strings of tokens)

Copyright 2002 Elsa L. Gunter Parse Trees Graphical representation of a derivation Example: (0 + 1) ()

Copyright 2002 Elsa L. Gunter Ambiguous Grammars and Languages A BNF grammar is ambiguous if its language contains strings for which there is more than one parse tree If all BNF’s for a language are ambiguous then the language is inherently ambiguous

Copyright 2002 Elsa L. Gunter Example: Ambiguous Grammar

Copyright 2002 Elsa L. Gunter Example Regular grammar: – ::= epsilon – ::= 0 – ::= 1 – ::= 0 – ::= 1 Generates string where every initial substring of even length has same number of 0’s as 1’s

Copyright 2002 Elsa L. Gunter Example What is the result for: * 5 + 6

Copyright 2002 Elsa L. Gunter Example What is the result for: * Possible answers: – 41 = ((3 + 4) * 5) + 6 – 47 = 3 + (4 * (5 + 6)) – 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) – 77 = (3 + 4) * (5 + 6)