H
hyzb
Unregistered / Unconfirmed
GUEST, unregistred user!
program Pdemo10;
{$APPTYPE CONSOLE}
uses
SysUtils;
const arrow = ' ---> ';
//保留字组
var ReserveWords: array[0..34] of String=('and', 'array', 'begin', 'end',
'case', 'const', 'div', 'do', 'downto', 'else', 'file',
'for', 'function', 'goto', 'if', 'in', 'label', 'mod', 'nil', 'not', 'of',
'or', 'packed','procedure', 'program', 'record',
'repeat', 'set', 'then', 'to', 'type', 'until', 'var', 'while', 'with');
strToken: String;
FileName: String='';
F: File of Char;
ch: Char=' ';
//将下一个输入字符读到ch,搜索指示器前移一字符位置
function GetChar():Char;
begin
Read(F,ch);
Result := ch;
end;
//检查ch中的字符是否为空白
function GetBC():Char;
begin
while (ch=' ') do
GetChar;
Result := ch;
end;
//IsLetter(),IsDigit()判断ch中的字符是字母还是数字
function IsDigit():Boolean;
begin
Result := False;
if ch in ['0'..'9'] then
Result := true
end;
function IsLetter():Boolean;
begin
Result := False;
if ch in ['a'..'z','A'..'Z'] then
Result := true
end;
//判断strToken是否为保留字
function Reserve():Boolean;
var
n: Integer;
begin
Result := False;
for n:=0 to 34 do
begin
if ReserveWords[n] = strToken then
begin
Result := True;
break;
end;
end;
end;
procedure WriteFile(sort: String);
var MyFile: TextFile;
begin
AssignFile(MyFile,'result.txt');
if not FileExists('result.txt') then
Rewrite(MyFile)
else
Append(MyFile);
try
Writeln(MyFile,strToken,arrow,sort);
finally
CloseFile(MyFile);
end;
end;
begin
//提供命令行支持,用户输入文件名
writeln('Please input FileName!');
while ch<>#13 do
begin
read(ch);
if ch=#13 then
break;
FileName:= FileName + ch;
end;
AssignFile(F,FileName);
Reset(F);
GetBC();
while not eof(F) do
begin
strToken := '';
//区分标识符以及保留字
if IsLetter() then
begin
while (IsDigit() or IsLetter() or (ch='_')) do
begin
strToken := strToken + ch;
GetChar();
end;
if Reserve() then
WriteFile('Reserve Word')
else
WriteFile('Identifier');
end
else if IsDigit() then
begin
while IsDigit() do
begin
strToken := strToken + ch;
GetChar();
end;
WriteFile('Number');
end
//区分字符串
else if (ch='''') then
begin
strToken := strToken + ch;
GetChar();
while not (ch='''') do
begin
strToken := strToken + ch;
GetChar();
end;
strToken := strToken + ch;
GetChar();
WriteFile('String');
end
else if (ch=' ') then
GetBC()
else if (ch=#13) or (ch=#10) then
GetChar()
else if (ch=';') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Semicolon');
end
//区分':',':='
else if (ch=':') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Assign');
end
else
WriteFile('Colon');
end
//区分'.','..'
else if (ch='.') then
begin
strToken := strToken + ch;
GetChar();
if (ch=' ') or (ch='') or (ch=#13) then
WriteFile('End symbol')
else if (ch='.') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Bound');
end
else
WriteFile('Point');
end
else if (ch=',') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Comma');
end
//'+','-','*','/','=' 以及一系列符号
else if (ch='+') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Plus');
end
else if (ch='-') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Minus');
end
else if (ch='*') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Multiply');
end
else if (ch='/') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Divide');
end
else if (ch='=') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Equal');
end
else if (ch='[') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Left Square Brackets');
end
else if (ch=']') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Right Square Brackets');
end
else if (ch='^') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Pointer');
end
else if (ch='(') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Left Parenthesis');
end
else if (ch=')') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Right Parenthesis');
end
//区分'<','<=','<>'
else if (ch='<') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
WriteFile('Less or Equal');
GetChar();
end
else if (ch='>') then
begin
strToken := strToken + ch;
WriteFile('Unequal');
GetChar();
end
else
WriteFile('Less');
end
//区分'>','>='
else if (ch='>') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
WriteFile('Greater or Equal');
GetChar();
end
else
WriteFile('Greater');
end
//屏蔽注解{....}
else if (ch='{') then
begin
while (ch<>'}') do
GetChar();
GetChar();
end
else
break;
end;
CloseFile(F);
writeln('Press Any Key To Out Of Program!');
readln(ch);
end.
编译能通过,可是在运行的时候出现错误:
Project Pdemo10.exe raised exception class EInOutError with message "File not found", process stopped. Use step or run to continue.
谁知道这是怎么回事啊?
{$APPTYPE CONSOLE}
uses
SysUtils;
const arrow = ' ---> ';
//保留字组
var ReserveWords: array[0..34] of String=('and', 'array', 'begin', 'end',
'case', 'const', 'div', 'do', 'downto', 'else', 'file',
'for', 'function', 'goto', 'if', 'in', 'label', 'mod', 'nil', 'not', 'of',
'or', 'packed','procedure', 'program', 'record',
'repeat', 'set', 'then', 'to', 'type', 'until', 'var', 'while', 'with');
strToken: String;
FileName: String='';
F: File of Char;
ch: Char=' ';
//将下一个输入字符读到ch,搜索指示器前移一字符位置
function GetChar():Char;
begin
Read(F,ch);
Result := ch;
end;
//检查ch中的字符是否为空白
function GetBC():Char;
begin
while (ch=' ') do
GetChar;
Result := ch;
end;
//IsLetter(),IsDigit()判断ch中的字符是字母还是数字
function IsDigit():Boolean;
begin
Result := False;
if ch in ['0'..'9'] then
Result := true
end;
function IsLetter():Boolean;
begin
Result := False;
if ch in ['a'..'z','A'..'Z'] then
Result := true
end;
//判断strToken是否为保留字
function Reserve():Boolean;
var
n: Integer;
begin
Result := False;
for n:=0 to 34 do
begin
if ReserveWords[n] = strToken then
begin
Result := True;
break;
end;
end;
end;
procedure WriteFile(sort: String);
var MyFile: TextFile;
begin
AssignFile(MyFile,'result.txt');
if not FileExists('result.txt') then
Rewrite(MyFile)
else
Append(MyFile);
try
Writeln(MyFile,strToken,arrow,sort);
finally
CloseFile(MyFile);
end;
end;
begin
//提供命令行支持,用户输入文件名
writeln('Please input FileName!');
while ch<>#13 do
begin
read(ch);
if ch=#13 then
break;
FileName:= FileName + ch;
end;
AssignFile(F,FileName);
Reset(F);
GetBC();
while not eof(F) do
begin
strToken := '';
//区分标识符以及保留字
if IsLetter() then
begin
while (IsDigit() or IsLetter() or (ch='_')) do
begin
strToken := strToken + ch;
GetChar();
end;
if Reserve() then
WriteFile('Reserve Word')
else
WriteFile('Identifier');
end
else if IsDigit() then
begin
while IsDigit() do
begin
strToken := strToken + ch;
GetChar();
end;
WriteFile('Number');
end
//区分字符串
else if (ch='''') then
begin
strToken := strToken + ch;
GetChar();
while not (ch='''') do
begin
strToken := strToken + ch;
GetChar();
end;
strToken := strToken + ch;
GetChar();
WriteFile('String');
end
else if (ch=' ') then
GetBC()
else if (ch=#13) or (ch=#10) then
GetChar()
else if (ch=';') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Semicolon');
end
//区分':',':='
else if (ch=':') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Assign');
end
else
WriteFile('Colon');
end
//区分'.','..'
else if (ch='.') then
begin
strToken := strToken + ch;
GetChar();
if (ch=' ') or (ch='') or (ch=#13) then
WriteFile('End symbol')
else if (ch='.') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Bound');
end
else
WriteFile('Point');
end
else if (ch=',') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Comma');
end
//'+','-','*','/','=' 以及一系列符号
else if (ch='+') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Plus');
end
else if (ch='-') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Minus');
end
else if (ch='*') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Multiply');
end
else if (ch='/') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Divide');
end
else if (ch='=') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Equal');
end
else if (ch='[') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Left Square Brackets');
end
else if (ch=']') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Right Square Brackets');
end
else if (ch='^') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Pointer');
end
else if (ch='(') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Left Parenthesis');
end
else if (ch=')') then
begin
strToken := strToken + ch;
GetChar();
WriteFile('Right Parenthesis');
end
//区分'<','<=','<>'
else if (ch='<') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
WriteFile('Less or Equal');
GetChar();
end
else if (ch='>') then
begin
strToken := strToken + ch;
WriteFile('Unequal');
GetChar();
end
else
WriteFile('Less');
end
//区分'>','>='
else if (ch='>') then
begin
strToken := strToken + ch;
GetChar();
if (ch='=') then
begin
strToken := strToken + ch;
WriteFile('Greater or Equal');
GetChar();
end
else
WriteFile('Greater');
end
//屏蔽注解{....}
else if (ch='{') then
begin
while (ch<>'}') do
GetChar();
GetChar();
end
else
break;
end;
CloseFile(F);
writeln('Press Any Key To Out Of Program!');
readln(ch);
end.
编译能通过,可是在运行的时候出现错误:
Project Pdemo10.exe raised exception class EInOutError with message "File not found", process stopped. Use step or run to continue.
谁知道这是怎么回事啊?