CS 230 - Spring 2020 4-1
Computer Systems Lecture 18 Tool Chain and DFAs CS 230 - Spring - - PowerPoint PPT Presentation
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
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!
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.
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
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
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
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
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.
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
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
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
CS 230 - Spring 2020 4-12
DFA Example 1
a b
Lets consider the input string:
Begin in the start state
ab
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
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
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
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
CS 230 - Spring 2020 4-17
DFA Example 2
a b d a c
Lets consider the input string: aac
CS 230 - Spring 2020 4-18
DFA Example 2
a b d a c
Lets consider the input string: aac
CS 230 - Spring 2020 4-19
DFA Example 2
a b d a c
Lets consider the input string: aac
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
CS 230 - Spring 2020 4-21
DFA Example 2
a b d a c
Now lets consider the input string: abc
CS 230 - Spring 2020 4-22
DFA Example 2
a b d a c
Now lets consider the input string: abc
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
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
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
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
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
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
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
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
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
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
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
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
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
CS 230 - Spring 2020 4-36
DFA From Language Example
Draw a DFA for the language of all natural numbers.
CS 230 - Spring 2020 4-37
DFA From Language Example
Draw a DFA for the language of all natural numbers.
CS 230 - Spring 2020 4-38
DFA From Language Example
Draw a DFA for the language of all natural numbers.
CS 230 - Spring 2020 4-39
DFA From Language Example
Draw a DFA for the language of all natural numbers.
1…9
CS 230 - Spring 2020 4-40
DFA From Language Example
Draw a DFA for the language of all natural numbers.
1…9 0…9
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.
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.
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
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
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
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