Changeset 7 for UGrammer.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
  • UGrammer.pas

    r6 r7  
    44
    55uses
    6   Classes, ComCtrls;
     6  Classes, ComCtrls, SysUtils;
    77
    88type
     
    5050    procedure DeleteEmpty;
    5151    procedure MergeNonterminal(ARule: TGrammerRule);
     52    procedure Join(ARule: TGrammerRule; ItemIndex: Integer);
    5253    function MergeToTerminal: string;
    5354    constructor Create;
    5455    destructor Destroy; override;
     56    procedure Assign(Source: TProgramItem);
    5557  end;
    5658
     
    6567  TGrammerItem = record
    6668  private
     69    Processed: Boolean;
    6770    Parent: TGrammerRule;
    68     Processed: Boolean;
    6971  public
    7072    ItemType: TRuleItemType;
    7173    Character: Char;
    7274    Rule: TGrammerRule;
     75    RuleName: string;
    7376    Optional: Boolean;
    7477    Repetition: Boolean;
     
    8083  private
    8184  public
     85    Ownership: TGrammerRule;
    8286    Name: string;
    8387    RuleType: TRuleType;
    8488    Items: array of TGrammerItem;
    8589    procedure ClearProcessed;
     90    function Add: TGrammerItem;
    8691    procedure AddTerminal(Character: Char; Optional, Repetition: Boolean);
    8792    procedure AddTerminalText(Text: string);
     
    100105    procedure ClearProcessed;
    101106    procedure Parse(Text: string; var ParsedProgram: TProgram);
     107    procedure CorrectRuleLinks;
    102108    destructor Destroy; override;
    103109  end;
     
    106112implementation
    107113
     114uses
     115  UMainForm;
     116
    108117{ TGrammerRule }
     118
     119function TGrammerRule.Add: TGrammerItem;
     120begin
     121  SetLength(Items, Length(Items) + 1);
     122  Result := Items[High(Items)];
     123  Result.Parent := Self;
     124end;
    109125
    110126procedure TGrammerRule.AddRule(Rule: TGrammerRule; Optional,
     
    147163constructor TGrammerRule.Create;
    148164begin
    149 
     165  Ownership := nil;
    150166end;
    151167
     
    225241  for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin
    226242    ClearProcessed;
     243  end;
     244end;
     245
     246procedure TGrammer.CorrectRuleLinks;
     247var
     248  I: Integer;
     249  II: Integer;
     250  J: Integer;
     251begin
     252  for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin
     253    for II := 0 to Length(Items) - 1 do with Items[II] do begin
     254      if (ItemType = itNonterminal) and (Rule = nil) then begin
     255        J := 0;
     256        while (J < Rules.Count) and (TGrammerRule(Rules[J]).Name <> RuleName)  do Inc(J);
     257        if J < Rules.Count then Rule := Rules[J] else
     258          raise Exception.Create('Rule link correction failed on rule ' +
     259            IntToStr(I) + ' item ' + IntToStr(II));
     260      end;
     261    end;
    227262  end;
    228263end;
     
    294329          ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character;
    295330        //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".');
     331        MainForm.Label1.Caption := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".';
    296332        break;
    297333      end;
     
    361397{ TProgramItem }
    362398
     399procedure TProgramItem.Assign(Source: TProgramItem);
     400begin
     401 
     402end;
     403
    363404constructor TProgramItem.Create;
    364405begin
     
    423464  Items.Free;
    424465  inherited;
     466end;
     467
     468procedure TProgramItem.Join(ARule: TGrammerRule; ItemIndex: Integer);
     469var
     470  SubProgramItem: TProgramItem;
     471  I: Integer;
     472begin
     473  if (ItemType = itNonterminal) and (Rule = ARule) then begin
     474    SubProgramItem := TProgramItem(Items[ItemIndex]);
     475    RuleBefore := SubProgramItem.RuleBefore;
     476    Rule := SubProgramItem.Rule;
     477    ItemType := SubProgramItem.ItemType;
     478    Value := SubProgramItem.Value;
     479    Items[ItemIndex] := nil;
     480    for I := 0 to Items.Count - 1 do
     481      if Assigned(Items[I]) then TProgramItem(Items[I]).Free;
     482    Items := SubProgramItem.Items;
     483  end;
     484  for I := 0 to Items.Count - 1 do
     485    TProgramItem(Items[I]).Join(ARule, ItemIndex);
    425486end;
    426487
Note: See TracChangeset for help on using the changeset viewer.