Ход работы. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids;

type

TForm1 = class(TForm)

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

Label6: TLabel;

Label7: TLabel;

Label8: TLabel;

StringGrid1: TStringGrid;

StringGrid2: TStringGrid;

StringGrid3: TStringGrid;

StringGrid4: TStringGrid;

StringGrid5: TStringGrid;

Button1: TButton;

Button2: TButton;

Memo1: TMemo;

Memo2: TMemo;

Memo3: TMemo;

OpenDialog1: TOpenDialog;

procedure Identifikator(s: string);

procedure Literal(s: string);

procedure Razdelitel(s: string);

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Изм.
Лист
№ докум.
Подп.
Дата
Лист
 
Form1: TForm1;

m_ID: array of string;

m_LT: array of string;

const

m_TR: array [1..7] of string = ('имена', 'для', 'от', 'все', 'всё', 'кнц', 'до');

m_RD: array [1..11] of string = (',', ';', '=', '>', '<', '+', '-', '*', '/', ':', '::');

b=['а'..'я'];

implementation

{$R *.dfm}

procedure TForm1.Identifikator(s: string);

var

i:byte;

begin

for i:=1 to 7 do if s=m_TR[i] then

begin

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='1,'+IntToStr(i);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

Exit;

end;

for i:=0 to length(m_ID)-1 do if s=m_ID[i] then

begin

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='3,'+IntToStr(i+1);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

Exit;

end;

if length(s)>20 then Memo3.Lines.Add('Ошибка в идентификаторе '+s+'. Длинна идентификатора должна быть не более 7 символов.');

m_ID[length(m_ID)-1]:=s;

SetLength(m_ID,length(m_ID)+1);

StringGrid2.Cells[0,StringGrid2.RowCount-1]:=IntToStr(StringGrid2.RowCount);

StringGrid2.Cells[1,StringGrid2.RowCount-1]:=s;

StringGrid2.RowCount:=StringGrid2.RowCount+1;

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='3,'+IntToStr(length(m_ID)-1);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

end;

procedure TForm1.Literal(s: string);

var

i:byte;

begin

for i:=0 to length(m_LT)-1 do if s=m_LT[i] then

begin

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='4,'+IntToStr(i+1);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

Exit;

end;

m_LT[length(m_LT)-1]:=s;

SetLength(m_LT,length(m_LT)+1);

StringGrid3.Cells[0,StringGrid3.RowCount-1]:=IntToStr(StringGrid3.RowCount);

StringGrid3.Cells[1,StringGrid3.RowCount-1]:=s;

StringGrid3.RowCount:=StringGrid3.RowCount+1;

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='4,'+IntToStr(length(m_LT)-1);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

end;

procedure TForm1.Razdelitel(s: string);

var

Изм.
Лист
№ докум.
Подп.
Дата
Лист
 
i:byte;

begin

for i:=1 to 11 do if s=m_RD[i] then

begin

StringGrid4.Cells[0,StringGrid4.RowCount-1]:=s;

StringGrid4.Cells[1,StringGrid4.RowCount-1]:='2,'+IntToStr(i);

StringGrid4.RowCount:=StringGrid4.RowCount+1;

Exit; end;end;

procedure TForm1.Button1Click(Sender: TObject);

label 1;

var

f,s,shold:string;

i,j,k:byte;

sint:boolean;

begin

SetLength(m_ID,1);

SetLength(m_LT,1);

if OpenDialog1.Execute=false then exit

else

begin

Memo1.Lines.LoadFromFile(OpenDialog1.FileName);

end;

for i:=0 to Memo1.Lines.Count-1 do

begin

j:=1;

while j<=Length(Memo1.Lines[i]) do

case Memo1.Lines[i][j] of

'A'..'Z','a'..'z','А'..'Я','а'..'я':

begin

s:=Memo1.Lines[i][j];

inc(j);

1: case Memo1.Lines[i][j] of

'A'..'Z','a'..'z','А'..'Я','а'..'я','0'..'9','_':

/////////////////////////////////////// ///////////////

begin

s:=s+Memo1.Lines[i][j];

inc(j);

goto 1;

end;

end;

//////////////////////////////////////////////////////////

Memo2.Lines.Add(S+' - идентификатор');

Identifikator(s);

end;

'0'..'9':

begin

s:=Memo1.Lines[i][j];

inc(j);

while Memo1.Lines[i][j] in ['0'..'9'] do

begin

s:=s+Memo1.Lines[i][j];

inc(j);

end;

Memo2.Lines.Add(S+' - литерал');

Literal(s);

end;

',',';',':','=','+','-','*','\':

begin

if (Memo1.Lines[i][j]=':') and (Memo1.Lines[i][j+1]=':') then

begin

s:=Memo1.Lines[i][j];

Изм.
Лист
№ докум.
Подп.
Дата
Лист
 
s:=s+Memo1.Lines[i][j];

inc(j); inc(j);

end else begin

s:=Memo1.Lines[i][j];

inc(j);

end;

Memo2.Lines.Add(S+' - разделитель');

Razdelitel(s);

end;

' ':inc(j);

end; end;

for i:=1 to 7 do

begin

StringGrid1.Cells[0,StringGrid1.RowCount-1]:=IntToStr(i);

StringGrid1.Cells[1,StringGrid1.RowCount-1]:=m_TR[i];

StringGrid1.RowCount:=StringGrid1.RowCount+1;

end;

for i:=1 to 11 do

begin

StringGrid5.Cells[0,StringGrid5.RowCount-1]:=IntToStr(i);

StringGrid5.Cells[1,StringGrid5.RowCount-1]:=m_RD[i];

StringGrid5.RowCount:=StringGrid5.RowCount+1;

end;

StringGrid1.RowCount:=StringGrid1.RowCount-1;

StringGrid2.RowCount:=StringGrid2.RowCount-1;

StringGrid3.RowCount:=StringGrid3.RowCount-1;

StringGrid4.RowCount:=StringGrid4.RowCount-1;

StringGrid5.RowCount:=StringGrid5.RowCount-1;

end;

end.

Вывод: в ходе данной работы я ознакомился с назначением и принципами работы

лексических анализаторов, получил практические навыки построения

сканера на примере заданного входного языка.


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: