Changeset 2
- Timestamp:
- Oct 1, 2007, 3:09:51 PM (17 years ago)
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
/
- Property svn:ignore
-
old new 6 6 *.dsk 7 7 *.cfg 8 __history
-
- Property svn:ignore
-
Program.pas
r1 r2 1 ABC1:=-10 1 p= b | '' | 'b' . -
UMainForm.dfm
r1 r2 1 1 object MainForm: TMainForm 2 Left = 03 Top = 02 Left = 320 3 Top = 189 4 4 Caption = 'P'#345'eklada'#269' pascalu' 5 ClientHeight = 4606 ClientWidth = 6825 ClientHeight = 672 6 ClientWidth = 1017 7 7 Color = clBtnFace 8 8 Font.Charset = DEFAULT_CHARSET … … 12 12 Font.Style = [] 13 13 OldCreateOrder = False 14 Position = poDesigned 14 15 OnCreate = FormCreate 15 16 OnDestroy = FormDestroy 17 DesignSize = ( 18 1017 19 672) 16 20 PixelsPerInch = 96 17 21 TextHeight = 13 … … 20 24 Top = 8 21 25 Width = 257 22 Height = 44126 Height = 313 23 27 TabOrder = 0 24 28 end … … 35 39 Left = 360 36 40 Top = 8 37 Width = 313 38 Height = 441 41 Width = 649 42 Height = 657 43 Anchors = [akLeft, akTop, akRight, akBottom] 39 44 Indent = 19 40 45 TabOrder = 2 46 ExplicitWidth = 313 47 ExplicitHeight = 441 48 end 49 object TreeView2: TTreeView 50 Left = 8 51 Top = 328 52 Width = 345 53 Height = 337 54 Anchors = [akLeft, akTop, akBottom] 55 Indent = 19 56 TabOrder = 3 41 57 end 42 58 end -
UMainForm.pas
r1 r2 58 58 59 59 TGrammerItem = record 60 public 60 61 ItemType: TRuleItemType; 61 62 Character: Char; … … 64 65 Repetition: Boolean; 65 66 procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters); 67 constructor Create(ItemType: TRuleType); 66 68 end; 67 69 … … 74 76 Items: array of TGrammerItem; 75 77 procedure AddTerminal(Character: Char; Optional, Repetition: Boolean); 78 procedure AddTerminalText(Text: string); 76 79 procedure AddRule(Rule: TGrammerRule; Optional, Repetition: Boolean); 77 80 procedure ProcessCharacter(Character: Char); 78 81 procedure GetPossibleCharacters(Path: TGrammerPath; 79 82 var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = 0); 83 constructor Create; 80 84 end; 81 85 … … 93 97 Button1: TButton; 94 98 TreeView1: TTreeView; 99 TreeView2: TTreeView; 95 100 procedure FormCreate(Sender: TObject); 96 101 procedure FormDestroy(Sender: TObject); … … 103 108 SourceProgram: TProgram; 104 109 procedure ShowProgramTree(SourceProgram: TProgram); 110 procedure ShowGrammerTree(Grammer: TGrammer); 105 111 end; 106 112 … … 119 125 procedure TMainForm.FormCreate(Sender: TObject); 120 126 var 127 LowerCaseAlphabeticCharacter, UpperCaseAlphabeticCharacter, 121 128 Digit, AlphabeticCharacter, Number, AlphaNumericCharacter, 122 Identifier, Assigmnent, Expression, RuleString: TGrammerRule; 129 Identifier, Assignment, Expression, RuleString, Rule, RuleList, 130 AlternateOptions, OptionBlock, RepetitionBlock, GroupingBlock, 131 AlternateOptionsSubBlock, Term, AllCharacters, WhiteSpace: TGrammerRule; 123 132 C: Char; 133 I: Integer; 134 SourceCode: string; 124 135 begin 125 136 Grammer := TGrammer.Create; 126 127 137 with Grammer do begin 138 WhiteSpace := TGrammerRule.Create; 139 with WhiteSpace do begin 140 Name := 'WhiteSpace'; 141 RuleType := rtAlternative; 142 AddTerminal(' ', False, False); 143 AddTerminal(#10, False, False); 144 AddTerminal(#13, False, False); 145 end; 146 Rules.Add(WhiteSpace); 147 148 LowerCaseAlphabeticCharacter := TGrammerRule.Create; 149 with LowerCaseAlphabeticCharacter do begin 150 Name := 'LowerCaseAlphabeticCharacter'; 151 RuleType := rtAlternative; 152 for C := 'a' to 'z' do AddTerminal(C, False, False); 153 end; 154 Rules.Add(LowerCaseAlphabeticCharacter); 155 156 UpperCaseAlphabeticCharacter := TGrammerRule.Create; 157 with UpperCaseAlphabeticCharacter do begin 158 Name := 'UpperCaseAlphabeticCharacter'; 159 RuleType := rtAlternative; 160 for C := 'A' to 'Z' do AddTerminal(C, False, False); 161 end; 162 Rules.Add(UpperCaseAlphabeticCharacter); 163 128 164 AlphabeticCharacter := TGrammerRule.Create; 129 165 with AlphabeticCharacter do begin 130 166 Name := 'AlphabeticCharacter'; 131 167 RuleType := rtAlternative; 132 for C := 'A' to 'Z' do AddTerminal(C, False, False); 168 AddRule(LowerCaseAlphabeticCharacter, False, False); 169 AddRule(UpperCaseAlphabeticCharacter, False, False); 133 170 end; 134 171 Rules.Add(AlphabeticCharacter); … … 169 206 Rules.Add(Identifier); 170 207 208 AllCharacters := TGrammerRule.Create; 209 with AllCharacters do begin 210 Name := 'AllCharacters'; 211 RuleType := rtAlternative; 212 for I := 32 to 38 do AddTerminal(Chr(I), False, False); 213 for I := 40 to 125 do AddTerminal(Chr(I), False, False); 214 end; 215 Rules.Add(AllCharacters); 216 171 217 RuleString := TGrammerRule.Create; 172 218 with RuleString do begin … … 174 220 RuleType := rtSequence; 175 221 AddTerminal('''', False, False); 176 AddRule(Al phaNumericCharacter, True, True);222 AddRule(AllCharacters, True, True); 177 223 AddTerminal('''', False, False); 178 224 end; 179 225 Rules.Add(RuleString); 180 226 227 AlternateOptions := TGrammerRule.Create; 181 228 Expression := TGrammerRule.Create; 229 230 AlternateOptionsSubBlock := TGrammerRule.Create; 231 with AlternateOptionsSubBlock do begin 232 Name := 'AlternateOptionsSubBlock'; 233 RuleType := rtSequence; 234 AddRule(WhiteSpace, True, True); 235 AddTerminal('|', False, False); 236 AddRule(WhiteSpace, True, True); 237 AddRule(AlternateOptions, True, True); 238 end; 239 Rules.Add(AlternateOptionsSubBlock); 240 241 OptionBlock := TGrammerRule.Create; 242 with OptionBlock do begin 243 Name := 'OptionBlock'; 244 RuleType := rtSequence; 245 AddTerminal('[', False, False); 246 AddRule(WhiteSpace, True, True); 247 AddRule(Expression, False, False); 248 AddRule(WhiteSpace, True, True); 249 AddTerminal(']', False, False); 250 end; 251 Rules.Add(OptionBlock); 252 253 RepetitionBlock := TGrammerRule.Create; 254 with RepetitionBlock do begin 255 Name := 'RepetitionBlock'; 256 RuleType := rtSequence; 257 AddTerminal('{', False, False); 258 AddRule(WhiteSpace, True, True); 259 AddRule(Expression, False, False); 260 AddRule(WhiteSpace, True, True); 261 AddTerminal('}', False, False); 262 end; 263 Rules.Add(RepetitionBlock); 264 265 GroupingBlock := TGrammerRule.Create; 266 with GroupingBlock do begin 267 Name := 'GroupingBlock'; 268 RuleType := rtSequence; 269 AddTerminal('(', False, False); 270 AddRule(WhiteSpace, True, True); 271 AddRule(Expression, False, False); 272 AddRule(WhiteSpace, True, True); 273 AddTerminal(')', False, False); 274 end; 275 Rules.Add(GroupingBlock); 276 277 Term := TGrammerRule.Create; 278 with Term do begin 279 Name := 'Term'; 280 RuleType := rtAlternative; 281 AddRule(Identifier, False, False); 282 AddRule(RuleString, False, False); 283 end; 284 Rules.Add(Term); 285 286 with AlternateOptions do begin 287 Name := 'AlternateOptions'; 288 RuleType := rtSequence; 289 AddRule(Term, False, False); 290 AddRule(AlternateOptionsSubBlock, True, True); 291 end; 292 Rules.Add(AlternateOptions); 293 182 294 with Expression do begin 183 295 Name := 'Expression'; 184 296 RuleType := rtAlternative; 297 AddRule(RepetitionBlock, False, False); 298 AddRule(OptionBlock, False, False); 299 AddRule(GroupingBlock, False, False); 300 AddRule(AlternateOptions, False, False); 301 end; 302 Rules.Add(Expression); 303 304 Rule := TGrammerRule.Create; 305 with Rule do begin 306 Name := 'Rule'; 307 RuleType := rtSequence; 308 AddRule(WhiteSpace, True, True); 185 309 AddRule(Identifier, False, False); 186 AddRule(Number, False, False); 187 AddRule(RuleString, False, False); 188 end; 189 Rules.Add(Expression); 190 191 Assigmnent := TGrammerRule.Create; 192 with Assigmnent do begin 193 Name := 'Assigmnent'; 194 RuleType := rtSequence; 195 AddRule(Identifier, False, False); 196 AddTerminal(':', False, False); 310 AddRule(WhiteSpace, True, True); 197 311 AddTerminal('=', False, False); 198 AddRule(Expression, False, False); 199 end; 200 Rules.Add(Assigmnent); 201 202 TopRule := Assigmnent; 203 end; 312 AddRule(WhiteSpace, True, True); 313 AddRule(Expression, False, True); 314 AddRule(WhiteSpace, True, True); 315 AddTerminal('.', False, False); 316 AddRule(WhiteSpace, True, True); 317 end; 318 Rules.Add(Rule); 319 320 RuleList := TGrammerRule.Create; 321 with RuleList do begin 322 Name := 'RuleList'; 323 RuleType := rtSequence; 324 AddRule(Rule, False, True); 325 end; 326 Rules.Add(RuleList); 327 328 TopRule := RuleList; 329 end; 330 331 ShowGrammerTree(Grammer); 204 332 205 333 SourceProgram := TProgram.Create; 206 207 Memo1.Lines.LoadFromFile('Program.pas'); 334 Memo1.Lines.LoadFromFile('P0.grm'); 335 SourceCode := '';; 336 // with Memo1.Lines do 337 // for I := 1 to Length(Text) do begin 338 // if (Text[I] <> ' ') and (Text[I] <> #10) and (Text[I] <> #13) then SourceCode := SourceCode + Text[I]; 339 // end; 340 208 341 Grammer.Parse(Memo1.Text, SourceProgram); 209 342 ShowProgramTree(SourceProgram); … … 229 362 Items[High(Items)].Optional := Optional; 230 363 Items[High(Items)].Repetition := Repetition; 364 end; 365 366 procedure TGrammerRule.AddTerminalText(Text: string); 367 var 368 I: Integer; 369 begin 370 for I := 1 to Length(Text) do AddTerminal(Text[I], False, False); 371 end; 372 373 constructor TGrammerRule.Create; 374 begin 375 231 376 end; 232 377 … … 264 409 begin 265 410 Memo1.Lines.SaveToFile('Program.pas'); 411 266 412 Grammer.Free; 267 413 SourceProgram.Free; 414 end; 415 416 procedure TMainForm.ShowGrammerTree(Grammer: TGrammer); 417 var 418 I, II: Integer; 419 NewTreeNode: TTreeNode; 420 NewTreeNode2: TTreeNode; 421 Attributs: string; 422 begin 423 with Grammer, TreeView2, Items do begin 424 BeginUpdate; 425 Clear; 426 TopItem := AddChild(nil, 'Gramatika'); 427 for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin 428 case RuleType of 429 rtSequence: Attributs := '(Seq)'; 430 rtAlternative: Attributs := '(Alt)'; 431 end; 432 NewTreeNode := AddChild(TopItem, Name + Attributs); 433 for II := 0 to Length(Items) - 1 do 434 with Items[II] do begin 435 if Optional then Attributs := '(Opt)' else Attributs := ''; 436 if Repetition then Attributs := Attributs + '(Rep)'; 437 case ItemType of 438 itTerminal: Attributs := Character + Attributs; 439 itNonterminal: if Assigned(Rule) then 440 Attributs := '<' + Rule.Name + '>' + Attributs; 441 else Attributs := '<?>' + Attributs; 442 end; 443 NewTreeNode2 := AddChild(NewTreeNode, Attributs); 444 end; 445 end; 446 TopItem.Expand(False); 447 EndUpdate; 448 end; 268 449 end; 269 450 … … 316 497 var 317 498 Path: TGrammerPath; 318 I : Integer;499 I, II: Integer; 319 500 C: Integer; 320 501 Scope: TPossibleCharacters; … … 322 503 UseCharIndex: Integer; 323 504 UseRule: TGrammerRule; 505 ExpectedCharacters: string; 324 506 begin 325 507 SetLength(Path.Items, Length(Path.Items) + 1); … … 344 526 ParsedProgram.Insert(Scope.Items[C].RulePath, Scope.Items[C].Character); 345 527 end else begin 346 raise Exception.Create('Parse error'); 528 ExpectedCharacters := ''; 529 for II := 0 to Length(Scope.Items) - 1 do 530 ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character; 531 break; 532 //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + 533 //'" but found "' + Text[I] + '".'); 347 534 end; 348 535 Path.Assign(Scope.Items[C].RulePath); … … 415 602 { TGrammerItem } 416 603 604 constructor TGrammerItem.Create(ItemType: TRuleType); 605 begin 606 Rule := nil; 607 end; 608 417 609 procedure TGrammerItem.GetPossibleCharacters(Path: TGrammerPath; 418 610 var Characters: TPossibleCharacters); … … 444 636 SetLength(Path.Items, PathIndex + 1); 445 637 Inc(Path.Items[PathIndex].ItemIndex); 446 Inc(Path.Items[PathIndex].CharIndex);638 //Inc(Path.Items[PathIndex].CharIndex); 447 639 Rule.Items[Index].GetPossibleCharacters(Path, Characters); 448 640 Found := True;
Note:
See TracChangeset
for help on using the changeset viewer.