Changeset 7 for UMainForm.pas


Ignore:
Timestamp:
Oct 12, 2007, 1:43:10 PM (17 years ago)
Author:
george
Message:

Tvorba gramatiky pascalu

File:
1 edited

Legend:

Unmodified
Added
Removed
  • UMainForm.pas

    r6 r7  
    1616    TreeView1: TTreeView;
    1717    TreeView2: TTreeView;
     18    Label1: TLabel;
     19    Button2: TButton;
    1820    procedure FormCreate(Sender: TObject);
    1921    procedure FormDestroy(Sender: TObject);
    2022    procedure Button1Click(Sender: TObject);
     23    procedure Button2Click(Sender: TObject);
    2124  private
    2225    procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem);
     
    2427  public
    2528    Grammer: TGrammer;
     29    PascalGrammer: TGrammer;
    2630    SourceProgram: TProgram;
    2731    procedure ShowProgramTree(SourceProgram: TProgram);
    2832    procedure ShowGrammerTree(Grammer: TGrammer);
     33    procedure ShowGrammerRule(TreeNode: TTreeNode; ARule: TGrammerRule;
     34      Attributs: string = '');
     35    procedure ProcessProgramTree;
     36    function ProcessConcatenationItem(ARule: TGrammerRule; ProgramItem: TProgramItem): TGrammerItem;
     37    function ProcessSeparationItem(var ARule: TGrammerRule; ProgramItem: TProgramItem): TGrammerItem;
    2938  end;
    3039
     
    4655    DeleteNonterminal(TGrammerRule(Grammer.Rules[0]));
    4756    DeleteEmpty;
     57    Join(TGrammerRule(Grammer.Rules[17]), 1);
     58    Join(TGrammerRule(Grammer.Rules[15]), 0);
     59    Join(TGrammerRule(Grammer.Rules[14]), 0);
    4860  end;
    4961  ShowProgramTree(SourceProgram);
     62end;
     63
     64procedure TMainForm.Button2Click(Sender: TObject);
     65begin
     66  ProcessProgramTree;
     67  PascalGrammer.CorrectRuleLinks;
     68  ShowGrammerTree(PascalGrammer);
    5069end;
    5170
     
    6281  SourceCode: string;
    6382begin
     83  PascalGrammer := TGrammer.Create;
     84
    6485  Grammer := TGrammer.Create;
    6586  with Grammer do begin
     
    303324begin
    304325  Memo1.Lines.SaveToFile(DefaultGrammerFileName);
    305 
     326  PascalGrammer.Free;
    306327  Grammer.Free;
    307328  SourceProgram.Free;
    308329end;
    309330
    310 procedure TMainForm.ShowGrammerTree(Grammer: TGrammer);
    311 var
    312   I, II: Integer;
     331function TMainForm.ProcessConcatenationItem(ARule: TGrammerRule; ProgramItem: TProgramItem): TGrammerItem;
     332var
     333  I: Integer;
     334  II: Integer;
     335  NewRule: TGrammerRule;
     336  NewSubRule: TGrammerRule;
     337  RuleItem: TGrammerItem;
     338type
     339  LowerChars = 'a'..'z';
     340begin
     341  with ProgramItem do begin
     342    ARule.RuleType := rtSequence;
     343    for II := 0 to Items.Count - 1 do begin
     344      RuleItem := ARule.Add;
     345      if TProgramItem(Items[II]).Rule.Name = 'Term' then begin
     346        RuleItem.ItemType := itNonterminal;
     347        with TProgramItem(TProgramItem(Items[II]).Items[0]) do
     348          if (Value[1] >= 'a') and (Value[1] <= 'z') then begin
     349            RuleItem.ItemType := itNonterminal;
     350            RuleItem.RuleName := Value;
     351            RuleItem.Rule := nil;
     352          end else begin
     353            NewRule := TGrammerRule.Create;
     354            NewRule.Ownership := ARule;
     355            NewRule.Name := 'Term';
     356            NewRule.AddTerminalText(Value);
     357            PascalGrammer.Rules.Add(NewRule);
     358            RuleItem.Rule := NewRule;
     359            RuleItem.ItemType := itNonterminal;
     360          end;
     361      end else if TProgramItem(Items[II]).Rule.Name = 'GroupingBlock' then begin
     362        NewRule := TGrammerRule.Create;
     363        NewRule.Ownership := ARule;
     364        NewRule.RuleType := rtSequence;
     365        NewRule.Name := 'Group';
     366        ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     367        PascalGrammer.Rules.Add(NewRule);
     368        RuleItem.ItemType := itNonterminal;
     369        RuleItem.Rule := NewRule;
     370      end else if TProgramItem(Items[II]).Rule.Name = 'OptionBlock' then begin
     371        NewRule := TGrammerRule.Create;
     372        NewRule.Ownership := ARule;
     373        NewRule.RuleType := rtSequence;
     374        NewRule.Name := 'Option';
     375        ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     376        PascalGrammer.Rules.Add(NewRule);
     377        RuleItem.ItemType := itNonterminal;
     378        RuleItem.Optional := True;
     379        RuleItem.Rule := NewRule;
     380      end else if TProgramItem(Items[II]).Rule.Name = 'RepetitionBlock' then begin
     381        NewRule := TGrammerRule.Create;
     382        NewRule.Ownership := ARule;
     383        NewRule.RuleType := rtSequence;
     384        NewRule.Name := 'Repetition';
     385        ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);
     386        PascalGrammer.Rules.Add(NewRule);
     387        RuleItem.Repetition := True;
     388        RuleItem.ItemType := itNonterminal;
     389        RuleItem.Rule := NewRule;
     390      end;
     391    end;
     392  end;
     393end;
     394
     395procedure TMainForm.ProcessProgramTree;
     396var
     397  I: Integer;
     398  NewRule: TGrammerRule;
     399begin
     400  for I := 0 to PascalGrammer.Rules.Count - 1 do
     401    TGrammerRule(PascalGrammer.Rules[I]).Free;
     402  PascalGrammer.Rules.Clear;
     403  PascalGrammer.TopRule := nil;
     404  with SourceProgram.TopItem do begin
     405    with TProgramItem(Items[0]) do begin
     406      for I := 0 to Items.Count - 1 do with TProgramItem(Items[I]) do begin
     407        NewRule := TGrammerRule.Create;
     408        with PascalGrammer do
     409          if TopRule = nil then TopRule := NewRule;
     410        NewRule.Name := TProgramItem(Items[0]).Value;
     411        ProcessSeparationItem(NewRule, TProgramItem(Items[2]));
     412        PascalGrammer.Rules.Add(NewRule);
     413      end;
     414    end;
     415  end;
     416end;
     417
     418function TMainForm.ProcessSeparationItem(var ARule: TGrammerRule;
     419  ProgramItem: TProgramItem): TGrammerItem;
     420var
     421  II: Integer;
     422  NewSubRule: TGrammerRule;
     423  RuleItem: TGrammerItem;
     424begin
     425  with ProgramItem do
     426    if Items.Count > 1 then begin
     427      ARule.RuleType := rtAlternative;
     428      for II := 0 to Items.Count - 1 do begin
     429        NewSubRule := TGrammerRule.Create;
     430        NewSubRule.Ownership := ARule;
     431        NewSubRule.Name := ARule.Name;
     432        RuleItem := ARule.Add;
     433        RuleItem.ItemType := itNonterminal;
     434        RuleItem.Rule := NewSubRule;
     435        ProcessConcatenationItem(NewSubRule, TProgramItem(Items[II]));
     436        PascalGrammer.Rules.Add(NewSubRule);
     437      end;
     438    end else begin
     439      ProcessConcatenationItem(ARule, TProgramItem(Items[0]));
     440    end;
     441end;
     442
     443procedure TMainForm.ShowGrammerRule(TreeNode: TTreeNode; ARule: TGrammerRule;
     444  Attributs: string = '');
     445var
     446  II: Integer;
    313447  NewTreeNode: TTreeNode;
    314   Attributs: string;
    315 begin
    316   with Grammer, TreeView2, Items do begin
    317     BeginUpdate;
    318     Clear;
    319     TopItem := AddChild(nil, 'Gramatika');
    320     for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin
     448//  NewNode: TTreeNode;
     449begin
     450  with TreeView2, Items, ARule do begin
    321451      case RuleType of
    322         rtSequence: Attributs := '(Seq)';
    323         rtAlternative: Attributs := '(Alt)';
     452        rtSequence: Attributs := Attributs + '(Seq)';
     453        rtAlternative: Attributs := Attributs + '(Alt)';
    324454      end;
    325       NewTreeNode := AddChild(TopItem, Name + Attributs);
     455      NewTreeNode := AddChild(TreeNode, ARule.Name + Attributs);
    326456      for II := 0 to Length(Items) - 1 do
    327457      with Items[II] do begin
     
    329459        if Repetition then Attributs := Attributs + '(Rep)';
    330460        case ItemType of
    331           itTerminal: Attributs := Character + Attributs;
    332           itNonterminal: if Assigned(Rule) then
    333           Attributs := '<' + Rule.Name + '>' + Attributs;
    334             else Attributs := '<?>' + Attributs;
     461          itTerminal: begin
     462            Attributs := Character + Attributs;
     463            AddChild(NewTreeNode, Attributs);
     464          end;
     465          itNonterminal: begin
     466            if Assigned(Rule) then begin
     467              if Rule.Ownership = ARule then
     468                ShowGrammerRule(NewTreeNode, Rule, Attributs)
     469                else AddChild(NewTreeNode, '<' + Rule.Name + '>' + Attributs);
     470            end else AddChild(NewTreeNode, '<?>' + Attributs);
     471          end;
    335472        end;
    336         AddChild(NewTreeNode, Attributs);
    337473      end;
    338474    end;
     475end;
     476
     477procedure TMainForm.ShowGrammerTree(Grammer: TGrammer);
     478var
     479  I: Integer;
     480begin
     481  with Grammer, TreeView2, Items do begin
     482    BeginUpdate;
     483    Clear;
     484    TopItem := AddChild(nil, 'Gramatika');
     485    for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do
     486      if Ownership = nil then ShowGrammerRule(TopItem, TGrammerRule(Rules[I]));
    339487    TopItem.Expand(False);
    340488    EndUpdate;
Note: See TracChangeset for help on using the changeset viewer.