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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Grids, Math;

type
  TAutoState = (AUTO_E,AUTO_H,AUTO_F1,AUTO_F2,AUTO_B1,AUTO_B2,AUTO_B3,AUTO_OR,AUTO_OR1,AUTO_IR,
  AUTO_IR1,AUTO_IR2,AUTO_NR,AUTO_NR1,AUTO_NR2,AUTO_T,AUTO_I,AUTO_P1,AUTO_P2,AUTO_Z,AUTO_C1,AUTO_D1,
  AUTO_DO,AUTO_DO1,AUTO_WH,AUTO_WH1,AUTO_WH2,AUTO_WH3,AUTO_WH4,
  AUTO_BE,AUTO_BE1,AUTO_BE2,AUTO_BE3,AUTO_BE4,AUTO_EN,AUTO_EN1,AUTO_EN2,AUTO_EN3,AUTO_EN4,AUTO_EN5,
  AUTO_PR,AUTO_PR1,AUTO_PR2,AUTO_PR3,AUTO_IF,AUTO_IF1,AUTO_TH,AUTO_TH1,
  AUTO_EL1,AUTO_EL2,AUTO_EL3,AUTO_SH,AUTO_SH1,AUTO_KO);

  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Memo2: TMemo;
    OpenDialog1: TOpenDialog;
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button4: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    Button2: TButton;
    Memo3: TMemo;
    Memo4: TMemo;
    Memo5: TMemo;
    Memo6: TMemo;
    StringGrid2: TStringGrid;
    StringGrid3: TStringGrid;
    Button3: TButton;
    Button5: TButton;
    Edit1: TEdit;
    Memo7: TMemo;
    Button6: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    GroupBox1: TGroupBox;
    Label8: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    GroupBox2: TGroupBox;
    Label14: TLabel;
    Label12: TLabel;
    Label15: TLabel;
    StringGrid4: TStringGrid;
    Label16: TLabel;
    Button7: TButton;
    Memo8: TMemo;
    TabSheet3: TTabSheet;
    TreeView1: TTreeView;
    Edit3: TEdit;
    Button8: TButton;
    Label10: TLabel;
    Button9: TButton;

    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);



  private
    { Private declarations }
  public
    { Public declarations }
    ukazatel,vssrav,knop,knop1:integer;
    srsrav,srsrav1,vssrav1: real;
    inputString:TStringList;
  end;
    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','doEwhile(E)','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','','','',''),
          ('do','E','while','(','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','','','','','',''));
var
  Form1: TForm1;

implementation
 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}

function Hesh(St:string):integer;
var dlina:integer;
begin
dlina:=length(St);
if dlina=0 then Result:=0
  else
begin
  dlina:=length(St);
  if dlina=0 then  Result:=0 else
    begin
     if dlina=1 then
        Result:=ord(St[1])
     else
        Result:=ord(St[1])+ord(St[2]);
    end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
  Memo1.Text:='';
  Memo2.Text:='';
  StringGrid1.Cells[0,0]:='Номер';
  StringGrid1.Cells[1,0]:='Значение';
  StringGrid1.Cells[2,0]:='Лексема';
  srsrav:=0;
  knop:=0;
  vssrav:=0;
  srsrav1:=0;
  knop1:=0;
  vssrav1:=0;
  Edit1.Text:='';
  Edit3.Text:='';
  Memo3.Text:='';
  Memo4.Text:='';
  Memo5.Text:='';
  Memo6.Text:='';
  Memo7.Text:='';
  Memo8.Text:='';
  StringGrid2.Cells[0,0]:='ХФ';
  StringGrid2.Cells[1,0]:='Иден-р';
  StringGrid3.Cells[0,0]:='Номер';
  StringGrid3.Cells[1,0]:='Иден-р';
  StringGrid4.Cells[0,0]:='ХФ';
  StringGrid4.Cells[1,0]:='Иден-р';
  for i:=1 to 510 do
    begin
      StringGrid3.Cells[0,i]:=IntToStr(i);
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
Const N1=26;
      N2=21;
      N3=26;
      N4=5;
