go to previous page   go to home page   go to next page

Answer:

URL Automaton with Actions

The transition from state 6 to state 7 initializes the buffer to contain http:// since the first part of a URL has just been recognized.

Now the transition from state 7 to itself adds characters to the buffer one-by-one until a white space character is found.


Implementation

Below is a partial implementation of the scanner. Assume for now that there is a method read() which gets the next character from the input file, and puts it in an int. If the end of the file has been reached, it returns -1. The program needs to convert this int to a char. Look at the red code to see how this is done. Also, let us continue the assumption that any character less than space counts as white space. Check that each transition in the diagram is included in the code.

  public String getURL() throws IOException
  {
    final char space = ' ';
    String buffer = null;   // buffer for the URL 
    int  state = 0;         // the current state
    int  readInt;           // value returned by read()
    char current;           // the current character

    while ( (readInt=in.read()) != -1 )
    {
      current = (char)readInt;

      if      ( state==0 && (current == 'H' || current == 'h') )
        state = 1;
      else if ( state==0 )      
        state  = 0; 
 
      else if ( state==1 && (current == 'T' || current == 't') )
        state = 2;
      else if ( state==1 )      
        state  = 0; 
 
      else if ( state==2 && (current == 'T' || current == 't') )
        state = 3;
      else if ( state==2 )      
        state  = 0; 
 
      else if ( state==3 && (current == 'P' || current == 'p') )
        state = 4;
      else if ( state==3 )      
        state  = 0; 
 
      else if ( state==4 && current == ':' )
        state = 5;
      else if ( state==4 )      
        state  = 0; 
 
      else if ( state==5 && current == '/' )
        state = 6;
      else if ( state==5 )      
        state  = 0; 
 
      else if ( state==6 && current == '/' )
      {
        state  = 7;
        buffer =   ; // init buffer
      }
      else if ( state==6 )      
        state  = 0; 
 
      else if ( state==7 && current > space  )
      {
        state  = 7;
        buffer +=  ; // append current character to buffer
      }
 
      else if ( state==7 )
        return   ;
    }

    return null;

  }

When called by another method, getURL() returns a string containing the next URL (if there is one) or null if it has scanned past the end of the file.


QUESTION 9:

A few of the actions are left unfinished. Think about what they should be and click on the button to verify your suspicions.