source: branches/Void/UGrammer.pas

Last change on this file was 19, checked in by george, 15 years ago
  • Upraveno: Vylepšeny třídy pro analýzu gramatiky.
File size: 2.3 KB
Line 
1unit UGrammer;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, fgl, UTokenizer;
9
10type
11 TRuleType = (rtSequence, rtAlternative);
12 TRuleItemType = (itTerminal, itNonterminal);
13
14 TGrammerRule = class;
15 TGrammer = class;
16
17 THandleEvent = procedure (GrammerRule: TGrammerRule) of object;
18
19 TGrammerItem = class
20 private
21 Parent: TGrammerRule;
22 public
23 TokenType: TTokenType;
24 Rule: TGrammerRule;
25 ItemType: TRuleItemType;
26 Optional: Boolean;
27 Repetition: Boolean;
28 Text: string;
29 end;
30
31 TGrammerItemList = specialize TFPGObjectList<TGrammerItem>;
32
33 { TGrammerRule }
34
35 TGrammerRule = class
36 private
37 FOnHandle: THandleEvent;
38 Parent: TGrammer;
39 public
40 Name: string;
41 RuleType: TRuleType;
42 Items: TGrammerItemList;
43 procedure AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean);
44 procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean);
45 constructor Create;
46 destructor Destroy; override;
47 property OnHandle: THandleEvent read FOnHandle write FOnHandle;
48 end;
49
50 TGrammerRuleList = specialize TFPGObjectList<TGrammerRule>;
51
52 TGrammer = class
53 Rules: TGrammerRuleList;
54 TopRule: TGrammerRule;
55 constructor Create;
56 destructor Destroy; override;
57 end;
58
59implementation
60
61procedure TGrammerRule.AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean
62 );
63begin
64 with Items[Items.Add(TGrammerItem.Create)] do begin
65 ItemType := itTerminal;
66 TokenType := ATokenType;
67 Text := AText;
68 Optional := AOptional;
69 Repetition := ARepetition;
70 Parent := Self;
71 end;
72end;
73
74procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional,
75 ARepetition: Boolean);
76begin
77 with Items[Items.Add(TGrammerItem.Create)] do begin
78 ItemType := itNonterminal;
79 Rule := ARule;
80 Optional := AOptional;
81 Repetition := ARepetition;
82 Parent := Self;
83 end;
84end;
85
86constructor TGrammerRule.Create;
87begin
88 Items := TGrammerItemList.Create;
89end;
90
91destructor TGrammerRule.Destroy;
92begin
93 inherited Destroy;
94end;
95
96{ TGrammer }
97
98constructor TGrammer.Create;
99begin
100 Rules := TGrammerRuleList.Create;
101end;
102
103destructor TGrammer.Destroy;
104begin
105 Rules.Destroy;
106 inherited Destroy;
107end;
108
109end.
110
Note: See TracBrowser for help on using the repository browser.