Download presentation

Presentation is loading. Please wait.

Published byReed Urie Modified over 2 years ago

1
Spring 2007NOEA - Computer Science1 Design Patterns Regular Expressions (Regular Languages) State Machines Implementation of State Machine State Pattern Observer Pattern Delegates

2
Spring 2007NOEA - Computer Science2 Regular Expressions Definition: Regular Expressions are strings constructed by the following rules: –Alphabet: Set of symbols: For instance: {a,b,c} or the ASCII char-set or… –Epsilon (ε): empty string –Selection: For instance a | b (a or b) –Iteration: For instance (a)* (0 or more ‘a’s concatenated) –Concatenation: One string followed by an other –For instance: ((a|b)a)* means: an a or a b followed by an a – everything repeated 0 or more times, that is: strings of the form: ε, aa, ba, aaaa, aaba, baaa, baba,….

3
Spring 2007NOEA - Computer Science3 Regular Expressions Names for regular expressions: It can be helpful to give names to long regular expressions L( (0 | 1 | 2 | … | 9) (0 | 1 | 2 | … | 9)* ) = {0,1,2,3, … 10,11,…} or we could write: digit digit* where digit = 0 | 1 | 2 | … | 9

4
Spring 2007NOEA - Computer Science4 Extensions –One or More repetition ‘+’ L( (0 | 1 | 2 | … | 9)+) = {0, 1, 2, … 10, 11,12} –Any Character ‘.’.*b.* means at least one b –A range of Characters ‘[]’ [a-zA-Z] = a | b | … | z | A | B | … | Z [abc] = a | b | c –Any Character Not In a Given Set ~(a | b | c) means not either a or b or c. –Optional ‘?’ a? means zero or one a

5
Spring 2007NOEA - Computer Science5 Examples Some other examples: –Let Σ = {a,b,c}. Consider the strings that contains exactly one b. L( (a | c)*b(a | c )* ) = {b, abc, abaca, … } –Let Σ = {a,b,c}. Consider the strings that contains at most one b. L((a | c)*b(a | c )* | (a | c)* ) = {ε, a, c, b, abc, abaca, … } or alternative L( (a | c)* (b | ε)(a | c )*)

6
Spring 2007NOEA - Computer Science6 Exercises 1.Write a regular expression that defines valid identifiers in some programming language (the only characters allowed in an identifier are letters, digits and underscore (‘_’), and it always starts with a letter). 2.Try to write a regular expression that defines valid email addresses. See RegExDemo.zip and jsjsgrpregexpsyntax[1].htmRegExDemo.zipjsjsgrpregexpsyntax[1].htm

7
Spring 2007NOEA - Computer Science7 State Machine (Deterministic) Finite Automata A Deterministic Finite Automata (DFA) is a device that is able to recognise regular expressions. There is an one-to-one relation between a regular expression and a DFA: –Given a regular expression, there is an algorithmic construction of corresponding DFA A DFA has no state from which more than one transition is possible on the same input symbol.

8
Spring 2007NOEA - Computer Science8 Examples End state If the DFA stops in a state that is not an end state, then input is not valid

9
Spring 2007NOEA - Computer Science9

10
Spring 2007NOEA - Computer Science10 DFA defining Integers

11
Spring 2007NOEA - Computer Science11 Scanner Loop state:= 1; error:= false; while(not eotxt and not error) if (exists transition from state marked with current input symbol) state:= the state that this transition leads to set current input to next symbol in the input sequence else error:= true; endif; endwhile; if(error) report “error in input”; else report “ input ok” endif

12
Spring 2007NOEA - Computer Science12 Exercise 3.Construct a DFA that recognises identifiers

13
Spring 2007NOEA - Computer Science13 State Pattern Implements state machines (DFA) encapsulating state. Provides addition of new states without changing existing code. Examples: –Dialog box for editing parameters to a program –XML –parsing protocols –Parser/scanner in a compiler or a browser or… –Event handling in a windows system –….. Regular Languages (Expressions)

14
Spring 2007NOEA - Computer Science14 State Pattern Implements the loop that gets next state and calls any operations connected current state

15
Spring 2007NOEA - Computer Science15 The Classes of the Pattern Context: Defines the objects that we want maintain state information about (for instance DialogBox). This class has a reference (static type: ContextState – the abstract super class) to some concrete state (that is an object of one of the sub classes – dynamic type). ContextState: The abstract super class defining a common interface to all the concrete states. ConcreteState1,...: The sub classes to ContextState. One sub class to each state in the DFA. The key to the design is in the processEvent-method, which takes an event as input and returns the next state.

16
Spring 2007NOEA - Computer Science16 OO Implementation State is an object State Pattern can be applied: –abstract class State specifies one or more abstract methods: transition(-) – returns state corresponding to input symbol action(-) – if any processing is to be done when a transition occurs (code generation, event handling etc.) each concrete state inherits from State and implements the abstract methods The parser loop uses references having the abstract class State as static type. Polymorphism and dynamic binding handles the rest!

17
Spring 2007NOEA - Computer Science17 Signed Integer Recogniser

18
Spring 2007NOEA - Computer Science18 OO Parser Loop public bool Scan(string input) { //input.Length>0 bool ok = false; int i = 0; char nextChar = input[i]; State currState = s1.Transition(nextChar); while (currState != s5 && currState != s4) { i++; if (i == input.Length) nextChar = '\0'; else nextChar = input[i]; currState = currState.Transition(nextChar); } if (currState == s5) ok = true; return ok; } View the Code

19
Spring 2007NOEA - Computer Science19 Regular Languages (Expressions) and DFAs A regular language is a language which syntax can be define by a regular expression or a regular grammar. Regular languages is a subset of the context free languages (most programming language are context free). Regular languages don’t allow recursive constructions. Recursion can be transformed to iteration. –This means that regular languages cannot have nested blocks as: Expressions with nested parenthesis begin-end –blocks (’{’-’}’) or similar constructs Regular expressions are important in validating input, parsing protocols etc.

20
Spring 2007NOEA - Computer Science20 RegEx in.NET bool IsValidEmail(string strIn) { // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.) | (([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } What is defined here? Documentation

21
Spring 2007NOEA - Computer Science21 Exercises 4.Do this exercise.this 5.Implement the DFA that recognises identifiers using the State Pattern

22
Spring 2007NOEA - Computer Science22 Observer Pattern View source

23
Spring 2007NOEA - Computer Science23 Observer Pattern En række forskellige objekter (observers) ønsker at få at vide, når et objekt (observable eller subject) skifter tilstand. Subject skal tillade observers at melde sig dynamisk og skal ikke vide noget om dem.

24
Spring 2007NOEA - Computer Science24 Observer Pattern Fx ønsker en række objekter at blive underrettet om ændringer i et andet objekt. Disse objekter (observers) skal implementere interfacet IObserver. Dvs. metoden NotifyMe, hvor deres handlinger kodes Subject skal holde styr på en collection af observers og kunne notificere alle objekter i collectionen (IObservable). Det konkrete Subject kalder NotifyAll, når der er et relevant tilstandsskift

25
Spring 2007NOEA - Computer Science25 Observer Pattern Observer Pattern er et OO-design, som simulerer funktionspointere i fx C og højereordensfunktioner i fx Pascal I C# findes sprogkonstruktionen delegate, som giver nogle af de samme muligheder. –Se eksempeleksempel

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google