Changeset 2 for UMainForm.pas


Ignore:
Timestamp:
Oct 1, 2007, 3:09:51 PM (17 years ago)
Author:
george
Message:

Zobrazení gramatiky.
Zkoušení analýzy jiný gramatiky.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • UMainForm.pas

    r1 r2  
    5858
    5959  TGrammerItem = record
     60  public
    6061    ItemType: TRuleItemType;
    6162    Character: Char;
     
    6465    Repetition: Boolean;
    6566    procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters);
     67    constructor Create(ItemType: TRuleType);
    6668  end;
    6769
     
    7476    Items: array of TGrammerItem;
    7577    procedure AddTerminal(Character: Char; Optional, Repetition: Boolean);
     78    procedure AddTerminalText(Text: string);
    7679    procedure AddRule(Rule: TGrammerRule; Optional, Repetition: Boolean);
    7780    procedure ProcessCharacter(Character: Char);
    7881    procedure GetPossibleCharacters(Path: TGrammerPath;
    7982      var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = 0);
     83    constructor Create;
    8084  end;
    8185
     
    9397    Button1: TButton;
    9498    TreeView1: TTreeView;
     99    TreeView2: TTreeView;
    95100    procedure FormCreate(Sender: TObject);
    96101    procedure FormDestroy(Sender: TObject);
     
    103108    SourceProgram: TProgram;
    104109    procedure ShowProgramTree(SourceProgram: TProgram);
     110    procedure ShowGrammerTree(Grammer: TGrammer);
    105111  end;
    106112
     
    119125procedure TMainForm.FormCreate(Sender: TObject);
    120126var
     127  LowerCaseAlphabeticCharacter, UpperCaseAlphabeticCharacter,
    121128  Digit, AlphabeticCharacter, Number, AlphaNumericCharacter,
    122     Identifier, Assigmnent, Expression, RuleString: TGrammerRule;
     129    Identifier, Assignment, Expression, RuleString, Rule, RuleList,
     130    AlternateOptions, OptionBlock, RepetitionBlock, GroupingBlock,
     131    AlternateOptionsSubBlock, Term, AllCharacters, WhiteSpace: TGrammerRule;
    123132  C: Char;
     133  I: Integer;
     134  SourceCode: string;
    124135begin
    125136  Grammer := TGrammer.Create;
    126 
    127137  with Grammer do begin
     138    WhiteSpace := TGrammerRule.Create;
     139    with WhiteSpace do begin
     140      Name := 'WhiteSpace';
     141      RuleType := rtAlternative;
     142      AddTerminal(' ', False, False);
     143      AddTerminal(#10, False, False);
     144      AddTerminal(#13, False, False);
     145    end;
     146    Rules.Add(WhiteSpace);
     147
     148    LowerCaseAlphabeticCharacter := TGrammerRule.Create;
     149    with LowerCaseAlphabeticCharacter do begin
     150      Name := 'LowerCaseAlphabeticCharacter';
     151      RuleType := rtAlternative;
     152      for C := 'a' to 'z' do AddTerminal(C, False, False);
     153    end;
     154    Rules.Add(LowerCaseAlphabeticCharacter);
     155
     156    UpperCaseAlphabeticCharacter := TGrammerRule.Create;
     157    with UpperCaseAlphabeticCharacter do begin
     158      Name := 'UpperCaseAlphabeticCharacter';
     159      RuleType := rtAlternative;
     160      for C := 'A' to 'Z' do AddTerminal(C, False, False);
     161    end;
     162    Rules.Add(UpperCaseAlphabeticCharacter);
     163
    128164    AlphabeticCharacter := TGrammerRule.Create;
    129165    with AlphabeticCharacter do begin
    130166      Name := 'AlphabeticCharacter';
    131167      RuleType := rtAlternative;
    132       for C := 'A' to 'Z' do AddTerminal(C, False, False);
     168      AddRule(LowerCaseAlphabeticCharacter, False, False);
     169      AddRule(UpperCaseAlphabeticCharacter, False, False);
    133170    end;
    134171    Rules.Add(AlphabeticCharacter);
     
    169206    Rules.Add(Identifier);
    170207
     208    AllCharacters := TGrammerRule.Create;
     209    with AllCharacters do begin
     210      Name := 'AllCharacters';
     211      RuleType := rtAlternative;
     212      for I := 32 to 38 do AddTerminal(Chr(I), False, False);
     213      for I := 40 to 125 do AddTerminal(Chr(I), False, False);
     214    end;
     215    Rules.Add(AllCharacters);
     216
    171217    RuleString := TGrammerRule.Create;
    172218    with RuleString do begin
     
    174220      RuleType := rtSequence;
    175221      AddTerminal('''', False, False);
    176       AddRule(AlphaNumericCharacter, True, True);
     222      AddRule(AllCharacters, True, True);
    177223      AddTerminal('''', False, False);
    178224    end;
    179225    Rules.Add(RuleString);
    180226
     227    AlternateOptions := TGrammerRule.Create;
    181228    Expression := TGrammerRule.Create;
     229
     230    AlternateOptionsSubBlock := TGrammerRule.Create;
     231    with AlternateOptionsSubBlock do begin
     232      Name := 'AlternateOptionsSubBlock';
     233      RuleType := rtSequence;
     234      AddRule(WhiteSpace, True, True);
     235      AddTerminal('|', False, False);
     236      AddRule(WhiteSpace, True, True);
     237      AddRule(AlternateOptions, True, True);
     238    end;
     239    Rules.Add(AlternateOptionsSubBlock);
     240
     241    OptionBlock := TGrammerRule.Create;
     242    with OptionBlock do begin
     243      Name := 'OptionBlock';
     244      RuleType := rtSequence;
     245      AddTerminal('[', False, False);
     246      AddRule(WhiteSpace, True, True);
     247      AddRule(Expression, False, False);
     248      AddRule(WhiteSpace, True, True);
     249      AddTerminal(']', False, False);
     250    end;
     251    Rules.Add(OptionBlock);
     252
     253    RepetitionBlock := TGrammerRule.Create;
     254    with RepetitionBlock do begin
     255      Name := 'RepetitionBlock';
     256      RuleType := rtSequence;
     257      AddTerminal('{', False, False);
     258      AddRule(WhiteSpace, True, True);
     259      AddRule(Expression, False, False);
     260      AddRule(WhiteSpace, True, True);
     261      AddTerminal('}', False, False);
     262    end;
     263    Rules.Add(RepetitionBlock);
     264
     265    GroupingBlock := TGrammerRule.Create;
     266    with GroupingBlock do begin
     267      Name := 'GroupingBlock';
     268      RuleType := rtSequence;
     269      AddTerminal('(', False, False);
     270      AddRule(WhiteSpace, True, True);
     271      AddRule(Expression, False, False);
     272      AddRule(WhiteSpace, True, True);
     273      AddTerminal(')', False, False);
     274    end;
     275    Rules.Add(GroupingBlock);
     276
     277    Term := TGrammerRule.Create;
     278    with Term do begin
     279      Name := 'Term';
     280      RuleType := rtAlternative;
     281      AddRule(Identifier, False, False);
     282      AddRule(RuleString, False, False);
     283    end;
     284    Rules.Add(Term);
     285
     286    with AlternateOptions do begin
     287      Name := 'AlternateOptions';
     288      RuleType := rtSequence;
     289      AddRule(Term, False, False);
     290      AddRule(AlternateOptionsSubBlock, True, True);
     291    end;
     292    Rules.Add(AlternateOptions);
     293
    182294    with Expression do begin
    183295      Name := 'Expression';
    184296      RuleType := rtAlternative;
     297      AddRule(RepetitionBlock, False, False);
     298      AddRule(OptionBlock, False, False);
     299      AddRule(GroupingBlock, False, False);
     300      AddRule(AlternateOptions, False, False);
     301    end;
     302    Rules.Add(Expression);
     303
     304    Rule := TGrammerRule.Create;
     305    with Rule do begin
     306      Name := 'Rule';
     307      RuleType := rtSequence;
     308      AddRule(WhiteSpace, True, True);
    185309      AddRule(Identifier, False, False);
    186       AddRule(Number, False, False);
    187       AddRule(RuleString, False, False);
    188     end;
    189     Rules.Add(Expression);
    190 
    191     Assigmnent := TGrammerRule.Create;
    192     with Assigmnent do begin
    193       Name := 'Assigmnent';
    194       RuleType := rtSequence;
    195       AddRule(Identifier, False, False);
    196       AddTerminal(':', False, False);
     310      AddRule(WhiteSpace, True, True);
    197311      AddTerminal('=', False, False);
    198       AddRule(Expression, False, False);
    199     end;
    200     Rules.Add(Assigmnent);
    201 
    202     TopRule := Assigmnent;
    203   end;
     312      AddRule(WhiteSpace, True, True);
     313      AddRule(Expression, False, True);
     314      AddRule(WhiteSpace, True, True);
     315      AddTerminal('.', False, False);
     316      AddRule(WhiteSpace, True, True);
     317    end;
     318    Rules.Add(Rule);
     319
     320    RuleList := TGrammerRule.Create;
     321    with RuleList do begin
     322      Name := 'RuleList';
     323      RuleType := rtSequence;
     324      AddRule(Rule, False, True);
     325    end;
     326    Rules.Add(RuleList);
     327
     328    TopRule := RuleList;
     329  end;
     330
     331  ShowGrammerTree(Grammer);
    204332
    205333  SourceProgram := TProgram.Create;
    206 
    207   Memo1.Lines.LoadFromFile('Program.pas');
     334  Memo1.Lines.LoadFromFile('P0.grm');
     335  SourceCode := '';;
     336//  with Memo1.Lines do
     337//  for I := 1 to Length(Text) do begin
     338//    if (Text[I] <> ' ') and (Text[I] <> #10) and (Text[I] <> #13) then SourceCode := SourceCode + Text[I];
     339//  end;
     340
    208341  Grammer.Parse(Memo1.Text, SourceProgram);
    209342  ShowProgramTree(SourceProgram);
     
    229362  Items[High(Items)].Optional := Optional;
    230363  Items[High(Items)].Repetition := Repetition;
     364end;
     365
     366procedure TGrammerRule.AddTerminalText(Text: string);
     367var
     368  I: Integer;
     369begin
     370  for I := 1 to Length(Text) do AddTerminal(Text[I], False, False);
     371end;
     372
     373constructor TGrammerRule.Create;
     374begin
     375
    231376end;
    232377
     
    264409begin
    265410  Memo1.Lines.SaveToFile('Program.pas');
     411
    266412  Grammer.Free;
    267413  SourceProgram.Free;
     414end;
     415
     416procedure TMainForm.ShowGrammerTree(Grammer: TGrammer);
     417var
     418  I, II: Integer;
     419  NewTreeNode: TTreeNode;
     420  NewTreeNode2: TTreeNode;
     421  Attributs: string;
     422begin
     423  with Grammer, TreeView2, Items do begin
     424    BeginUpdate;
     425    Clear;
     426    TopItem := AddChild(nil, 'Gramatika');
     427    for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin
     428      case RuleType of
     429        rtSequence: Attributs := '(Seq)';
     430        rtAlternative: Attributs := '(Alt)';
     431      end;
     432      NewTreeNode := AddChild(TopItem, Name + Attributs);
     433      for II := 0 to Length(Items) - 1 do
     434      with Items[II] do begin
     435        if Optional then Attributs := '(Opt)' else Attributs := '';
     436        if Repetition then Attributs := Attributs + '(Rep)';
     437        case ItemType of
     438          itTerminal: Attributs := Character + Attributs;
     439          itNonterminal: if Assigned(Rule) then
     440          Attributs := '<' + Rule.Name + '>' + Attributs;
     441            else Attributs := '<?>' + Attributs;
     442        end;
     443        NewTreeNode2 := AddChild(NewTreeNode, Attributs);
     444      end;
     445    end;
     446    TopItem.Expand(False);
     447    EndUpdate;
     448  end;
    268449end;
    269450
     
    316497var
    317498  Path: TGrammerPath;
    318   I: Integer;
     499  I, II: Integer;
    319500  C: Integer;
    320501  Scope: TPossibleCharacters;
     
    322503  UseCharIndex: Integer;
    323504  UseRule: TGrammerRule;
     505  ExpectedCharacters: string;
    324506begin
    325507  SetLength(Path.Items, Length(Path.Items) + 1);
     
    344526        ParsedProgram.Insert(Scope.Items[C].RulePath, Scope.Items[C].Character);
    345527      end else begin
    346         raise Exception.Create('Parse error');
     528        ExpectedCharacters := '';
     529        for II := 0 to Length(Scope.Items) - 1 do
     530          ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character;
     531        break;
     532        //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters +
     533          //'" but found "' + Text[I] + '".');
    347534      end;
    348535      Path.Assign(Scope.Items[C].RulePath);
     
    415602{ TGrammerItem }
    416603
     604constructor TGrammerItem.Create(ItemType: TRuleType);
     605begin
     606  Rule := nil;
     607end;
     608
    417609procedure TGrammerItem.GetPossibleCharacters(Path: TGrammerPath;
    418610  var Characters: TPossibleCharacters);
     
    444636          SetLength(Path.Items, PathIndex + 1);
    445637          Inc(Path.Items[PathIndex].ItemIndex);
    446           Inc(Path.Items[PathIndex].CharIndex);
     638          //Inc(Path.Items[PathIndex].CharIndex);
    447639          Rule.Items[Index].GetPossibleCharacters(Path, Characters);
    448640          Found := True;
Note: See TracChangeset for help on using the changeset viewer.