Changeset 8 for UGrammer.pas


Ignore:
Timestamp:
Oct 15, 2007, 8:28:00 AM (17 years ago)
Author:
george
Message:

Opraveno: Vytváření nové gramatiky z jiné. Zobrazení výsledku a další fáze parsování pascalu.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • UGrammer.pas

    r7 r8  
    6565  end;
    6666
    67   TGrammerItem = record
     67  TGrammerItem = class
    6868  private
    6969    Processed: Boolean;
     
    7777    Repetition: Boolean;
    7878    procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters);
    79     constructor Create(ItemType: TRuleType);
     79    constructor Create;
    8080  end;
    8181
     
    8686    Name: string;
    8787    RuleType: TRuleType;
    88     Items: array of TGrammerItem;
     88    Items: TList; // of TGrammerItem;
    8989    procedure ClearProcessed;
    9090    function Add: TGrammerItem;
    91     procedure AddTerminal(Character: Char; Optional, Repetition: Boolean);
     91    procedure AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean);
    9292    procedure AddTerminalText(Text: string);
    93     procedure AddRule(Rule: TGrammerRule; Optional, Repetition: Boolean);
     93    procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean);
    9494    procedure ProcessCharacter(Character: Char);
    9595    procedure GetPossibleCharacters(Path: TGrammerPath;
    9696      var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1);
    9797    constructor Create;
     98    destructor Destroy; override;
    9899  end;
    99100
     
    119120function TGrammerRule.Add: TGrammerItem;
    120121begin
    121   SetLength(Items, Length(Items) + 1);
    122   Result := Items[High(Items)];
     122  Result := TGrammerItem.Create;
     123  Items.Add(Result);
    123124  Result.Parent := Self;
    124125end;
    125126
    126 procedure TGrammerRule.AddRule(Rule: TGrammerRule; Optional,
    127   Repetition: Boolean);
    128 begin
    129   SetLength(Items, Length(Items) + 1);
    130   Items[High(Items)].ItemType := itNonterminal;
    131   Items[High(Items)].Rule := Rule;
    132   Items[High(Items)].Optional := Optional;
    133   Items[High(Items)].Repetition := Repetition;
    134   Items[High(Items)].Parent := Self;
    135 end;
    136 
    137 procedure TGrammerRule.AddTerminal(Character: Char; Optional, Repetition: Boolean);
    138 begin
    139   SetLength(Items, Length(Items) + 1);
    140   Items[High(Items)].ItemType := itTerminal;
    141   Items[High(Items)].Character := Character;
    142   Items[High(Items)].Optional := Optional;
    143   Items[High(Items)].Repetition := Repetition;
    144   Items[High(Items)].Parent := Self;
     127procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional,
     128  ARepetition: Boolean);
     129begin
     130  with Add do begin
     131    ItemType := itNonterminal;
     132    Rule := ARule;
     133    Optional := AOptional;
     134    Repetition := ARepetition;
     135    Parent := Self;
     136  end;
     137end;
     138
     139procedure TGrammerRule.AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean);
     140begin
     141  with Add do begin
     142    ItemType := itTerminal;
     143    Character := ACharacter;
     144    Optional := AOptional;
     145    Repetition := ARepetition;
     146    Parent := Self;
     147  end;
    145148end;
    146149
     
    156159  I: Integer;
    157160begin
    158   for I := 0 to High(Items) do with Items[I] do begin
     161  for I := 0 to Items.Count - 1 do with TGrammerItem(Items[I]) do begin
    159162    Processed := False;
    160163  end;
     
    164167begin
    165168  Ownership := nil;
     169  Items := TList.Create;
     170end;
     171
     172destructor TGrammerRule.Destroy;
     173var
     174  I: Integer;
     175begin
     176  for I := 0 to Items.Count - 1 do
     177    TGrammerItem(Items[I]).Free;
     178  Items.Free;   
     179  inherited;
    166180end;
    167181
     
    196210      end else begin
    197211        // Generate alternatives
    198         for I := 0 to High(Items) do begin
     212        for I := 0 to Items.Count - 1 do begin
    199213          Path.Items[High(Path.Items)].ItemIndex := I;
    200214          //Inc(Path.Items[High(Path.Items)].CharIndex);
    201           Items[I].GetPossibleCharacters(Path, Characters);
     215          TGrammerItem(Items[I]).GetPossibleCharacters(Path, Characters);
    202216        end;
    203217      end;
     
    205219    rtSequence: begin
    206220      TempPath.Assign(Path);
    207       if UseIndex >= Length(Items) then begin
     221      if UseIndex >= Items.Count then begin
    208222        // Forward generation to upper item
    209223        SetLength(Path.Items, Length(Path.Items) - 1);
     
    217231      end else begin
    218232        Path.Items[High(Path.Items)].ItemIndex := UseIndex;
    219         Items[UseIndex].GetPossibleCharacters(Path, Characters);
     233        TGrammerItem(Items[UseIndex]).GetPossibleCharacters(Path, Characters);
    220234      end;
    221235      // Check repetition
    222       if (UseIndex > 0) and not Items[UseIndex - 1].Processed then
    223         if Items[UseIndex - 1].Repetition then begin
     236      if (UseIndex > 0) and not TGrammerItem(Items[UseIndex - 1]).Processed then
     237        if TGrammerItem(Items[UseIndex - 1]).Repetition then begin
    224238          TempPath.Items[High(TempPath.Items)].ItemIndex := UseIndex - 1;
    225           Items[UseIndex - 1].GetPossibleCharacters(TempPath, Characters);
     239          TGrammerItem(Items[UseIndex - 1]).GetPossibleCharacters(TempPath, Characters);
    226240        end;
    227241    end;
     
    251265begin
    252266  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
     267    for II := 0 to Items.Count - 1 do with TGrammerItem(Items[II]) do begin
    254268      if (ItemType = itNonterminal) and (Rule = nil) then begin
    255269        J := 0;
     
    499513{ TGrammerItem }
    500514
    501 constructor TGrammerItem.Create(ItemType: TRuleType);
     515constructor TGrammerItem.Create;
    502516begin
    503517  Rule := nil;
     
    557571  Index := High(Items);
    558572  while not Success and (Index >= 0) do begin
    559     with Items[Index] do if Rule.Items[ItemIndex].Repetition then begin
     573    with Items[Index] do if TGrammerItem(Rule.Items[ItemIndex]).Repetition then begin
    560574      Success := True;
    561575      //Inc(CharIndex);
    562576    end else begin
    563       if ((ItemIndex + 1) < Length(Rule.Items)) and (Rule.RuleType = rtSequence) then begin
     577      if ((ItemIndex + 1) < Rule.Items.Count) and (Rule.RuleType = rtSequence) then begin
    564578        Inc(ItemIndex);
    565579        //Inc(CharIndex);
Note: See TracChangeset for help on using the changeset viewer.