Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NaurTextProcessing {
- private final int MAXPOS = 10;
- private boolean debugOn = false;
- public void turnDebugOn(){
- debugOn = true;
- }
- public void turnDebugOff(){
- debugOn = false;
- }
- private char returnChar(RandomAccessFile inFile){ //get character()
- try{
- char characterRead;
- if(inFile.getFilePointer() == inFile.length()) //if we are at end of file return an empty character.
- characterRead = '\0';
- else //if we are not at end of inFile then read in next char
- characterRead = (char)inFile.readByte();
- if(characterRead =='\n') //if character read is a newline then return a blank
- characterRead = ' ';
- return(characterRead); //Otherwise return character that was read from the file
- }catch(Exception e){
- System.out.println("**** Error in NaurApp.returnChar () ****");
- System.out.println("\t" + e);
- return '\0';
- }
- }
- public void runApp(){
- try{
- char currentChar; //current character being processed
- int lineLength = 0; //length of current output line
- char state = '0'; //current state of finite state machine
- StringBuffer word = new StringBuffer(); //current word being built
- File inputFile = new File("naur.dat");
- if(!inputFile.exists()){
- System.out.println("input file 'naur.dat' does not exist.");
- return;
- }
- RandomAccessFile inFile = new RandomAccessFile(inputFile, "r"); //character.digit(state,10) returns integer value corresponding to character state
- while(Character.digit(state,10) < 4){ //Computation is performed in base-10 arithmetic.
- currentChar = returnChar(inFile);
- if(debugOn){
- System.out.print("State: " + state + "\t");
- switch(currentChar){ //if switch(state)
- case '\0':
- System.out.println("End of file.");
- break;
- case ' ':
- System.out.println("Blank/Carriage Return.");
- break;
- default:
- System.out.println("Character read: " + currentChar +".");
- }
- }
- switch(state){
- case '0':
- if(debugOn)
- System.out.println("Entering state 0.");
- switch(currentChar){
- case ' ': //character is a blank or a newline
- break; //T1 state = '4';
- case '\0': //end of file was reached
- state = '4'; //this terminates loop
- break; //T2
- default: //T3
- word.append(currentChar);
- state = '1';
- break;
- } //switch (currentChar)
- break;
- case '1':
- if(debugOn)
- System.out.println("Entering state 1");
- switch(currentChar){
- case ' ': //character is a blank or a newline
- System.out.print(word.toString()); //T4
- lineLength = word.length();
- word = new StringBuffer();
- state = '2';
- break;
- case '\0': //end of file was reached
- System.out.println(word.toString()); //T5
- state = '4'; //this terminates loop
- break;
- default:
- if(word.length() <= MAXPOS) //T6
- word.append(currentChar);
- else
- state = '5'; //this terminates loop
- break; //T7
- }
- case '2':
- if(debugOn)
- System.out.println("Entering state 2");
- switch(currentChar){
- case ' ': //character is a blank or a newline
- break; //T8
- case '\0': //end of file was reached
- state = '4'; //this terminates loop
- break; //T9
- default: //T10
- word.append(currentChar);
- state = '3';
- break;
- }
- break;
- case '3':
- if(debugOn)
- System.out.println("Entering state 3");
- switch(currentChar){
- case ' ':
- case '\0':
- if((lineLength + word.length()) <= MAXPOS){
- System.out.print(currentChar);
- System.out.print(word.toString());
- lineLength = lineLength + 1 + word.length();
- word = new StringBuffer();
- }
- else{
- System.out.println();
- System.out.print(word.toString());
- lineLength = word.length();
- word = new StringBuffer();
- }
- if(currentChar != '\0')
- state = '2'; //T11
- else
- state = '4'; //this terminates loop //T12
- break;
- }
- break;
- }
- }
- while(Character.digit(state, 10) < 4)
- System.out.println();
- if(state == '5')
- System.out.println("Word longer than permitted length encountered");
- inFile.close();
- }catch(Exception e){
- System.out.println("**** Error in NaurApp.main() ****");
- System.out.println("\t + e");
- }
- } //runApp()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement