Changeset 9 for UMainForm.pas


Ignore:
Timestamp:
Oct 16, 2007, 9:16:28 AM (17 years ago)
Author:
george
Message:

Úpravy parsování a sestavování gramatity pro pasacal.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • UMainForm.pas

    r8 r9  
    1717    TreeView1: TTreeView;
    1818    TreeView2: TTreeView;
    19     Label1: TLabel;
    2019    Button2: TButton;
    2120    Memo2: TMemo;
    2221    Button3: TButton;
     22    StatusBar1: TStatusBar;
     23    Button4: TButton;
    2324    procedure FormCreate(Sender: TObject);
    2425    procedure FormDestroy(Sender: TObject);
     
    2627    procedure Button2Click(Sender: TObject);
    2728    procedure Button3Click(Sender: TObject);
     29    procedure Button4Click(Sender: TObject);
    2830  private
    2931    procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem);
     
    5557  SourceProgram := TProgram.Create;
    5658  Grammer.Parse(Memo1.Text, SourceProgram);
    57   with SourceProgram.TopItem do begin
    58     MergeNonterminal(TGrammerRule(Grammer.Rules[7]));
    59     MergeNonterminal(TGrammerRule(Grammer.Rules[9]));
    60     DeleteNonterminal(TGrammerRule(Grammer.Rules[0]));
    61     DeleteEmpty;
    62     Join(TGrammerRule(Grammer.Rules[17]), 1);
    63     Join(TGrammerRule(Grammer.Rules[15]), 0);
    64     Join(TGrammerRule(Grammer.Rules[14]), 0);
    65   end;
    6659  ShowProgramTree(SourceProgram);
    6760end;
     
    9184end;
    9285
     86procedure TMainForm.Button4Click(Sender: TObject);
     87begin
     88  with SourceProgram.TopItem do begin
     89    MergeNonterminal(TGrammerRule(Grammer.Rules[7]));
     90    MergeNonterminal(TGrammerRule(Grammer.Rules[9]));
     91    DeleteNonterminal(TGrammerRule(Grammer.Rules[0]));
     92    DeleteEmpty;
     93    Join(TGrammerRule(Grammer.Rules[17]), 1);
     94    Join(TGrammerRule(Grammer.Rules[15]), 0);
     95    Join(TGrammerRule(Grammer.Rules[14]), 0);
     96  end;
     97  ShowProgramTree(SourceProgram);
     98end;
     99
    93100procedure TMainForm.FormCreate(Sender: TObject);
    94101var
     
    260267      Name := 'ConcatenationBlock';
    261268      RuleType := rtSequence;
    262       AddRule(WhiteSpace, False, True);
     269      AddRule(WhiteSpace, True, True);
    263270      AddRule(Expression, True, False);
    264271    end;
     
    269276      RuleType := rtSequence;
    270277      AddRule(Expression, False, False);
     278      AddRule(WhiteSpace, True, True);
    271279      AddRule(ConcatenationBlock, True, True);
    272280    end;
     
    288296      RuleType := rtSequence;
    289297      AddRule(Concatenation, False, False);
     298      AddRule(WhiteSpace, True, True);
    290299      AddRule(SeparationBlock, True, True);
    291300    end;
     
    374383            RuleItem.RuleName := Value;
    375384            RuleItem.Rule := nil;
     385          end else if Value = 'NAME' then begin
     386            RuleItem.Rule := TGrammerRule(PascalGrammer.Rules[7]);
     387          end else if Value = 'NUMBER' then begin
     388            RuleItem.Rule := TGrammerRule(PascalGrammer.Rules[5]);
     389          end else if (Length(Value) > 1) and (Value[1] = '''') and
     390          (Value[Length(Value)] = '''') then begin
     391            if Length(Value) > 3 then begin
     392              NewRule := TGrammerRule.Create;
     393              NewRule.Ownership := ARule;
     394              NewRule.Name := 'Term';
     395              NewRule.AddTerminalText(Copy(Value, 2, Length(Value) - 2));
     396              PascalGrammer.Rules.Add(NewRule);
     397              RuleItem.Rule := NewRule;
     398              RuleItem.ItemType := itNonterminal;
     399            end else if Length(Value) = 3 then begin
     400              RuleItem.ItemType := itTerminal;
     401              RuleItem.Character := Value[2]
     402            end;
    376403          end else begin
    377404            NewRule := TGrammerRule.Create;
     
    422449  I: Integer;
    423450  NewRule: TGrammerRule;
     451  WhiteSpace, LowerCaseAlphabeticCharacter, UpperCaseAlphabeticCharacter,
     452  AlphabeticCharacter, Digit, Number, AlphaNumericCharacter,
     453  Identifier: TGrammerRule;
     454  C: Char;
    424455begin
    425456  for I := 0 to PascalGrammer.Rules.Count - 1 do
    426457    TGrammerRule(PascalGrammer.Rules[I]).Free;
    427458  PascalGrammer.Rules.Clear;
     459  with PascalGrammer do begin
     460    WhiteSpace := TGrammerRule.Create;
     461    with WhiteSpace do begin
     462      Name := 'WhiteSpace';
     463      RuleType := rtAlternative;
     464      AddTerminal(' ', False, False);
     465      AddTerminal(#10, False, False);
     466      AddTerminal(#13, False, False);
     467    end;
     468    Rules.Add(WhiteSpace);
     469
     470    LowerCaseAlphabeticCharacter := TGrammerRule.Create;
     471    with LowerCaseAlphabeticCharacter do begin
     472      Name := 'LowerCaseAlphabeticCharacter';
     473      RuleType := rtAlternative;
     474      for C := 'a' to 'z' do AddTerminal(C, False, False);
     475    end;
     476    Rules.Add(LowerCaseAlphabeticCharacter);
     477
     478    UpperCaseAlphabeticCharacter := TGrammerRule.Create;
     479    with UpperCaseAlphabeticCharacter do begin
     480      Name := 'UpperCaseAlphabeticCharacter';
     481      RuleType := rtAlternative;
     482      for C := 'A' to 'Z' do AddTerminal(C, False, False);
     483    end;
     484    Rules.Add(UpperCaseAlphabeticCharacter);
     485
     486    AlphabeticCharacter := TGrammerRule.Create;
     487    with AlphabeticCharacter do begin
     488      Name := 'AlphabeticCharacter';
     489      RuleType := rtAlternative;
     490      AddRule(LowerCaseAlphabeticCharacter, False, False);
     491      AddRule(UpperCaseAlphabeticCharacter, False, False);
     492    end;
     493    Rules.Add(AlphabeticCharacter);
     494
     495    Digit := TGrammerRule.Create;
     496    with Digit do begin
     497      Name := 'Digit';
     498      RuleType := rtAlternative;
     499      for C := '0' to '9' do AddTerminal(C, False, False);
     500    end;
     501    Rules.Add(Digit);
     502
     503    Number := TGrammerRule.Create;
     504    with Number do begin
     505      Name := 'Number';
     506      RuleType := rtSequence;
     507      AddTerminal('-', True, False);
     508      AddRule(Digit, False, True);
     509    end;
     510    Rules.Add(Number);
     511
     512    AlphaNumericCharacter := TGrammerRule.Create;
     513    with AlphaNumericCharacter do begin
     514      Name := 'AlphaNumericCharacter';
     515      RuleType := rtAlternative;
     516      AddRule(Digit, False, False);
     517      AddRule(AlphabeticCharacter, False, False);
     518      AddTerminal('_', False, False);
     519    end;
     520    Rules.Add(AlphaNumericCharacter);
     521
     522    Identifier := TGrammerRule.Create;
     523    with Identifier do begin
     524      Name := 'Identifier';
     525      RuleType := rtSequence;
     526      AddRule(AlphabeticCharacter, False, False);
     527      AddRule(AlphaNumericCharacter, True, True);
     528    end;
     529    Rules.Add(Identifier);
     530  end;
     531
    428532  PascalGrammer.TopRule := nil;
    429533  with SourceProgram.TopItem do begin
Note: See TracChangeset for help on using the changeset viewer.