sss:='S';
po:=@sss;
q1:=Form2.dxOrgChart1.Add(nil, po);
q1.Text:='S';
q1.Shape:=shEllipse;
q1.Color:=clAqua;
q1.Width:=20;
q1.Height:=20;
end;
function GetFirst(S:String):String;
var i:byte;
S1:String;
begin
S1:='';
for i:=1 to Length(S) do
if (S[i]+S[i+1])='->' then break else
S1:=S1+S[i];
GetFirst:=S1;
end;
function GetRight(S:String):String;
var i:byte;
S1:String;
begin
S1:='';
for i:=Length(S) downto 1 do
if (S[i]=' ')or((S[i-1]+S[i])='->') then break else
S1:=S[i]+S1;
GetRight:=S1;
end;
procedure GetR(S:String; var num1:byte; var S1:String);
var i, num:byte;
begin
S1:='';
num:=3;
for i:=1 to Length(S) do
if (S[i]+S[i+1])='->' then break else
inc(num);
num1:=1;
for i:=num to Length(S) do
if S[i]=' ' then
begin
inc(num1);
S1:=S1+S[i];
end else
begin
S1:=S1+S[i];
end;
end;
function GetLeft(S:String):String;
var i, num:byte;
S1:String;
begin
S1:='';
num:=3;
for i:=1 to Length(S) do
if (S[i]+S[i+1])='->' then break else
inc(num);
for i:=num to Length(S) do
if S[i]=' ' then break else
S1:=S1+S[i];
GetLeft:=S1;
end;
function GetLast(S:String):String;
var i, num:byte;
S1:String;
begin
S1:='';
num:=3;
for i:=1 to Length(S) do
if (S[i]+S[i+1])='->' then break else
inc(num);
for i:=num to Length(S) do
begin
if S[i]=' ' then S1:=S1+'_'
else S1:=S1+S[i];
end;
GetLast:=S1;
end;
procedure TForm2.Button4Click(Sender: TObject);
var bb, i,ii, jj, j,k, m,n, nn, num, num1,Mas_of_Net_Length, MasL_Length, MasR_Length:byte;
S, S1,A, L,R:String;
V, V1: array of String;
MasL, MasL1: array of array of String;
MasR, MasR1: array of array of String;
N1,N2: array of Integer;
flag, step2,flag1:boolean;
begin
bb:=1;
//Список нетерминалов
Mas_of_Net_Length:=0;
SetLength(Mas_of_Net, Mas_of_Net_Length);
For i:=0 to Form2.ListBox1.Count-1 do
begin
S:=GetFirst(Form2.ListBox1.Items[i]);
flag:=true;
if Mas_of_Net_Length>0 then
for j:=0 to Mas_of_Net_Length-1 do
if Mas_of_Net[j]=S then flag:=false;
if flag then
begin
inc(Mas_of_Net_Length);
SetLength(Mas_of_Net, Mas_of_Net_Length);
Mas_of_Net[Mas_of_Net_Length-1]:=S;
end;
end;
//Множества крайних левых и крайних правых символов
///////шаг1
SetLength(MasL, Mas_of_Net_Length);
SetLength(MasR, Mas_of_Net_Length);
for i:=0 to Mas_of_Net_Length-1 do
begin
SetLength(MasL[i],1);
SetLength(MasR[i],1);
MasL[i,0]:='0';
MasR[i,0]:='0';
end;
for i:=0 to Mas_of_Net_Length-1 do
begin
A:=Mas_of_Net[i];
MasL_Length:=strtoint(MasL[i,0]);
MasR_Length:=strtoint(MasR[i,0]);
for j:=0 to Form2.ListBox1.Count-1 do
begin
if A=GetFirst(Form2.ListBox1.Items[j]) then
begin
flag:=true;
L:=GetLeft(Form2.ListBox1.Items[j]);
if MasL_Length>0 then
for k:=1 to MasL_Length do
if MasL[i, k]=L then flag:=false;
if flag then
begin
inc(MasL_Length);
SetLength(MasL[i],MasL_Length+1);
MasL[i, MasL_Length]:=L;
end;
flag:=true;
R:=GetRight(Form2.ListBox1.Items[j]);
if MasR_Length>0 then
for k:=1 to MasR_Length do
if MasR[i, k]=R then flag:=false;
if flag then
begin
inc(MasR_Length);
SetLength(MasR[i],MasR_Length+1);
MasR[i, MasR_Length]:=R;
end;
end;
end;
L:='';
R:='';
MasL[i,0]:=IntToStr(MasL_Length);
MasR[i,0]:=IntToStr(MasR_Length);
for k:=1 to MasL_Length do
L:=L+', '+MasL[i, k];
L:=copy(L,3,Length(L)-2);
Form2.Memo1.Lines. Add('L0('+Mas_of_Net[i]+')={'+L+'}');
for k:=1 to MasR_Length do
R:=R+', '+MasR[i, k];
R:=copy(R,3,Length(R)-2);
Form2.Memo2.Lines. Add('R0('+Mas_of_Net[i]+')={'+R+'}');
end;
///////шаг2,3
step2:=true;
while step2 do
begin
SetLength(MasL1,Mas_of_Net_Length);
SetLength(MasR1,Mas_of_Net_Length);
for i:=0 to Mas_of_Net_Length-1 do
begin
SetLength(MasL1[i],StrToInt(MasL[i,0])+1);
SetLength(MasR1[i],StrToInt(MasR[i,0])+1);
for j:=0 to StrToInt(MasL[i,0]) do
MasL1[i, j]:=MasL[i, j];
for j:=0 to StrToInt(MasR[i,0]) do
MasR1[i, j]:=MasR[i, j];
end;
for i:=0 to Mas_of_Net_Length-1 do
begin
MasL_Length:=StrToInt(MasL[i,0]);
MasR_Length:=StrToInt(MasR[i,0]);
for j:=1 to MasL_Length do
begin
A:=MasL[i, j]; //A-символ из множества L, для i-ой строки
num:=0;
for k:=0 to Mas_of_Net_Length-1 do
begin
if Mas_of_Net[k]=A then break
else inc(num);
end;
if num<Mas_of_Net_Length then //Если A нетерминал, и номер строки - num
for k:=1 to StrToInt(MasL[num,0]) do //Добавить в i-ую строку символы из строки num
begin
flag:=true;
for n:=1 to MasL_Length do
if MasL[num, k]=MasL[i, n] then flag:=false;
if flag then //Если символа нет то добавить
begin
inc(MasL_Length);
SetLength(MasL[i],MasL_Length+1);
MasL[i, MasL_Length]:=MasL[num, k];
end;
end;
end;
for j:=1 to MasR_Length do
begin
A:=MasR[i, j]; //A-символ из множества R, для i-ой строки
num:=0;
for k:=0 to Mas_of_Net_Length-1 do
begin
if Mas_of_Net[k]=A then break
else inc(num);
end;
if num<Mas_of_Net_Length then //Если A нетерминал, и номер строки - num
for k:=1 to StrToInt(MasR[num,0]) do //Добавить в i-ую строку символы из строки num
begin
flag:=true;
for n:=1 to MasR_Length do
if MasR[num, k]=MasR[i, n] then flag:=false;
if flag then //Если символа нет то добавить
begin
inc(MasR_Length);
SetLength(MasR[i],MasR_Length+1);
MasR[i, MasR_Length]:=MasR[num, k];
end;
end;
end;
MasL[i,0]:=IntToStr(MasL_Length);
MasR[i,0]:=IntToStr(MasR_Length);
end;
flag:=true;
for i:=0 to Mas_of_Net_Length-1 do
if (MasL1[i,0]<>MasL[i,0])or(MasR1[i,0]<>MasR[i,0]) then
flag:=false;
for i:=0 to Mas_of_Net_Length-1 do
begin
S:=MasL[i,1];
if StrToInt(MasL[i,0])>1 then
for j:=2 to StrToInt(MasL[i,0]) do
begin
S:=S+','+MasL[i, j];
end;
Form2.Memo1.Lines. Add('L'+IntToStr(bb)+'('+Mas_of_Net[i]+')={'+S+'}');
S:=MasR[i,1];
if StrToInt(MasR[i,0])>1 then
for j:=2 to StrToInt(MasR[i,0]) do
begin
S:=S+','+MasR[i, j];
end;
Form2.Memo2.Lines. Add('R'+IntToStr(bb)+'('+Mas_of_Net[i]+')={'+S+'}');
end;
inc(bb);
if flag then step2:=false;
end; //while
///Построение таблицы предшествования
//Получение V=VN+VT
SetLength(V,1);
V[0]:='0';
for i:=0 to Form2.ListBox1.Count-1 do
begin
S:=GetFirst(Form2.ListBox1.Items[i]);
flag:=true;
if StrToInt(V[0])>0 then
for j:=1 to StrToInt(V[0]) do
if V[j]=S then flag:=false;
if flag then
begin
V[0]:=IntToStr(StrToInt(V[0])+1);
SetLength(V, StrToInt(V[0])+1);
V[StrToInt(V[0])]:=S;
end;
k:=Length(S);
S:=GetLeft(Form2.ListBox1.Items[i]);
k:=k+Length(S)+4;
flag:=true;
if StrToInt(V[0])>0 then
for j:=1 to StrToInt(V[0]) do
if V[j]=S then flag:=false;
if flag then
begin
V[0]:=IntToStr(StrToInt(V[0])+1);
SetLength(V, StrToInt(V[0])+1);
V[StrToInt(V[0])]:=S;
end;
S:=copy(Form2.ListBox1.Items[i],k, Length(Form2.ListBox1.Items[i]));
flag:=true;
While flag do
begin
if Length(S)<2 then flag:=false;
num:=1;
A:='';
for j:=1 to Length(S) do
begin
if S[j]=' ' then break;
A:=A+S[j];
inc(num);
end;
flag1:=true;
if (StrToInt(V[0])>0)and(A<>'') then
for j:=1 to StrToInt(V[0]) do
if V[j]=A then flag1:=false;
if (flag1)and(A<>'') then
begin
V[0]:=IntToStr(StrToInt(V[0])+1);
SetLength(V, StrToInt(V[0])+1);
V[StrToInt(V[0])]:=A;
end;
S:=Copy(S, num+1,Length(S));
end;
end;
///Построение таблицы
SetLength(Table, StrToInt(V[0])+2);
for i:=0 to StrToInt(V[0])+1 do
begin
SetLength(Table[i],StrToInt(V[0])+2);
for j:=0 to StrToInt(V[0]) do
Table[i, j]:='';
end;
for i:=1 to StrToInt(V[0]) do
begin
Table[i,0]:=V[i];
Table[0,i]:=V[i];
end;
for i:=0 to Form2.ListBox1.Count-1 do ///отношение =
begin
//посмотреть сколько символов входит в правило после ->
//Если больше одного, тогда для предыдущего и следующего найти номера в таблице и
// установить отношение =
SetLength(V1,0);
S1:='';
num:=3;
S:=Form2.ListBox1.Items[i];
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 7 8 9 10 11 |


