Changeset 8
- Timestamp:
- Oct 15, 2007, 8:28:00 AM (17 years ago)
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
PascalCompiler.bdsproj
r1 r8 33 33 <Compiler Name="O">1</Compiler> 34 34 <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> 37 37 <Compiler Name="S">0</Compiler> 38 38 <Compiler Name="T">0</Compiler> -
UGrammer.pas
r7 r8 65 65 end; 66 66 67 TGrammerItem = record67 TGrammerItem = class 68 68 private 69 69 Processed: Boolean; … … 77 77 Repetition: Boolean; 78 78 procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters); 79 constructor Create (ItemType: TRuleType);79 constructor Create; 80 80 end; 81 81 … … 86 86 Name: string; 87 87 RuleType: TRuleType; 88 Items: arrayof TGrammerItem;88 Items: TList; // of TGrammerItem; 89 89 procedure ClearProcessed; 90 90 function Add: TGrammerItem; 91 procedure AddTerminal( Character: Char; Optional,Repetition: Boolean);91 procedure AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean); 92 92 procedure AddTerminalText(Text: string); 93 procedure AddRule( Rule: TGrammerRule; Optional,Repetition: Boolean);93 procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean); 94 94 procedure ProcessCharacter(Character: Char); 95 95 procedure GetPossibleCharacters(Path: TGrammerPath; 96 96 var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1); 97 97 constructor Create; 98 destructor Destroy; override; 98 99 end; 99 100 … … 119 120 function TGrammerRule.Add: TGrammerItem; 120 121 begin 121 SetLength(Items, Length(Items) + 1);122 Result := Items[High(Items)];122 Result := TGrammerItem.Create; 123 Items.Add(Result); 123 124 Result.Parent := Self; 124 125 end; 125 126 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; 127 procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional, 128 ARepetition: Boolean); 129 begin 130 with Add do begin 131 ItemType := itNonterminal; 132 Rule := ARule; 133 Optional := AOptional; 134 Repetition := ARepetition; 135 Parent := Self; 136 end; 137 end; 138 139 procedure TGrammerRule.AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean); 140 begin 141 with Add do begin 142 ItemType := itTerminal; 143 Character := ACharacter; 144 Optional := AOptional; 145 Repetition := ARepetition; 146 Parent := Self; 147 end; 145 148 end; 146 149 … … 156 159 I: Integer; 157 160 begin 158 for I := 0 to High(Items) do with Items[I]do begin161 for I := 0 to Items.Count - 1 do with TGrammerItem(Items[I]) do begin 159 162 Processed := False; 160 163 end; … … 164 167 begin 165 168 Ownership := nil; 169 Items := TList.Create; 170 end; 171 172 destructor TGrammerRule.Destroy; 173 var 174 I: Integer; 175 begin 176 for I := 0 to Items.Count - 1 do 177 TGrammerItem(Items[I]).Free; 178 Items.Free; 179 inherited; 166 180 end; 167 181 … … 196 210 end else begin 197 211 // Generate alternatives 198 for I := 0 to High(Items)do begin212 for I := 0 to Items.Count - 1 do begin 199 213 Path.Items[High(Path.Items)].ItemIndex := I; 200 214 //Inc(Path.Items[High(Path.Items)].CharIndex); 201 Items[I].GetPossibleCharacters(Path, Characters);215 TGrammerItem(Items[I]).GetPossibleCharacters(Path, Characters); 202 216 end; 203 217 end; … … 205 219 rtSequence: begin 206 220 TempPath.Assign(Path); 207 if UseIndex >= Length(Items)then begin221 if UseIndex >= Items.Count then begin 208 222 // Forward generation to upper item 209 223 SetLength(Path.Items, Length(Path.Items) - 1); … … 217 231 end else begin 218 232 Path.Items[High(Path.Items)].ItemIndex := UseIndex; 219 Items[UseIndex].GetPossibleCharacters(Path, Characters);233 TGrammerItem(Items[UseIndex]).GetPossibleCharacters(Path, Characters); 220 234 end; 221 235 // Check repetition 222 if (UseIndex > 0) and not Items[UseIndex - 1].Processed then223 if Items[UseIndex - 1].Repetition then begin236 if (UseIndex > 0) and not TGrammerItem(Items[UseIndex - 1]).Processed then 237 if TGrammerItem(Items[UseIndex - 1]).Repetition then begin 224 238 TempPath.Items[High(TempPath.Items)].ItemIndex := UseIndex - 1; 225 Items[UseIndex - 1].GetPossibleCharacters(TempPath, Characters);239 TGrammerItem(Items[UseIndex - 1]).GetPossibleCharacters(TempPath, Characters); 226 240 end; 227 241 end; … … 251 265 begin 252 266 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 begin267 for II := 0 to Items.Count - 1 do with TGrammerItem(Items[II]) do begin 254 268 if (ItemType = itNonterminal) and (Rule = nil) then begin 255 269 J := 0; … … 499 513 { TGrammerItem } 500 514 501 constructor TGrammerItem.Create (ItemType: TRuleType);515 constructor TGrammerItem.Create; 502 516 begin 503 517 Rule := nil; … … 557 571 Index := High(Items); 558 572 while not Success and (Index >= 0) do begin 559 with Items[Index] do if Rule.Items[ItemIndex].Repetition then begin573 with Items[Index] do if TGrammerItem(Rule.Items[ItemIndex]).Repetition then begin 560 574 Success := True; 561 575 //Inc(CharIndex); 562 576 end else begin 563 if ((ItemIndex + 1) < Length(Rule.Items)) and (Rule.RuleType = rtSequence) then begin577 if ((ItemIndex + 1) < Rule.Items.Count) and (Rule.RuleType = rtSequence) then begin 564 578 Inc(ItemIndex); 565 579 //Inc(CharIndex); -
UMainForm.dfm
r7 r8 31 31 Top = 8 32 32 Width = 346 33 Height = 28333 Height = 186 34 34 ScrollBars = ssBoth 35 35 TabOrder = 0 … … 37 37 object Button1: TButton 38 38 Left = 304 39 Top = 29739 Top = 401 40 40 Width = 50 41 41 Height = 25 … … 55 55 object TreeView2: TTreeView 56 56 Left = 8 57 Top = 32857 Top = 432 58 58 Width = 345 59 Height = 33759 Height = 233 60 60 Anchors = [akLeft, akTop, akBottom] 61 61 Indent = 19 … … 64 64 object Button2: TButton 65 65 Left = 232 66 Top = 29766 Top = 401 67 67 Width = 66 68 68 Height = 25 … … 71 71 OnClick = Button2Click 72 72 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 73 90 end -
UMainForm.pas
r7 r8 9 9 const 10 10 DefaultGrammerFileName: string = 'grammer/test.grm'; 11 DefaultPascalFileName: string = 'pascal/test.pas'; 11 12 12 13 type … … 18 19 Label1: TLabel; 19 20 Button2: TButton; 21 Memo2: TMemo; 22 Button3: TButton; 20 23 procedure FormCreate(Sender: TObject); 21 24 procedure FormDestroy(Sender: TObject); 22 25 procedure Button1Click(Sender: TObject); 23 26 procedure Button2Click(Sender: TObject); 27 procedure Button3Click(Sender: TObject); 24 28 private 25 29 procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem); … … 29 33 PascalGrammer: TGrammer; 30 34 SourceProgram: TProgram; 35 PascalProgram: TProgram; 31 36 procedure ShowProgramTree(SourceProgram: TProgram); 32 37 procedure ShowGrammerTree(Grammer: TGrammer); … … 69 74 end; 70 75 76 procedure TMainForm.Button3Click(Sender: TObject); 77 begin 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); 91 end; 92 71 93 procedure TMainForm.FormCreate(Sender: TObject); 72 94 var … … 311 333 SourceProgram := TProgram.Create; 312 334 Memo1.Lines.LoadFromFile(DefaultGrammerFileName); 335 Memo2.Lines.LoadFromFile(DefaultPascalFileName); 313 336 SourceCode := ''; 314 337 // with Memo1.Lines do … … 317 340 // end; 318 341 319 Grammer.Parse(Memo1.Text, SourceProgram);320 ShowProgramTree(SourceProgram);342 //Grammer.Parse(Memo1.Text, SourceProgram); 343 //ShowProgramTree(SourceProgram); 321 344 end; 322 345 … … 324 347 begin 325 348 Memo1.Lines.SaveToFile(DefaultGrammerFileName); 349 Memo2.Lines.SaveToFile(DefaultPascalFileName); 326 350 PascalGrammer.Free; 327 351 Grammer.Free; … … 386 410 PascalGrammer.Rules.Add(NewRule); 387 411 RuleItem.Repetition := True; 412 RuleItem.Optional := True; 388 413 RuleItem.ItemType := itNonterminal; 389 414 RuleItem.Rule := NewRule; … … 454 479 end; 455 480 NewTreeNode := AddChild(TreeNode, ARule.Name + Attributs); 456 for II := 0 to Length(Items)- 1 do457 with Items[II]do begin481 for II := 0 to Items.Count - 1 do 482 with TGrammerItem(Items[II]) do begin 458 483 if Optional then Attributs := '(Opt)' else Attributs := ''; 459 484 if Repetition then Attributs := Attributs + '(Rep)'; -
grammer/test.grm
r5 r8 1 ab1=bc | (s 'g') [{a}] . 1 program = block '.' . 2 block = { declaration } statement . 3 declaration = constant | variable | function . 4 constant = CONST constant_definition { ',' constant_definition } ';' . 5 constant_definition = NAME '=' NUMBER . 6 variable = VAR NAME { ',' NAME } ';' . 7 function = FUNCTION NAME '(' [ NAME { ',' NAME } ] ')' block ';' . 8 expression = [ ( '-' | '+' ) ] term { ( '-' | '+' ) term } . 9 term = factor { ( '*' | '/' ) factor } . 10 factor = NAME '(' [ expression { ',' expression } ] ')' 11 | NUMBER | NAME | '(' expression ')' . 12 statement = [ NAME ':=' expression 13 | BEGIN statement { ';' statement } END 14 | IF condition THEN statement 15 | WHILE condition DO statement 16 | RETURN expression 17 | WRITE expression ] . 18 condition = ODD expression 19 | expression ( '=' | '<>' | '<' | '<=' | '>' | '>=' ) expression .
Note:
See TracChangeset
for help on using the changeset viewer.