Struktury počítačových...

47
15.12.2014 1 Computer System Structures cz:Struktury počítačových systémů Lecturer: Richard Šusta ČVUT-FEL in Prague, CR subject A0B35SPS Version: 1.0 2 Arithmetics tricks According to my calculation, you should not drown... at least I think soObrázky z knihy: Barhani: Computer Arithmetic, Oxford 2012

Transcript of Struktury počítačových...

Page 1: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

1

Computer System Structures

cz:Struktury počítačových systémů

Lecturer: Richard Šusta

ČVUT-FEL in Prague, CR – subject A0B35SPS

Version: 1.0

2

Arithmetics tricks

“According to my calculation, you

should not drown...

at least I think so”

Obrázky z knihy: Barhani: Computer Arithmetic, Oxford 2012

Page 2: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

2

SPS 3

Arithmetic and number systems

A possible classification

Fixed-point number representation systems

cz: čísla s pevnou řádovou čárkou

Integers

Rational numbers (cz:racionální čísla) in forms x = y/2f,

where x, y and f and positive integer.

Floating-point number representation systems

cz: čísla s pohyblivou řádovou čárkou

nebo také s plovoucí řádovou čárkou

x * bE, where x racional number, b integer base and E

exponent

Note: All number are coded as {0,1} strings

SPS 4

0 2 4 6 8 10 12 14 16 2 4 6 8 10 12 14 16

Unsigned integers

Signed-magnitude

3 + 1 fixed-point, xxx.x

Signed fraction, .xxx

2’s-compl. fraction, x.xxx

2 + 2 floating-point, s 2

e in [2, 1], s in [0, 3]

2 + 2 logarithmic (log = xx.xx)

Number format

log x

s e e

fixed

point

floating

point

Some code for 4 bit number

Převzato z Barhani: Computer Arithmetic, Oxford 2012

Page 3: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

3

SPS 5

Fix point

Integer number can represent fractional number

integer part fraction

SPS 6

Operations with fix points

Multiply by 2: Left shift

Division by 2: Right shift

Log2N: Integer part - number of right shift while binary

number >= 1. Real part in range <1,2) by squares. If result

is >=2 then´1´ and divide by 2, otherwise -> ´0´ (http://en.wikipedia.org/wiki/Binary_logarithm)

Sine, Cosine, Log: Tables

Addition and subtractions are OK: mX mY=m(X Y) , where m is scale

Corrections are necessary after multiplication

or divisions

aX aY=a2 X Y ; aX/aY=X/Y

Page 4: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

4

SPS 7

Slide rule) Oughtred (1621) a Schickard (1623)

Logaritmus

2x2 2x1 2x3 2x5

1 2 3 4 5 6 7 8 9 10

=

=

=

=

[Picture from: Museum of HP calculators]

1.3 x 31.5 = 41 (exactly 40.95)

1.14 x 5.61 = 6.4 (exactly 6.3954]

SPS 8

Cylindrical and Spiral Slide Rules

Longer spiral scales increase accuracy, but there are mechanical limits.

Note: Logarithmic number system (LNS) for DSP (digital signal processing)

represents real numbers as logarithms, e.g. used in speech recognitions or

DNA sequencing, results can be more accurate than the floating-point.

There are theoretically no limits for their accuracy.

Page 5: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

5

SPS 9

Astrolabe

(cz:astroláb, -u, m.; astrolabium, -ia, s.)

150 BC Greek astronomer and

geographer Hipparchos z Bithynie

Programmable Ancient Computer

[Image source: http://www.waywarde.com/astronomy/ ]

Replaceable plates with geographic 'programs'

SPS 10

Concurrent Solver of Differential Equations

1. Differential equation x ’ ’ + 0 .5 x ’ + x = 4

2. Two integrators

x’’ x’ x

3. Adding element

x’’ 4

x 0.5 x’

x(t)=c1 e-0.25t sin(0.968246 t)+c2 e

-0.25t cos(0.968246 t)+4

6. Numeric solution of the equation

[ the numeric solution was computed by WolframAlpha ]

5. Result 10 Volts

a voltage

unit

4 Volts

x

0.5 x’

4. The solver of the equation

Page 6: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

6

SPS 11

Mechanical Solution of Integration

Vannevar Bush

Differential Analyzer

Variable-size friction wheels

to simulate the behavior

of differential equations

[Source: http://www.mit.edu/~klund/analyzer/]

video: http://www.mit.edu/~klund/analyzer/video3.mov

SPS 12

1943 Analog Computer Whirlwind from MIT

1980 Analog

Computer Dornet 80

inflexible

programming

by wires

designed as flight simulator for

training bomber crews based on

realistic aerodynamic system running

in real time, but it was inaccurate and

inflexible

Page 7: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

7

Computer Systems

Sequential/Concurrent

SPS 14

Digital computers -> Simple +/- tasks

Calculus (cz:kalkul) from Greek name for pebbles (cz: oblázek, valounek)

[ Photo: Martina Nicolls in Georgia (cz:Gruzie)]

Abacus, pl. abaci or abacuses (cz:abakus, abak, počítadlo)

the first appearance in Sumer,

2700-2300 BC

an adapted abacus

is used until today

Page 8: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

8

SPS 15

Blaise Pascal’s Pascaline (1645)

Simple +/- operations,

ie. improved abacus…

SPS 16

1944 Harvard Mark I Digital Calculator

Weight 4500 kg, built from 730000 parts,

800 km wires, 3300 relays and 2200

counter wheels.

Memory: 72 numbers with 23 decimal digits

plus sign

Operation Seconds

Addition 0.3

Subtraction 0.3

Multiplication 6.0

Division 11.4

Logarithm 68.4

sin(x) 60.0 [ Source: http://www.inf.fu-berlin.de/lehre/SS01/hc/eniac/ ]

Page 9: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

9

SPS 17

The First Computer Bug

Grace Murray Hopper found the first computer bug

Lab book!!

with numbered pages

for USA patents

beaten to death in the jaws of a relay of Mark I computer.

(moth = cz:mol, můra)

Logic function

Binary decision diagrams

Page 10: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

10

SPS

Decision tree

19

Y 1 0 1 1 0 0 0 0 1 1 1 1 1 0 1 1

B0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

A0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1

B1 0 1 0 1

A1 0 0 1 1

0 0 0 0 0 0

1 0 1 0 1 0

0 1 0 1 0 1

1 1 1 1 1 1

Vertex represents decision

Follow blue line for value 0 (False)

Follow red line for value 1 (True)

Function value determined by leaf value.

Linear time of evaluation, but many duplicities

0

B0

1

B0

A0

0

B0

0

B0

A0

B1

1

B0

1

B0

A0

0

B0

1

B0

1

A0

B1

A1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

A1 A0 >= B1 B0

1 1 0 0 1 1 1

SPS

Reduced Binary Decision Trees

20

F T

x

y y

T T

x + y

F F

x

y y

F T

x . y

F F

x

y y

T T

x

F F

x

y y

F F

False

F T

x

y

F T

x

y F T

x F

BDD – binary decision diagrams

Page 11: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

11

SPS

Variable Ordering

Assign arbitrary total ordering to variables

e.g., x1 < x2 < x3

Variables must appear in ascending order along

all paths OK Not OK

Properties

No conflicting variable assignments along path

Simplifies manipulation

x1

x2

x3

x1

x3

x3

x2

x1

x1

x1

[Source: Bryant: Symbolic Boolean Manipulation with OBDD] 21

SPS

Reduction Rule #1

Merge equivalent leaves

a a

x3 x3

x2

x3

0 1

x3

x2

x1

a

[Source: Bryant: Symbolic Boolean Manipulation with OBDD]

0 0

x 3

0 1

x 3

x 2

0 1

x 3

0 1

x 3

x 2

x 1

22

Page 12: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

12

SPS

Reduction Rule #2

y

x

z

x

Merge isomorphic nodes

x3 x3

x2

x3

0 1

x3

x2

x1

x3

x2

0 1

x3

x2

x1

y

x

z

x

y

x

z

x

[Source: Bryant: Symbolic Boolean Manipulation with OBDD] 23

SPS

Reduction Rule #3

x3

x2

0 1

x3

x2

x1

Eliminate Redundant Tests

y

x

y

x2

0 1

x3

x1

[Source: Bryant: Symbolic Boolean Manipulation with OBDD] 24

Page 13: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

13

SPS

Comparator 1/2

25

0

B0

1 1

B0

1

A0

0

B0

0 0

B0

0

A0

B1

1

B0

1 1

B0

1

A0

0

B0

1 1

B0

1

A0

B1

A1

A1 A0 >= B1 B0

0

B0

1

A0

B1 B1

A1

A1 A0 >= B1 B0

1 0

1

0

B0

1

A0

1

Rule #3

SPS

Comparator 2/2

26

0

B0

1

A0

B1 B1

A1

1 0

1

0

B0

1

A0

1

0

B0

1

A0

B1 B1

A1

1 0

1

0

B0

1

A0

B1 B1

A1

BDD

Rule #1

Page 14: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

14

SPS

Effect of Variable Ordering

Good Ordering Bad Ordering

Linear Growth

0

b3

a3

b2

a2

1

b1

a1

Exponential Growth

a3 a3

a2

b1 b1

a3

b2

b1

0

b3

b2

1

b1

a3

a2

a1

a1.b1+a2.b2+a3.b3

27

SPS

Good ordering in 2 bit Comparator

Y= A0.B1'+B1'B0'+A1.B1'+A1.A0+A1.B0'

Good order according to the fastest reduction according to influence to the

result A1, B1, A0, B0

A1 cofactors are nearly minimal

A1'.(A0.B1'+B1'.B0')+A1.(A0.B1'+B1'.B0'+B1'+A0+B0' ) =

A1'.B1'.(A0+B0')+A1.(B1'+A0+B0' ) {+the absorption}

B1 cofactor no further reduction, the same for A0, B0

A1'.B1'.(A0+B0')+A1.(B1'+B1(A0+B0' ))

= A1'.B1'.(A0+B0')+A1.B1'+A1.B1(A0+B0' ))

Common leafs

= (A1'.B1'+A1.B1). (A0+B0')+A1.B1'

= (A1≡B1).(A0>=B0)+(A1>B1)

28

1

0

3

2

A1 B1

A0+B0'

0

1

A0

B0 A1 A0 >= B1 B0

Page 15: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

15

SPS

Bad ordering in Comparator

Y= A0.B1'+B1'B0'+A1.B1'+A1.A0+A1.B0'

Bad order: B0, A0, B1, A1

Cofactors B0 – no reduction, but more complex functions

B0'.(A0.B1'+B1'+A1.B1'+A1.A0+A1)+B0.(A0.B1'+A1.B1'+A1.A0)

Cofactors A0 – minimum reduction

B0'.(A0'.(B1'+A1.B1'+A1)+A0. (B1'+A1.B1'+A1))

+B0.(A0'.(A1.B1'))+ A0.(B1'+A1.B1'+A1)) { by the absorption law }

=B0'.(A0'.(B1'+A1)+A0. (B1'+A1))+B0.(A0'.(A1.B1'))+ A0.(B1'+A1))

=B0'.(B1'+A1)+B0.(A0'.(A1.B1'))+ A0.(B1'+A1))

Cofactors B1 – no reduction

B0'.( (B1'+A1))+B0.(A0'.(B1'.A1))+ A0.((B1'+A1)))

Cofactors A1 – no reduction -> a complex result

B0'.( B1'+A1)+B0.(A0'.B1'.A1)+ A0.(B1'+A1)) =

= (B0'+ A0).( B1'+A1)+B0.A0'.B1'.A1

29

SPS

Advantages of BDDs

Powerful Operations

Many free tools and libraries ready for usage,

e.g. JavaBDD, Buddy, Biddy

Each step has polynomial complexity

Generally Stay Small Enough

Especially for digital circuit applications

Verification of programs

Weak Competition

No other method comes close in overall strength,

BDD are really fundamental data structures in computer

science -

30

Page 16: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

16

SPS

Drawbacks of BDDs

Doesn’t Solve All Problems

Some problems just too big,

e.g. logic functions of multipliers

No good search capabilities

Variable ordering

Critical effect on efficiency

The problem of finding the best variable ordering

is NP-hard.

Insights into problem characteristics are required.

Working around limitations

31

Mealy and Moore

Finite State Machines/Automata

Flip

Flops

Output

Combinatorial

Logic

Next State

Combinatorial

Logic

Mealy Machine

Inputs Outputs

Flip

Flops

Output

Combinatorial

Logic

Next State

Combinatorial

Logic

Moore Machine

Inputs Outputs

32

Page 17: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

17

State Machines...

),(

),(

uxhy

uxfx

dt

d x is the state vector (length n)

u is an input vector (length m)

y is the output vector (length p)

Nonlinear

DuCxy

BuAxx

dt

dLinear

])[],[(][

])[],[(]1[

kkk

kkk

uxhy

uxfx

Discrete

A is n n , B is n m

C is p n , D is p m

Vectors are now at

discrete time steps.

33

Finite State Machines

• Any Circuit with Memory Is a Finite State

Machine

– Even computers can be viewed as huge FSMs

• Design of FSMs Involves

– Defining states

– Defining transitions between states

– Optimization / minimization

• Above Approach Is Practical for Small

FSMs Only 34

Page 18: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

18

SPS 35

Concept of State Machine Example: Positive Edge Triggered Synchronous System

On rising edge, inputs sampled outputs, next state computed After propagation delay, outputs and next state become stable Immediate Outputs: affect datapath immediately could cause inputs from datapath to change Delayed Outputs: take effect on next clock edge propagation delays must exceed hold times

Outputs

State T ime

Clock

Inputs

SPS 36

Cz:Definice konečného automatu

FSM – Finite State Machine

δ - přechodová funkce - zobrazení δ: X x S -> S

ω - výstupní funkce - zobrazení ω:

ω: S -> Z (Moore)

ω: X x S -> Z (Mealy)

X - konečná množina všech vstupních vektorů

Z - konečná množina všech výstupních vektorů

S - konečná množina všech vnitřních stavů

Uspořádaná šestice

M = < X, S, Z, ω, δ, s0 S >

s0 - počáteční stav S0 S

Page 19: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

19

37

Definition of finite state machine (automaton)

FSM – Finite State Machine

δ – transient function- mapping δ: X x S -> S

ω – output function - mapping ω:

ω: S -> Z (Moore)

ω: X x S -> Z (Mealy)

X – finite set of all input vectors

Z - finite set of all output vectors

S - finite set of all inner states

Ordered tuple

M = < X, S, Z, ω, δ, s0 >

s0 – initial state S0 S

Moore and Mealy Machines • Both these machine types follow the basic characteristics

of state machines, but differ in the way that outputs are

produced.

• Moore Machine:

– Outputs are independent of the inputs, ie outputs are

effectively produced from within the state of the state

machine.

• Mealy Machine:

– Outputs can be determined by the present state alone, or

by the present state and the present inputs, ie outputs

are produced as the machine makes a transition from

one state to another. 38

Page 20: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

20

Moore Machine Diagrams

State 2

x,y

State 1

q,r a,b

i,j

Input condition that

must exist in order

to execute these

transitions from

State 1

Output condition that

results from being in

a particular present

state

The Moore State Machine

output is shown inside the

state bubble, because the

output remains the same as

long as the state machine

remains in that state.

The output can be arbitrarily

complex but must be the

same every time the

machine enters that

state.

39

Mealy Machine Diagrams

State 2

State 1 a,b q,r

i,j x,y

Input condition that

must exist in order

to execute these

transitions from

State 1

Output condition that

results from being in

a particular present

state

The Mealy State Machine

generates outputs based on:

The Present State, and

The Inputs to the M/c.

So, it is capable of generating

many different patterns of output

signals for the same state,

depending on the inputs present

on the clock cycle.

Outputs are shown on transitions

since they are determined in the

same way as is the next state.

40

Page 21: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

21

Moore Machine • Outputs depend on states only

state 1 /

output 1

state 2 /

output 2

transition

condition 1

transition

condition 2

41

Mealy Machine

• Outputs depend on states and inputs

state 1 state 2

transition condition 1-1 /

output 1-1

transition condition 2-1 /

output 2-1

transition condition 2-2 /

output 2-2

transition condition n /

output 1-n

42

Page 22: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

22

Moore FSM - Example 1 • Moore FSM that Recognizes Sequence 10

S0 / 0 S1 / 0 S2 / 1

0 0

0

1

1 1

reset

Meaning

of states:

S0: No

elements

of the

sequence

observed

S1: “1”

observed

S1: “10”

observed

43

Mealy FSM - Example 1

• Mealy FSM that Recognizes Sequence 10

S0 S1

0 / 0 1 / 0 1 / 0

0 / 1 reset

Meaning

of states:

S0: No

elements

of the

sequence

observed

S1: “1”

observed

44

Page 23: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

23

Moore & Mealy FSMs –

Example 1

clock

input

Moore

Mealy

0 1 0 0 0

S0 S1 S2 S0 S0

S0 S1 S0 S0 S0

45

SPS 46

Coding of FSM states

Page 24: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

24

SPS 47

4 types of transitions

1. Stable state

d(xi,sj) = sj

2. Simple transition

3. Cycle - across unstable states

4. Oscilation

Order of FSM - the number of

unstable state in the longest

transition

SPS 48

Description of FSM

1.Table - 2 parts- next state ( d ), outputs( w )

columns - inputs X

rows - states S

intersection of row and column - next state and output

MEALY

.

d w

MOORE

d w

stable state sj = d(xi,sj) - marked by circle unstable state sk = d(xi,sj) sk <> sj

Page 25: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

25

SPS 49

nodes - states - marked as sj oriented edges - transitions xi / w (xi,sj) Mealy input / output

Oriented Graph

Example:

If signal A went to '1' before signal B then set Y to 1

50

A

B

Y1

Y2

t

Y

No

required

A customer

requires Y

Q: Is the task defined unambiguously? - A: No!

Page 26: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

26

51

Graph

52

Page 27: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

27

SPS 53

Equivalent States

Two states of an FSM are equivalent (or

indistinguishable) if for each input they produce the

same output and their next states are identical.

Si

Sj

Sm

Sn

1/0

1/0

0/0

0/0

Si,j

Sm

Sn

1/0

0/0

Si and Sj are equivalent and

merged into a single state.

54

Minimization of States

Equivalent

states

Page 28: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

28

55

Languages and FSM

The Tower of Babel, Pieter Brueghel, c. 1563, Kunsthistorisches Museum, Vienna 55

56

Languages

Alphabet – finite character set (S)

String – finite sequence of characters –

can be e, the empty string (Some texts use

l as the empty string)

Language – possibly infinite set of strings

over some alphabet – can be { }, the empty

language.

Page 29: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

29

57

Examples of Languages

Let be given alphabet S = {a, b, c},

than we may create languages:

{aa,ab,ac,bb,bc,cc}

{ab,abc,abcc,abccc,. . .}

{ e }

{ }

{a, b, c, e}

58

Regular Expressions

Formally describe tokens in the language

Regular Expressions

NFA

DFA

Regular Expressions finite automata

Java, PHP, Python, C# and other

implements regular expressions by NFA.

Page 30: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

30

59

Examples DFA

}baL n1

}baL 2

