Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE564 Lecture 12

CSE564 Lecture 12

Software Design
State Machine and Singleton
(202102)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

September 12, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 12: State Machine and

    Singleton Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Did you use If-Else to Identify Tokens? State Machine

  3. jgs 564 00001010 Using IF-ELSE It is not a good

    idea! February 13th, 2008 by Rich Sharpe. Posted in Software Quality, Software Quality Metrics
  4. jgs 564 00001010 Using a State Machine | Model S

    0 S 1 S 2 S 3 b 0 1 ... Delimiter, operator, whitespace, quotation mark S0 SE S1 SE SE Stop S1 S2 SE SE SE Stop S2 SE S3 S3 SE Stop S3 SE S3 S3 SE Stop SE SE SE SE SE Stop
  5. jgs 564 00001010 Using a State Machine | Code b

    0 1 ... Delimiter, operator, whitespace, quotation mark S 0 S E S 1 S E S E Stop S 1 S 2 S E S E S E Stop S 2 S E S 3 S 3 S E Stop S 3 S E S 3 S 3 S E Stop S E S E S E S E S E Stop // constants private static final int ZERO = 1; private static final int ONE = 2; private static final int B = 0; private static final int OTHER = 3; private static final int DELIMITER = 4; private static final int ERROR = 4; private static final int STOP = -2; // table as a 2D array private static int[][] stateTable = { {ERROR, 1, ERROR, ERROR, STOP}, { 2, ERROR, ERROR, ERROR, STOP}, {ERROR, 3, 3, ERROR, STOP}, {ERROR, 3, 3, ERROR, STOP}, {ERROR, ERROR, ERROR, ERROR, STOP} };
  6. jgs 564 00001010 Using a State Machine STEP 3. Algorithm

    void splitLine (line) { state = S0; String string =""; do { l = line.readNextLetter(); go = calculateNextState(state, l); if( go != STOP ) { string = string + l; state = go; } } while (line.hasLetters() && go != STOP); if (state == S3) print (“It is a BINARY number”); else print (“error”); if( isDelimiter(currentChar)) print (“Also, there is a DELIMITER”); else if (isOperator(currentChar) ) print (“Also, there is an OPERATOR”); // loop if (line.hasLetters() )) splitLine( line – string ); } b 0 1 ... S0 SE S1 SE SE Stop S1 S2 SE SE SE Stop S2 SE S3 S3 SE Stop S3 SE S3 S3 SE Stop SE SE SE SE SE Stop
  7. jgs 564 00001010 Lexer – Step by Step

  8. jgs 564 00001010 Lexer – Step by Step 1 =

    INTEGER 2 = INTEGER 3 = IDENTIFIER 5 = OCTAL 7 = INTEGER 8 = IDENTIFIER 9 = BINARY 10 = HEXADECIMAL columns [a-z] = [A] B [C-F] [G-W] X [Y-Z] [a-f] = [A] B [C-F] 0 $ _ [1] [2-7] [8-9] [A] B [C-F] [G-W] X [Y-Z] states
  9. jgs 564 00001010 Lexer – Step by Step 0 $

    _ [1] [2-7] [8-9] A B [C-F] [G-W] X [Y-Z] ... Delimiter, operator, whitespace, quotation mark S 0 S 1 S 3 S 3 S 2 S 2 S 2 S 3 S 3 S 3 S 3 S 3 S 3 S E Stop S 1 S 5 S E S E S 5 S 5 S E S E S 4 S E S E S 6 S E S E Stop S 2 S 7 S E S E S 7 S 7 S 7 S E S E S E S E S E S E S E Stop S 3 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S E Stop S 4 S 9 S E S E S 9 S E S E S E S E S E S E S E S E S E Stop S 5 S 5 S E S E S 5 S 5 S E S E S E S E S E S E S E S E Stop S 6 S 10 S E S E S 10 S 10 S 10 S 10 S 10 S 10 S E S E S E S E Stop S 7 S 7 S E S E S 7 S 7 S 7 S E S E S E S E S E S E S E Stop S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S 8 S E Stop S 9 S 9 S E S E S 9 S E S E S E S E S E S E S E S E S E Stop S 10 S 10 S E S E S 10 S 10 S 10 S 10 S 10 S 10 S E S E S E S E Stop S E S E S E S E S E S E S E S E S E S E S E S E S E S E Stop
  10. jgs 564 00001010 Programming Assignment 1. Read a File; Split

    the lines using the System.lineSeparator 2. For each line read character by character and use the character as an input for the state machine 3. Concatenate the character, creating the largest STRING possible. Stop when a delimiter, white space, operator, or quotation mark and the current state allowed. If there are more characters in the line, create a new line with those characters and go to step 2. 4. For each STRING and WORD report its TOKEN or ERROR as correspond.
  11. jgs 564 00001010 Code | Token.java

  12. jgs 564 00001010 Code | Lexer.java

  13. jgs 564 00001010 All States

  14. jgs 564 00001010 All Columns

  15. jgs 564 00001010 Code | Lexer.java {

  16. jgs 564 00001010 Code | Lexer.java

  17. jgs 564 00001010 Code | Lexer.java

  18. jgs 564 00001010 Code | Lexer.java

  19. jgs 564 00001010 Code | Lexer.java

  20. jgs Singleton Design Patterns

  21. jgs 564 00001010 GoF Patterns

  22. jgs 564 00001010 Singleton

  23. jgs 564 00001010 Singleton class MainApp { public static void

    Main(String []a){ // Constructor is protected -- cannot use new Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); // Test for same instance if (s1 == s2){ // true - Objects are the same instance } } }
  24. jgs 564 00001010 Singleton class Singleton{ private static Singleton _instance;

    // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } }
  25. jgs 564 00001010 Scenario Quiz Tutor System Companion ControlCenter <<

    singleton>>
  26. jgs 564 00001010 Questions

  27. jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez javiergs@asu.edu Fall

    2020 Disclaimer. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.