帮忙看看这个词法分析程序 ( 积分: 20 )

  • 主题发起人 主题发起人 hyzb
  • 开始时间 开始时间
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 &quot;File not found&quot;, process stopped. Use step or run to continue.
谁知道这是怎么回事啊?
 
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 &quot;File not found&quot;, process stopped. Use step or run to continue.
谁知道这是怎么回事啊?
 
没有好人吗?
 
看来是刚学编程的吧

这个错误是:没有找到你要读入的文件
运行程序,让输入文件名的时候,要输入一个程序目录下存在的文件名
譬如 程序存放在D:/,在那里新建一个1.txt的文件
然后运行程序,输入1.txt,运行结果就会产生一个result.txt的文件,里面是1.txt文件语法分析的结果
 
我照你的方法做了,还是不行啊
 
接受答案了.
 

Similar threads

I
回复
0
查看
578
import
I
I
回复
0
查看
584
import
I
I
回复
0
查看
647
import
I
后退
顶部