var ind,pos,dl,jl,i,j,k,fl,ab,zn,sch,jkj,jll:integer;
    st,str,sInput,iden:string;
    iState:TAutoState;
    dlina,hash,f:integer;
    CepofV1:array [1..100]of integer;
    p,stek,d:TStringList;
    i1,i2,i4,j2,fl2,vetv12,u:integer;
    stroka,o1,o2,o3,o4,o5,o6,pr:string;
    MyTreeNode:TTreeNode;
    nodeTree: TTreeNode;

label 1,2,M1,M2,Myend,zap,tree;


begin
   u:=1;
   inputString:=TStringList.Create;
   inputString.Sorted:=False;
   sch:=0;
   if OpenDialog1.Execute then Memo1.Lines.LoadFromFile(OpenDialog1.FileName)  ;
     for i:=0 to 2 do
      for j:=1 to 999 do
        StringGrid1.Cells[i,j]:=' ';
     j:=Memo1.Lines.Count;
     ind:=1;
     pos:=1;
     for i:=1 to j do
          begin
                str:='';
                sInput:=Memo1.Lines[i-1];
                sInput:=sInput+' ';
                dl:=Length(sInput);
                iState:=AUTO_H;
                fl:=0;
                zn:=0;
             for jkj:=1 to dl do
              begin
               st:=sInput[jkj];
               case iState of
                AUTO_H:
                case sInput[jkj] of
                     '{': begin iState:=AUTO_KO; end;
                     '(': begin iState:=AUTO_F1;str:=str+st; end;
                     ')': begin iState:=AUTO_F2;str:=str+st; end;
                     ';': begin iState:=AUTO_T;str:=str+st; end;
                     'o': begin iState:=AUTO_OR;str:=str+st; end;
                     'a': begin iState:=AUTO_IR;str:=str+st; end;
                     'n': begin iState:=AUTO_NR;str:=str+st; end;
                     'd': begin iState:=AUTO_DO;str:=str+st; end;
                     'w': begin iState:=AUTO_WH;str:=str+st; end;
                     'b': begin iState:=AUTO_BE;str:=str+st; end;
                     'e': begin iState:=AUTO_EN;str:=str+st; end;
                     'p': begin iState:=AUTO_PR;str:=str+st; end;
                     'i': begin iState:=AUTO_IF;str:=str+st; end;
                     't': begin iState:=AUTO_TH;str:=str+st; end;
                     'c','f'..'h','j'..'m','q'..'s','u','v','x'..'z': begin iState:=AUTO_I;str:=str+st; end;
                     ':': begin iState:=AUTO_P1;str:=str+st; end;
                     '+','-','*','/':begin iState:=AUTO_Z;str:=str+st; end;
                     '>': begin iState:=AUTO_B1;str:=str+st; end;
                     '<': begin iState:=AUTO_B2;str:=str+st; end;
                     '=': begin iState:=AUTO_B3;str:=str+st; end;
                     '1'..'9': begin iState:=AUTO_C1;str:=str+st; end;
                     '0': begin iState:=AUTO_D1;str:=str+st; end;
                     'A'..'F': begin iState:=AUTO_C1;str:=str+st; end;
                     ' ': begin iState:=AUTO_H;str:='';end;

                     else begin iState:= AUTO_E;str:=str+st; end;
                     end;

     AUTO_TH:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'h': begin iState:=AUTO_TH1;str:=str+st;end;
     'a'..'g','i'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_TH1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'e': begin iState:=AUTO_BE3;str:=str+st;end;
      'a'..'d','f'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_IF:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'f': begin iState:=AUTO_IF1;str:=str+st;end;
     'a'..'e','g'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_IF1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_PR:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'r': begin iState:=AUTO_PR1;str:=str+st;end;
     'a'..'q','s'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_PR1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'o': begin iState:=AUTO_PR2;str:=str+st;end;
      'a'..'n','p'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_PR2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'g': begin iState:=AUTO_PR3;str:=str+st;end;
     'a'..'f','h'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_PR3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
      'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EN:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'n': begin iState:=AUTO_EN1;str:=str+st;end;
     'l': begin iState:=AUTO_EL1;str:=str+st;end;
     'a'..'k','m','o'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EN1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'd': begin iState:=AUTO_EN2;str:=str+st;end;
      'a'..'c','e'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EN2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     '.': begin iState:=AUTO_EN3;str:=str+st;end;
     'i': begin iState:=AUTO_EN4;str:=str+st;end;
     'a'..'h','j'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EN3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_EN4:
     case sInput[jkj] of
     'f': begin iState:=AUTO_EN5;str:=str+st;end;
     'a'..'e','g'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EN5:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_EL1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     's': begin iState:=AUTO_EL2;str:=str+st;end;
      'a'..'r','t'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EL2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     'e': begin iState:=AUTO_EL3;str:=str+st;end;
     'a'..'d','f'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_EL3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_BE:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'e': begin iState:=AUTO_BE1;str:=str+st;end;
     'a'..'d','f'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_BE1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'g': begin iState:=AUTO_BE2;str:=str+st;end;
      'a'..'f','h'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;
     AUTO_BE2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'i': begin iState:=AUTO_BE3;str:=str+st;end;
     'a'..'h','j'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_BE3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'n': begin iState:=AUTO_BE4;str:=str+st;end;
      'a'..'m','o'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_BE4:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_WH:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'h': begin iState:=AUTO_WH1;str:=str+st;end;
     'a'..'g','i'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_WH1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'i': begin iState:=AUTO_WH2;str:=str+st;end;
      'a'..'h','j'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;
     AUTO_WH2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'l': begin iState:=AUTO_WH3;str:=str+st;end;
     'a'..'k','m'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_WH3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'e': begin iState:=AUTO_WH4;str:=str+st;end;
      'a'..'d','f'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_WH4:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
      'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_DO:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'o': begin iState:=AUTO_DO1;str:=str+st;end;
     'a'..'n','p'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_DO1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=15;end;
      'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_NR:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'o': begin iState:=AUTO_NR1;str:=str+st;end;
     'a'..'n','p'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_NR1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     't': begin iState:=AUTO_NR2;str:=str+st;end;
     'a'..'s','u'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_NR2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=14;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_IR:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'n': begin iState:=AUTO_IR1;str:=str+st;end;
     'a'..'m','o'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_IR1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'd': begin iState:=AUTO_IR2;str:=str+st;end;
     'a'..'c','e'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_IR2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=13;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_OR:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;end;
     'r': begin iState:=AUTO_OR1;str:=str+st;end;
     'a'..'q','s'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_OR1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=12;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_B1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=9;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_B2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=10;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_B3:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=11;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_F1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=1;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_F2:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=7;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_KO:
     case sInput[jkj] of
     '}':begin iState:=AUTO_H;end;
     else begin iState:=AUTO_KO;end;
     end;

     AUTO_T:
     case sInput[jkj] of
     ' ':begin iState:=AUTO_H;fl:=1;zn:=8;   end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_I:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=2;sch:=sch+1;end;
     'a'..'z','0'..'9':begin iState:=AUTO_I;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_P1:
     case sInput[jkj] of
     '=':begin iState:=AUTO_P2;str:=str+st;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_P2:
     case sInput[jkj] of
     ' ':begin iState:=AUTO_H;fl:=1;zn:=3;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_Z:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=4;end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_C1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=5;end;
     '0'..'9':begin iState:=AUTO_C1;str:=str+st; end;
     else begin iState:=AUTO_E; str:=str+st; end;
     end;

     AUTO_D1:
     case sInput[jkj] of
     ' ': begin iState:=AUTO_H;fl:=1;zn:=6; end;
     'h': begin iState:=AUTO_SH;str:=str+st; end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_SH:
     case sInput[jkj] of
     '1'..'9','A'..'F': begin iState:=AUTO_SH1;str:=str+st; end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;

     AUTO_SH1:
     case sInput[jkj] of
     ' ':begin iState:=AUTO_H;fl:=1;zn:=16;end;
     '0'..'9','A'..'F': begin iState:=AUTO_SH1;str:=str+st; end;
     else begin iState:=AUTO_E;str:=str+st; end;
     end;


                                                          AUTO_E:      begin
                                          if sInput[jkj]<>' 'then
                                                              begin
                                                              iState:=AUTO_E;
                                                              str:=str+st;
                                                              end
                                                           else
                                                              begin
                                                              iState:=AUTO_H;
                                                              Memo8.Lines.Append(str);
                                                              str:='';
                                                              end;


     end;
     end;
                 if fl=1 then
                    begin
                     StringGrid1.Cells[0,ind]:=IntToStr(ind);
                     StringGrid1.Cells[1,ind]:=str;
                                          case zn of
                                                1: begin StringGrid1.Cells[2,ind]:='Открывающаяся скобка';inputString.Add('(');end;
                                                2: begin StringGrid1.Cells[2,ind]:='Идентификатор';inputString.Add('a');end;
                                                3: begin StringGrid1.Cells[2,ind]:='Оператор присваивания';inputString.Add(':=');end;
                                                4: begin StringGrid1.Cells[2,ind]:='Арифметическая операция';inputString.Add(StringGrid1.Cells[1,ind]); end;
                                                5, 6: begin StringGrid1.Cells[2,ind]:='Целое число';inputString.Add('a');end;
                                                7: begin StringGrid1.Cells[2,ind]:='Закрывающаяся скобка';inputString.Add(')');end;
                                                8: begin StringGrid1.Cells[2,ind]:='Разделитель';inputString.Add(';');end;
                                                9: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "больше"';inputString.Add('>');end;
                                                10: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "меньше"';inputString.Add('<');end;
                                                11: begin StringGrid1.Cells[2,ind]:='Операвтор сравнения "равно"';inputString.Add('=');end;
                                                12: begin StringGrid1.Cells[2,ind]:='Логическое "или"';inputString.Add('or');end;
                                                13: begin StringGrid1.Cells[2,ind]:='Логическое "и"';inputString.Add('and');end;
                                                14: begin StringGrid1.Cells[2,ind]:='Логическое "не"';inputString.Add('not');end;
                                                15: begin StringGrid1.Cells[2,ind]:='Ключевое слово';inputString.Add(StringGrid1.Cells[1,ind]);end;
                                                16: begin StringGrid1.Cells[2,ind]:='Шестнадцатиричная константа';inputString.Add('a');end;
                                                end;

                     str:='';
                     ind:=ind+1;
                     fl:=0;
                    end;
                    end;
     end;
  for i:=1 to 999 do
    begin
      if StringGrid1.Cells[2,i]='Идентификатор' then
        begin
          iden:=StringGrid1.Cells[1,i];
          Memo2.Lines.Append(iden);
        end;
    end;

      for i:=1 to 510 do
    begin
      StringGrid4.Cells[0,i]:=IntToStr(i);
    end;
   j:=0;
   k:=2;
   ukazatel:=0;
   dlina:=memo2.Lines.Count;
   if dlina<>0 then
     begin
      for i:=0 to dlina-1 do
         begin
           stroka:=memo2.Lines[i];
           hash:=Hesh(stroka);
           if (StringGrid4.cells[1,hash])='' then
             begin
              StringGrid4.cells[1,hash]:=stroka;
              end
           else
             begin
  1:           if (StringGrid4.cells[1,hash])=stroka then
                 begin
                   memo4.Lines[j]:='Удален повтор '+stroka;
                   j:=j+1;
                   ukazatel:=ukazatel+1;
                   goto 2;
                 end
               else
                 begin
                  hash:=(Hesh(stroka)*k) mod 509;
                  if (StringGrid4.cells[1,hash])='' then
                    begin
                      StringGrid4.cells[1,hash]:=stroka;
                    end
                  else
                    begin
                      k:=k+1;
                      goto 1;
                    end;
                 end;
             end;
 2:      end;
     end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;

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

procedure TForm1.Button3Click(Sender: TObject);
var i,k,dlina,hash,j,f,kol:integer;
    stroka:string;
    a,b,buf:string;
    max1,lk,ss,li,min,max,p,lj,sr,lf:integer;
label 1,2;
begin
      for i:=1 to 510 do
    begin
      StringGrid2.Cells[0,i]:=IntToStr(i);
    end;
   j:=0;
   k:=2;
   kol:=0;
   ukazatel:=0;
   dlina:=memo3.Lines.Count;
   if dlina<>0 then
     begin
      for i:=0 to dlina-1 do
         begin
           stroka:=memo3.Lines[i];
           hash:=Hesh(stroka);
           if (StringGrid2.cells[1,hash])='' then
             begin
              StringGrid2.cells[1,hash]:=stroka;
              end
           else
             begin
  1:           if (StringGrid2.cells[1,hash])=stroka then
                 begin
                   memo4.Lines[j]:='Удален повтор '+stroka;
                   j:=j+1;
                   ukazatel:=ukazatel+1;
                   goto 2;
                 end
               else
                 begin
                  kol:=kol+1;
                  hash:=(Hesh(stroka)*k) mod 509;
                  if (StringGrid2.cells[1,hash])='' then
                    begin
                      StringGrid2.cells[1,hash]:=stroka;
                    end
                  else
                    begin
                      k:=k+1;
                      goto 1;
                    end;
                 end;
             end;
 2:      end;
     end;
    Label8.Caption:='Кол-во коллизий                   '+inttostr(kol);

for li:=1 to 510 do
 begin
   StringGrid3.Cells[1,li]:='';
 end;
lk:=0;
p:=Memo3.Lines.Count;
if p>=1 then
  begin
    StringGrid3.Cells[1,1]:=Memo3.Lines[0];
    memo6.Lines[0]:=Memo3.Lines[0];
  end;
for li:=1 to p-1 do
 begin
   lf:=0;
   min:=0;
   max:=510;
   a:=Memo3.Lines[li];
   while max-min<>1 do
    begin
      sr:=(max+min) div 2;
      if  StringGrid3.Cells[1,sr]=a then
       begin
         Memo4.Lines[lk]:='Удален повтор '+a;
         lk:=lk+1;
         lf:=1;
         break;
       end
        else
           if (StringGrid3.Cells[1,sr]>a) or (StringGrid3.Cells[1,sr]='') then
              max:=sr
           else
              min:=sr;
    end;
    lj:=509;
   if lf=0 then
     begin  while lj>min do
       begin
         StringGrid3.Cells[1,lj+1]:=StringGrid3.Cells[1,lj];
         lj:=lj-1;
       end;
       StringGrid3.Cells[1,max]:=a;
       memo6.Lines.Append(a)
     end;
 end;
   while  (StringGrid3.Cells[1,max1]<>'') do
    begin
      max1:=max1+1;
    end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var max1,k,hash,dlina,i,srav,j,si: integer;
    stroka: string;
    lk,ss,min,max,p,sr,sravn:integer;
    a:string;
label met1,met2,met3;
begin
  memo5.Lines[0]:='';
  memo7.Lines[0]:='';
  k:=2;
  srav:=0;
  stroka:=Edit1.Text;
  if stroka='' then
    begin
      memo5.Lines[0]:='Поле пусто!';
      memo7.Lines[0]:='Поле пусто!';
      goto met3;
    end;
  dlina:=memo3.Lines.Count;
  hash:=Hesh(stroka);
  for i:=1 to 510 do
    begin
      srav:=srav+1;

      if StringGrid2.Cells[0,hash]=IntToStr(hash) then
        begin
          if stroka=StringGrid2.Cells[1,hash] then
            begin
              memo5.Lines[0]:='Иден-р  '+stroka+'  найден! Его номер '+ IntToStr(hash);
              goto met1;
            end
          else
            begin
              for j:=1 to 510 do
               begin
                srav:=srav+1;
                hash:=(Hesh(stroka)*k) mod 509;
                k:=k+1;
                if StringGrid2.Cells[0,hash]=IntToStr(hash) then
                  begin
                    if stroka=StringGrid2.Cells[1,hash] then
                      begin
                        memo5.Lines[0]:='Иден-р  '+stroka+'  найден! Его номер '+ IntToStr(hash);
                        goto met1;
                      end;
                  end
                else memo5.Lines[0]:='Иден-р  '+stroka+'  не найден!';
               end;
            end;
        end
      else begin
        memo5.Lines[0]:='Иден-р  '+stroka+'  не найден!';
        goto met1; end;
    end;
met1: Label9.Caption:='Кол-во сравнений                 '+inttostr(srav);

dlina:=memo6.Lines.Count;
for si:=1 to dlina do
 begin
  k:=2;
  srav:=0;
  stroka:=memo6.Lines[si];
  hash:=Hesh(stroka);
  for i:=1 to 510 do
    begin
      srav:=srav+1;
      if StringGrid2.Cells[0,hash]=IntToStr(hash) then
        begin
          if stroka=StringGrid2.Cells[1,hash] then
            begin
              goto met2;
            end
          else
            begin
              for j:=1 to 510 do
               begin
                srav:=srav+1;
                hash:=(Hesh(stroka)*k) mod 509;
                k:=k+1;
                if StringGrid2.Cells[0,hash]=IntToStr(hash) then
                  begin
                    if StringGrid2.Cells[1,hash]=stroka then
                      begin
                          goto met2;
                      end;
                  end;
               end;
            end;
        end
      else
        goto met2;
    end;
met2:      vssrav:=vssrav+srav;
 end;
      srsrav:=vssrav/dlina;
      Label11.Caption:='Среднее кол-во сравнений  '+floattostr(srsrav);
      vssrav:=0;
      srsrav:=0;

  knop1:=knop1+1;
  sravn:=0;
  max1:=1;
  dlina:=Memo6.Lines.Count;
  while  (StringGrid3.Cells[1,max1]<>'') do
    begin
      max1:=max1+1;
    end;
  for i:=1 to 10 do
    begin
     min:=0;
     max:=max1;
     a:=Edit1.Text;
     while (StringGrid3.Cells[1,sr]<>a) and (max-min<>1) do
       begin
        sravn:=sravn+1;
        sr:=(max+min) div 2;
        if (StringGrid3.Cells[1,sr]>a) or (StringGrid3.Cells[1,sr]='') then
          begin
            max:=sr;
          end
        else
          begin
            min:=sr;
          end;
       end;
    end;
  if  StringGrid3.Cells[1,sr]=a then
    Memo7.Lines[0]:='Иден-р  '+a+'  найден! Его номер  '+ IntToStr(sr)
  else Memo7.Lines[0]:='Иден-р '+a+' не найден!';
    dlina:=dlina-(dlina-max1);
    srsrav1:=1+log2(dlina);
    Label12.Caption:='Кол-во сравнений                  '+inttostr(sravn);
    Label14.Caption:='Среднее кол-во сравнений  '+floattostr(srsrav1);

met3: end;

procedure TForm1.Button6Click(Sender: TObject);
var i: integer;
begin
  for i:=1 to 510 do StringGrid2.Cells[1,i]:='';
  for i:=1 to 510 do StringGrid2.Cells[2,i]:='';
  for i:=1 to 510 do StringGrid3.Cells[1,i]:='';
  for i:=0 to Memo6.Lines.Count-1 do Memo6.Text:='';
  for i:=0 to Memo3.Lines.Count-1 do Memo3.Text:='';
  for i:=0 to Memo4.Lines.Count-1 do Memo4.Text:='';
  for i:=0 to Memo5.Lines.Count-1 do Memo5.Text:='';
  for i:=0 to Memo7.Lines.Count-1 do Memo7.Text:='';
  Label8.Caption:='Кол-во коллизий';
  Label9.Caption:='Кол-во сравнений';
  Label11.Caption:='Среднее кол-во сравнений';
  Label12.Caption:='Кол-во сравнений';
  Label14.Caption:='Среднее кол-во сравнений';
  Edit1.Text:='';
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
   if OpenDialog1.Execute then Memo3.Lines.LoadFromFile(OpenDialog1.FileName)  ;
end;

procedure TForm1.Button8Click(Sender: TObject);
label Myend,zap,tree;

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

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

if inputString.Count=0 then
                       begin
                       ShowMessage('Таблица лексем или исходный файл не загружены');
                       exit
                       end;
inputString.Add('!');
{Объявляем стэк}
SymbStack:=TStringList.Create;
SymbStack.Sorted:=False;
SymbStack.Add('!');
{SymbStack.Add(inputString[0]);
{inputString.Delete(0);  }
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:Edit3.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.
Соседние файлы в папке Курсовая по СПО ГОТОВАЯ 33