Скачиваний:
23
Добавлен:
02.05.2014
Размер:
40.75 Кб
Скачать
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, ComCtrls, Buttons, ExtCtrls ;

type



TForm1 = class(TForm)
Label3: TLabel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Bevel1: TBevel;
Label2: TLabel;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
GroupBox1: TGroupBox;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label8: TLabel;
Label9: TLabel;
GroupBox2: TGroupBox;
Label10: TLabel;
Label12: TLabel;
Label14: TLabel;
Button5: TButton;
Label1: TLabel;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
Button6: TButton;
Button4: TButton;
Button3: TButton;
Memo2: TMemo;
Label11: TLabel;
Label13: TLabel;
Panel1: TPanel;
ListBox2: TListBox;
Label16: TLabel;
Panel2: TPanel;
ListBox1: TListBox;
Label15: TLabel;
StringGrid1: TStringGrid;
TabSheet4: TTabSheet;
TreeView1: TTreeView;
Label7: TLabel;
Edit1: TEdit;
Label17: TLabel;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure vCreat(Sender: TObject);


public
procedure TabID(str:string);
procedure Shirina();
procedure Prosto(str:string;nomer:integer);
procedure Derevo();
end;


var
Form1: TForm1;
ukazatel,knop1,knop,vssrav,nomer:integer;
HashArray : Array[0..500,1..2] of string;
MetSpisok: Array[0..500,1..2] of string;
srsrav,srsrav1,vssrav1:real;
inputString:TStringList;

