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 and Caching CS 230 CPU Instructions and Pipelining Binary Signals & Number Representation Finished! Logic Gates Transistors and Electrical Properties CS 230 - Spring 2020 0-2
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-3
Classical Tool Chain Executable or Library Source Code Assembly Code Object File .text beq $2, $0, 1 if a: 0001000000 0001010000 addi $3, $3, 1 b += 1 0010000000 0010000000 .data Compiler Assembler Linker (binasm) Libraries can be created or Loader imported by the linker Main Memory CS 230 - Spring 2020 4-4
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-5
Compiler Source Code Assembly Code beq $2, $0, 1 if a: addi $3, $3, 1 b += 1 Program translation from source language to target language (usually assembly code) Typically followed by assembler to generate machine code CS 230 - Spring 2020 4-6
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-7
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-8
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-9
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-10
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-11
DFA Example 1 a b ab Lets consider the input string: Begin in the start state CS 230 - Spring 2020 4-12
DFA Example 1 a b ab 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 CS 230 - Spring 2020 4-13
DFA Example 1 a b ab 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 CS 230 - Spring 2020 4-14
DFA Example 1 a b ab 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 CS 230 - Spring 2020 4-15
DFA Example 1 a b ab 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 CS 230 - Spring 2020 4-16
DFA Example 2 b d a a c Lets consider the input string: aac CS 230 - Spring 2020 4-17
DFA Example 2 b d a a c Lets consider the input string: aac CS 230 - Spring 2020 4-18
DFA Example 2 b d a a c Lets consider the input string: aac CS 230 - Spring 2020 4-19
DFA Example 2 b d a a c aac Lets consider the input string: We say the DFA accepts the string CS 230 - Spring 2020 4-20
DFA Example 2 b d a a c Now lets consider the input string: abc CS 230 - Spring 2020 4-21
DFA Example 2 b d a a c Now lets consider the input string: abc CS 230 - Spring 2020 4-22
DFA Example 2 b d a a c abc Now lets consider the input string: We’re stuck! We say the DFA rejects the string CS 230 - Spring 2020 4-23
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: CS 230 - Spring 2020 4-24
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: CS 230 - Spring 2020 4-25
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: CS 230 - Spring 2020 4-26
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: CS 230 - Spring 2020 4-27
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: CS 230 - Spring 2020 4-28
DFA Try it Yourself q d a q m p,l,r amqd Does this DFA accept the string: Yes! DFA accepts the string CS 230 - Spring 2020 4-29
DFA Try it Yourself q d a q m p,l,r amlr Does this DFA accept the string: CS 230 - Spring 2020 4-30
DFA Try it Yourself q d a q m p,l,r amlr Does this DFA accept the string: CS 230 - Spring 2020 4-31
DFA Try it Yourself q d a q m p,l,r amlr Does this DFA accept the string: CS 230 - Spring 2020 4-32
DFA Try it Yourself q d a q m p,l,r amlr Does this DFA accept the string: CS 230 - Spring 2020 4-33
DFA Try it Yourself q d a q m p,l,r amlr Does this DFA accept the string: We’re stuck! The DFA rejects the string CS 230 - Spring 2020 4-34
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-35
DFA From Language Example Draw a DFA for the language of all natural numbers. 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. 0 CS 230 - Spring 2020 4-38
DFA From Language Example Draw a DFA for the language of all natural numbers. 0 1…9 CS 230 - Spring 2020 4-39
DFA From Language Example Draw a DFA for the language of all natural numbers. 0 1…9 0 …9 CS 230 - Spring 2020 4-40
Try it Yourself Draw a DFA for the language of all combinations of 3 or more lowercase English letters that start with lowercase b. CS 230 - Spring 2020 4-41
Try it Yourself Draw a DFA for the language of all combinations of 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 of 3 or more lowercase English letters that start with lowercase b. b CS 230 - Spring 2020 4-43
Try it Yourself Draw a DFA for the language of all combinations of 3 or more lowercase English letters that start with lowercase b. a…z b CS 230 - Spring 2020 4-44
Try it Yourself Draw a DFA for the language of all combinations of 3 or more lowercase English letters that start with lowercase b. a…z a…z b CS 230 - Spring 2020 4-45
Try it Yourself Draw a DFA for the language of all combinations of 3 or more lowercase English letters that start with lowercase b. a…z a…z b a…z CS 230 - Spring 2020 4-46
Recommend
More recommend