Ignore:
Timestamp:
Nov 11, 2009, 11:11:13 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Seznamy objektů přepsány na TFPGObjectList a použití generických typů.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Analyzátor gramatiky/UMainForm.pas

    r28 r29  
    66
    77uses
    8   LCLIntf, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     8  LCLIntf, SysUtils, Classes, Graphics, Controls, Forms,
    99  Dialogs, StdCtrls, ComCtrls, LResources, UGrammer, UProgram;
    1010
    1111const
    12   DefaultGrammerFileName: string = 'grammer/test.grm';
    13   DefaultPascalFileName: string = 'pascal/test.pas';
     12  DefaultGrammerFileName: string = '../../grammer/test.grm';
     13  DefaultPascalFileName: string = '../../pascal/test.pas';
    1414
    1515type
     
    3232  private
    3333    procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem);
    34     { Private declarations }
    3534  public
    3635    Grammer: TGrammer;
     
    5049  MainForm: TMainForm;
    5150
     51
    5252implementation
    53 
    5453
    5554procedure TMainForm.Button1Click(Sender: TObject);
     
    8887begin
    8988  with SourceProgram.TopItem do begin
    90     MergeNonterminal(TGrammerRule(Grammer.Rules[7]));
    91     MergeNonterminal(TGrammerRule(Grammer.Rules[9]));
    92     DeleteNonterminal(TGrammerRule(Grammer.Rules[0]));
     89    MergeNonterminal(Grammer.Rules[7]);
     90    MergeNonterminal(Grammer.Rules[9]);
     91    DeleteNonterminal(Grammer.Rules[0]);
    9392    DeleteEmpty;
    94     Join(TGrammerRule(Grammer.Rules[17]), 1);
    95     Join(TGrammerRule(Grammer.Rules[15]), 0);
    96     Join(TGrammerRule(Grammer.Rules[14]), 0);
     93    Join(Grammer.Rules[17], 1);
     94    Join(Grammer.Rules[15], 0);
     95    Join(Grammer.Rules[14], 0);
    9796  end;
    9897  ShowProgramTree(SourceProgram);
     
    109108  C: Char;
    110109  I: Integer;
    111   SourceCode: string;
    112110begin
    113111  PascalGrammer := TGrammer.Create;
     
    118116    with WhiteSpace do begin
    119117      Name := 'WhiteSpace';
     118      Token := True;
    120119      RuleType := rtAlternative;
    121120      AddTerminal(' ', False, False);
     
    161160    with Number do begin
    162161      Name := 'Number';
     162      Token := True;
    163163      RuleType := rtSequence;
    164164      AddTerminal('-', True, False);
     
    180180    with Identifier do begin
    181181      Name := 'Identifier';
     182      Token := True;
    182183      RuleType := rtSequence;
    183184      AddRule(AlphabeticCharacter, False, False);
     
    198199    with RuleString do begin
    199200      Name := 'String';
     201      Token := True;
    200202      RuleType := rtSequence;
    201203      AddTerminal('''', False, False);
     
    344346  Memo1.Lines.LoadFromFile(DefaultGrammerFileName);
    345347  Memo2.Lines.LoadFromFile(DefaultPascalFileName);
    346   SourceCode := '';
    347348//  with Memo1.Lines do
    348349//  for I := 1 to Length(Text) do begin
     
    365366function TMainForm.ProcessConcatenationItem(ARule: TGrammerRule; ProgramItem: TProgramItem): TGrammerItem;
    366367var
    367   I: Integer;
    368368  II: Integer;
    369369  NewRule: TGrammerRule;
    370   NewSubRule: TGrammerRule;
    371370  RuleItem: TGrammerItem;
    372 type
    373   LowerChars = 'a'..'z';
    374371begin
    375372  with ProgramItem do begin
     
    392389            if Length(Value) > 3 then begin
    393390              NewRule := TGrammerRule.Create;
    394               NewRule.Ownership := ARule;
     391              NewRule.Parent := ARule;
    395392              NewRule.Name := 'Term';
    396393              NewRule.AddTerminalText(Copy(Value, 2, Length(Value) - 2));
     
    404401          end else begin
    405402            NewRule := TGrammerRule.Create;
    406             NewRule.Ownership := ARule;
     403            NewRule.PArent := ARule;
    407404            NewRule.Name := 'Term';
    408405            NewRule.AddTerminalText(Value);
     
    413410      end else if TProgramItem(Items[II]).Rule.Name = 'GroupingBlock' then begin
    414411        NewRule := TGrammerRule.Create;
    415         NewRule.Ownership := ARule;
     412        NewRule.Parent := ARule;
    416413        NewRule.RuleType := rtSequence;
    417414        NewRule.Name := 'Group';
    418         ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     415        ProcessSeparationItem(NewRule, Items[II].Items[1]);
    419416        PascalGrammer.Rules.Add(NewRule);
    420417        RuleItem.ItemType := itNonterminal;
     
    422419      end else if TProgramItem(Items[II]).Rule.Name = 'OptionBlock' then begin
    423420        NewRule := TGrammerRule.Create;
    424         NewRule.Ownership := ARule;
     421        NewRule.Parent := ARule;
    425422        NewRule.RuleType := rtSequence;
    426423        NewRule.Name := 'Option';
    427         ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     424        ProcessSeparationItem(NewRule, Items[II].Items[1]);
    428425        PascalGrammer.Rules.Add(NewRule);
    429426        RuleItem.ItemType := itNonterminal;
    430427        RuleItem.Optional := True;
    431428        RuleItem.Rule := NewRule;
    432       end else if TProgramItem(Items[II]).Rule.Name = 'RepetitionBlock' then begin
     429      end else if Items[II].Rule.Name = 'RepetitionBlock' then begin
    433430        NewRule := TGrammerRule.Create;
    434         NewRule.Ownership := ARule;
     431        NewRule.Parent := ARule;
    435432        NewRule.RuleType := rtSequence;
    436433        NewRule.Name := 'Repetition';
    437         ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     434        ProcessSeparationItem(NewRule, Items[II].Items[1]);
    438435        PascalGrammer.Rules.Add(NewRule);
    439436        RuleItem.Repetition := True;
     
    456453begin
    457454  for I := 0 to PascalGrammer.Rules.Count - 1 do
    458     TGrammerRule(PascalGrammer.Rules[I]).Free;
     455    PascalGrammer.Rules[I].Free;
    459456  PascalGrammer.Rules.Clear;
    460457  with PascalGrammer do begin
     
    533530  PascalGrammer.TopRule := nil;
    534531  with SourceProgram.TopItem do begin
    535     with TProgramItem(Items[0]) do begin
    536       for I := 0 to Items.Count - 1 do with TProgramItem(Items[I]) do begin
     532    with Items[0] do begin
     533      for I := 0 to Items.Count - 1 do with Items[I] do begin
    537534        NewRule := TGrammerRule.Create;
    538535        with PascalGrammer do
    539536          if TopRule = nil then TopRule := NewRule;
    540         NewRule.Name := TProgramItem(Items[0]).Value;
    541         ProcessSeparationItem(NewRule, TProgramItem(Items[2]));
     537        NewRule.Name := Items[0].Value;
     538        ProcessSeparationItem(NewRule, Items[2]);
    542539        PascalGrammer.Rules.Add(NewRule);
    543540      end;
     
    558555      for II := 0 to Items.Count - 1 do begin
    559556        NewSubRule := TGrammerRule.Create;
    560         NewSubRule.Ownership := ARule;
     557        NewSubRule.Parent := ARule;
    561558        NewSubRule.Name := ARule.Name;
    562559        RuleItem := ARule.Add;
    563560        RuleItem.ItemType := itNonterminal;
    564561        RuleItem.Rule := NewSubRule;
    565         ProcessConcatenationItem(NewSubRule, TProgramItem(Items[II]));
     562        ProcessConcatenationItem(NewSubRule, Items[II]);
    566563        PascalGrammer.Rules.Add(NewSubRule);
    567564      end;
    568565    end else begin
    569       ProcessConcatenationItem(ARule, TProgramItem(Items[0]));
     566      ProcessConcatenationItem(ARule, Items[0]);
    570567    end;
    571568end;
     
    585582      NewTreeNode := AddChild(TreeNode, ARule.Name + Attributs);
    586583      for II := 0 to Items.Count - 1 do
    587       with TGrammerItem(Items[II]) do begin
     584      with Items[II] do begin
    588585        if Optional then Attributs := '(Opt)' else Attributs := '';
    589586        if Repetition then Attributs := Attributs + '(Rep)';
     
    595592          itNonterminal: begin
    596593            if Assigned(Rule) then begin
    597               if Rule.Ownership = ARule then
     594              if Rule.Parent = ARule then
    598595                ShowGrammerRule(NewTreeNode, Rule, Attributs)
    599596                else AddChild(NewTreeNode, '<' + Rule.Name + '>' + Attributs);
     
    614611    TopItem := AddChild(nil, 'Gramatika');
    615612    for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do
    616       if Ownership = nil then ShowGrammerRule(TopItem, TGrammerRule(Rules[I]));
     613      if not Assigned(Parent) then ShowGrammerRule(TopItem, TGrammerRule(Rules[I]));
    617614    TopItem.Expand(False);
    618615    EndUpdate;
     
    651648initialization
    652649  {$i UMainForm.lrs}
    653   {$i UMainForm.lrs}
    654650
    655651end.
Note: See TracChangeset for help on using the changeset viewer.