Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

58
Fall 2007 CS 225 1 Miscellaneous Topics Cloning Patterns Recursion and Grammars
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    229
  • download

    0

Transcript of Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Page 1: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 1

Miscellaneous Topics

Cloning

Patterns

Recursion and Grammars

Page 2: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 2

The Shallow Copy Problem

Page 3: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 3

Cloning• The purpose of cloning in object-oriented

programming is analogous to cloning in biology– Create an independent copy of an object

• Initially, both objects will store the same information• Since they are different objects, you can change one

object without affecting the other

Page 4: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 4

• The statement e1.setAddressLine1("Room 224"); creates a new String object that is referenced by e1.address.line1 and e2.address.line1

Page 5: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 5

The Object.clone method

• Java provides the Object.clone method to help solve the shallow copy problem

• The initial copy is a shallow copy as the current object’s data fields are copied

• To make a deep copy, you must create cloned copies of all components by invoking their respective clone methods

Page 6: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 6

Cloning

• After e1.setAddressLine1("Room 224"); only e1.address.line1 references the new String object.

Page 7: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 7

Regular Expressions

Page 8: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 8

Regular Expressions• First described by Stephen Kleene• Used for pattern matching

– Unix utilities like grep and awk– built into many scripting languages (e.g. perl)– libraries exist for other languages (Pattern and Matcher

classes in Java)

• No standard notation– Many languages use Perl Compatible Regular Expressions

• Useful for describing things like identifiers and numbers for a programming language

Page 9: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 9

Regular Expression Components

• Atoms - the characters that can be combined to make the pattern being described

• Concatenation - a sequence of atoms • Alternation - a choice between several patterns• Kleene closure (*) - 0 or more occurrences• Positive closure (+) - 1 or more occurrences• nothing ()

Page 10: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 10

Patterns and Matching• a pattern is generally enclosed between a

matched pair of characters, most commonly //– /pattern/

• Languages that support pattern matching may have a match operator

~=, m//Perl

!~~AWK

No Match operator

Match operator

Language

Page 11: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 11

Metacharacters• Characters that have a special meaning

within a pattern

OR |

0 or 1 occurrences?

1 or more occurrences+

0 or more occurrences*

used to group characters()

uses to enclose a character class

[ ]

matches end of string$

matches beginning of string^

escape character\

any single character.

Page 12: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 12

Simple Examples

• A single character : /a/– Matches any string that contains the letter a

• A sequence of characters– /ab/ matches any string that contains the letter a followed

immediately by the letter b– /bird/ matches any string that contains the word bird– /Regular/ matches any string that contains the word

Regular (matches are case-sensitive by default)

Page 13: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 13

More Examples

• Any character : a.– a followed by any character

• A choice of two characters : a | b– a b ac ab bc but not cd ef

• Optional repeated character : ab*– a ab abb abbbb abracadabra

• Optional repeated sequence : a(bc)*– a abc abcbc

• At least one of a sequence : ab+

– ab abb abbbb abracadabra

Page 14: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 14

Character Classes• You can put a set of characters inside square

brackets to create a character class– [abc] means any one of a b or c

• A ^ as the first character means any character that isn't in the set– [^abc] means any character except a b or c

• You can also specify ranges of characters (based on ASCII codes)– [0-9] is any digit

Page 15: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 15

Regular Expressions for String Manipulation

• split( regexp, string) tokenizes a string

• s/regexp/replacement/ substitutes for regexp– g at end means do all occurrences

• Expression memory allows you to remember what matches parts of pattern in parentheses

Page 16: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

CS 225 16

Regular Expressions in Java

• Java has classes for using regular expressions– The String class has a matches method

• parameter is a regular expression

– The java.util.regex package has classes that can be used for pattern matching operations

• Pattern represents regular expressions• Matcher creates an object that performs various pattern

matching operations

Page 17: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 17

Recursion and Grammars

Page 18: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 18

Grammars and Recursion• A grammar is a formal description of a

language– For natural languages, this is hard– In order to be able to use a program to

translate a program, programming languages need to be relatively simple.

• Regular expressions can be used to specify the simplest grammars

• BNF is a notation that was invented to describe programming languages

Page 19: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 19

Backus-Naur Form

• Generally referred to as BNF

• Most widely known method for describing programming language syntax

• BNF description of a language consists of a set of rules that can be used to generate statements in the language

Page 20: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 20

BNF Rules• Left hand side of a rule is a non-terminals;

something that is built from smaller pieces– there may be several rules for a single non-terminal

• Right hand side of a rule consists of terminals and other non-terminals in the order they need to occur– Terminals are typically keywords and symbols

• A grammar is a collection of rules

Page 21: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 21

Sample Grammar

• <S> -> <A><B>

• <S> -> <B>

• <A> -> aa<A>

• <A> -> a

• <B> -> b<B>

• <B> -> b

• <S> is known as the start symbol

Page 22: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 22

Derivations

• BNF is a generative device– Use a grammar to generate sentences that

belong to the language the grammar describes

