Changeset 17


Ignore:
Timestamp:
Apr 9, 2009, 11:16:15 AM (16 years ago)
Author:
george
Message:
  • Upraveno: Analýza gramatiky .
Location:
branches
Files:
4 added
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • branches/Analyzátor gramatiky

    • Property svn:ignore set to
      *.exe
      *.dcu
  • branches/Analyzátor gramatiky/PascalCompiler.dpr

    r10 r17  
    66  Forms,
    77  UMainForm in 'UMainForm.pas' {MainForm},
    8   UGrammer in 'UGrammer.pas';
     8  UGrammer in 'UGrammer.pas',
     9  UProgram in 'UProgram.pas';
    910
    1011{$R *.res}
    1112
    1213begin
     14  {$WARN SYMBOL_PLATFORM OFF}
     15  ReportMemoryLeaksOnShutdown := DebugHook <> 0;
     16  {$WARN SYMBOL_PLATFORM ON}
    1317  Application.Initialize;
    1418  Application.CreateForm(TMainForm, MainForm);
  • branches/Analyzátor gramatiky/UGrammer.pas

    r10 r17  
    44
    55uses
    6   Classes, ComCtrls, SysUtils;
     6  Classes, ComCtrls, SysUtils, Math, UGrammerRules;
    77
    88type
     
    3737  TRuleType = (rtSequence, rtAlternative);
    3838  TRuleItemType = (itTerminal, itNonterminal);
    39 
    40   TProgramItem = class
    41   public
    42     RuleBefore: TGrammerRule;
    43     Items: TList; // of TProgramItem;
    44     ItemType: TRuleItemType;
    45     Rule: TGrammerRule;
    46     Value: string;
    47     procedure Delete;
    48     procedure DeleteItem(Index: Integer);
    49     procedure DeleteNonterminal(ARule: TGrammerRule);
    50     procedure DeleteEmpty;
    51     procedure MergeNonterminal(ARule: TGrammerRule);
    52     procedure Join(ARule: TGrammerRule; ItemIndex: Integer);
    53     function MergeToTerminal: string;
    54     constructor Create;
    55     destructor Destroy; override;
    56     procedure Assign(Source: TProgramItem);
    57   end;
    58 
    59   TProgram = class
    60   public
    61     TopItem: TProgramItem;
    62     constructor Create;
    63     procedure Insert(Path: TGrammerPath; Character: Char);
    64     destructor Destroy; override;
    65   end;
    6639
    6740  TGrammerItem = class
     
    10578    constructor Create;
    10679    procedure ClearProcessed;
    107     procedure Parse(Text: string; var ParsedProgram: TProgram);
    10880    procedure CorrectRuleLinks;
    10981    destructor Destroy; override;
     
    11284
    11385implementation
    114 
    115 uses
    116   UMainForm;
    11786
    11887{ TGrammerRule }
     
    289258  Rules.Free;
    290259  inherited;
    291 end;
    292 
    293 procedure TGrammer.Parse(Text: string; var ParsedProgram: TProgram);
    294 var
    295   Path: TGrammerPath;
    296   I, II: Integer;
    297   Q: Integer;
    298   C: Integer;
    299   Scope: TPossibleCharacters;
    300   UseIndex: Integer;
    301   UseCharIndex: Integer;
    302   UseRule: TGrammerRule;
    303   ExpectedCharacters: string;
    304   Level: array of Integer;
    305   LevelLength: Integer;
    306   LevelIsLeft: Boolean;
    307 begin
    308   SetLength(Path.Items, Length(Path.Items) + 1);
    309   with Path.Items[High(Path.Items)] do begin
    310     Rule := TopRule;
    311     ItemIndex := 0;
    312     CharIndex := -1;
    313   end;
    314 
    315   for I := 1 to Length(Text) do begin
    316     if (Text[I] <> #13) and (Text[I] <> #10) then begin
    317       UseIndex := Path.Items[High(Path.Items)].ItemIndex;
    318       UseCharIndex := Path.Items[High(Path.Items)].CharIndex;
    319       UseRule := Path.Items[High(Path.Items)].Rule;
    320       SetLength(Path.Items, Length(Path.Items) - 1);
    321       SetLength(Scope.Items, 0);
    322       ClearProcessed;
    323       UseRule.GetPossibleCharacters(Path, Scope, UseIndex, UseCharIndex);
    324       C := Length(Scope.Items);
    325 
    326       SetLength(Level, 0);
    327       for II := 0 to High(Scope.Items) do with Scope.Items[II] do begin
    328         if (Character = Text[I]) then begin
    329           if Length(RulePath.Items) > Length(Level) then begin
    330             LevelLength := Length(Level);
    331             SetLength(Level, Length(RulePath.Items));
    332             for Q := LevelLength to High(Level) do
    333               Level[Q] := High(Integer);
    334           end;
    335           LevelIsLeft := True;
    336           for Q := 0 to High(Level) do begin
    337             if Level[Q] > RulePath.Items[Q].ItemIndex then begin
    338               LevelIsLeft := False;
    339               Break;
    340             end;
    341             if Level[Q] < RulePath.Items[Q].ItemIndex then Break;
    342           end;
    343           if (not LevelIsLeft) or (Length(RulePath.Items) > Length(Level)) then begin
    344             SetLength(Level, Length(RulePath.Items));
    345             for Q := 0 to High(Level) do
    346               Level[Q] := RulePath.Items[Q].ItemIndex;
    347             C := II;
    348           end;
    349         end;
    350       end;
    351 
    352       if C < Length(Scope.Items) then begin
    353         Path.Assign(Scope.Items[C].RulePath);
    354         for II := 0 to Length(Path.Items) - 1 do with Path.Items[II] do begin
    355           if Affected then Inc(CharIndex);
    356         end;
    357         ParsedProgram.Insert(Path, Scope.Items[C].Character);
    358         for II := 0 to Length(Path.Items) - 1 do with Path.Items[II] do begin
    359           Affected := False;
    360         end;
    361         //Path.Next;
    362       end else begin
    363         ExpectedCharacters := '';
    364         for II := 0 to Length(Scope.Items) - 1 do
    365           ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character;
    366         //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".');
    367         MainForm.StatusBar1.SimpleText := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".';
    368         break;
    369       end;
    370     end;
    371   end;
    372 
    373 end;
    374 
    375 { TProgram }
    376 
    377 constructor TProgram.Create;
    378 begin
    379   TopItem := TProgramItem.Create;
    380 end;
    381 
    382 destructor TProgram.Destroy;
    383 begin
    384   TopItem.Free;
    385   inherited;
    386 end;
    387 
    388 procedure TProgram.Insert(Path: TGrammerPath; Character: Char);
    389 var
    390   I: Integer;
    391   SelectedProgramItem: TProgramItem;
    392 begin
    393   SelectedProgramItem := TopItem;
    394   for I := 0 to High(Path.Items) do with Path.Items[I] do begin
    395     SelectedProgramItem.ItemType := itNonterminal;
    396     if not Assigned(SelectedProgramItem.Rule) then SelectedProgramItem.Rule := Rule;
    397     if SelectedProgramItem.Rule = Rule then begin
    398 //      if (Rule.RuleType = rtSequence) or (SelectedProgramItem.Rule = SelectedProgramItem.RuleBefore) then begin
    399         if SelectedProgramItem.Items.Count < (CharIndex + 1) then
    400           SelectedProgramItem.Items.Count := CharIndex + 1;
    401         if not Assigned(SelectedProgramItem.Items[CharIndex]) then
    402           SelectedProgramItem.Items[CharIndex] := TProgramItem.Create;
    403         SelectedProgramItem := TProgramItem(SelectedProgramItem.Items[CharIndex]);
    404 (*      end else begin
    405         SelectedProgramItem.Items.Count := SelectedProgramItem.Items.Count + 1;
    406         if not Assigned(SelectedProgramItem.Items[SelectedProgramItem.Items.Count - 1]) then
    407           SelectedProgramItem.Items[SelectedProgramItem.Items.Count - 1] := TProgramItem.Create;
    408         SelectedProgramItem := TProgramItem(SelectedProgramItem.Items[SelectedProgramItem.Items.Count - 1]);
    409       end;
    410 *)
    411     end;
    412   end;
    413   with SelectedProgramItem do begin
    414     ItemType := itTerminal;
    415     Value := Character;
    416   end;
    417 end;
    418 
    419 procedure TProgramItem.MergeNonterminal(ARule: TGrammerRule);
    420 var
    421   I: Integer;
    422 begin
    423   for I := 0 to Items.Count - 1 do with TProgramItem(Items[I]) do begin
    424     if ItemType = itNonterminal then begin
    425       if Rule.Name = ARule.Name then begin
    426         ItemType := itTerminal;
    427         Value := MergeToTerminal;
    428       end else MergeNonterminal(ARule);
    429     end;
    430   end;
    431 end;
    432 
    433 { TProgramItem }
    434 
    435 procedure TProgramItem.Assign(Source: TProgramItem);
    436 begin
    437  
    438 end;
    439 
    440 constructor TProgramItem.Create;
    441 begin
    442   Items := TList.Create;
    443 end;
    444 
    445 procedure TProgramItem.Delete;
    446 var
    447   I: Integer;
    448 begin
    449   for I := 0 to Items.Count - 1 do with TProgramItem(Items[I]) do begin
    450     if ItemType = itNonterminal then Delete;
    451   end;
    452 end;
    453 
    454 procedure TProgramItem.DeleteEmpty;
    455 var
    456   I: Integer;
    457 begin
    458   I := 0;
    459   while I < Items.Count do with TProgramItem(Items[I]) do begin
    460     if ItemType = itNonterminal then begin
    461       if Items.Count = 0 then begin
    462         Self.DeleteItem(I);
    463       end else begin
    464         DeleteEmpty;
    465         Inc(I);
    466       end;
    467     end else Inc(I);
    468   end;
    469 end;
    470 
    471 procedure TProgramItem.DeleteItem(Index: Integer);
    472 begin
    473   if TProgramItem(Items[Index]).ItemType = itNonterminal then
    474     TProgramItem(Items[Index]).Delete;
    475   Items.Delete(Index);
    476 end;
    477 
    478 procedure TProgramItem.DeleteNonterminal(ARule: TGrammerRule);
    479 var
    480   I: Integer;
    481 begin
    482   I := 0;
    483   while I < Items.Count do with TProgramItem(Items[I]) do begin
    484     if ItemType = itNonterminal then begin
    485       if Rule.Name = ARule.Name then begin
    486         Self.DeleteItem(I);
    487       end else begin
    488         DeleteNonterminal(ARule);
    489         Inc(I);
    490       end;
    491     end else Inc(I);
    492   end;
    493 end;
    494 
    495 destructor TProgramItem.Destroy;
    496 var
    497   I: Integer;
    498 begin
    499   for I := 0 to Items.Count - 1 do TProgramItem(Items[I]).Free;
    500   Items.Free;
    501   inherited;
    502 end;
    503 
    504 procedure TProgramItem.Join(ARule: TGrammerRule; ItemIndex: Integer);
    505 var
    506   SubProgramItem: TProgramItem;
    507   I: Integer;
    508 begin
    509   if (ItemType = itNonterminal) and (Rule = ARule) then begin
    510     SubProgramItem := TProgramItem(Items[ItemIndex]);
    511     RuleBefore := SubProgramItem.RuleBefore;
    512     Rule := SubProgramItem.Rule;
    513     ItemType := SubProgramItem.ItemType;
    514     Value := SubProgramItem.Value;
    515     Items[ItemIndex] := nil;
    516     for I := 0 to Items.Count - 1 do
    517       if Assigned(Items[I]) then TProgramItem(Items[I]).Free;
    518     Items := SubProgramItem.Items;
    519   end;
    520   for I := 0 to Items.Count - 1 do
    521     TProgramItem(Items[I]).Join(ARule, ItemIndex);
    522 end;
    523 
    524 function TProgramItem.MergeToTerminal: string;
    525 var
    526   I: Integer;
    527 begin
    528   Result := '';
    529   for I := 0 to Items.Count - 1 do with TProgramItem(Items[I]) do begin
    530     if ItemType = itTerminal then Result := Result + Value
    531       else Result := Result + MergeToTerminal;
    532   end;
    533260end;
    534261
  • branches/Analyzátor gramatiky/UMainForm.pas

    r10 r17  
    55uses
    66  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    7   Dialogs, StdCtrls, ComCtrls, UGrammer;
     7  Dialogs, StdCtrls, ComCtrls, UGrammer, UProgram;
    88
    99const
     
    5656  SourceProgram.Free;
    5757  SourceProgram := TProgram.Create;
    58   Grammer.Parse(Memo1.Text, SourceProgram);
     58  SourceProgram.Parse(Grammer, Memo1.Text);
    5959  ShowProgramTree(SourceProgram);
    6060end;
     
    7171  PascalProgram.Free;
    7272  PascalProgram := TProgram.Create;
    73   PascalGrammer.Parse(Memo2.Text, PascalProgram);
     73  PascalProgram.Parse(PascalGrammer, Memo2.Text);
    7474  with PascalProgram.TopItem, PascalGrammer do begin
    7575//    MergeNonterminal(TGrammerRule(Rules[7]));
     
    267267      Name := 'ConcatenationBlock';
    268268      RuleType := rtSequence;
    269       AddRule(WhiteSpace, True, True);
    270       AddRule(Expression, True, False);
     269      AddRule(Expression, False, False);
     270      AddRule(WhiteSpace, True, True);
    271271    end;
    272272    Rules.Add(ConcatenationBlock);
     
    285285      Name := 'SeparationBlock';
    286286      RuleType := rtSequence;
    287       AddRule(WhiteSpace, True, True);
     287      //AddRule(WhiteSpace, True, True);
    288288      AddTerminal('|', False, False);
    289289      AddRule(WhiteSpace, True, True);
     
    296296      RuleType := rtSequence;
    297297      AddRule(Concatenation, False, False);
    298       AddRule(WhiteSpace, True, True);
     298      //AddRule(WhiteSpace, True, True);
    299299      AddRule(SeparationBlock, True, True);
    300300    end;
     
    311311      AddRule(WhiteSpace, True, True);
    312312      AddRule(Separation, False, False);
    313       AddRule(WhiteSpace, True, True);
     313      //AddRule(WhiteSpace, True, True);
    314314      AddTerminal('.', False, False);
    315315      AddRule(WhiteSpace, True, True);
  • branches/DelphiToC

    • Property svn:ignore
      •  

        old new  
        44*.~dsk
        55*.dcu
         6ProjectGroup1.bdsgroup
Note: See TracChangeset for help on using the changeset viewer.