Cse321, Programming Languages and Compilers 1 7/14/2015 Lecture #3b, Jan. 22, 2007 References While Loops Accumulating parameter functions Regular Expressions.

Slides:



Advertisements
Similar presentations
Programming with Lists
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
CSE 105 Theory of Computation Alexander Tsiatas Spring 2012 Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Advanced Programming Andrew Black and Tim Sheard Lecture 9 More Regular Expressions NFAs.
Cse321, Programming Languages and Compilers 1 6/26/2015 Lecture #4, Jan. 24, 2007 Homework 3 Representing sets as lists the cross product of two sets epsilon.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CPSC 388 – Compiler Design and Construction
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
COMP313A Programming Languages Lexical Analysis. Lecture Outline Lexical Analysis The language of Lexical Analysis Regular Expressions.
Chapter 9: Functional Programming in a Typed Language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Transition Diagrams Lecture 3 Wed, Jan 21, Building Transition Diagrams from Regular Expressions A regular expression consists of symbols a, b,
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
Lexical Analysis: DFA Minimization & Wrap Up. Automating Scanner Construction PREVIOUSLY RE  NFA ( Thompson’s construction ) Build an NFA for each term.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Lexical Analysis – Part II EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006.
Chapter SevenModern Programming Languages1 A Second Look At ML.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
November 2003Computational Morphology III1 CSA405: Advanced Topics in NLP Xerox Notation.
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Deterministic Finite Automata Nondeterministic Finite Automata.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
CS314 – Section 5 Recitation 2
Principles of programming languages 12: Functional programming
Theory of Computation Lecture #
ML: a quasi-functional language with strong typing
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Syntax Specification and Analysis
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Week 14 - Friday CS221.
Two issues in lexical analysis
Review: Compiler Phases:
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Lecture 5 Scanning.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Presentation transcript:

Cse321, Programming Languages and Compilers 1 7/14/2015 Lecture #3b, Jan. 22, 2007 References While Loops Accumulating parameter functions Regular Expressions as programs RE to NFA Patterns for RE’s

Cse321, Programming Languages and Compilers 2 7/14/2015 Libraries There are lots of predefined types and functions in SML when it starts up. You can find out about them at: Many more can be found in the other libraries. – Libraries are encapsulated in Structures which are classified by Signatures (a list of what is in the structure).

Cse321, Programming Languages and Compilers 3 7/14/2015 Peeking inside a Library To see what is inside a Structure you can open it. This is somewhat of a hack, but it is useful. Standard ML of New Jersey v [built: Mon Nov 21 21:46: ] - open Int; opening Int type int = ?.int val precision : Int31.int option val minInt : int option val maxInt : int option val toLarge : int -> IntInf.int val fromLarge : IntInf.int -> int val toInt : int -> Int31.int val fromInt : Int31.int -> int val div : int * int -> int val mod : int * int -> int val quot : int * int -> int val rem : int * int -> int val min : int * int -> int val max : int * int -> int