a

b

ab

1M

2M

60

Union

1M

2M

21 LL

Page 31: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

31

61

Example

a

b

ab

}baL n1

}baL 2

} }abbaLL n ,21

62

Concatenation

21LL

1M 2M

Page 32: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

32

63

Example

a

b ab

}baL n1 }baL 2

} } }bbaababaLL nn 21

64

Operation * (Kleene star)

*1L

1M

Page 33: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

33

65

Example *

}*1* baL n

a

b

}baL n1

66

Complement of language

a

b ba,

ba,

}baL n

a

b ba,

ba,

}baL n

Fneg = S-F , by words: we invert end states

Page 34: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

34

Example

from deterministic

to nondeterministic

automaton

67

68

Safety Lock with Binary Key

Opened when 0011, otherwise not.

Generate A (Accepted) in Q5 state

Such automaton is called Finite State Acceptor or Acceptor

Finite State Machine.

0

Q1

1

Q1

Q2

Q3

Q4 A

1 0 0

QE

1 1 0

0,1 Accepted Start

Page 35: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

35

69

Code Safety Lock 2

Input must begin by 00 and terminate by 11

1 0

0 0

0

1 A B C D E

70

Code Safety Lock 2 cont'd

NFA – Nondeterministic Finite Automat/Acceptor

