Changeset 32
- Timestamp:
- Nov 11, 2009, 2:14:44 PM (15 years ago)
- Location:
- branches/Analyzátor gramatiky
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Analyzátor gramatiky/PascalCompiler.lpi
r31 r32 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 0"/>11 <ActiveEditorIndexAtStart Value="4"/> 12 12 </General> 13 13 <VersionInfo> … … 31 31 </Item1> 32 32 </RequiredPackages> 33 <Units Count=" 7">33 <Units Count="9"> 34 34 <Unit0> 35 35 <Filename Value="PascalCompiler.lpr"/> … … 37 37 <CursorPos X="28" Y="8"/> 38 38 <TopLine Value="1"/> 39 <EditorIndex Value=" 1"/>40 <UsageCount Value="2 7"/>39 <EditorIndex Value="2"/> 40 <UsageCount Value="28"/> 41 41 <Loaded Value="True"/> 42 42 </Unit0> … … 48 48 <ResourceBaseClass Value="Form"/> 49 49 <UnitName Value="UMainForm"/> 50 <CursorPos X=" 20" Y="58"/>51 <TopLine Value=" 54"/>52 <EditorIndex Value=" 2"/>53 <UsageCount Value="2 7"/>50 <CursorPos X="15" Y="336"/> 51 <TopLine Value="315"/> 52 <EditorIndex Value="3"/> 53 <UsageCount Value="28"/> 54 54 <Loaded Value="True"/> 55 55 </Unit1> … … 58 58 <IsPartOfProject Value="True"/> 59 59 <UnitName Value="UGrammer"/> 60 <CursorPos X=" 10" Y="398"/>61 <TopLine Value=" 384"/>60 <CursorPos X="9" Y="238"/> 61 <TopLine Value="237"/> 62 62 <EditorIndex Value="0"/> 63 <UsageCount Value="2 7"/>63 <UsageCount Value="28"/> 64 64 <Loaded Value="True"/> 65 65 </Unit2> … … 68 68 <IsPartOfProject Value="True"/> 69 69 <UnitName Value="UProgram"/> 70 <CursorPos X="1" Y="1 01"/>71 <TopLine Value=" 86"/>72 <EditorIndex Value=" 3"/>73 <UsageCount Value="2 7"/>70 <CursorPos X="1" Y="122"/> 71 <TopLine Value="107"/> 72 <EditorIndex Value="4"/> 73 <UsageCount Value="28"/> 74 74 <Loaded Value="True"/> 75 75 </Unit3> … … 93 93 <UsageCount Value="10"/> 94 94 </Unit6> 95 <Unit7> 96 <Filename Value="UParser.pas"/> 97 <IsPartOfProject Value="True"/> 98 <UnitName Value="UParser"/> 99 <CursorPos X="14" Y="3"/> 100 <TopLine Value="1"/> 101 <EditorIndex Value="1"/> 102 <UsageCount Value="21"/> 103 <Loaded Value="True"/> 104 </Unit7> 105 <Unit8> 106 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\classesh.inc"/> 107 <CursorPos X="3" Y="604"/> 108 <TopLine Value="589"/> 109 <EditorIndex Value="5"/> 110 <UsageCount Value="10"/> 111 <Loaded Value="True"/> 112 </Unit8> 95 113 </Units> 96 114 <JumpHistory Count="30" HistoryIndex="29"> 97 115 <Position1> 98 <Filename Value=" PascalCompiler.lpr"/>99 <Caret Line="12 " Column="11" TopLine="1"/>116 <Filename Value="UGrammer.pas"/> 117 <Caret Line="125" Column="41" TopLine="125"/> 100 118 </Position1> 101 119 <Position2> 102 <Filename Value=" PascalCompiler.lpr"/>103 <Caret Line="1 8" Column="15" TopLine="1"/>120 <Filename Value="UGrammer.pas"/> 121 <Caret Line="129" Column="18" TopLine="125"/> 104 122 </Position2> 105 123 <Position3> 106 <Filename Value="U Program.pas"/>107 <Caret Line=" 19" Column="20" TopLine="1"/>124 <Filename Value="UGrammer.pas"/> 125 <Caret Line="58" Column="17" TopLine="37"/> 108 126 </Position3> 109 127 <Position4> 110 <Filename Value="U MainForm.pas"/>111 <Caret Line=" 83" Column="15" TopLine="57"/>128 <Filename Value="UGrammer.pas"/> 129 <Caret Line="314" Column="1" TopLine="293"/> 112 130 </Position4> 113 131 <Position5> 114 <Filename Value="U MainForm.pas"/>115 <Caret Line="6 42" Column="15" TopLine="621"/>132 <Filename Value="UGrammer.pas"/> 133 <Caret Line="63" Column="3" TopLine="48"/> 116 134 </Position5> 117 135 <Position6> 118 <Filename Value="U MainForm.pas"/>119 <Caret Line=" 629" Column="55" TopLine="589"/>136 <Filename Value="UGrammer.pas"/> 137 <Caret Line="228" Column="3" TopLine="206"/> 120 138 </Position6> 121 139 <Position7> 122 <Filename Value="U MainForm.pas"/>123 <Caret Line=" 58" Column="20" TopLine="54"/>140 <Filename Value="UGrammer.pas"/> 141 <Caret Line="83" Column="3" TopLine="68"/> 124 142 </Position7> 125 143 <Position8> 126 144 <Filename Value="UProgram.pas"/> 127 <Caret Line="1 16" Column="21" TopLine="93"/>145 <Caret Line="101" Column="1" TopLine="86"/> 128 146 </Position8> 129 147 <Position9> 130 148 <Filename Value="UProgram.pas"/> 131 <Caret Line=" 118" Column="22" TopLine="102"/>149 <Caret Line="90" Column="43" TopLine="86"/> 132 150 </Position9> 133 151 <Position10> 134 <Filename Value="U Grammer.pas"/>135 <Caret Line=" 238" Column="10" TopLine="233"/>152 <Filename Value="UProgram.pas"/> 153 <Caret Line="39" Column="40" TopLine="39"/> 136 154 </Position10> 137 155 <Position11> 138 <Filename Value="U Grammer.pas"/>139 <Caret Line=" 93" Column="22" TopLine="78"/>156 <Filename Value="UProgram.pas"/> 157 <Caret Line="111" Column="13" TopLine="96"/> 140 158 </Position11> 141 159 <Position12> 142 <Filename Value="U Grammer.pas"/>143 <Caret Line=" 89" Column="57" TopLine="74"/>160 <Filename Value="UProgram.pas"/> 161 <Caret Line="197" Column="1" TopLine="175"/> 144 162 </Position12> 145 163 <Position13> 146 164 <Filename Value="UProgram.pas"/> 147 <Caret Line=" 119" Column="31" TopLine="96"/>165 <Caret Line="95" Column="1" TopLine="88"/> 148 166 </Position13> 149 167 <Position14> 150 <Filename Value="U Grammer.pas"/>151 <Caret Line="102" Column=" 63" TopLine="88"/>168 <Filename Value="UProgram.pas"/> 169 <Caret Line="102" Column="1" TopLine="87"/> 152 170 </Position14> 153 171 <Position15> 154 172 <Filename Value="UProgram.pas"/> 155 <Caret Line="1 01" Column="1" TopLine="86"/>173 <Caret Line="113" Column="1" TopLine="98"/> 156 174 </Position15> 157 175 <Position16> 158 <Filename Value="U Grammer.pas"/>159 <Caret Line=" 309" Column="36" TopLine="286"/>176 <Filename Value="UProgram.pas"/> 177 <Caret Line="114" Column="9" TopLine="99"/> 160 178 </Position16> 161 179 <Position17> 162 <Filename Value="U Grammer.pas"/>163 <Caret Line=" 25" Column="23" TopLine="10"/>180 <Filename Value="UProgram.pas"/> 181 <Caret Line="94" Column="1" TopLine="93"/> 164 182 </Position17> 165 183 <Position18> 166 <Filename Value="U Grammer.pas"/>167 <Caret Line=" 22" Column="45" TopLine="7"/>184 <Filename Value="UProgram.pas"/> 185 <Caret Line="97" Column="1" TopLine="88"/> 168 186 </Position18> 169 187 <Position19> 170 <Filename Value="U Grammer.pas"/>171 <Caret Line=" 309" Column="48" TopLine="371"/>188 <Filename Value="UProgram.pas"/> 189 <Caret Line="112" Column="1" TopLine="100"/> 172 190 </Position19> 173 191 <Position20> 174 <Filename Value="U Program.pas"/>175 <Caret Line=" 101" Column="1" TopLine="86"/>192 <Filename Value="UGrammer.pas"/> 193 <Caret Line="87" Column="19" TopLine="63"/> 176 194 </Position20> 177 195 <Position21> 178 <Filename Value="U Grammer.pas"/>179 <Caret Line=" 327" Column="36" TopLine="305"/>196 <Filename Value="UProgram.pas"/> 197 <Caret Line="112" Column="13" TopLine="97"/> 180 198 </Position21> 181 199 <Position22> 182 200 <Filename Value="UProgram.pas"/> 183 <Caret Line=" 101" Column="1" TopLine="86"/>201 <Caret Line="96" Column="20" TopLine="81"/> 184 202 </Position22> 185 203 <Position23> 186 <Filename Value="U Grammer.pas"/>187 <Caret Line=" 320" Column="1" TopLine="305"/>204 <Filename Value="UProgram.pas"/> 205 <Caret Line="113" Column="39" TopLine="88"/> 188 206 </Position23> 189 207 <Position24> 190 208 <Filename Value="UProgram.pas"/> 191 <Caret Line="1 01" Column="1" TopLine="86"/>209 <Caret Line="112" Column="33" TopLine="97"/> 192 210 </Position24> 193 211 <Position25> 194 <Filename Value="U Grammer.pas"/>195 <Caret Line=" 316" Column="11" TopLine="310"/>212 <Filename Value="UProgram.pas"/> 213 <Caret Line="90" Column="59" TopLine="75"/> 196 214 </Position25> 197 215 <Position26> 198 <Filename Value="U Grammer.pas"/>199 <Caret Line=" 292" Column="22" TopLine="277"/>216 <Filename Value="UProgram.pas"/> 217 <Caret Line="112" Column="20" TopLine="97"/> 200 218 </Position26> 201 219 <Position27> 202 220 <Filename Value="UGrammer.pas"/> 203 <Caret Line=" 43" Column="26" TopLine="28"/>221 <Caret Line="87" Column="21" TopLine="72"/> 204 222 </Position27> 205 223 <Position28> 206 <Filename Value="U Grammer.pas"/>207 <Caret Line=" 38" Column="62" TopLine="23"/>224 <Filename Value="UProgram.pas"/> 225 <Caret Line="112" Column="20" TopLine="97"/> 208 226 </Position28> 209 227 <Position29> 210 228 <Filename Value="UGrammer.pas"/> 211 <Caret Line=" 290" Column="39" TopLine="283"/>229 <Caret Line="88" Column="66" TopLine="72"/> 212 230 </Position29> 213 231 <Position30> 214 <Filename Value="U Grammer.pas"/>215 <Caret Line=" 38" Column="17" TopLine="17"/>232 <Filename Value="UProgram.pas"/> 233 <Caret Line="112" Column="20" TopLine="97"/> 216 234 </Position30> 217 235 </JumpHistory> … … 237 255 </CompilerOptions> 238 256 <Debugging> 239 <BreakPoints Count=" 3">257 <BreakPoints Count="2"> 240 258 <Item1> 241 259 <Source Value="UMainForm.pas"/> … … 244 262 <Item2> 245 263 <Source Value="UProgram.pas"/> 246 <Line Value=" 101"/>264 <Line Value="98"/> 247 265 </Item2> 248 <Item3>249 <Source Value="UGrammer.pas"/>250 <Line Value="323"/>251 </Item3>252 266 </BreakPoints> 253 267 <Exceptions Count="3"> -
branches/Analyzátor gramatiky/PascalCompiler.lpr
r29 r32 7 7 UMainForm in 'UMainForm.pas' {MainForm}, 8 8 UGrammer in 'UGrammer.pas', 9 UProgram in 'UProgram.pas' ;9 UProgram in 'UProgram.pas', UParser; 10 10 11 11 {$IFDEF WINDOWS}{$R PascalCompiler.rc}{$ENDIF} -
branches/Analyzátor gramatiky/UGrammer.pas
r31 r32 6 6 7 7 uses 8 Classes, ComCtrls, SysUtils, Math, UGrammerRules, fgl;8 Classes, ComCtrls, SysUtils, fgl, UParser; 9 9 10 10 type … … 30 30 end; 31 31 32 { TPossibleCharacter }33 34 32 TPossibleCharacter = class 35 33 Character: Char; … … 41 39 42 40 TPossibleCharacterList = specialize TFPGObjectList<TPossibleCharacter>; 43 44 { TPossibleCharacters }45 41 46 42 TPossibleCharacters = class … … 60 56 public 61 57 ItemType: TRuleItemType; 62 Character: Char;58 Text: string; 63 59 Rule: TGrammerRule; 64 60 RuleName: string; 65 61 Optional: Boolean; 66 62 Repetition: Boolean; 67 procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters);63 // procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters); 68 64 constructor Create; 69 65 end; 70 66 71 67 TGrammerItemList = specialize TFPGObjectList<TGrammerItem>; 68 69 { TGrammerRule } 72 70 73 71 TGrammerRule = class … … 81 79 procedure ClearProcessed; 82 80 function Add: TGrammerItem; 83 procedure AddTerminal(A Character: Char; AOptional, ARepetition: Boolean);81 procedure AddTerminal(AText: string; AOptional, ARepetition: Boolean); 84 82 procedure AddTerminalText(Text: string); 85 83 procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean); 86 84 procedure ProcessCharacter(Character: Char); 87 procedure GetPossibleCharacters(Path: TGrammerPath; 88 var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1); 85 // procedure GetPossibleCharacters(Path: TGrammerPath; 86 // var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1); 87 function Check(Path: TGrammerPath; Token: string; 88 UseIndex: Integer = 0; UseCharIndex: Integer = -1): Boolean; 89 89 constructor Create; 90 90 destructor Destroy; override; … … 127 127 end; 128 128 129 procedure TGrammerRule.AddTerminal(A Character: Char; AOptional, ARepetition: Boolean);129 procedure TGrammerRule.AddTerminal(AText: string; AOptional, ARepetition: Boolean); 130 130 begin 131 131 with Add do begin 132 132 ItemType := itTerminal; 133 Character := ACharacter;133 Text := AText; 134 134 Optional := AOptional; 135 135 Repetition := ARepetition; … … 166 166 end; 167 167 168 (* 168 169 procedure TGrammerRule.GetPossibleCharacters(Path: TGrammerPath; 169 170 var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1); … … 229 230 TempPath.Destroy; 230 231 end; 232 *) 231 233 232 234 procedure TGrammerRule.ProcessCharacter(Character: Char); 233 235 begin 236 end; 237 238 function TGrammerRule.Check(Path: TGrammerPath; Token: string; 239 UseIndex: Integer; UseCharIndex: Integer): Boolean; 240 begin 241 234 242 end; 235 243 … … 281 289 end; 282 290 291 (* 283 292 procedure TGrammerItem.GetPossibleCharacters(Path: TGrammerPath; 284 293 var Characters: TPossibleCharacters); 285 294 var 286 295 Found: Boolean; 287 PathIndex: Integer;288 296 NextItemIndex, NextCharIndex: Integer; 289 297 NextRule: TGrammerRule; … … 315 323 end; 316 324 end; 325 *) 317 326 318 327 { TGrammerPath } -
branches/Analyzátor gramatiky/UProgram.pas
r31 r32 37 37 constructor Create; 38 38 procedure Insert(Path: TGrammerPath; Character: Char); 39 procedure Parse(Grammer: TGrammer; T ext: string);39 procedure Parse(Grammer: TGrammer; Tokens: TStringList); 40 40 destructor Destroy; override; 41 41 end; … … 88 88 end; 89 89 90 procedure TProgram.Parse(Grammer: TGrammer; T ext: string);90 procedure TProgram.Parse(Grammer: TGrammer; Tokens: TStringList); 91 91 var 92 92 Path: TGrammerPath; 93 93 I, II: Integer; 94 C: Integer;95 Scope: TPossibleCharacters;96 94 UseIndex: Integer; 97 95 UseCharIndex: Integer; 98 96 UseRule: TGrammerRule; 99 ExpectedCharacters: string;100 97 Level: array of Integer; 101 98 begin 102 99 Path := TGrammerPath.Create; 103 Scope := TPossibleCharacters.Create;104 105 100 with Path.Items[Path.Items.Add(TPathItem.Create)] do begin 106 101 Rule := Grammer.TopRule; … … 109 104 end; 110 105 111 for I := 1 to Length(Text) do begin 112 if (Text[I] <> #13) and (Text[I] <> #10) then begin 106 for I := 0 to Tokens.Count - 1 do begin 113 107 UseIndex := Path.Items[Path.Items.Count - 1].ItemIndex; 114 108 UseCharIndex := Path.Items[Path.Items.Count - 1].CharIndex; 115 109 UseRule := Path.Items[Path.Items.Count - 1].Rule; 116 110 Path.Items.Delete(Path.Items.Count - 1); 117 Scope.Items.Clear;118 111 Grammer.ClearProcessed; 119 UseRule.GetPossibleCharacters(Path, Scope, UseIndex, UseCharIndex); 120 C := Scope.Items.Count; 121 122 // Possible character selection 123 SetLength(Level, 0); 124 for II := 0 to Scope.Items.Count - 1 do 125 with Scope.Items[II] do 112 if UseRule.Check(Path, Tokens[I], UseIndex, UseCharIndex) then 126 113 begin 127 if (Character = Text[I]) then 128 begin 129 C := II; 130 Break; 131 { 132 MaxLevel := Max(Length(RulePath.Items), Length(Path.Items)); 133 Different := False; 134 for Q := 0 to MaxLevel - 1 do 135 begin 136 if (Q >= Length(RulePath.Items)) or (Q >= Length(Path.Items)) or 137 (RulePath.Items[Q].ItemIndex <> Path.Items[Q].ItemIndex) then 138 begin 139 Different := True; 140 Break; 141 end; 142 end; 143 if Different and (Q >= LevelLength) then 144 begin 145 LevelLength := Q; 146 C := II; 147 end; 148 149 } 150 { 151 // Append missing level items 152 if Length(RulePath.Items) > Length(Level) then begin 153 LevelLength := Length(Level); 154 SetLength(Level, Length(RulePath.Items)); 155 for Q := LevelLength to High(Level) do 156 Level[Q] := High(Integer); 157 end; 158 // Compare leftness for all rulepath items 159 LevelIsLeft := True; 160 for Q := 0 to High(Level) do begin 161 if Level[Q] > RulePath.Items[Q].ItemIndex then begin 162 LevelIsLeft := False; 163 Break; 164 end; 165 if Level[Q] < RulePath.Items[Q].ItemIndex then Break; 166 end; 167 168 if (not LevelIsLeft) or (Length(RulePath.Items) > Length(Level)) then begin 169 SetLength(Level, Length(RulePath.Items)); 170 for Q := 0 to High(Level) do 171 Level[Q] := RulePath.Items[Q].ItemIndex; 172 C := II; 173 end; 174 } 175 end; 176 end; 177 178 if C < Scope.Items.Count then begin 179 Path.Assign(Scope.Items[C].RulePath); 180 for II := 0 to Path.Items.Count - 1 do 181 with Path.Items[II] do begin 182 if Affected then Inc(CharIndex); 183 end; 184 Insert(Path, Scope.Items[C].Character); 185 for II := 0 to Path.Items.Count - 1 do 186 with Path.Items[II] do begin 187 Affected := False; 188 end; 189 //Path.Next; 114 190 115 end else begin 191 ExpectedCharacters := ''; 192 for II := 0 to Scope.Items.Count - 1 do 193 ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character; 194 //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'); 116 //raise Exception.Create('Parse error. Expected "' + Tokens[I] + '" but found "' + Text[I] + '".'); 195 117 //MainForm.StatusBar1.SimpleText := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'; 196 118 break; 197 119 end; 198 end;199 120 end; 200 121 Path.Destroy; 201 Scope.Destroy;202 122 end; 203 123
Note:
See TracChangeset
for help on using the changeset viewer.