fork download
  1. program LexicalAnalyzer;
  2.  
  3. function CheckFirstWord(s: string; var p: integer): boolean;
  4. var
  5. start: integer;
  6. begin
  7. start := p;
  8.  
  9. { (000)* }
  10. while (p + 2 <= Length(s)) and
  11. (s[p] = '0') and
  12. (s[p + 1] = '0') and
  13. (s[p + 2] = '0') do
  14. p := p + 3;
  15.  
  16. { 001 }
  17. if (p + 2 > Length(s)) or
  18. (s[p] <> '0') or
  19. (s[p + 1] <> '0') or
  20. (s[p + 2] <> '1') then
  21. begin
  22. p := start;
  23. CheckFirstWord := false;
  24. Exit;
  25. end;
  26.  
  27. p := p + 3;
  28.  
  29. { (010)* }
  30. while (p + 2 <= Length(s)) and
  31. (s[p] = '0') and
  32. (s[p + 1] = '1') and
  33. (s[p + 2] = '0') do
  34. p := p + 3;
  35.  
  36. CheckFirstWord := true;
  37. end;
  38.  
  39.  
  40. function CheckSecondWord(s: string; var p: integer): boolean;
  41. begin
  42. { First two characters must be ab }
  43.  
  44. if p + 1 > Length(s) then
  45. begin
  46. CheckSecondWord := false;
  47. Exit;
  48. end;
  49.  
  50. if (s[p] <> 'a') or (s[p + 1] <> 'b') then
  51. begin
  52. CheckSecondWord := false;
  53. Exit;
  54. end;
  55.  
  56. p := p + 2;
  57.  
  58. { Other characters can be a, b, c or d }
  59.  
  60. while (p <= Length(s)) and
  61. (s[p] in ['a', 'b', 'c', 'd']) do
  62. p := p + 1;
  63.  
  64. CheckSecondWord := true;
  65. end;
  66.  
  67.  
  68. function AnalyzeLine(s: string; lineNumber: integer): boolean;
  69. var
  70. p: integer;
  71. ok: boolean;
  72. begin
  73. p := 1;
  74. AnalyzeLine := true;
  75.  
  76. while p <= Length(s) do
  77. begin
  78.  
  79. { Skip spaces }
  80. while (p <= Length(s)) and (s[p] = ' ') do
  81. p := p + 1;
  82.  
  83. if p > Length(s) then
  84. Exit;
  85.  
  86. { Comment }
  87. if s[p] = '''' then
  88. begin
  89. Exit;
  90. end;
  91.  
  92. { First word }
  93. if s[p] = '0' then
  94. begin
  95. ok := CheckFirstWord(s, p);
  96.  
  97. if not ok then
  98. begin
  99. writeln('Error in line ', lineNumber,
  100. ', position ', p,
  101. ': invalid first word.');
  102. AnalyzeLine := false;
  103. Exit;
  104. end;
  105. end
  106.  
  107. { Second word }
  108. else if s[p] in ['a', 'b', 'c', 'd'] then
  109. begin
  110. ok := CheckSecondWord(s, p);
  111.  
  112. if not ok then
  113. begin
  114. writeln('Error in line ', lineNumber,
  115. ', position ', p,
  116. ': invalid second word.');
  117. AnalyzeLine := false;
  118. Exit;
  119. end;
  120. end
  121.  
  122. { Invalid character }
  123. else
  124. begin
  125. writeln('Error in line ', lineNumber,
  126. ', position ', p,
  127. ': invalid character.');
  128. AnalyzeLine := false;
  129. Exit;
  130. end;
  131.  
  132. end;
  133. end;
  134.  
  135.  
  136. var
  137. n: integer;
  138. i: integer;
  139. line: string;
  140. allCorrect: boolean;
  141.  
  142. begin
  143.  
  144. writeln('=============================================');
  145. writeln('LEXICAL ANALYZER');
  146. writeln('VARIANT 34');
  147. writeln('=============================================');
  148. writeln;
  149.  
  150. writeln('WORD 1: (000)*001(010)*');
  151. writeln('WORD 2: ab(a | b | c | d)*');
  152. writeln('COMMENT: apostrophe to end of line');
  153. writeln;
  154.  
  155. write('Enter number of lines: ');
  156. readln(n);
  157.  
  158. writeln;
  159. writeln('Enter text:');
  160.  
  161. allCorrect := true;
  162.  
  163. for i := 1 to n do
  164. begin
  165. readln(line);
  166.  
  167. if not AnalyzeLine(line, i) then
  168. allCorrect := false;
  169. end;
  170.  
  171. writeln;
  172.  
  173. if allCorrect then
  174. writeln('No errors found.');
  175.  
  176. writeln;
  177. writeln('Press Enter to exit.');
  178. readln;
  179.  
  180. end.
Success #stdin #stdout 0s 5316KB
stdin
3
001 ab
000001010 abcd
' this is a comment
stdout
=============================================
LEXICAL ANALYZER
VARIANT 34
=============================================

WORD 1: (000)*001(010)*
WORD 2: ab(a | b | c | d)*
COMMENT: apostrophe to end of line

Enter number of lines: 
Enter text:

No errors found.

Press Enter to exit.