DFA – Deterministic Finite Automat/Acceptor

0 1

0 0

0

1 A B C D E

0, 1

0 0 1 1

Nondeterministic

transition

A B C D E

The both automata

accept the same inputs!

Page 36: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

36

71

NFA- animation 1/2

NFA – Nondeterministic Finite Automaton/Acceptor

0, 1

0 0 1 1

Non-deterministic

transition

A B C D E

0 0 1 0 1 1

backtracking

stack stack

72

NFA – animation 2/2

NFA – Nondeterministic Finite Automat/Acceptor

0, 1

0 0 1 1

Non-deterministic

transition

A B C D E

0 0 1 0 1 1

stack

Accepted

Page 37: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

37

Note

There are other more complex methods for

processing NFA that do not use backtracking

and can operate in linear time, e.g. Thompson

NFA, see

http://swtch.com/~rsc/regexp/regexp1.html

73

74

Non-deterministic behavior is not random

Deterministic:

f(1) → 1 always

Náhodné:

f(1) → 1 in 50% cases,

f(1) → 2 otherwise

Non-deterministic behavior

f(1) → 1 or f(1) → 2,

we do not tell when it happens.

Non-deterministic behavior can look as

deterministic, random or much worse...

Page 38: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

38

75

Finite acceptor

δ - mapping

