unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Memo2: TMemo; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); function find_lable(tmp:String):boolean; private { Private declarations } public { Public declarations } end;var Form1: TForm1; baoliuzi:array[1..25] of String; implementation{$R *.dfm} function tform1.find_lable(tmp:String):boolean; var i:integer; begin for i:=1 to 25 do begin if tmp=baoliuzi[i] then begin result:=true; exit; end; end; result:=false; end; procedure TForm1.FormCreate(Sender: TObject); begin baoliuzi[1]:=('and'); baoliuzi[2]:=('array'); baoliuzi[3]:=('begin'); baoliuzi[4]:=('case'); baoliuzi[5]:=('const'); baoliuzi[6]:=('else'); baoliuzi[7]:=('end'); baoliuzi[8]:=('for'); baoliuzi[9]:=('function');baoliuzi[10]:=('if'); baoliuzi[11]:=('not'); baoliuzi[12]:=('of'); baoliuzi[13]:=('or'); baoliuzi[14]:=('procedure'); baoliuzi[15]:=('program');baoliuzi[16]:=('record'); baoliuzi[17]:=('repeat'); baoliuzi[18]:=('set'); baoliuzi[19]:=('then'); baoliuzi[20]:=('to'); baoliuzi[21]:=('type'); baoliuzi[22]:=('until'); baoliuzi[23]:=('var'); baoliuzi[24]:=('while'); baoliuzi[25]:=('with'); //以上程序初始化部分 end;procedure TForm1.Button1Click(Sender: TObject); //词法分析主程序 var first_ch_id:integer; flag:boolean; i,j,len,mynum:integer; buff,beginmystr,mystr,before_ch:string; ch:char; begin for i:=0 to memo1.Lines.Count-1 do begin buff:=memo1.Lines[i]; j:=0; while j<length(buff) do begin //11 j:=j+1; ch:=buff[j]; if ch<>' ' then begin //22 first_ch_id:=0; if ch in ['a'..'z','A'..'Z'] then first_ch_id:=1 else begin //33 if ch in ['0'..'9'] then first_ch_id:=2 else begin //44 if ch in [';',',','(',')','.','[',']']then first_ch_id:=3 else begin //55 if ch in ['<','>','=','+','-','*','/',':'] then first_ch_id:=4 else first_ch_id:=5 end; //55 end;//44 end;//33 case first_ch_id of 1: begin len:=1;mystr:=ch; while j<length(buff) do begin j:=j+1; ch:=buff[j]; if ch in ['a'..'z','A'..'Z','0'..'9'] then begin if len<10 then beginmystr:=mystr+ch;len:=len+1; if j=length(buff) then begin if find_lable(mystr) then //查表是否保留字 memo2.Lines.Add(mystr+' 保留字') else memo2.Lines.Add(mystr+' 标识符'); break; end; end else begin if find_lable(mystr) then //查表是否保留字 memo2.Lines.Add(mystr+' 保留字') else memo2.Lines.Add(mystr+' 标识符'); j:=j-1; break; end; end; end;2: begin myNum:=strtoint(ch); while j<=length(buff) do begin j:=j+1; ch:=buff[j]; if ch in ['0'..'9'] then myNum:=myNum*10+strtoint(ch) else begin memo2.Lines.Add(inttostr(myNum)+' 整型常量');j:=j-1;break;end; end; end; 3: begin mystr:=ch;memo2.Lines.Add(mystr+' 界符'); end; //界符 4: begin mystr:=ch;j:=j+1;before_ch:=ch; ch:=buff[j]; flag:=false; if (before_ch= ':') and(ch='=') then begin mystr:=mystr+ch; flag:=true;end; if (before_ch= '<') and(ch='=') then begin mystr:=mystr+ch; flag:=true;end; if (before_ch= '>') and(ch='=') then begin mystr:=mystr+ch; flag:=true;end; if (before_ch='<') and (ch='>') then begin mystr:=mystr+ch; flag:=true;end; if (before_ch='-') and (ch='>') then begin mystr:=mystr+ch; flag:=true;end; if flag=false then j:=j-1; memo2.Lines.Add(mystr+' 运算符'); continue; end; //运算符 5: memo2.Lines.add(ch+' 非法字符'); end //case end; //22 end;//11 end; end;end.
很急着用,能帮我改吗?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
function find_lable(tmp:String):boolean;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
baoliuzi:array[1..25] of String;
implementation{$R *.dfm}
function tform1.find_lable(tmp:String):boolean;
var
i:integer;
begin
for i:=1 to 25 do
begin
if tmp=baoliuzi[i] then
begin
result:=true;
exit;
end;
end;
result:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
baoliuzi[1]:=('and'); baoliuzi[2]:=('array');
baoliuzi[3]:=('begin'); baoliuzi[4]:=('case');
baoliuzi[5]:=('const'); baoliuzi[6]:=('else');
baoliuzi[7]:=('end'); baoliuzi[8]:=('for');
baoliuzi[9]:=('function');baoliuzi[10]:=('if');
baoliuzi[11]:=('not'); baoliuzi[12]:=('of');
baoliuzi[13]:=('or'); baoliuzi[14]:=('procedure');
baoliuzi[15]:=('program');baoliuzi[16]:=('record');
baoliuzi[17]:=('repeat'); baoliuzi[18]:=('set');
baoliuzi[19]:=('then'); baoliuzi[20]:=('to');
baoliuzi[21]:=('type'); baoliuzi[22]:=('until');
baoliuzi[23]:=('var'); baoliuzi[24]:=('while');
baoliuzi[25]:=('with');
//以上程序初始化部分
end;procedure TForm1.Button1Click(Sender: TObject); //词法分析主程序
var
first_ch_id:integer;
flag:boolean;
i,j,len,mynum:integer;
buff,beginmystr,mystr,before_ch:string;
ch:char;
begin
for i:=0 to memo1.Lines.Count-1 do
begin
buff:=memo1.Lines[i];
j:=0;
while j<length(buff) do
begin //11
j:=j+1;
ch:=buff[j]; if ch<>' ' then
begin //22
first_ch_id:=0;
if ch in ['a'..'z','A'..'Z'] then
first_ch_id:=1
else
begin //33
if ch in ['0'..'9'] then
first_ch_id:=2
else
begin //44
if ch in [';',',','(',')','.','[',']']then
first_ch_id:=3
else
begin //55
if ch in ['<','>','=','+','-','*','/',':'] then
first_ch_id:=4
else first_ch_id:=5
end; //55
end;//44
end;//33 case first_ch_id of
1: begin
len:=1;mystr:=ch;
while j<length(buff) do
begin
j:=j+1;
ch:=buff[j];
if ch in ['a'..'z','A'..'Z','0'..'9'] then
begin
if len<10 then
beginmystr:=mystr+ch;len:=len+1;
if j=length(buff) then
begin
if find_lable(mystr) then //查表是否保留字
memo2.Lines.Add(mystr+' 保留字')
else memo2.Lines.Add(mystr+' 标识符');
break;
end;
end
else
begin
if find_lable(mystr) then //查表是否保留字
memo2.Lines.Add(mystr+' 保留字')
else memo2.Lines.Add(mystr+' 标识符');
j:=j-1;
break;
end;
end;
end;2: begin
myNum:=strtoint(ch);
while j<=length(buff) do
begin
j:=j+1;
ch:=buff[j];
if ch in ['0'..'9'] then
myNum:=myNum*10+strtoint(ch)
else begin memo2.Lines.Add(inttostr(myNum)+' 整型常量');j:=j-1;break;end;
end;
end;
3: begin mystr:=ch;memo2.Lines.Add(mystr+' 界符'); end; //界符
4: begin
mystr:=ch;j:=j+1;before_ch:=ch;
ch:=buff[j]; flag:=false;
if (before_ch= ':') and(ch='=') then
begin mystr:=mystr+ch; flag:=true;end;
if (before_ch= '<') and(ch='=') then
begin mystr:=mystr+ch; flag:=true;end;
if (before_ch= '>') and(ch='=') then
begin mystr:=mystr+ch; flag:=true;end;
if (before_ch='<') and (ch='>') then
begin mystr:=mystr+ch; flag:=true;end;
if (before_ch='-') and (ch='>') then
begin mystr:=mystr+ch; flag:=true;end;
if flag=false then j:=j-1; memo2.Lines.Add(mystr+' 运算符');
continue;
end; //运算符
5: memo2.Lines.add(ch+' 非法字符');
end //case
end; //22
end;//11
end;
end;end.
[email protected]