Changeset 29 for branches/Analyzátor gramatiky/UMainForm.pas
- Timestamp:
- Nov 11, 2009, 11:11:13 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Analyzátor gramatiky/UMainForm.pas
r28 r29 6 6 7 7 uses 8 LCLIntf, Messages,SysUtils, Classes, Graphics, Controls, Forms,8 LCLIntf, SysUtils, Classes, Graphics, Controls, Forms, 9 9 Dialogs, StdCtrls, ComCtrls, LResources, UGrammer, UProgram; 10 10 11 11 const 12 DefaultGrammerFileName: string = ' grammer/test.grm';13 DefaultPascalFileName: string = ' pascal/test.pas';12 DefaultGrammerFileName: string = '../../grammer/test.grm'; 13 DefaultPascalFileName: string = '../../pascal/test.pas'; 14 14 15 15 type … … 32 32 private 33 33 procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem); 34 { Private declarations }35 34 public 36 35 Grammer: TGrammer; … … 50 49 MainForm: TMainForm; 51 50 51 52 52 implementation 53 54 53 55 54 procedure TMainForm.Button1Click(Sender: TObject); … … 88 87 begin 89 88 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]); 93 92 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); 97 96 end; 98 97 ShowProgramTree(SourceProgram); … … 109 108 C: Char; 110 109 I: Integer; 111 SourceCode: string;112 110 begin 113 111 PascalGrammer := TGrammer.Create; … … 118 116 with WhiteSpace do begin 119 117 Name := 'WhiteSpace'; 118 Token := True; 120 119 RuleType := rtAlternative; 121 120 AddTerminal(' ', False, False); … … 161 160 with Number do begin 162 161 Name := 'Number'; 162 Token := True; 163 163 RuleType := rtSequence; 164 164 AddTerminal('-', True, False); … … 180 180 with Identifier do begin 181 181 Name := 'Identifier'; 182 Token := True; 182 183 RuleType := rtSequence; 183 184 AddRule(AlphabeticCharacter, False, False); … … 198 199 with RuleString do begin 199 200 Name := 'String'; 201 Token := True; 200 202 RuleType := rtSequence; 201 203 AddTerminal('''', False, False); … … 344 346 Memo1.Lines.LoadFromFile(DefaultGrammerFileName); 345 347 Memo2.Lines.LoadFromFile(DefaultPascalFileName); 346 SourceCode := '';347 348 // with Memo1.Lines do 348 349 // for I := 1 to Length(Text) do begin … … 365 366 function TMainForm.ProcessConcatenationItem(ARule: TGrammerRule; ProgramItem: TProgramItem): TGrammerItem; 366 367 var 367 I: Integer;368 368 II: Integer; 369 369 NewRule: TGrammerRule; 370 NewSubRule: TGrammerRule;371 370 RuleItem: TGrammerItem; 372 type373 LowerChars = 'a'..'z';374 371 begin 375 372 with ProgramItem do begin … … 392 389 if Length(Value) > 3 then begin 393 390 NewRule := TGrammerRule.Create; 394 NewRule. Ownership:= ARule;391 NewRule.Parent := ARule; 395 392 NewRule.Name := 'Term'; 396 393 NewRule.AddTerminalText(Copy(Value, 2, Length(Value) - 2)); … … 404 401 end else begin 405 402 NewRule := TGrammerRule.Create; 406 NewRule. Ownership:= ARule;403 NewRule.PArent := ARule; 407 404 NewRule.Name := 'Term'; 408 405 NewRule.AddTerminalText(Value); … … 413 410 end else if TProgramItem(Items[II]).Rule.Name = 'GroupingBlock' then begin 414 411 NewRule := TGrammerRule.Create; 415 NewRule. Ownership:= ARule;412 NewRule.Parent := ARule; 416 413 NewRule.RuleType := rtSequence; 417 414 NewRule.Name := 'Group'; 418 ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);415 ProcessSeparationItem(NewRule, Items[II].Items[1]); 419 416 PascalGrammer.Rules.Add(NewRule); 420 417 RuleItem.ItemType := itNonterminal; … … 422 419 end else if TProgramItem(Items[II]).Rule.Name = 'OptionBlock' then begin 423 420 NewRule := TGrammerRule.Create; 424 NewRule. Ownership:= ARule;421 NewRule.Parent := ARule; 425 422 NewRule.RuleType := rtSequence; 426 423 NewRule.Name := 'Option'; 427 ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);424 ProcessSeparationItem(NewRule, Items[II].Items[1]); 428 425 PascalGrammer.Rules.Add(NewRule); 429 426 RuleItem.ItemType := itNonterminal; 430 427 RuleItem.Optional := True; 431 428 RuleItem.Rule := NewRule; 432 end else if TProgramItem(Items[II]).Rule.Name = 'RepetitionBlock' then begin429 end else if Items[II].Rule.Name = 'RepetitionBlock' then begin 433 430 NewRule := TGrammerRule.Create; 434 NewRule. Ownership:= ARule;431 NewRule.Parent := ARule; 435 432 NewRule.RuleType := rtSequence; 436 433 NewRule.Name := 'Repetition'; 437 ProcessSeparationItem(NewRule, TProgramItem(Items[II]).Items[1]);434 ProcessSeparationItem(NewRule, Items[II].Items[1]); 438 435 PascalGrammer.Rules.Add(NewRule); 439 436 RuleItem.Repetition := True; … … 456 453 begin 457 454 for I := 0 to PascalGrammer.Rules.Count - 1 do 458 TGrammerRule(PascalGrammer.Rules[I]).Free;455 PascalGrammer.Rules[I].Free; 459 456 PascalGrammer.Rules.Clear; 460 457 with PascalGrammer do begin … … 533 530 PascalGrammer.TopRule := nil; 534 531 with SourceProgram.TopItem do begin 535 with TProgramItem(Items[0])do begin536 for I := 0 to Items.Count - 1 do with TProgramItem(Items[I])do begin532 with Items[0] do begin 533 for I := 0 to Items.Count - 1 do with Items[I] do begin 537 534 NewRule := TGrammerRule.Create; 538 535 with PascalGrammer do 539 536 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]); 542 539 PascalGrammer.Rules.Add(NewRule); 543 540 end; … … 558 555 for II := 0 to Items.Count - 1 do begin 559 556 NewSubRule := TGrammerRule.Create; 560 NewSubRule. Ownership:= ARule;557 NewSubRule.Parent := ARule; 561 558 NewSubRule.Name := ARule.Name; 562 559 RuleItem := ARule.Add; 563 560 RuleItem.ItemType := itNonterminal; 564 561 RuleItem.Rule := NewSubRule; 565 ProcessConcatenationItem(NewSubRule, TProgramItem(Items[II]));562 ProcessConcatenationItem(NewSubRule, Items[II]); 566 563 PascalGrammer.Rules.Add(NewSubRule); 567 564 end; 568 565 end else begin 569 ProcessConcatenationItem(ARule, TProgramItem(Items[0]));566 ProcessConcatenationItem(ARule, Items[0]); 570 567 end; 571 568 end; … … 585 582 NewTreeNode := AddChild(TreeNode, ARule.Name + Attributs); 586 583 for II := 0 to Items.Count - 1 do 587 with TGrammerItem(Items[II])do begin584 with Items[II] do begin 588 585 if Optional then Attributs := '(Opt)' else Attributs := ''; 589 586 if Repetition then Attributs := Attributs + '(Rep)'; … … 595 592 itNonterminal: begin 596 593 if Assigned(Rule) then begin 597 if Rule. Ownership= ARule then594 if Rule.Parent = ARule then 598 595 ShowGrammerRule(NewTreeNode, Rule, Attributs) 599 596 else AddChild(NewTreeNode, '<' + Rule.Name + '>' + Attributs); … … 614 611 TopItem := AddChild(nil, 'Gramatika'); 615 612 for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do 616 if Ownership = nilthen ShowGrammerRule(TopItem, TGrammerRule(Rules[I]));613 if not Assigned(Parent) then ShowGrammerRule(TopItem, TGrammerRule(Rules[I])); 617 614 TopItem.Expand(False); 618 615 EndUpdate; … … 651 648 initialization 652 649 {$i UMainForm.lrs} 653 {$i UMainForm.lrs}654 650 655 651 end.
Note:
See TracChangeset
for help on using the changeset viewer.