Changeset 8


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.

Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • PascalCompiler.bdsproj

    r1 r8  
    3333                        <Compiler Name="O">1</Compiler>
    3434                        <Compiler Name="P">1</Compiler>
    35                         <Compiler Name="Q">0</Compiler>
    36                         <Compiler Name="R">0</Compiler>
     35                        <Compiler Name="Q">1</Compiler>
     36                        <Compiler Name="R">1</Compiler>
    3737                        <Compiler Name="S">0</Compiler>
    3838                        <Compiler Name="T">0</Compiler>
  • 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);
  • UMainForm.dfm

    r7 r8  
    3131    Top = 8
    3232    Width = 346
    33     Height = 283
     33    Height = 186
    3434    ScrollBars = ssBoth
    3535    TabOrder = 0
     
    3737  object Button1: TButton
    3838    Left = 304
    39     Top = 297
     39    Top = 401
    4040    Width = 50
    4141    Height = 25
     
    5555  object TreeView2: TTreeView
    5656    Left = 8
    57     Top = 328
     57    Top = 432
    5858    Width = 345
    59     Height = 337
     59    Height = 233
    6060    Anchors = [akLeft, akTop, akBottom]
    6161    Indent = 19
     
    6464  object Button2: TButton
    6565    Left = 232
    66     Top = 297
     66    Top = 401
    6767    Width = 66
    6868    Height = 25
     
    7171    OnClick = Button2Click
    7272  end
     73  object Memo2: TMemo
     74    Left = 8
     75    Top = 200
     76    Width = 346
     77    Height = 195
     78    ScrollBars = ssBoth
     79    TabOrder = 5
     80  end
     81  object Button3: TButton
     82    Left = 152
     83    Top = 401
     84    Width = 74
     85    Height = 25
     86    Caption = 'Parse pascal'
     87    TabOrder = 6
     88    OnClick = Button3Click
     89  end
    7390end
  • UMainForm.pas

    r7 r8  
    99const
    1010  DefaultGrammerFileName: string = 'grammer/test.grm';
     11  DefaultPascalFileName: string = 'pascal/test.pas';
    1112
    1213type
     
    1819    Label1: TLabel;
    1920    Button2: TButton;
     21    Memo2: TMemo;
     22    Button3: TButton;
    2023    procedure FormCreate(Sender: TObject);
    2124    procedure FormDestroy(Sender: TObject);
    2225    procedure Button1Click(Sender: TObject);
    2326    procedure Button2Click(Sender: TObject);
     27    procedure Button3Click(Sender: TObject);
    2428  private
    2529    procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem);
     
    2933    PascalGrammer: TGrammer;
    3034    SourceProgram: TProgram;
     35    PascalProgram: TProgram;
    3136    procedure ShowProgramTree(SourceProgram: TProgram);
    3237    procedure ShowGrammerTree(Grammer: TGrammer);
     
    6974end;
    7075
     76procedure TMainForm.Button3Click(Sender: TObject);
     77begin
     78  PascalProgram.Free;
     79  PascalProgram := TProgram.Create;
     80  PascalGrammer.Parse(Memo2.Text, PascalProgram);
     81  with PascalProgram.TopItem, PascalGrammer do begin
     82//    MergeNonterminal(TGrammerRule(Rules[7]));
     83//    MergeNonterminal(TGrammerRule(Rules[9]));
     84//    DeleteNonterminal(TGrammerRule(Rules[0]));
     85//    DeleteEmpty;
     86//    Join(TGrammerRule(Rules[17]), 1);
     87//    Join(TGrammerRule(Rules[15]), 0);
     88//    Join(TGrammerRule(Rules[14]), 0);
     89  end;
     90  ShowProgramTree(PascalProgram);
     91end;
     92
    7193procedure TMainForm.FormCreate(Sender: TObject);
    7294var
     
    311333  SourceProgram := TProgram.Create;
    312334  Memo1.Lines.LoadFromFile(DefaultGrammerFileName);
     335  Memo2.Lines.LoadFromFile(DefaultPascalFileName);
    313336  SourceCode := '';
    314337//  with Memo1.Lines do
     
    317340//  end;
    318341
    319   Grammer.Parse(Memo1.Text, SourceProgram);
    320   ShowProgramTree(SourceProgram);
     342  //Grammer.Parse(Memo1.Text, SourceProgram);
     343  //ShowProgramTree(SourceProgram);
    321344end;
    322345
     
    324347begin
    325348  Memo1.Lines.SaveToFile(DefaultGrammerFileName);
     349  Memo2.Lines.SaveToFile(DefaultPascalFileName);
    326350  PascalGrammer.Free;
    327351  Grammer.Free;
     
    386410        PascalGrammer.Rules.Add(NewRule);
    387411        RuleItem.Repetition := True;
     412        RuleItem.Optional := True;
    388413        RuleItem.ItemType := itNonterminal;
    389414        RuleItem.Rule := NewRule;
     
    454479      end;
    455480      NewTreeNode := AddChild(TreeNode, ARule.Name + Attributs);
    456       for II := 0 to Length(Items) - 1 do
    457       with Items[II] do begin
     481      for II := 0 to Items.Count - 1 do
     482      with TGrammerItem(Items[II]) do begin
    458483        if Optional then Attributs := '(Opt)' else Attributs := '';
    459484        if Repetition then Attributs := Attributs + '(Rep)';
  • grammer/test.grm

    r5 r8  
    1 ab1=bc | (s 'g') [{a}] .
     1program = block '.' .
     2block =  { declaration } statement .
     3declaration = constant | variable | function .
     4constant = CONST constant_definition { ',' constant_definition } ';' .
     5constant_definition = NAME '=' NUMBER .
     6variable = VAR NAME { ',' NAME } ';' .
     7function = FUNCTION NAME '(' [ NAME { ',' NAME } ]  ')' block ';' .
     8expression = [ ( '-' | '+' ) ] term { ( '-' | '+' ) term } .
     9term = factor { ( '*' | '/' ) factor } .
     10factor = NAME '(' [ expression { ',' expression } ] ')'
     11  | NUMBER | NAME | '(' expression ')' .
     12statement = [ NAME ':=' expression
     13  | BEGIN statement { ';' statement } END
     14  | IF condition THEN statement
     15  | WHILE condition DO statement
     16  | RETURN expression
     17  | WRITE expression ] .
     18condition = ODD expression
     19  | expression ( '=' | '<>' | '<' | '<=' | '>' | '>=' ) expression .
Note: See TracChangeset for help on using the changeset viewer.