Cse321, Programming Languages and Compilers 4 7/14/2015 The List library - open List; opening List datatype 'a list = :: of 'a * 'a list | nil exception Empty val null : 'a list -> bool val hd : 'a list -> 'a val tl : 'a list -> 'a list val last : 'a list -> 'a val getItem : 'a list -> ('a * 'a list) option val nth : 'a list * int -> 'a val take : 'a list * int -> 'a list val drop : 'a list * int -> 'a list val length : 'a list -> int val rev : 'a list -> 'a list : 'a list * 'a list -> 'a list val concat : 'a list list -> 'a list val revAppend : 'a list * 'a list -> 'a list val app : ('a -> unit) -> 'a list -> unit val map : ('a -> 'b) -> 'a list -> 'b list val mapPartial : ('a -> 'b option) -> 'a list -> 'b list val find : ('a -> bool) -> 'a list -> 'a option val filter : ('a -> bool) -> 'a list -> 'a list val partition : ('a -> bool) -> 'a list -> 'a list * 'a list val foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val exists : ('a -> bool) -> 'a list -> bool val all : ('a -> bool) -> 'a list -> bool val tabulate : int * (int -> 'a) -> 'a list val collate : ('a * 'a -> order) -> 'a list * 'a list -> order -

Cse321, Programming Languages and Compilers 5 7/14/2015 References References allow one to write programs with mutable variables. The interface to assignment and update is slightly different from other languages –val r = (ref 5) Create a new reference that can be updated –!r Get the value stored in the reference –(ref n) => … Pattern match to get value »fun ! (ref n) = n –r := 6 + z Update a reference with a new value Later today we will use the following: val next = ref 0; fun new () = let val ref n = next in (next := n+1; n) end; Alternatively fun new () = let val n = !next in (next := n+1; n) end;

Cse321, Programming Languages and Compilers 6 7/14/2015 While Loops While loops are similar to other languages, they usually require use of references (to get the condition to eventually change). Statements are inside ()’s and separated by “;” val n = ref 4; val w1 = while (!n > 0) do (print (Int.toString (!n) ^ "\n"); n := (!n) – 1 ); Semicolon to separate statements Semicolon to end the “val w1 = …” declaration

Cse321, Programming Languages and Compilers 7 7/14/2015 Factorial as a while loop Factorial fun fact3 n = let val ans = ref 1 val count = ref n in while (!count > 0) do (ans := !ans * !count ; count := !count - 1); !ans end; fun fact1 n = if n=0 then 1 else n * (fact1 (n-1)); fun fact2 0 = 1 | fact2 n = n * (fact2 (n-1)); Inside a let between “in” and “end” we don’t need to surround statements with ()s, but we still separate with “;” Return the value stored in the reference ans compare with the recursive versions

Cse321, Programming Languages and Compilers 8 7/14/2015 Accumulating parameters Many loops look like this { ans = init; While test do stuff; Return ans} There is a pattern that mimics this in ML called functions with accumulating parameter. The pattern consists of a recursive function with two (or more) parameters. The first parameter drives the loop (usually by pattern matching), the second accumulates an answer (like ans in example above). We call the function with init as the value of the second argument to get started. We return the second argument when the function is done recurring.

Cse321, Programming Languages and Compilers 9 7/14/2015 Fact as an accumulating function fun fact4 n = let fun loop 0 ans = ans | loop n ans = loop (n-1) (n*ans) in loop n 1 end; { ans = init; While test do stuff; Return ans}

Cse321, Programming Languages and Compilers 10 7/14/2015 Flat as an accumulating function datatype Tree = Tip | Node of Tree * int * Tree; fun flat3 x = let fun help Tip ans = ans | help (Node(x,y,z)) ans = help x (y::(help z ans)) in help x [] end; { ans = init; While test do stuff; Return ans}

Cse321, Programming Languages and Compilers 11 7/14/2015 Regular Expressions Regular Languages and Regular expressions are used to describe the patterns which describe lexemes. Regular expressions are composed of empty-string, concatenation, union, and closure. Examples: A(A | D)* where A is alphabetic and D is a digit (+ | - | ε ) D D* closure union Empty-string Concatenation is implicit

Cse321, Programming Languages and Compilers 12 7/14/2015 Meaning of Regular Expressions Let A,B be sets of strings: The empty string: "" ε= { "" } (sometimes ) Concatenation by juxtaposition: AB = a^b where a in A and b in B A = {"x", "qw"} and B = {"v", "A"} then AB = { "xv", "xA", "qwv", "qwA"}

Cse321, Programming Languages and Compilers 13 7/14/2015 Meaning of Regular Expressions (cont.) Union by | (or other symbols like U etc) A = {"x", "qw"} and B = {"v", "A"} then A|B = {"x", "qw", "v", "A"} Closure by * Thus A* = {""} | A | AA | AAA |... = A 0 | A 1 | A 2 | A 3 |... A = {"x", "qw"} then A* = { "" } | {"x", "qw"} | {"xqw", "qwx","xx", "qwqw"} |...

Cse321, Programming Languages and Compilers 14 7/14/2015 Regular Expressions as a language We can treat regular expressions as a programming language. Each expression is a new program. Programs can be compiled. How do we represent the regular expression language? By using a datatype. datatype RE = Empty | Union of RE * RE | Concat of RE * RE | Star of RE | C of char;

Cse321, Programming Languages and Compilers 15 7/14/2015 Example RE program (+ | - | ε ) D D* val re1 = Concat(Union(C #”+”,Union(C #”-”,Empty)),Concat(C #”D”,Star (C #”D”)))

Cse321, Programming Languages and Compilers 16 7/14/2015 R.E.’s and FSA’s Algorithm that constructs a FSA from a regular expression. FSA –alphabet, A –set of states, S –a transition function, A x S -> S –a start state, S 0 –a set of accepting states, S F subset of S Defined by cases over the structure of regular expressions Let A,B be R.E.’s, “x” in A, then – ε is a R.E. –“x” is a R.E. –AB is a R.E. –A|B is a R.E. –A* is a R.E. 1 Rule for each case

Cse321, Programming Languages and Compilers 17 7/14/2015 Rules ε “x” AB A|B A* ε x B A A B ε ε ε ε A ε ε ε ε

Cse321, Programming Languages and Compilers 18 7/14/2015 Example: (a|b)*abb ε ε ε ε 8 a b ε a b b 1 Note the many ε transitions Loops caused by the * Non-Determinism, many paths out of a state on “a” ε ε ε

Cse321, Programming Languages and Compilers 19 7/14/2015 Building an NFA from a RE datatype Label = Epsilon | Char of char; type Start = int; type Finish = int; datatype Edge = Edge of Start * Label * Finish; val next = ref 0; fun new () = let val ref n = next in (next := n+1; n) end; Ref makes a mutable variable Semi colon separates commands (inside parenthesis)

Cse321, Programming Languages and Compilers 20 7/14/2015 fun nfa Empty = let val s = new() val f = new() in (s,f,[Edge(s,Epsilon,f)]):Nfa end | nfa (C x) = let val s = new() val f = new() in (s,f,[Edge(s,Char x,f)]) end | nfa (Union(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y val s = new() val f = new() val newes = [Edge(s,Epsilon,sx),Edge(s,Epsilon,sy),Edge(fx,Epsilon,f),Edge(fy,Epsilon,f)] in yes) end ε x A B ε ε ε ε

Cse321, Programming Languages and Compilers 21 7/14/2015 | nfa (Concat(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y in (sx,fy,(Edge(fx,Epsilon,sy)):: yes)) end | nfa (Star r) = let val (sr,fr,res) = nfa r val s = new() val f = new() val newes = [Edge(s,Epsilon,sr),Edge(fr,Epsilon,f),Edge(s,Epsilon,f),Edge(f,Epsilon,s)] in res) end A ε ε ε ε B A

Cse321, Programming Languages and Compilers 22 7/14/2015 Example use val re1 = Concat(Union(C #”+”,Union(C #”-”,Empty)),Concat(C #”D”,Star (C #”D”))) Val ex6 = nfa re1; val ex6 = (8,15, [Edge (9,Epsilon,10),Edge (8,Epsilon,0),Edge (8,Epsilon,6),Edge (1,Epsilon,9),Edge (7,Epsilon,9),Edge (0,Char #,1),Edge (6,Epsilon,2),Edge (6,Epsilon,4),Edge (3,Epsilon,7),Edge (5,Epsilon,7),Edge (2,Char #,3),Edge (4,Epsilon,5),...]) : Nfa

Cse321, Programming Languages and Compilers 23 7/14/2015 Assignment #3 CS321 Prog Lang & Compilers Assignment # 3 Assigned: Jan 22, 2007 Due: Wed. Jan 24, 2007 Turn in a listing, and a transcript that shows you have tested your code. A minimum of 3 tests is necessary. Some functions may require more than 3 tests to receive full credit. 1) Write the following functions over lists. You must use pattern matching and recursion. A. reverse a list so that its elements appear in the oposite order. reverse [1,2,3,4] ----> [4,3,2,1] B. Count the number of occurrences of an element in a list count 4 [1,2,3,4,5,4] ---> 2 count 4 [1,2,3,2,1] ---> 0 C. concatenate together a list of lists concat [[1,2],[],[5,6]] ----> [1,2,5,6] 2) Using the datatype for Regular Expressions we defined in class datatype RE = Empty | Union of RE * RE | Concat of RE * RE | Star of RE | C of char; Write a function that turns a RE into a string, so that it can be printed. Minimize the number of parenthesis, but keep the string unambigouous by using the following rules. 1) Star has highest precedence so: ab* means a(b*) 2) Concat has the next highest precedence so: a+bc means a+(bc) 3) Union has lowest precedence so: a+bc+c* means a+(bc)+(c*) 4) Use the hash mark (#) as the empty string. 5) Special characters *+()\ should be escaped by using a preceeding backslash. So (Concat (C #"+") (C #"a")) should be "\+a" Hints: 1) The string concatenation operator is usefull: "abc" ^ "zx" -----> "abczx" 2) Write this is two steps. First, fully paranethesize every RE Second, Change the function to not add the parenthesis which the rules don't require.