for DFA -mapping δ: X x S -> S

for NFA – mapping δ: {X + ε} x S -> S,

where ε is random input

X – finite set of input vectors

S – finite set of states

A tuple

M = < X, S, δ, s0, F >

s0 – initial state s0 S

F – finite set of accepted states F S

76

DFA versus NFA

NFA can be built faster, but they are more

complex for usage, they require

backtracking.

NFA can be always converted in DFA, but

corresponding DFA can have exponential

explosion of its states.

Page 39: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

39

77

Any sequence of a b that terminates by a b

A B C a b

a,b

A A,B a b

b

A,C

a b

a

NFA

DFA

NFA versus DFA

78

Regular Expresions

http://www.dotnetcoders.com/web/Learning/Regex/default.aspx

Page 40: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

40

79

Regular Expressions

Formally describe tokens in the language

Regular Expressions

NFA

DFA

Regular Expressions finite automata

Java, PHP, Python, C# and other

implements regular expressions by NFA.

80

Example: Building regular expression 1/5

a

1 2 3 4 5

6 7

d b c

d 0

d a

d b b

c

1 2 3 4 5

6 7

d [abc] d 0

d a

d b [bc]

[abc] – on character from [ ] list.

a or b or c.

Page 41: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

41

81

Example: Building regular expression 2/5

