130 likes | 226 Views
The joy of lex. Or : How I learned to stop worrying and love the deterministic, finite-state automaton. Administrivia. Reminder: schedule rollout meeting w/ me & Blake During finals week Allow ~1.5 hours for presentation, demos, code walk-through, doc reviews, etc.
E N D
The joy of lex Or: How I learned to stop worrying and love the deterministic, finite-state automaton...
Administrivia • Reminder: schedule rollout meeting w/ me & Blake • During finals week • Allow ~1.5 hours for presentation, demos, code walk-through, doc reviews, etc. • Everything must be completed by noon, Friday Dec 15 • Slots are first-come, first-served • Reminder: final exam 12:30-2:30 PM, Tues Dec 12
Historical review • Last Thursday: • Thanksgiving! Hope you all had excellent chow and good family time. • Before that: • Thinking about the timer design • Use of finite state machine in P2 design • Today: • Finite state machines in (some) detail...
Finite State Machines • Mathematical abstraction for thinking about how a process works • Processing data stream to identify regular expressions • States of assembly line activity in GrellSim • Many consumer electronics (DVD, VCR, cruise controls, alarm clocks, etc.) • UNIT behaviors in JCiv... • AKA: Finite state automata (FSA), DFA, et al.
Example: Cruise control • Car’s cruise control can on or off • Hit button to turn on • Hit off button or press brake to turn off • Have to enable the CC system before you can activate CC • Separate button to enable/disable • Stays enabled even if CC is off
enable button on/ enable disable button off/ brake/ enable on/off/ brake/ disable off button brake disable button on button Enabled; on The state diagram Disabled; off Enabled; off
Key characteristics • Finite set of states • Have to completely characterize all possibilities • Finite set of inputs • A.k.a., characters • Transition function • For each state/input pair: next state • Finite set of actions
Designing w/ FSAs • Decide what states are necessary • E.g., idle, working-on-job, about-to-start-job • Work out all possible inputs/characters • E.g., all ASCII chars, job completion events, etc. • Draw state transition diagram • MUST account for all states and all inputs for every state • Turn diagram into code...
Diagrams to code • Representation of states: • int (byte, long, etc.): unique code for each state • enum • general objects
Diagrams to code • Representation of inputs: • int/char/etc.: unique code for each input • enum/general object/etc.
Diagrams to code • Hard part: representing transition function • “for each state and input pair, return the next state” • Could do it with a big series of if/case statements • Ugh... • “Classical” FSA code: use a 2-d array (table) • next_state=table[curr_state][input];
Example code:Table-driven finite state machines for lexical analysis
Analysis of table-driven FSA • Define • Defend • Attack