DOC PREVIEW
Berkeley COMPSCI 164 - Lecture 3: Finite Automata

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 3: Finite AutomataClassical Pattern-Matching ImplementationReview: FA operationExample: What does this DFA recognize?Example: What does this NFA recognize?Example: What does this NFA recognize?Review: Classical Regular Expressions to NFAs (I)Review: Classical Regular Expressions to NFAs (II)Extensions?Example of ConversionAbstract Implementation of NFAsReview: Converting to DFAsDFAs as ProgramsWhat Flex DoesHow Do They Do It?Lecture 3: Finite AutomataAdministrivia• Log into your class account ASAP (I still have account forms).• Start forming teams:– Choose team name (letters, digits, underscores only, starting withcapital letter)– Email me ([email protected]) name of team, and classlogins of members (also mail changes).• Good time to start learning Python (manuals online).• Readings for next time: Subversion manual, Course Notes §2.1–2.7.• Don’t forget homework due Friday.Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 1Classical Pattern-Matching Implementation• For compilers, can generally make do with “classical” regular expres-sions.• Implementable usingfinite(-state) automataorFAs.(“Finite state”= “finite memory”).• Classical construction:regular expression ⇒ nondeterministic FA (NFA)⇒ deterministic FA (DFA) ⇒ table-driven program.Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 2Review: FA operation• A FA is a graph whose nodes arestates (of memory) and whose edgesarestate transitions. There are a finite number of nodes.• One state is the designated start state.• Some subset of the nodes are final states.• Each transition is labeled with a set of symbols (characters, etc.) orǫ.• A FArecognizes a string c1c2· · · cnif there is a path (sequence ofedges) from the start state to a final state such that the labelsof the edges in sequence, aside from ǫ edges, respectively containc1, c2, . . . , cn.• If the edges leaving any node have disjoint sets of characters andif there are no ǫ nodes, FA is a DFA, else an NFA.Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 3Example: What does this DFA recognize?1 1 1 1 110 0 0 0 0 0What is the simplest equivalent NFA you can think of?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 4Example: What does this NFA recognize?A B C D A B D[A-Z]What is the simplest equivalent DFA you can think of?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 5Example: What does this NFA recognize?X Y[XY] Z[XY]ǫǫǫWhat is the simplest equivalent DFA you can think of?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 6Review: Classical Regular Expressions to NFAs (I)ǫaaR1R2R1R2Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 7Review: Classical Regular Expressions to NFAs (II)R1| R2R1R2ǫǫǫǫR∗RǫǫLast modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 8Extensions?• How would you translate φ (the empty language, containing no strings)into an FA?• How could you translate ‘R?’ into an NFA?• How could you translate ‘R+’ into an NFA?• How could you translate ‘R1|R2| · · · |Rn’ into an NFA?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 9Example of ConversionHow would you translate ((ab)*|c)* into an NFA?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 10Abstract Implementation of NFAs1X2Y34[XY]5Z6[XY]ǫ0ǫǫ1X2Y34[XY]5Z6[XY]ǫ0ǫǫ1X2Y34[XY]5Z6[XY]ǫ0ǫǫ1X2Y34[XY]5Z6[XY]ǫ0ǫǫ1X2Y34[XY]5Z6[XY]ǫ0ǫǫString: XYYZLast modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 11Review: Converting to DFAs• OBSERVATION: Theset of states that are marked (colored red)changes with each character in a way that depends only on the setand the character.• In other words, machine on previous slide acted like this DFA:014X25Y355Z6[XY]Y X [XY] ZZLast modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 12DFAs as Programs• Can realize DFA in program with control structure:state = INITIAL;for (s = input; *s != ’\0’; s += 1) {switch (state):case INITIAL:if (*s == ’a’) state = A_STATE; break;case A_STATE:if (*s == ’b’) state = B_STATE; else state = INITIAL; break;...}}return state == FINAL1 || state == FINAL2;• Or with data structure (table driven):state = INITIAL;for (s = input; *s != ’\0’; s += 1)state = transition[state][s];return isfinal[state];Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 13What Flex Does• Flex program specification is giant regular expression of the formR1|R2| · · · |Rn, where none of the Rimatch ǫ.• Each final state labeled with some action.• Converted, by previous methods, into a table-driven DFA.• But, this particular DFA is used to recognizeprefixesof the (re-maining) input: initial portions that put machine in a final state.• Which final state(s) we end up in determine action. To deal withmultiple actions:– Matchlongestprefix (“maximum munch”).– If there are multiple matches, applyfirstrule in order.Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3 14How Do They Do It?• How can we use a DFA to recognize longest match?• How can we use DFA to act on first of equal-length matches?• How can we use a DFA to handle the R1/R2pattern (matches justR1but only if followed by R2, like R1(?=R2) in Python)?Last modified: Tue Mar 10 12:31:58 2009 CS164: Lecture #3


View Full Document

Berkeley COMPSCI 164 - Lecture 3: Finite Automata

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture 3: Finite Automata
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 3: Finite Automata and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 3: Finite Automata 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?