Changeset 9
- Timestamp:
- Oct 16, 2007, 9:16:28 AM (17 years ago)
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
UGrammer.pas
r8 r9 295 295 Path: TGrammerPath; 296 296 I, II: Integer; 297 Q: Integer; 297 298 C: Integer; 298 299 Scope: TPossibleCharacters; … … 301 302 UseRule: TGrammerRule; 302 303 ExpectedCharacters: string; 303 Level: Integer; 304 Level: array of Integer; 305 LevelLength: Integer; 306 LevelIsLeft: Boolean; 304 307 begin 305 308 SetLength(Path.Items, Length(Path.Items) + 1); … … 320 323 UseRule.GetPossibleCharacters(Path, Scope, UseIndex, UseCharIndex); 321 324 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 330 352 if C < Length(Scope.Items) then begin 331 353 Path.Assign(Scope.Items[C].RulePath); … … 343 365 ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character; 344 366 //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] + '".'; 346 368 break; 347 369 end; -
UMainForm.dfm
r8 r9 3 3 Top = 189 4 4 Caption = 'P'#345'eklada'#269' pascalu' 5 ClientHeight = 6 726 ClientWidth = 10175 ClientHeight = 653 6 ClientWidth = 734 7 7 Color = clBtnFace 8 8 Font.Charset = DEFAULT_CHARSET … … 17 17 OnDestroy = FormDestroy 18 18 DesignSize = ( 19 101720 6 72)19 734 20 653) 21 21 PixelsPerInch = 96 22 22 TextHeight = 13 23 object Label1: TLabel24 Left = 825 Top = 29726 Width = 327 Height = 1328 end29 23 object Memo1: TMemo 30 24 Left = 8 … … 47 41 Left = 360 48 42 Top = 8 49 Width = 64950 Height = 6 5743 Width = 366 44 Height = 638 51 45 Anchors = [akLeft, akTop, akRight, akBottom] 52 46 Indent = 19 53 47 TabOrder = 2 48 ExplicitWidth = 649 49 ExplicitHeight = 657 54 50 end 55 51 object TreeView2: TTreeView … … 57 53 Top = 432 58 54 Width = 345 59 Height = 2 3355 Height = 214 60 56 Anchors = [akLeft, akTop, akBottom] 61 57 Indent = 19 62 58 TabOrder = 3 59 ExplicitHeight = 233 63 60 end 64 61 object Button2: TButton 65 Left = 23262 Left = 176 66 63 Top = 401 67 64 Width = 66 … … 80 77 end 81 78 object Button3: TButton 82 Left = 15279 Left = 96 83 80 Top = 401 84 81 Width = 74 … … 88 85 OnClick = Button3Click 89 86 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 90 106 end -
UMainForm.pas
r8 r9 17 17 TreeView1: TTreeView; 18 18 TreeView2: TTreeView; 19 Label1: TLabel;20 19 Button2: TButton; 21 20 Memo2: TMemo; 22 21 Button3: TButton; 22 StatusBar1: TStatusBar; 23 Button4: TButton; 23 24 procedure FormCreate(Sender: TObject); 24 25 procedure FormDestroy(Sender: TObject); … … 26 27 procedure Button2Click(Sender: TObject); 27 28 procedure Button3Click(Sender: TObject); 29 procedure Button4Click(Sender: TObject); 28 30 private 29 31 procedure ShowProgramNode(Node: TTreeNode; SourceProgram: TProgramItem); … … 55 57 SourceProgram := TProgram.Create; 56 58 Grammer.Parse(Memo1.Text, SourceProgram); 57 with SourceProgram.TopItem do begin58 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;66 59 ShowProgramTree(SourceProgram); 67 60 end; … … 91 84 end; 92 85 86 procedure TMainForm.Button4Click(Sender: TObject); 87 begin 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); 98 end; 99 93 100 procedure TMainForm.FormCreate(Sender: TObject); 94 101 var … … 260 267 Name := 'ConcatenationBlock'; 261 268 RuleType := rtSequence; 262 AddRule(WhiteSpace, False, True);269 AddRule(WhiteSpace, True, True); 263 270 AddRule(Expression, True, False); 264 271 end; … … 269 276 RuleType := rtSequence; 270 277 AddRule(Expression, False, False); 278 AddRule(WhiteSpace, True, True); 271 279 AddRule(ConcatenationBlock, True, True); 272 280 end; … … 288 296 RuleType := rtSequence; 289 297 AddRule(Concatenation, False, False); 298 AddRule(WhiteSpace, True, True); 290 299 AddRule(SeparationBlock, True, True); 291 300 end; … … 374 383 RuleItem.RuleName := Value; 375 384 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; 376 403 end else begin 377 404 NewRule := TGrammerRule.Create; … … 422 449 I: Integer; 423 450 NewRule: TGrammerRule; 451 WhiteSpace, LowerCaseAlphabeticCharacter, UpperCaseAlphabeticCharacter, 452 AlphabeticCharacter, Digit, Number, AlphaNumericCharacter, 453 Identifier: TGrammerRule; 454 C: Char; 424 455 begin 425 456 for I := 0 to PascalGrammer.Rules.Count - 1 do 426 457 TGrammerRule(PascalGrammer.Rules[I]).Free; 427 458 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 428 532 PascalGrammer.TopRule := nil; 429 533 with SourceProgram.TopItem do begin -
pascal/test.pas
r8 r9 1 CONSTNAME '='NUMBER';''.'1 CONSTNAME:=NUMBER';''.'
Note:
See TracChangeset
for help on using the changeset viewer.