1 2 3 4 5

6 7

d [abc] d 0

d a

d b [bc]

3 4 5

d[abc]d d 0

a

b[bc]d

82

Example: Building regular expression 3/5

3 4 5

d[abc]d d 0

a

b[bc]d

b(b|c)da

3 4 5

d[abc]d d 0

a

b[bc]da

Page 42: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

42

83

Example: Building regular expression 4/5

3 4 5

d[abc]d d 0

a

b[bc]da

3 4 5

d[abc]d 0

a (b[bc]da)*d

84

Example: Building regular expression 5/5

5 0

d[abc]da(b[bc]da)*d

3 4 5

d[abc]d 0

a (b[bc]da)*d

* - 0 or more repetition of ( ).

(b[bc]da)

Page 43: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

43

85

Non-Regular Languages

Non-regular languages cannot be

described using REs, NFAs and

DFAs. Example:

language W = {anbn | n>0} Note: More advance features of modern regular expression

libraries far exceeds the regular languages and they can

recognize also some non-regular languages, e.g if an regular

expression use back references. So any regular language can

be recognize by regular expressions, but modern regular

expressions can recognize bigger set than regular languages.

86

Character classes

. Matches any character except \n. E.g.. a.a matches aea, aia, aca, and a a

[XY] Matches any single character included in the specified set of characters

[A-Z] Use of a hyphen (–) allows specification of contiguous character ranges.

[A-Za-z] form A to Z, from a to z

[^AB] Matches any single character not in the specified set of characters.

Page 44: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

44

87

Metacharacters

Some "character-class" metacharacters

\w is equivalent to [a-zA-Z_0-9]

\W is equivalent to [^a-zA-Z_0-9].

\s Matches any white-space character. [ \f\n\r\t\v].

\S Matches any non-white-space character. [^ \f\n\r\t\v].

\d Matches any decimal digit. [0-9].

\D Matches any nondigit.

Example \d\d\d\s matches: "123 456"

88

Example Metacharacters

Regex rx1 = new Regex(@"\d\d\d\s");

bool b1 = rx1.IsMatch("123 456"); // true

bool b2 = rx1.IsMatch(" 123 456"); // true

bool b3 = rx1.IsMatch(" 123 456"); // true

bool b4 = rx1.IsMatch(" 123456"); // false

bool b5 = rx1.IsMatch("a123 456"); // true

Regex rx2 = new Regex(@"^\d\d\d\s");

bool b6 = rx2.IsMatch("a123 456"); // false

Page 45: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

45

89

Modificators

| Matches any one of the terms separated

by the | (vertical bar) character; for

example, cat|dog|tiger. The leftmost

successful match wins.

\ next char is literal

^ from beginning

$ at the end of text

"^list" matches "listing", but not "A list"

"data$" matches "subdata", but not "data1"

90

Quantifiers

* Specifies zero or more matches

+ Specifies one or more matches

? Specifies zero or one matches

{n} Specifies exactly n matches

{n,} Specifies at least n matches

{n,m} Specifies at least n, but no more than m, matches

() Grouping

Page 46: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

46

91

What is the correct answer?

92

Example of Java program

import java.util.regex.Matcher;

import java.util.regex.Pattern; public class DateMatcher { public DateMatcher() {

String aDate = "date: 12-15-2003";

Pattern datePattern = Pattern.compile(

"date: (\\d{2})-(\\d{2})-(\\d{4})");

Matcher dateMatcher = datePattern.matcher(aDate);

if (dateMatcher.find()) { System.out.println("Month is: " + dateMatcher.group(1)); System.out.println("Day is: " + dateMatcher.group(2)); System.out.println("Year is: " + dateMatcher.group(3)); } } public static void main(String[] args) { new DateMatcher(); } }

Page 47: Struktury počítačových systémůdcenet.felk.cvut.cz/edu/fpga/lectures/Eng2014pr12_Applications.pdf · ¶V-compl. fraction, x.xxx 2 + 2 floating-point, s u 2 e in [ 2, 1], s in

15.12.2014

47

93

MS-Word knows subset of regular expressions

Regulární

výrazy se

skrývají pod

zástupnými

znaky

Nabídku

operátorů

najdete pod

Speciální