Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- uses
- strutils;
- type
- states = (no, id, error, const_int, const_signed, const_float,
- readOneQuot, find_Quot, CheckQuot);
- function isDot(c: char): boolean;
- begin
- if (c = '.') then
- Result := True
- else
- Result := False;
- end;
- function IsLetter(c: char): boolean;
- begin
- if (c in ['a'..'z']) or (c in ['A'..'Z']) or (c = '_') then
- Result := True
- else
- Result := False;
- end;
- function IsDigit(c: char): boolean;
- begin
- if (c in ['0'..'9']) then
- Result := True
- else
- Result := False;
- end;
- function isSign(c: char): boolean;
- begin
- if (c = '+') or (c = '-') then
- Result := True
- else
- Result := False;
- end;
- function WhatState(c: char): states;
- begin
- if (isLetter(c)) then
- Result := id
- else if (isDot(c)) then
- Result := const_float
- else if (IsDigit(c)) then
- Result := const_int
- else if (IsSign(c)) then
- Result := const_signed
- else if (c = chr(39)) then
- Result := Find_Quot
- else
- Result := error;
- end;
- var
- state: states;
- s, buf: string;
- i: integer;
- begin
- while (True) do
- begin
- Readln(s);
- s := TrimRightSet(s, [' ']);
- state := no;
- buf := '';
- i := 1;
- while (True) do
- begin
- case state of
- NO: begin
- if (i > length(s)) then break;
- if (s[i] = ' ') then begin
- state := no;
- end
- else begin
- buf += s[i];
- state := WhatState(s[i]);
- end;
- end;
- ID: begin
- if (isLetter(s[i])) or (isDigit(s[i])) then begin
- buf += s[i];
- end;
- if (s[i] = ' ') or (i > length(s)) then begin
- state := no;
- writeln(buf, ' ', 'IDENT');
- buf := '';
- end
- else if not (isLetter(s[i])) and not (isDigit(s[i])) then begin
- buf += s[i];
- state := error;
- end;
- end;
- CONST_FLOAT: begin
- if (s[i] = ' ') or (i > length(s)) then begin
- writeln(buf, ' ', 'CONST');
- state := no;
- buf := '';
- end
- else begin
- buf += s[i];
- if (IsDigit(s[i])) then begin
- state := const_float;
- end;
- if (isDot(s[i])) then begin
- state := error;
- end;
- end;
- end;
- CONST_SIGNED: begin
- if (i > length(s)) or (s[i] = ' ') then begin
- writeln(buf, ' ', 'ERROR');
- state := no;
- buf := '';
- end
- else begin
- buf += s[i];
- if (isDigit(s[i])) then begin
- state := const_int;
- end
- else if (isDot(s[i])) then begin
- state := const_float;
- end
- else begin
- state := error;
- end;
- end;
- end;
- CONST_INT: begin
- if (i > length(s)) or (s[i] = ' ') then begin
- writeln(buf, ' ', 'CONST');
- state := no;
- buf := '';
- end
- else begin
- buf += s[i];
- if (isDot(s[i])) then begin
- state := const_float;
- end
- else if not (isDigit(s[i])) then begin
- state := error;
- end;
- end;
- end;
- FIND_QUOT: begin
- if (i > length(s)) then begin
- writeln(buf, ' ', 'ERROR');
- buf := '';
- state := no;
- end
- else begin
- buf += s[i];
- if (s[i] = '''') then begin
- state := CheckQuot;
- end
- else begin
- state := Find_Quot;
- end;
- end;
- end;
- CHECKQUOT: begin
- if (s[i] = ' ') or (i > length(s)) then begin
- writeln(buf, ' ', 'CONST');
- buf := '';
- state := no;
- end
- else begin
- buf += s[i];
- if (s[i] = chr(39)) then begin
- state := Find_Quot;
- end
- else begin
- state := error;
- end;
- end;
- end;
- ERROR: begin
- if (i > length(s)) or (s[i] = ' ') then begin
- writeln(buf, ' ', 'ERROR');
- buf := '';
- state := no;
- end
- else begin
- buf += s[i];
- end;
- end;
- end;
- i += 1;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement