Advertisement
regergr

Untitled

Jan 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. program project1;
  2.  
  3. uses
  4. strutils;
  5.  
  6. type
  7. states = (no, id, error, const_int, const_signed, const_float,
  8. readOneQuot, find_Quot, CheckQuot);
  9.  
  10. function isDot(c: char): boolean;
  11. begin
  12. if (c = '.') then
  13. Result := True
  14. else
  15. Result := False;
  16. end;
  17.  
  18. function IsLetter(c: char): boolean;
  19. begin
  20. if (c in ['a'..'z']) or (c in ['A'..'Z']) or (c = '_') then
  21. Result := True
  22. else
  23. Result := False;
  24.  
  25. end;
  26.  
  27. function IsDigit(c: char): boolean;
  28. begin
  29. if (c in ['0'..'9']) then
  30. Result := True
  31. else
  32. Result := False;
  33. end;
  34.  
  35. function isSign(c: char): boolean;
  36. begin
  37. if (c = '+') or (c = '-') then
  38. Result := True
  39. else
  40. Result := False;
  41. end;
  42.  
  43. function WhatState(c: char): states;
  44. begin
  45. if (isLetter(c)) then
  46. Result := id
  47. else if (isDot(c)) then
  48. Result := const_float
  49. else if (IsDigit(c)) then
  50. Result := const_int
  51. else if (IsSign(c)) then
  52. Result := const_signed
  53. else if (c = chr(39)) then
  54. Result := Find_Quot
  55. else
  56. Result := error;
  57. end;
  58.  
  59.  
  60. var
  61. state: states;
  62. s, buf: string;
  63. i: integer;
  64. begin
  65. while (True) do
  66. begin
  67. Readln(s);
  68. s := TrimRightSet(s, [' ']);
  69. state := no;
  70. buf := '';
  71. i := 1;
  72. while (True) do
  73. begin
  74. case state of
  75.  
  76. NO: begin
  77. if (i > length(s)) then break;
  78. if (s[i] = ' ') then begin
  79. state := no;
  80. end
  81. else begin
  82. buf += s[i];
  83. state := WhatState(s[i]);
  84. end;
  85. end;
  86.  
  87. ID: begin
  88. if (isLetter(s[i])) or (isDigit(s[i])) then begin
  89. buf += s[i];
  90. end;
  91. if (s[i] = ' ') or (i > length(s)) then begin
  92. state := no;
  93. writeln(buf, ' ', 'IDENT');
  94. buf := '';
  95. end
  96. else if not (isLetter(s[i])) and not (isDigit(s[i])) then begin
  97. buf += s[i];
  98. state := error;
  99. end;
  100. end;
  101.  
  102. CONST_FLOAT: begin
  103. if (s[i] = ' ') or (i > length(s)) then begin
  104. writeln(buf, ' ', 'CONST');
  105. state := no;
  106. buf := '';
  107. end
  108. else begin
  109. buf += s[i];
  110. if (IsDigit(s[i])) then begin
  111. state := const_float;
  112. end;
  113. if (isDot(s[i])) then begin
  114. state := error;
  115. end;
  116. end;
  117. end;
  118.  
  119. CONST_SIGNED: begin
  120. if (i > length(s)) or (s[i] = ' ') then begin
  121. writeln(buf, ' ', 'ERROR');
  122. state := no;
  123. buf := '';
  124. end
  125. else begin
  126. buf += s[i];
  127. if (isDigit(s[i])) then begin
  128. state := const_int;
  129. end
  130. else if (isDot(s[i])) then begin
  131. state := const_float;
  132. end
  133. else begin
  134. state := error;
  135. end;
  136. end;
  137. end;
  138.  
  139. CONST_INT: begin
  140. if (i > length(s)) or (s[i] = ' ') then begin
  141. writeln(buf, ' ', 'CONST');
  142. state := no;
  143. buf := '';
  144. end
  145. else begin
  146. buf += s[i];
  147. if (isDot(s[i])) then begin
  148. state := const_float;
  149. end
  150. else if not (isDigit(s[i])) then begin
  151. state := error;
  152. end;
  153. end;
  154. end;
  155.  
  156. FIND_QUOT: begin
  157. if (i > length(s)) then begin
  158. writeln(buf, ' ', 'ERROR');
  159. buf := '';
  160. state := no;
  161. end
  162. else begin
  163. buf += s[i];
  164. if (s[i] = '''') then begin
  165. state := CheckQuot;
  166. end
  167. else begin
  168. state := Find_Quot;
  169. end;
  170. end;
  171. end;
  172.  
  173. CHECKQUOT: begin
  174. if (s[i] = ' ') or (i > length(s)) then begin
  175. writeln(buf, ' ', 'CONST');
  176. buf := '';
  177. state := no;
  178. end
  179. else begin
  180. buf += s[i];
  181. if (s[i] = chr(39)) then begin
  182. state := Find_Quot;
  183. end
  184. else begin
  185. state := error;
  186. end;
  187. end;
  188. end;
  189.  
  190. ERROR: begin
  191. if (i > length(s)) or (s[i] = ' ') then begin
  192. writeln(buf, ' ', 'ERROR');
  193. buf := '';
  194. state := no;
  195. end
  196. else begin
  197. buf += s[i];
  198. end;
  199. end;
  200. end;
  201. i += 1;
  202. end;
  203. end;
  204. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement