Computer Systems Lecture 18 Tool Chain and DFAs CS 230 - Spring - - PowerPoint PPT Presentation

computer systems
SMART_READER_LITE
LIVE PREVIEW

Computer Systems Lecture 18 Tool Chain and DFAs CS 230 - Spring - - PowerPoint PPT Presentation

CS 230 Introduction to Computers and Computer Systems Lecture 18 Tool Chain and DFAs CS 230 - Spring 2020 4-1 System Layers Python/C/Racket Code Multiprocessing and Operating Systems Build and Runtime Here now! Environments Memory


slide-1
SLIDE 1

CS 230 - Spring 2020 4-1

CS 230 – Introduction to Computers and Computer Systems Lecture 18 – Tool Chain and DFAs

slide-2
SLIDE 2

System Layers

CS 230 - Spring 2020 0-2

Transistors and Electrical Properties Logic Gates Binary Signals & Number Representation CPU Instructions and Pipelining Memory and Caching Build and Runtime Environments Multiprocessing and Operating Systems Python/C/Racket Code CS 230 Finished! Here now!

slide-3
SLIDE 3

CS 230 - Spring 2020 4-3

Classical Tool Chain

 Compiler translates high level language into

assembly program.

 Assembler translates assembly program into

machine code in object file.

 Linker combines multiple object files of machine

code into program file.

 Loader loads program file into main memory.  Library is special object file that can be added

to program file during linking or loading.

slide-4
SLIDE 4

CS 230 - Spring 2020 4-4

Classical Tool Chain

if a: b += 1 beq $2, $0, 1 addi $3, $3, 1

Compiler

0001000000 0010000000

Assembler (binasm)

.text 0001010000 0010000000 .data

Linker Source Code Assembly Code Object File Executable or Library Loader Main Memory

Libraries can be created or imported by the linker

slide-5
SLIDE 5

CS 230 - Spring 2020 4-5

Other Execution Approaches

 Interpretation

 execute source code directly  execute binary code by software

 Byte Code

 compile to intermediate binary representation

 Just-In-Time Compilation

 compile during runtime

slide-6
SLIDE 6

CS 230 - Spring 2020 4-6

Compiler

 Program translation

 from source language  to target language (usually assembly code)

 Typically followed by assembler

 to generate machine code

beq $2, $0, 1 addi $3, $3, 1

Source Code Assembly Code

if a: b += 1

slide-7
SLIDE 7

CS 230 - Spring 2020 4-7

Basic Compilation Steps

 Scanning

 also called Lexical Analysis  source code to token sequence

 Syntax analysis

 token sequence to parse tree

 Semantic analysis

 use parse tree to generate a symbol table  type check the parse tree against the symbol table

 Code generation

 parse tree and symbol table to target language

slide-8
SLIDE 8

CS 230 - Spring 2020 4-8

Scanner / Tokenizer

 Also called a “Tokenizer”  Convert program text into stream of tokens  Types of tokens (sample):

 keyword – 'for', 'while', etc.  operator – '+', '&&', etc.  constant – '1000', '3.5', etc.  delimiter – ':', etc.  variable name – 'minpos', etc.  subroutine name – 'power2', etc.

slide-9
SLIDE 9

CS 230 - Spring 2020 4-9

Background

 Formal languages  Regular languages  Well-studied in theoretical computer science

 how to determine if a string is in the language?

 deterministic finite automata (DFA)  non-deterministic finite automata (NFA)  regular expressions

 Standard algorithms exist

slide-10
SLIDE 10

CS 230 - Spring 2020 4-10

Deterministic Finite Automata (DFA)

 Also known as a deterministic finite state

machine (FSM)

 Comprised of

 A finite set of states

 Includes exactly one start state  Includes at least one final (also called accept) state(s)

 A finite set of input symbols known as the alphabet  A finite set of transitions from one state to another

based on the input

 Can determine if input is accepted or rejected

slide-11
SLIDE 11

CS 230 - Spring 2020 4-11

DFA Example 1

a b

 Start state has arrow from nowhere  Final (accept) states are double circles  Notice the start state is also an accept state

in this particular DFA

 Transitions are marked with the input(s) they

consume

slide-12
SLIDE 12

CS 230 - Spring 2020 4-12

DFA Example 1

a b

Lets consider the input string:

 Begin in the start state

ab

slide-13
SLIDE 13

CS 230 - Spring 2020 4-13

DFA Example 1

a b

Lets consider the input string:

 Begin in the start state  Place our marker at the start of the string  Look at what transitions (arrows) are available

ab

slide-14
SLIDE 14

CS 230 - Spring 2020 4-14

DFA Example 1

a b

Lets consider the input string:

 We take the a transition because a is the first

character after our marker

 Consume the a character and move to the state

at the end of the a transition

 Look at what transitions are available

ab

slide-15
SLIDE 15

CS 230 - Spring 2020 4-15

DFA Example 1

a b

Lets consider the input string:

 We take the b transition because b is the first

character after our marker

 Consume the b character and move to the state

at the end of the a transition

 Look at what transitions are available

ab

slide-16
SLIDE 16

CS 230 - Spring 2020 4-16

DFA Example 1

a b

Lets consider the input string:

 Our marker is at the end of the string  No transitions are available  Check if we are in an accept state  We say this DFA accepts the string ab

ab

slide-17
SLIDE 17

CS 230 - Spring 2020 4-17

DFA Example 2

a b d a c

Lets consider the input string: aac

slide-18
SLIDE 18

CS 230 - Spring 2020 4-18

DFA Example 2

a b d a c

Lets consider the input string: aac

slide-19
SLIDE 19

CS 230 - Spring 2020 4-19

DFA Example 2

a b d a c

Lets consider the input string: aac

slide-20
SLIDE 20

CS 230 - Spring 2020 4-20

DFA Example 2

a b d a c

Lets consider the input string: We say the DFA accepts the string

aac

slide-21
SLIDE 21

CS 230 - Spring 2020 4-21

DFA Example 2

a b d a c

Now lets consider the input string: abc

slide-22
SLIDE 22

CS 230 - Spring 2020 4-22

DFA Example 2

a b d a c

Now lets consider the input string: abc

slide-23
SLIDE 23

CS 230 - Spring 2020 4-23

DFA Example 2

a b d a c

Now lets consider the input string: We’re stuck! We say the DFA rejects the string

abc

slide-24
SLIDE 24

CS 230 - Spring 2020 4-24

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amqd

m q

slide-25
SLIDE 25

CS 230 - Spring 2020 4-25

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amqd

m q

slide-26
SLIDE 26

CS 230 - Spring 2020 4-26

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amqd

m q

slide-27
SLIDE 27

CS 230 - Spring 2020 4-27

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amqd

m q

slide-28
SLIDE 28

CS 230 - Spring 2020 4-28

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amqd

m q

slide-29
SLIDE 29

CS 230 - Spring 2020 4-29

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string: Yes! DFA accepts the string

amqd

m q

slide-30
SLIDE 30

CS 230 - Spring 2020 4-30

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amlr

m q

slide-31
SLIDE 31

CS 230 - Spring 2020 4-31

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amlr

m q

slide-32
SLIDE 32

CS 230 - Spring 2020 4-32

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amlr

m q

slide-33
SLIDE 33

CS 230 - Spring 2020 4-33

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string:

amlr

m q

slide-34
SLIDE 34

CS 230 - Spring 2020 4-34

DFA Try it Yourself

a q d p,l,r

Does this DFA accept the string: We’re stuck! The DFA rejects the string

amlr

m q

slide-35
SLIDE 35

CS 230 - Spring 2020 4-35

DFA From Languages

 The set of strings accepted by a DFA is called

the language accepted by the DFA.

 DFAs can be used to detect regular languages

 Design DFA to recognize a regular language

 used as part of lexical analysis step of compilation  also for input validation  “is this a valid phone number?”

 Important DFA restriction: in a given state, there

can be at most one choice for a given character

slide-36
SLIDE 36

CS 230 - Spring 2020 4-36

DFA From Language Example

Draw a DFA for the language of all natural numbers.

slide-37
SLIDE 37

CS 230 - Spring 2020 4-37

DFA From Language Example

Draw a DFA for the language of all natural numbers.

slide-38
SLIDE 38

CS 230 - Spring 2020 4-38

DFA From Language Example

Draw a DFA for the language of all natural numbers.

slide-39
SLIDE 39

CS 230 - Spring 2020 4-39

DFA From Language Example

Draw a DFA for the language of all natural numbers.

1…9

slide-40
SLIDE 40

CS 230 - Spring 2020 4-40

DFA From Language Example

Draw a DFA for the language of all natural numbers.

1…9 0…9

slide-41
SLIDE 41

CS 230 - Spring 2020 4-41

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

slide-42
SLIDE 42

CS 230 - Spring 2020 4-42

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

slide-43
SLIDE 43

CS 230 - Spring 2020 4-43

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

b

slide-44
SLIDE 44

CS 230 - Spring 2020 4-44

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

b a…z

slide-45
SLIDE 45

CS 230 - Spring 2020 4-45

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

b a…z a…z

slide-46
SLIDE 46

CS 230 - Spring 2020 4-46

Try it Yourself

Draw a DFA for the language of all combinations

  • f 3 or more lowercase English letters that start

with lowercase b.

b a…z a…z a…z