const
PredMatrix:array [1..26,1..26] of char=
( {pr end. ; if then els enf beg end do wle a := or and not < > = ( ) - + * / !}
{prog} (' ','=','<','<',' ',' ',' ','<',' ',' ','<','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{end.} (' ',' ','>',' ',' ',' ','>',' ','>',' ',' ','>',' ',' ',' ',' ',' ',' ',' ',' ','>',' ',' ',' ',' ','>'),
{;} (' ','>','>','<',' ',' ',' ','<','>',' ','<','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{if} (' ',' ',' ',' ','=',' ',' ',' ',' ',' ',' ','<',' ','<','<','<','<','<','<','<',' ','<','<','<','<',' '),
{then} (' ',' ',' ','<',' ','=','=','<',' ',' ','<','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{else} (' ',' ',' ','<',' ',' ','=','<',' ',' ','<','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{endif}(' ','>','>',' ',' ','>','>',' ','>','>','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{begin}(' ',' ','<','<',' ',' ',' ','<','=',' ','>','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{end} (' ','>','>',' ',' ','>','>',' ','>',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{do} (' ','>','>','<',' ','>',' ','<','<',' ','<','<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '),
{while}(' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','=',' ',' ',' ',' ',' ',' '),
{a} (' ','>','>',' ','>','>','>',' ','>','>','>',' ','=','>','>','>','>','>','>',' ','>','>','>','>','>',' '),
{:=} (' ','>','>',' ',' ','>','>',' ','>','>',' ','<',' ',' ',' ',' ',' ',' ',' ','<','>','<','<','<','<',' '),
{or} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<','<','<','<','<','<','>','<','<','<','<',' '),
{and} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<','<','<','<','<','<','>','<','<','<','<',' '),
{not} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<','<','<','<','<','<','>','<','<','<','<',' '),
{<} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<',' ',' ',' ',' ','<','>','<','<','<','<',' '),
{>} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<',' ',' ',' ',' ','<','>','<','<','<','<',' '),
{=} (' ',' ',' ',' ','>',' ',' ',' ',' ',' ',' ','<',' ','>','<',' ',' ',' ',' ','<','>','<','<','<','<',' '),
{(} (' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','<',' ','<','<','<','<','<','<','<','=','<','<','<','<',' '),
{)} (' ','>','>',' ','>','>','>',' ','>','=','<',' ',' ','>','>',' ','>','>','>',' ','>','>','>','>','>',' '),
{-} (' ','>','>',' ','>','>','>',' ','>','>','>','<',' ','>','>',' ','>','>','>',' ','>','>','>','>','>',' '),
{+} (' ','>','>',' ','>','>','>',' ','>','>','>','<',' ','>','>',' ','>','>','>',' ','>','>','>','>','>',' '),
{*} (' ','>','>',' ','>','>','>',' ','>','>','>','<',' ','>','>',' ','>','>','>',' ','>','>','>','>','>',' '),
{/} (' ','>','>',' ','>','>','>',' ','>','>','>','<',' ','>','>',' ','>','>','>',' ','>','>','>','>','>',' '),
{!} ('<',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '));
{Описание правил грамматики}

grammar:array [1..27] of string=
('progEend.','E','E;E','E;','ifEthenEelseEendif','ifEthenEendif','beginEend','while(E)doE','E','a:=E',
'EorE','E','EandE','E','notE','E','E<E','E>E','E=E','(E)','E-E','E+E','E*E','E/E','E','(E)','a');
term:array [1..26] of string=
('prog','end.',';','if','then','else','endif','begin','end','do','while','a',':=','or','and','not','<','>','=','(',')','-','+','*','/','!');
notterm:array [1..27] of char=
('E','E','E','E','E','E','E','E','E','E','B','E','E','E','E','E',
'E','E','E','E','E','E','E','E','E','E','E');
CanonO:array [1..27,1..7] of string=
(('prog','E','end.','','','',''),
('E','','','','','',''),
('E',';','E','','','',''),
('E',';','','','','',''),
('if','E','then','E','else','E','endif'),
('if','E','then','E','endif','',''),
('begin','E','end','','','',''),
('while','(','E',')','do','E',''),
('E','','','','','',''),
('a',':=','E','','','',''),
('E','or','E','','','',''),
('E','','','','','',''),
('E','and','E','','','',''),
('E','','','','','',''),
('not','E','','','','',''),
('E','','','','','',''),
('E','<','E','','','',''),
('E','>','E','','','',''),
('E','=','E','','','',''),
('(','E',')','','','',''),
('E','-','E','','','',''),
('E','+','E','','','',''),
('E','*','E','','','',''),
('E','/','E','','','',''),
('E','','','','','',''),
('(','E',')','','','',''),
('a','','','','','',''));


implementation

uses Unit2;

function IndMatrix(SymS:String):integer;
var i:integer;
begin
indMatrix:=0;
if term[1]=SymS then IndMatrix:=1;
for i:=1 to 26 do
if term[i]=SymS then IndMatrix:=i;
end;


function inArray(myS:String;A:Array of string;N:integer):boolean;
var i:integer;
begin
inArray:=False;
if myS='prog' then inArray:=true;
for i:=1 to N do
if myS=A[i] then begin inArray:=true end;
end;


function NomRul(myS:String):integer;
var i:integer;
begin
NomRul:=0;
if (myS=grammar[1])then NomRul:=1;
for i:=1 to 27 do
if grammar[i]=myS then NomRul:=i;
end;


function findnt(myS:String):string;
begin
findnt:='';
findnt:=notterm[nomRul(myS)]
end;




{$R *.dfm}


procedure TForm1.vCreat(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='Номер';
StringGrid1.Cells[1,0]:='Лексема';
StringGrid1.Cells[2,0]:='Инфа';
nomer:=0;
ukazatel:=0;
srsrav:=0;
knop:=0;
vssrav:=0;
srsrav1:=0;
knop1:=0;
vssrav1:=0;


end;

procedure TForm1.Button3Click(Sender: TObject);
begin
{ Выход из программы }
Close;
end;


function Xesh(St:string):integer; {процедура вычисление значения хэш-функции}
var dlina:integer;
twosyb:char;
fivesyb:char;
begin
dlina:=length(St);
if dlina=0 then Result:=0 else
begin
if dlina>4 then begin
twosyb:=St[1];
fivesyb:=St[5];
Result:=ord(fivesyb)+ord(twosyb);
end
else begin
twosyb:=St[2];
Result:=ord( twosyb);
end;

end;
end;

procedure TForm1.Prosto(str:string;nomer:integer);

begin


MetSpisok[nomer,1]:=IntToStr(nomer);
MetSpisok[nomer,2]:=str ;
listbox2.Items.Add(MetSpisok[nomer,1]+' '+MetSpisok[nomer,2]);

end;

procedure TForm1.TabID(str:string) ;
var i,k,dlina,hash,j,f,kol:integer;
stroka:string;
label 1,2;
begin

j:=0;
k:=2;
kol:=0;
ukazatel:=0;
hash:=Xesh(str);

if HashArray[hash,1]='' then
begin
HashArray[hash,1]:=IntToStr(hash);
HashArray[hash,2]:=str;
listbox1.Items.Add(HashArray[hash,1]+' '+HashArray[hash,2]);
inc(nomer);
Prosto(str,nomer);
end
else
begin
1: if (HashArray[hash,2])=str then
begin
kol:=kol+1;
memo2.Lines.Add('Удален повтор '+str);
j:=j+1;
ukazatel:=ukazatel+1;
goto 2;
end
else
begin
memo2.Lines.Add('Коллизия элемент - ('+str+') ('+HashArray[hash,2]+') -'+HashArray[hash,1]);
kol:=kol+1;
hash:=(Xesh(str)+k) mod 509;
if (HashArray[hash,1])='' then
begin

HashArray[hash,1]:=IntToStr(hash);
HashArray[hash,2]:=str;
inc(nomer);
inc(k);
listbox1.Items.Add(HashArray[hash,1]+' '+HashArray[hash,2]);
Prosto(str,nomer);
end
else
begin
inc(k);
goto 1;
end;
end;

2:

label5.Caption:='Кол-во коллизий '+IntToStr(kol);

end;

end;






procedure TForm1.Button2Click(Sender: TObject);
var k,hash,dlina,i,srav,j,si: integer;
stroka: string;


label met1,met2;
begin

inc(knop);
k:=2;
srav:=0;
stroka:=Edit2.Text;
if stroka='' then
begin
memo2.Lines.add('Поле пусто!');
Abort ;
end;
dlina:=memo1.Lines.Count;
hash:=Xesh(stroka);
for i:=0 to 500 do
begin
inc(srav);
if HashArray[hash,1]=IntToStr(hash) then
begin
if stroka=HashArray[hash,2] then
begin

memo2.Lines.Add('Иден-р '+stroka+' найден! Его номер '+ IntToStr(hash));
goto met1;
end
else
begin
for j:=0 to 500 do
begin
inc(srav);
hash:=(Xesh(stroka)+k) mod 499;
inc(k);
if HashArray[hash,1]=IntToStr(hash) then
begin
if stroka=HashArray[hash,2] then
begin

memo2.Lines.Add('Иден-р '+stroka+' найден! Его номер '+ IntToStr(hash));
goto met1;
end;
end;
//else memo2.Lines.Add('Иден-р '+stroka+' не найден!');
end;
end;
end
else begin
memo2.Lines.add('Иден-р '+stroka+' не найден!');

abort;
end;
end;
met1: Label4.Caption:='Номер '+inttostr(hash); { Номер}
Label6.Caption:='Кол-во сравнений '+inttostr(srav); {Кол-во сравнений}
dlina:=memo1.Lines.Count;
vssrav:=vssrav+srav;

srsrav:=vssrav/dlina;
label8.Caption:='Среднее кол-во сравнений '+floattostr(srsrav); {Среднее кол-во сравнений}
label9.Caption:='Кол-во поисков '+inttostr(knop); {Кол-во поисков}
vssrav:=0;
srsrav:=0;

end;

procedure Tform1.Shirina();
var x, y, w: integer; s: string;
MaxWidth: integer;
begin
with StringGrid1 do
ClientHeight := DefaultRowHeight * RowCount + 5;
with StringGrid1 do
begin
for x := 0 to ColCount - 1 do
begin
MaxWidth := 0;
for y := 0 to RowCount - 1 do
begin
w := Canvas.TextWidth(Cells[x,y]);
if w > MaxWidth then
MaxWidth := w;
end;
ColWidths[x] := MaxWidth + 5;
end;
end;
end;




procedure TForm1.Button4Click(Sender: TObject);
begin
form2.Show;
end;




procedure TForm1.Button5Click(Sender: TObject);
var i,dlina,srav:integer;
stroka:string;

label met1;
begin

Inc(knop1);
stroka:=Edit2.Text;
dlina:=listbox2.Items.Count;
if stroka='' then
begin
memo2.Lines.add('Поле пусто!');
Abort;
end;

for i:=0 to nomer-1 do
begin
srav:=srav+1;
if MetSpisok[i,2]=stroka then begin
label10.Caption:='Номер '+inttostr(i) ;
label14.Caption:='Кол-во сравнений '+inttostr(srav);
label12.Caption:='Кол-во поисков '+inttostr(knop1);
Memo2.lines.add('Найден - '+stroka);
Abort;
end;

end;
Memo2.Lines.Add('не найден ');
met1:
vssrav1:=vssrav1+srav;
srsrav1:=vssrav1/dlina;
label11.Caption:='Среднее кол-во сравнений: '+floattostr(srsrav1);

end;

procedure TForm1.Button6Click(Sender: TObject); //сброс
var i,j: integer;
begin
edit1.Clear;
TreeView1.Items.Clear;
for i:=0 to 500 do MetSpisok[i,1]:='';
for i:=0 to 500 do MetSpisok[i,2]:='';
for i:=0 to 500 do HashArray[i,1]:='';
for i:=0 to 500 do HashArray[i,2]:='';
for i:=0 to memo1.Lines.Count-1 do memo1.Lines[i]:='';
for i:=0 to memo2.Lines.Count-1 do memo2.Lines[i]:='';
for j:=0 to 2 do begin
for i:=0 to 350 do StringGrid1.Cells[j,i]:=' ' end;

Edit2.Text:='';
listbox1.Clear;
listbox2.Clear;
nomer:=0;
nomer:=0;
ukazatel:=0;
srsrav:=0;
knop:=0;
vssrav:=0;
srsrav1:=0;
knop1:=0;
vssrav1:=0;
label10.Caption:='Номер ' ;
label14.Caption:='Кол-во сравнений ';
label12.Caption:='Кол-во поисков ';
Label4.Caption:='Номер ' ;
Label5.Caption:='Кол-во коллизий ' ;
Label6.Caption:='Кол-во сравнений ';
label8.Caption:='Среднее кол-во сравнений ';
label9.Caption:='Кол-во поисков ' ;

end;

procedure TForm1.Button1Click(Sender: TObject); // Лексический анализатор
type
TAState = ( A_H, A_OR1, A_OR2, A_NO1,A_NO2,A_NO3,A_VA1,A_VA2,A_VA3,A_CH1,
A_DO1,A_DO2,A_WH1,A_WH2,A_WH3,A_WH4,A_WH5,A_BY1,A_BY2,A_BY3,A_BY4,A_KO1,A_KO2,
A_KO3,A_P1,A_P2,A_P3,A_P4,A_BE1,A_BE2,A_BE3,A_BE4,A_BE5,A_EL1,A_EL2,A_EL3,A_TH1,
A_TH2,A_TH3,A_TH4,A_IF1,A_IF2,A_AN1,A_AN2,A_AN3,A_TO,A_Z,A_C1,A_C2,A_SL,A_GA,
A_PR1,A_PR2,A_PR3,A_TZ,A_PL,A_GA2,A_PO,A_MI,A_E,A_ID,A_IR1,A_IR2,A_KO4, A_IR3,A_BY5,A_PR4,A_BI2,A_BI1,A_BI3,A_KO5,A_SK,A_SK1);



var ind,pos,dl,i,j,i1,fl,zn,m:integer;
st,str,stroka:string;
sost:TAState;
CepofV:array [1..100]of integer;
p,stek,d:TStringList;
i2,i4,j2,fl2,n,k,vetv,u,ZZZ:integer;


MyTreeNode:TTreeNode;
nodeTree: TTreeNode;

begin
if OpenDialog1.execute then
Memo1.lines.loadFromFile(OpenDialog1.FileName) ;
inputString:=TStringList.Create;
inputString.Sorted:=False;

for i:=0 to 2 do
for i1:=1 to 999 do
StringGrid1.Cells[i,i1]:='';

i1:=Memo1.Lines.Count;
ind:=1;
pos:=1;
ZZZ:=0;
sost:=A_H;
for i:=1 to i1 do
begin

str:='';
stroka:=Memo1.Lines[i-1];
stroka:=stroka+' ';
dl:=Length(stroka);
if ZZZ=1 then sost:=A_C1 else sost:=A_H;
fl:=0;
zn:=0;
for j:=1 to dl do
begin

st:=stroka[j];
case sost of
A_H:
case stroka[j] of

';': begin sost:=A_TZ;str:=str+st; end;
'>': begin sost:=A_GA2;str:=str+st; end;
'<': begin sost:=A_GA; str:=str+st end;
'=': begin sost:=A_PO; str:=str+st end;
':': begin sost:=A_P1;str:=str+st; end;
'n': begin sost:=A_NO1;str:=str+st; end;
'p': begin sost:=A_PR1;str:=str+st; end;
'e': begin sost:=A_KO1;str:=str+st; end;
'i': begin sost:=A_IF1;str:=str+st; end;
't': begin sost:=A_TH1;str:=str+st; end;
'o': begin sost:=A_OR1;str:=str+st; end;
'+': begin sost:=A_PL;str:=str+st; end;
'-': begin sost:=A_MI;str:=str+st; end;
'a': begin sost:=A_IR1;str:=str+st; end;
'{': begin sost:=A_C1;str:=str+st;ZZZ:=1 end;
'*': begin sost:=A_Z;str:=str+st; end;
'/': begin sost:=A_SL;str:=str+st; end;
'd': begin sost:=A_DO1;str:=str+st; end;
'w': begin sost:=A_WH1;str:=str+st; end;
'b': begin sost:=A_BY1;str:=str+st; end;
'(': begin sost:=A_SK;str:=str+st; end;
')': begin sost:=A_SK1;str:=str+st; end;
'0','1': begin sost:=A_CH1;str:=str+st; end;
'c','f'..'h','m','q'..'s','u','v','x'..'z': begin sost:=A_ID;str:=str+st; end;
' ': begin sost:=A_H;str:=''; end;
else begin sost:=A_E;str:=str+st; end;
end;


A_C1:
case stroka[j] of
'}':begin sost:=A_H; ZZZ:=0; end;
else begin sost:=A_C1;str:=str+st; end;
end;

{ A_C2:
case stroka[j] of
' ':begin sost:=A_H; end;
else begin sost:=A_H;str:=str+st;flk:=0 end;
end; }

A_SK:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=2; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_SK1:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=3; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_TZ:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=1; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_GA2:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=12; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_GA:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=13; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_MI:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=17; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_PL:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=16; end;
else begin sost:=A_E;str:=str+st; end;
end;


A_PO:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=6; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_Z:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=15; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_SL:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=15;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_CH1:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=11; end;
'2'..'9' : begin sost:=A_ID; str:=str+st; end;
'0','1': begin sost:=A_CH1;str:=str+st; end;
else begin sost:=A_E;str:=str+st; end; end;

A_DO1:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=5; end;
'a'..'n','p'..'z','0'..'9' : begin sost:=A_ID; str:=str+st; end;
'o': begin sost:=A_DO2;str:=str+st; end;
else begin sost:=A_E;str:=str+st; end;
end;

A_DO2:
case stroka[j] of
'a'..'z','0'..'9': begin sost:=A_ID;str:=str+st; end;
' ':begin sost:=A_H;fl:=1;zn:=10;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_WH1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'h': begin sost:=A_WH2;str:=str+st;end;
'a'..'g','i'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_WH2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'i': begin sost:=A_WH3;str:=str+st;end;
'a'..'h','j'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;
A_WH3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'l': begin sost:=A_WH4;str:=str+st;end;
'a'..'k','m'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_WH4:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'e': begin sost:=A_WH5;str:=str+st;end;
'a'..'d','f'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_WH5:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=24;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;



A_ID:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;



A_BY1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'y' : begin sost:=A_BI1;str:=str+st;end;
'e': begin sost:=A_BY2;str:=str+st;end;
'a'..'d','f'..'x','z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BY2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'g': begin sost:=A_BY3;str:=str+st;end;
'a'..'f','h'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BY3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'i': begin sost:=A_BY4;str:=str+st;end;
'a'..'h','j'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BY4:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'n': begin sost:=A_BY5;str:=str+st;end;
'a'..'m','o'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BY5:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=21;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BI1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
't': begin sost:=A_BI2;str:=str+st;end;
'a'..'s','u'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BI2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'e': begin sost:=A_BI3;str:=str+st;end;
'a'..'d','f'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_BI3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=14;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;



A_PR1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'r': begin sost:=A_PR2;str:=str+st;end;
'a'..'q','s'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_PR2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'o': begin sost:=A_PR3; str:=str+st;end;
'a'..'n','p'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_PR3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'g': begin sost:=A_PR4; str:=str+st;end;
'a'..'f','h'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_PR4:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=25;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;


A_IF1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'f': begin sost:=A_IF2;str:=str+st;end;
'a'..'e','g'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_IF2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=18;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_OR1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'r': begin sost:=A_OR2;str:=str+st;end;
'a'..'q','s'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_OR2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=7;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_P1:
case stroka[j] of
'=':begin sost:=A_P2;str:=str+st;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_P2:
case stroka[j] of
' ':begin sost:=A_H;fl:=1;zn:=4;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_KO1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'n': begin sost:=A_KO2;str:=str+st;end;
'l': begin sost:=A_EL1;str:=str+st;end;
'a'..'k','m','o'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_KO2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'd': begin sost:=A_KO3;str:=str+st;end;
'a'..'c','e'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_KO3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=22;end;
'i','.': begin sost:=A_KO4;str:=str+st;end;
'a'..'h','j'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_KO4:
case stroka[j] of
'f': begin sost:=A_KO5;str:=str+st;end;
' ': begin sost:=A_H;fl:=1;zn:=23;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_KO5:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=26;end;
else begin sost:=A_E;str:=str+st; end;
end;

A_EL1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
's': begin sost:=A_EL2;str:=str+st;end;
'a'..'r','t'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_EL2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'e': begin sost:=A_EL3;str:=str+st;end;
'a'..'d','f'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_EL3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=20;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_IR1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'n': begin sost:=A_IR2;str:=str+st;end;
'a'..'m','o'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_IR2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'd': begin sost:=A_IR3;str:=str+st;end;
'a'..'c','e'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_IR3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=8;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_NO1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'o': begin sost:=A_NO2;str:=str+st;end;
'a'..'n','p'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_NO2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
't': begin sost:=A_NO3;str:=str+st;end;
'a'..'s','u'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;
A_NO3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=9;end;
'a'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_TH1:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'h': begin sost:=A_TH2;str:=str+st;end;
'a'..'g','i'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_TH2:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'e': begin sost:=A_TH3;str:=str+st;end;
'a'..'d','f'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_TH3:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=5;end;
'n': begin sost:=A_TH4;str:=str+st;end;
'a'..'m','o'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;

A_TH4:
case stroka[j] of
' ': begin sost:=A_H;fl:=1;zn:=19;end;
'a'..'d','f'..'z','0'..'9':begin sost:=A_ID;str:=str+st; end;
else begin sost:=A_E; str:=str+st; end;
end;





A_E: begin
if stroka[j]<>' 'then
begin
sost:=A_E;
str:=str+st;
end
else
begin
sost:=A_H;
Memo2.Lines.Append('Ошибочные символы -'+str);
str:='';
end;
end;
end;
if fl=1 then
begin

StringGrid1.Cells[0,ind]:=IntToStr(ind);
StringGrid1.Cells[1,ind]:=str;

if (zn=5) or (zn=11) then TabID(str);
case zn of
1: begin StringGrid1.Cells[2,ind]:='Разделяюший знак ;'; inputString.Add(';'); end;
2: begin StringGrid1.Cells[2,ind]:=' открывающиеся скобки'; inputString.Add('('); end;
3: begin StringGrid1.Cells[2,ind]:=' закрывающиеся скобки'; inputString.Add(')'); end;
4: begin StringGrid1.Cells[2,ind]:='Знак присваивания'; inputString.Add(':='); end;
5: begin StringGrid1.Cells[2,ind]:='Идентификатор'; inputString.Add('a'); end;
6: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "равно"'; inputString.Add('='); end;
7: begin StringGrid1.Cells[2,ind]:='Логическое "или"'; inputString.Add('or'); end;
8: begin StringGrid1.Cells[2,ind]:='Логическое "и"'; inputString.Add('and'); end;
9: begin StringGrid1.Cells[2,ind]:='Логическое "не"'; inputString.Add('not'); end;
10: begin StringGrid1.Cells[2,ind]:='Ключевое слово do'; inputString.Add('do'); end;
11: begin StringGrid1.Cells[2,ind]:='константа'; inputString.Add('a'); end;
12: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "меньше" '; inputString.Add('>'); end;
13: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "больше"'; inputString.Add('<'); end;
15: begin StringGrid1.Cells[2,ind]:='Дополнительные операции / '; inputString.Add('/'); end;
16: begin StringGrid1.Cells[2,ind]:='Арифметические операции +'; inputString.Add('+'); end;
17: begin StringGrid1.Cells[2,ind]:='Арифметические операции -';inputString.Add('-'); end;
18: begin StringGrid1.Cells[2,ind]:='Ключевое слово IF'; inputString.Add('if'); end;
19: begin StringGrid1.Cells[2,ind]:='Ключевое слово then'; inputString.Add('then'); end;
20: begin StringGrid1.Cells[2,ind]:='Ключевое слово else'; inputString.Add('else'); end;
21: begin StringGrid1.Cells[2,ind]:='Ключевое слово begin'; inputString.Add('begin'); end;
22: begin StringGrid1.Cells[2,ind]:='Ключевое слово end'; inputString.Add('end'); end;
23: begin StringGrid1.Cells[2,ind]:='Ключевое слово end.'; inputString.Add('end.'); end;
24: begin StringGrid1.Cells[2,ind]:='Ключевое слово while'; inputString.Add('while'); end;
25: begin StringGrid1.Cells[2,ind]:='Ключевое слово prog'; inputString.Add('prog'); end;
26: begin StringGrid1.Cells[2,ind]:='Ключевое слово endif'; inputString.Add('endif'); end;
end;
str:='';
ind:=ind+1;
fl:=0;
end;


end;
end;

Derevo();
Shirina();
end;

procedure TForm1.Derevo();
label Myend,zap,tree;

var SymbStack:TStringList;{входная строка и стэк соответственно}
i,j,verh,k,vetv,n,m,bezz:integer;
gamma,MyinS,syms,tek,cep:String;
mycase:char;
CepofV:array [1..100]of integer;
begin

if inputString.Count=0 then
begin
ShowMessage('Таблица лексем или исходный файл не загружены');
exit
end;

for i:=0 to inputString.Count-1 do MyinS:=MyinS+InputString[i];
{Edit2.Text:=MyinS;}


inputString.Add('!');
{Объявляем стэк}
SymbStack:=TStringList.Create;
SymbStack.Sorted:=False;
SymbStack.Add('!');
i:=0;
k:=0;
cep:='';
while i<=inputString.Count-1 do
begin
verh:=SymbStack.Count-1;
syms:=SymbStack[verh];
while (not(inArray(SymbStack[verh],term,26))) do verh:=verh-1;
if (SymbStack[verh]='!') and (inputString[i]='!') then goto MyEnd;
mycase:=PredMatrix[IndMatrix(SymbStack[verh]),IndMatrix(inputString[i])];
if ((mycase='<')or(mycase='='))then
begin {сдвиг}
SymbStack.Add(inputString[i]);
i:=i+1;
end
else
if (mycase='>')
then
begin {свертка}
gamma:='';
tek:='';
for j:=SymbStack.Count-1 downto 0 do
if not(inArray(SymbStack[j],term,26)) then
begin
gamma:=SymbStack[j]+gamma;
SymbStack.Delete(j)
end
else
if (inArray(SymbStack[j],term,26))and(tek='') then
begin
tek:=SymbStack[j];
gamma:=SymbStack[j]+gamma;
SymbStack.Delete(j)
end
else
if predMatrix[Indmatrix(SymbStack[j]),IndMatrix(tek)]='=' then
begin
tek:=SymbStack[j];
gamma:=SymbStack[j]+gamma;
SymbStack.Delete(j)
end
else break;



if findnt(gamma)<>''
then
begin
SymbStack.Add(findnt(gamma));
k:=k+1;
CepofV[k]:=NomRul(gamma);
{ ShowMessage('Цепочка '+gamma+' заменяется на '+findnt(gamma)+' по правилу '+inttostr(Nomrul(gamma))); }
cep:=inttostr(CepofV[k])+' '+cep;
end
else
begin {ошибка}
ShowMessage('Ошибка! Цепочка '+gamma+' недопустима');
goto tree
end;
end;
if mycase=' ' then
begin {ошибка}
ShowMessage('Ошибка! Символы '+Symbstack[verh]+' и '+InputString[i]+' не могут следовать друг за другом');
goto tree;
end;
end;
Myend:Edit1.Text:=cep;
{вывод дерева}
tree:n:=k;
with TreeView1 do
begin
Items.Add(Nil,'E');
vetv:=0;
for k:=n downto 1 do begin
m:=Items.Count;
if k<n then
for i:=m-1 downto 0 do
if Items[i].Text=notterm[CepofV[k]] then
if Items[i].HasChildren=False then
begin
vetv:=i;
goto zap
end;
zap: for i:=1 to 7 do
if CanonO[CepofV[k],i]<>'' then
Items.AddChild(Items[vetv],CanonO[CepofV[k],i])
end;
FullExpand

end;
InputString.Destroy;
SymbStack.Destroy;
end;




end.
Соседние файлы в папке курс
  • #
    02.05.2014261 б23Project1.dpr
  • #
    02.05.2014876 б23Project1.res
  • #
    02.05.201435.33 Кб23Unit1.dcu
  • #
    02.05.201451 б23Unit1.ddp
  • #
    02.05.201412.98 Кб23Unit1.dfm
  • #
    02.05.201440.75 Кб23Unit1.pas
  • #
    02.05.20143.72 Кб24Unit2.dcu
  • #
    02.05.201451 б23Unit2.ddp
  • #
    02.05.20141 Кб23Unit2.dfm
  • #
    02.05.2014482 б25Unit2.pas
  • #
    02.05.2014405 б24тест1.txt