Advertisement
Guest User

Untitled

a guest
May 30th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. public class NaurTextProcessing {
  2. private final int MAXPOS = 10;
  3. private boolean debugOn = false;
  4. public void turnDebugOn(){
  5. debugOn = true;
  6. }
  7. public void turnDebugOff(){
  8. debugOn = false;
  9. }
  10.  
  11. private char returnChar(RandomAccessFile inFile){ //get character()
  12. try{
  13. char characterRead;
  14. if(inFile.getFilePointer() == inFile.length()) //if we are at end of file return an empty character.
  15. characterRead = '\0';
  16. else //if we are not at end of inFile then read in next char
  17. characterRead = (char)inFile.readByte();
  18.  
  19. if(characterRead =='\n') //if character read is a newline then return a blank
  20. characterRead = ' ';
  21. return(characterRead); //Otherwise return character that was read from the file
  22. }catch(Exception e){
  23. System.out.println("**** Error in NaurApp.returnChar () ****");
  24. System.out.println("\t" + e);
  25. return '\0';
  26. }
  27. }
  28.  
  29. public void runApp(){
  30. try{
  31. char currentChar; //current character being processed
  32. int lineLength = 0; //length of current output line
  33. char state = '0'; //current state of finite state machine
  34. StringBuffer word = new StringBuffer(); //current word being built
  35. File inputFile = new File("naur.dat");
  36. if(!inputFile.exists()){
  37. System.out.println("input file 'naur.dat' does not exist.");
  38. return;
  39. }
  40. RandomAccessFile inFile = new RandomAccessFile(inputFile, "r"); //character.digit(state,10) returns integer value corresponding to character state
  41. while(Character.digit(state,10) < 4){ //Computation is performed in base-10 arithmetic.
  42. currentChar = returnChar(inFile);
  43. if(debugOn){
  44. System.out.print("State: " + state + "\t");
  45. switch(currentChar){ //if switch(state)
  46. case '\0':
  47. System.out.println("End of file.");
  48. break;
  49. case ' ':
  50. System.out.println("Blank/Carriage Return.");
  51. break;
  52. default:
  53. System.out.println("Character read: " + currentChar +".");
  54. }
  55. }
  56. switch(state){
  57. case '0':
  58. if(debugOn)
  59. System.out.println("Entering state 0.");
  60. switch(currentChar){
  61. case ' ': //character is a blank or a newline
  62. break; //T1 state = '4';
  63. case '\0': //end of file was reached
  64. state = '4'; //this terminates loop
  65. break; //T2
  66. default: //T3
  67. word.append(currentChar);
  68. state = '1';
  69. break;
  70. } //switch (currentChar)
  71. break;
  72. case '1':
  73. if(debugOn)
  74. System.out.println("Entering state 1");
  75. switch(currentChar){
  76. case ' ': //character is a blank or a newline
  77. System.out.print(word.toString()); //T4
  78. lineLength = word.length();
  79. word = new StringBuffer();
  80. state = '2';
  81. break;
  82. case '\0': //end of file was reached
  83. System.out.println(word.toString()); //T5
  84. state = '4'; //this terminates loop
  85. break;
  86. default:
  87. if(word.length() <= MAXPOS) //T6
  88. word.append(currentChar);
  89. else
  90. state = '5'; //this terminates loop
  91. break; //T7
  92. }
  93. case '2':
  94. if(debugOn)
  95. System.out.println("Entering state 2");
  96. switch(currentChar){
  97. case ' ': //character is a blank or a newline
  98. break; //T8
  99. case '\0': //end of file was reached
  100. state = '4'; //this terminates loop
  101. break; //T9
  102. default: //T10
  103. word.append(currentChar);
  104. state = '3';
  105. break;
  106. }
  107. break;
  108. case '3':
  109. if(debugOn)
  110. System.out.println("Entering state 3");
  111. switch(currentChar){
  112. case ' ':
  113. case '\0':
  114. if((lineLength + word.length()) <= MAXPOS){
  115. System.out.print(currentChar);
  116. System.out.print(word.toString());
  117. lineLength = lineLength + 1 + word.length();
  118. word = new StringBuffer();
  119. }
  120. else{
  121. System.out.println();
  122. System.out.print(word.toString());
  123. lineLength = word.length();
  124. word = new StringBuffer();
  125. }
  126. if(currentChar != '\0')
  127. state = '2'; //T11
  128. else
  129. state = '4'; //this terminates loop //T12
  130. break;
  131. }
  132. break;
  133. }
  134. }
  135. while(Character.digit(state, 10) < 4)
  136. System.out.println();
  137. if(state == '5')
  138. System.out.println("Word longer than permitted length encountered");
  139. inFile.close();
  140. }catch(Exception e){
  141. System.out.println("**** Error in NaurApp.main() ****");
  142. System.out.println("\t + e");
  143. }
  144. } //runApp()
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement