Технический форум

Технический форум (http://www.tehnari.ru/)
-   Помощь студентам (http://www.tehnari.ru/f41/)
-   -   Закончить синтаксический анализатор логических выражений (http://www.tehnari.ru/f41/t52284/)

K0CTbIJIb 26.05.2011 21:45

Закончить синтаксический анализатор логических выражений
 
Дано задание:
Вывести значение логического выражения, заданного в виде строки S. Выражение определяется следующим образом ("T" — True, "F" — False):
<выражение> ::= T | F | And (<операнды>) | Or (<операнды>)
<операнды> ::= <выражение>,<выражение>
Т.Е. должна работать с выражениями такого типа:f and t, t or f, (t or f) or (t and f), и наоборот и т.п.
Вот что у меня есть:
Код HTML:

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  S:string='';
  ind,pn,Dli,Ind2,PrSc,LvSc:integer;
  s2,s1,s3,s4,W1:string;
  Fl:boolean;
implementation
 
{$R *.dfm}
function Filter(h:string):string;
 begin
 h:=StringReplace(h,'not','/',[rfReplaceAll,rfIgnoreCase]);
 h:=StringReplace(h,'and','^',[rfReplaceAll,rfIgnoreCase]);
 h:=StringReplace(h,'or','|',[rfReplaceAll,rfIgnoreCase]);
 result:=h;
 end;
 
 
function ChrToBool(h:char):boolean;
 begin
 if h in['T','t'] then result:=true
                  else result:=false;
 end;
function BoolToStr(h:boolean):char;
 begin
 if h then result:='T'
      else result:='F';
 end;
function LogAnswer(h:string):boolean;
 var
  First,second,redy:boolean;
  i,j1,j2:integer;
 begin
  while pos('(',h)<>0 do
  begin
  redy:=false;
  for i:=1 to length(h)do
  begin
  if redy then continue;
  case h[i] of
    '(':j1:=i;
    ')':begin
        j2:=i;
        redy:=true;
        end;
  end;
  end;
  h:=copy(h,1,j1-1)+BoolToStr(LogAnswer(copy(h,j1+1,j2-j1-1)))+copy(h,j2+1,length(h)-j2+1);
  end;
  while pos('/',h)<>0 do
  begin
  i:=pos('/',h);
  if h[i+1]in['t','T'] then first:=False
                        else first:=True;
  h:=copy(h,1,i-1)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
  end;
  while pos('^',h)<>0 do
  begin
  i:=pos('^',h);
  first:=ChrToBool(h[i-1]);
  second:=ChrToBool(h[i+1]);
  first:=first and second;
  h:=copy(h,1,i-2)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
  end;
  while pos('|',h)<>0 do
  begin
  i:=pos('|',h);
  first:=ChrToBool(h[i-1]);
  second:=ChrToBool(h[i+1]);
  first:=first or second;
  h:=copy(h,1,i-2)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
  end;
  result:=ChrToBool(h[1]);
 end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
s:=edit1.Text;
Dli:=length(s) ;
PrSc:=0;
LvSc:=0;
Fl:=true;
for Ind2:=1 to Dli do
  begin
  if s[Ind2]='(' then PrSC:=PrSc+1;
  if s[Ind2]=')' then LvSc:=LvSc+1;
  end;
  if PrSc > LvSc then begin label2.Caption:='=Nexvataet ZakriV Scobki' ;  Fl:=false ; end;
  if PrSc < LvSc then begin label2.Caption:='=Nexvataet OtkrV Scobki' ; Fl:=false; end;
if Fl<>
false then
  begin
 while (Dli<>1) and (Dli<>4) and (Dli<>7) and (Dli<>8) do
      begin
      Dli:=length(s);
        for ind:=1 to Dli do
            begin
            If s[ind]=')' then
                            begin
                            PN:=Ind;
                            break;
                            end;
            end;
      if s[Pn-2] ='(' then
                        begin
                        W1:='Not'+s[pn-1];
                            if LogAnswer(filter(W1))
                                          then
                                            begin
                                              s[pn-5]:='T';
                                              s3:=copy(s,1,Pn-5);
                                              s4:=copy(s,pn+1,dli);
                                              s:=s3+s4;
                                              Dli:=length(s);
                                            end
                                          else
                                              begin
                                              s[pn-5]:='F';
                                              s3:=copy(s,1,pn-5);
                                              s4:=copy(s,pn+1,dli);
                                              s:=s3+s4;
                                              dli:=length(s);
                                              end;
                          end
                        else      //else1  Esli ne Not
                            begin //else 1.1
                              if s[pn-5]='r'
                                          then
                                          begin//then2.1
                                            W1:=s[pn-3]+'or'+s[pn-1];
                                                if LogAnswer(filter(W1))
                                                  then//then2.2
                                                    begin//then2.2
                                                      s[pn-6]:='T';
                                                      s3:=copy(s,1,pn-6);
                                                      s4:=copy(s,pn+1,dli);
                                                      s:=s3+s4;
                                                      Dli:=length(s);
                                                    end//then2.2
                                                    else//else2.2
                                                      begin//else2.2
                                                      s[pn-6]:='F';
                                                      s3:=copy(s,1,pn-6);
                                                      s4:=copy(s,pn+1,dli);
                                                      s:=s3+s4;
                                                      Dli:=length(s);
                                                      end; //else2.2
                                          end//then2.1
                                          else//else2.1
                                            begin//else2.2
                                            W1:=s[pn-3]+'and'+s[pn-1];
                                                  if LogAnswer(filter(W1))
                                                    then//then2.3
                                                      begin//then2.3
                                                        s[pn-7]:='T';
                                                        s3:=copy(s,1,pn-7);
                                                        s4:=copy(s,pn+1,dli);
                                                        s:=s3+s4;
                                                        Dli:=length(s);
                                                      end//then2.3
                                                    else//else2.3
                                                      begin//else2.3
                                                        s[pn-7]:='F';
                                                        s3:=copy(s,1,pn-7);
                                                        s4:=copy(s,pn+1,dli);
                                                        s:=s3+s4;
                                                        Dli:=length(s);
                                                      end;//else2.3
                                            end;//else2.2
                            end; //End Else 1.1
        Dli:=length(s);
 
      end;  //while
 
  case Dli of
    1:begin//1
            if LogAnswer(filter(s)) then label2.caption:='=True'
                                    else label2.caption:='=False';
        end;//1
    6:begin//6
            if LogAnswer(filter(s)) then label2.caption:='=True'
                                    else label2.caption:='=False';
        end;//6
    7:begin//7
        s1:=s[4]+'or'+s[6];
            if LogAnswer(filter(s1)) then label2.caption:='=True'
                                    else label2.caption:='=False';
        end;//7
    8:begin//8
        s2:=s[5]+'and'+s[7];
          if LogAnswer(filter(s2)) then label2.caption:='=True'
                                    else label2.caption:='=False';
        end;//8
  end;
 end;
end;
 
procedure TForm1.Edit1Change(Sender: TObject);
begin
label2.Caption:='=';
end;
 
end.

Она должна уже работать с простыми выражениями типа t and f и наоборот и аналогично с or. Однако с or не работает - где то ошибка =(
Помогите пожалста найти ошибку и доделать проект.
Заранее благодарю!


Часовой пояс GMT +4, время: 21:23.

Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.