Changeset 9


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

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

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • UGrammer.pas

    r8 r9  
    295295  Path: TGrammerPath;
    296296  I, II: Integer;
     297  Q: Integer;
    297298  C: Integer;
    298299  Scope: TPossibleCharacters;
     
    301302  UseRule: TGrammerRule;
    302303  ExpectedCharacters: string;
    303   Level: Integer;
     304  Level: array of Integer;
     305  LevelLength: Integer;
     306  LevelIsLeft: Boolean;
    304307begin
    305308  SetLength(Path.Items, Length(Path.Items) + 1);
     
    320323      UseRule.GetPossibleCharacters(Path, Scope, UseIndex, UseCharIndex);
    321324      C := Length(Scope.Items);
    322       Level := 0; //High(Integer);
    323       for II := High(Scope.Items) downto 0 do begin
    324         if (Scope.Items[II].Character = Text[I]) and (Level < Length(Scope.Items[II].RulePath.Items))
    325         then begin
    326           C := II;
    327           Level := Length(Scope.Items[II].RulePath.Items);
    328         end;
    329       end;
     325
     326      SetLength(Level, 0);
     327      for II := 0 to High(Scope.Items) do with Scope.Items[II] do begin
     328        if (Character = Text[I]) then begin
     329          if Length(RulePath.Items) > Length(Level) then begin
     330            LevelLength := Length(Level);
     331            SetLength(Level, Length(RulePath.Items));
     332            for Q := LevelLength to High(Level) do
     333              Level[Q] := High(Integer);
     334          end;
     335          LevelIsLeft := True;
     336          for Q := 0 to High(Level) do begin
     337            if Level[Q] > RulePath.Items[Q].ItemIndex then begin
     338              LevelIsLeft := False;
     339              Break;
     340            end;
     341            if Level[Q] < RulePath.Items[Q].ItemIndex then Break;
     342          end;
     343          if (not LevelIsLeft) or (Length(RulePath.Items) > Length(Level)) then begin
     344            SetLength(Level, Length(RulePath.Items));
     345            for Q := 0 to High(Level) do
     346              Level[Q] := RulePath.Items[Q].ItemIndex;
     347            C := II;
     348          end;
     349        end;
     350      end;
     351
    330352      if C < Length(Scope.Items) then begin
    331353        Path.Assign(Scope.Items[C].RulePath);
     
    343365          ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character;
    344366        //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".');
    345         MainForm.Label1.Caption := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".';
     367        MainForm.StatusBar1.SimpleText := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".';
    346368        break;
    347369      end;
  • UMainForm.dfm

    r8 r9  
    33  Top = 189
    44  Caption = 'P'#345'eklada'#269' pascalu'
    5   ClientHeight = 672
    6   ClientWidth = 1017
     5  ClientHeight = 653
     6  ClientWidth = 734
    77  Color = clBtnFace
    88  Font.Charset = DEFAULT_CHARSET
     
    1717  OnDestroy = FormDestroy
    1818  DesignSize = (
    19     1017
    20     672)
     19    734
     20    653)
    2121  PixelsPerInch = 96
    2222  TextHeight = 13
    23   object Label1: TLabel
    24     Left = 8
    25     Top = 297
    26     Width = 3
    27     Height = 13
    28   end
    2923  object Memo1: TMemo
    3024    Left = 8
     
    4741    Left = 360
    4842    Top = 8
    49     Width = 649
    50     Height = 657
     43    Width = 366
     44    Height = 638
    5145    Anchors = [akLeft, akTop, akRight, akBottom]
    5246    Indent = 19
    5347    TabOrder = 2
     48    ExplicitWidth = 649
     49    ExplicitHeight = 657
    5450  end
    5551  object TreeView2: TTreeView
     
    5753    Top = 432
    5854    Width = 345
    59     Height = 233
     55    Height = 214
    6056    Anchors = [akLeft, akTop, akBottom]
    6157    Indent = 19
    6258    TabOrder = 3
     59    ExplicitHeight = 233
    6360  end
    6461  object Button2: TButton
    65     Left = 232
     62    Left = 176
    6663    Top = 401
    6764    Width = 66
     
    8077  end
    8178  object Button3: TButton
    82     Left = 152
     79    Left = 96
    8380    Top = 401
    8481    Width = 74
     
    8885    OnClick = Button3Click
    8986  end
     87  object StatusBar1: TStatusBar
     88    Left = 0
     89    Top = 634
     90    Width = 734
     91    Height = 19
     92    Panels = <>
     93    ExplicitLeft = 128
     94    ExplicitTop = 408
     95    ExplicitWidth = 0
     96  end
     97  object Button4: TButton
     98    Left = 248
     99    Top = 400
     100    Width = 50
     101    Height = 25
     102    Caption = 'Reduce'
     103    TabOrder = 8
     104    OnClick = Button4Click
     105  end
    90106end
  • 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
  • pascal/test.pas

    r8 r9  
    1 CONSTNAME'='NUMBER';''.'
     1CONSTNAME:=NUMBER';''.'
Note: See TracChangeset for help on using the changeset viewer.