• A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

Page 23: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 23

Sample Derivations<S> -> <B> -> b

<S> -> <A><B> -> a<B>-> ab

<S> -> <A><B> -> aa<A><B> -> aaaa<A><B> -> aaaaa<B> -> aaaaab<B> -> aaaaabb<B> -> aaaaabbb

Page 24: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 24

Extended BNF

• Optional parts are placed in brackets [ ]<proc_call> -> ident [(<expr_list>)]

• Alternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> → <term> (+|-) const

• Repetitions (0 or more) are placed inside braces { }<ident> → letter {letter|digit}

Page 25: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 25

Sample Grammar

• <S> -> <A><B> | <B>

• <A> -> aa<A> | a

• <B> -> b<B> | b

Page 26: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 26

• What does this have to do with recursion?

• One of the common techniques for parsing a program (checking its syntax and putting it into a form that can be translated into an executable format) uses the BNF rules to implement a set of recursive methods

Page 27: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 27

Recursive-Descent Parsing• There is a method for each nonterminal in the

grammar– If there are several rules, the method needs to

determine which to use– The method checks that each element in the rule

is present

• EBNF is ideally suited for being the basis for a recursive-descent parser, because EBNF minimizes the number of nonterminals

Page 28: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 28

Recursive-Descent Methods

• For a single rule:– For each terminal symbol in the RHS,

compare it with the next input token; if they match, continue, else there is an error

– For each nonterminal symbol in the RHS, call its associated parsing subprogram

Page 29: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 29

Recursive-Descent Methods

• A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to parse– The correct RHS is chosen on the basis of

the next token of input– The next token is compared with the first

token that can be generated by each RHS until a match is found

– If no match is found, it is a syntax error

Page 30: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 30

Example

• Our sample grammar would have three methods– S()– A()– B()

• Algorithm for S()call A()

call B()

Page 31: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 31

Algorithm for A()

checks for an a

if presentcheck for a second a

if present

call A

else

fail

call B()

Page 32: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 32

Algorithm for B()

checks for an b

if presentcheck for a second b

if present

call B

else

fail

Page 33: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 33

Finite State Machines

• Material from The Object of Data abstraction and structures using Java by David Riley

Page 34: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 34

An entire collection of useful table-driven algorithms makes use of a theoretical concept known as a finite state machine (FSM).

Example AlgorithmInput a stream of “bits” (‘0’ or ‘1’ characters)

from a text stream.Output to the standard output stream, according to the following rules: 1) Write an ‘X’ for every odd-numbered bit (i.e., 1st, 3rd, 5th, etc.) 2) Write a ‘Z’ for every even-numbered bit with value of ‘0’. 3) Write an ‘N’ for every even-numbered bit with value of ‘1’.

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z X N

Question

How could we draw a picture of this algorithm? The Object of Data

Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 35: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 35

A graphical model for the preceding algorithm is shown below.

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

Notation•Each circle is a separate state.

• Each arc represents a potential transition from one state to another. The label a / b denotes an input symbol of a and output of b.

• One state has an incoming arc without a state on the opposite end. This state is called the start state.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 36: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 36

Sample input stream

resulting output

0 0 0 1 1 1 1 0 0 1

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 37: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 37

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 38: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 38

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 39: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 39

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 40: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 40

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 41: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 41

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 42: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 42

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 43: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 43

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 44: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 44

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 45: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 45

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 46: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 46

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 47: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 47

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 48: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 48

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 49: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 49

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 50: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 50

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 51: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 51

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 52: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 52

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 53: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 53

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z X

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 54: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 54

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z X

After Even

After Odd

1 X

0 X

0 Z

1 N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 55: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 55

Sample input stream

resulting output

0

X

0 0 1 1 1 1 0 0 1

Z X N X N X Z X N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 56: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 56

An (Finite State Machine) FSM is a 6-tuple: 1) A set of

states2) A start state3) A set of input symbols4) A set of output symbols5) A next state function

( State Input State)6) An output function

( State Input Output)

{ AfterEven, AfterOdd } AfterEven

{ 0, 1 }

{ N, X, Z }

NextState(AfterEven, 0) AfterOdd NextState(AfterEven, 1) AfterOddNextState(AfterOdd, 0) AfterEvenNextState(AfterOdd, 1) AfterEven

Output(AfterEven, 0) XOutput(AfterEven, 1) XOutput(AfterOdd, 0) ZOutput(AfterOdd, 1) N

After Even

After Odd

1 / X

0 / X

0 / Z

1 / N

Name the parts below

Actions other than output are also permitted in some FSMs.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 57: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Fall 2007 CS 225 57

How many states?

0 / 0

1 / 1

0 / 2

1 / 3

1 / 30 / 00 /

2

1 / 1

Which is the start state? What is the input alphabet? What is the output alphabet? Select meaningful state labels.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 58: Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.

Spring 2010 CS 225 58

A 4-Step Strategy 1) Select the

states. 2) Identify the start state. 3) Complete the Next State function. 4) Complete the Output function.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.