Download presentation
Presentation is loading. Please wait.
Published byAbner Copeland Modified over 9 years ago
1
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635 www.cs.njit.edu/~elsa/635
2
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
3
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
4
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
5
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);
6
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
7
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
8
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
9
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 tree) @ (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
10
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
11
Copyright 2002 Elsa L. Gunter Features of a Good Syntax Readable Writeable Lack of ambiguity Suggestive of correct meaning Ease of translation
12
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)
13
Copyright 2002 Elsa L. Gunter Elements of Syntax Expressions Type expressions Declarations Statements (in imperative languages) Subprograms (subroutines)
14
Copyright 2002 Elsa L. Gunter Elements of Syntax Modules Interfaces Classes (for object-oriented languages) Libraries
15
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
16
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
17
Copyright 2002 Elsa L. Gunter Sample Grammar Language: Parenthesized sums of 0’s and 1’s ::= 0 ::= 1 ::= + ::= ( )
18
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
19
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
20
Copyright 2002 Elsa L. Gunter Sample Grammar Terminals: 0 1 + ( ) Nonterminals: Start symbol = ::= 0 ::= 1 ::= + ::= ( ) Can be abreviated as ::= 0 | 1 | + | ( )
21
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
22
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
23
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
24
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)
25
Copyright 2002 Elsa L. Gunter Parse Trees Graphical representation of a derivation Example: (0 + 1) + 0 + + 0 0 1 ()
26
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
27
Copyright 2002 Elsa L. Gunter Example: Ambiguous Grammar 0 + 1 + 0 + 0 0 1 + + 0 + 10
28
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
29
Copyright 2002 Elsa L. Gunter Example What is the result for: 3 + 4 * 5 + 6
30
Copyright 2002 Elsa L. Gunter Example What is the result for: 3 + 4 * 5 + 6 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.