Changeset 19


Ignore:
Timestamp:
Nov 10, 2009, 4:15:47 PM (14 years ago)
Author:
george
Message:
  • Upraveno: Vylepšeny třídy pro analýzu gramatiky.
Location:
branches/Void
Files:
1 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UCompilator.pas

    r18 r19  
    66
    77uses
    8   Classes, SysUtils, UOutputGenerator, UModel, UVoidParser, fgl;
     8  Classes, SysUtils, UOutputGenerator, UModel, UTokenizer, fgl, UGrammer;
    99
    1010type
     
    2323  private
    2424    FOnError: TOnErrorEvent;
     25    Grammer: TGrammer;
    2526    procedure DoError(AText: string);
    2627    procedure ParseBeginEnd;
     
    2930    procedure ParseTypeDefinition;
    3031    procedure ParseExpression;
     32    procedure InitGrammer;
    3133  public
    3234    ErrorMessages: TErrorMessageList;
     
    4951  Terminate: Boolean;
    5052begin
    51   with ErrorMessages.Add do begin
     53  with ErrorMessages[ErrorMessages.Add(TErrorMessage.Create)] do begin
    5254    CodePosition := Parser.TokenStartPosition;
    5355    Text := AText;
     
    147149end;
    148150
     151procedure TCompilator.InitGrammer;
     152var
     153  RuleProgram, RuleBlock, RuleDeclaration, RuleStatement, RuleConstant,
     154  RuleVariable, RuleFunction: TGrammerRule;
     155begin
     156  Grammer := TGrammer.Create;
     157  with Grammer do begin
     158    RuleConstantDefinition := TGrammerRule.Create;
     159    with RuleConstantDefinition do begin
     160      Name := 'ConstantDefinition';
     161      RuleType := rtSequence;
     162    end;
     163    Rules.Add(RuleConstantDefinition);
     164
     165    RuleConstant := TGrammerRule.Create;
     166    with RuleConstant do begin
     167      Name := 'Constant';
     168      RuleType := rtSequence;
     169      AddTerminal('const', ttIdentifier, False, False);
     170      AddRule(RuleConstantDefinition, False, False);
     171    end;
     172    Rules.Add(RuleConstant);
     173
     174    RuleFunction := TGrammerRule.Create;
     175    with RuleFunction do begin
     176      Name := 'Function';
     177      RuleType := rtSequence;
     178    end;
     179    Rules.Add(RuleFunction);
     180
     181    RuleVariable := TGrammerRule.Create;
     182    with RuleVariable do begin
     183      Name := 'Variable';
     184      RuleType := rtSequence;
     185    end;
     186    Rules.Add(RuleVariable);
     187
     188    RuleStatement := TGrammerRule.Create;
     189    with RuleStatement do begin
     190      Name := 'Statement';
     191      RuleType := rtSequence;
     192    end;
     193    Rules.Add(RuleStatement);
     194
     195    RuleDeclaration := TGrammerRule.Create;
     196    with RuleDeclaration do begin
     197      Name := 'Declaration';
     198      RuleType := rtAlternative;
     199      AddRule(RuleFunction, False, False);
     200      AddRule(RuleConstant, False, False);
     201      AddRule(RuleVariable, False, False);
     202    end;
     203    Rules.Add(RuleDeclaration);
     204
     205    RuleBlock := TGrammerRule.Create;
     206    with RuleBlock do begin
     207      Name := 'Block';
     208      RuleType := rtSequence;
     209      AddRule(RuleDeclaration, False, True);
     210      AddRule(RuleStatement, False, False);
     211    end;
     212    Rules.Add(RuleBlock);
     213
     214    RuleProgram := TGrammerRule.Create;
     215    with RuleProgram do begin
     216      Name := 'Program';
     217      RuleType := rtSequence;
     218      AddRule(RuleBlock, False, False);
     219      AddTerminal('.', ttSymbol, False, False);
     220    end;
     221    Rules.Add(RuleProgram);
     222
     223    TopRule := RuleProgram;
     224  end;
     225end;
     226
    149227procedure TCompilator.Compile;
    150228var
     
    167245  Variable: TVariable;
    168246  SourceVariable: TVariable;
    169   VariableName: string;
    170247  Value: string;
    171248  P: Integer;
     
    282359constructor TCompilator.Create;
    283360begin
     361  Grammer := TGrammer.Create;
     362  InitGrammer;
    284363  ErrorMessages := TErrorMessageList.Create;
    285364  SourceCode := TMemoryStream.Create;
     
    290369destructor TCompilator.Destroy;
    291370begin
     371  Grammer.Destroy;
    292372  ErrorMessages.Destroy;
    293373  SourceCode.Destroy;
  • branches/Void/UGrammer.pas

    r18 r19  
    66
    77uses
    8   Classes, SysUtils, fgl;
     8  Classes, SysUtils, fgl, UTokenizer;
    99
    1010type
     
    1212  TRuleItemType = (itTerminal, itNonterminal);
    1313
     14  TGrammerRule = class;
     15  TGrammer = class;
     16
     17  THandleEvent = procedure (GrammerRule: TGrammerRule) of object;
     18
    1419  TGrammerItem = class
     20  private
     21    Parent: TGrammerRule;
     22  public
     23    TokenType: TTokenType;
     24    Rule: TGrammerRule;
    1525    ItemType: TRuleItemType;
    1626    Optional: Boolean;
    1727    Repetition: Boolean;
     28    Text: string;
    1829  end;
    1930
    2031  TGrammerItemList = specialize TFPGObjectList<TGrammerItem>;
    2132
     33  { TGrammerRule }
     34
    2235  TGrammerRule = class
     36  private
     37    FOnHandle: THandleEvent;
     38    Parent: TGrammer;
     39  public
    2340    Name: string;
    2441    RuleType: TRuleType;
    2542    Items: TGrammerItemList;
     43    procedure AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean);
     44    procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean);
    2645    constructor Create;
    2746    destructor Destroy; override;
     47    property OnHandle: THandleEvent read FOnHandle write FOnHandle;
    2848  end;
    2949
    3050  TGrammerRuleList = specialize TFPGObjectList<TGrammerRule>;
    31 
    32   { TGrammer }
    3351
    3452  TGrammer = class
     
    4058
    4159implementation
     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;
    4285
    4386constructor TGrammerRule.Create;
  • branches/Void/project1.lpi

    r18 r19  
    99      <Icon Value="0"/>
    1010      <UseXPManifest Value="True"/>
    11       <ActiveEditorIndexAtStart Value="10"/>
     11      <ActiveEditorIndexAtStart Value="2"/>
    1212    </General>
    1313    <VersionInfo>
     
    3737      </Item2>
    3838    </RequiredPackages>
    39     <Units Count="24">
     39    <Units Count="25">
    4040      <Unit0>
    4141        <Filename Value="project1.lpr"/>
     
    4444        <CursorPos X="51" Y="9"/>
    4545        <TopLine Value="1"/>
    46         <UsageCount Value="56"/>
     46        <UsageCount Value="57"/>
    4747      </Unit0>
    4848      <Unit1>
     
    5555        <TopLine Value="114"/>
    5656        <EditorIndex Value="0"/>
    57         <UsageCount Value="56"/>
     57        <UsageCount Value="57"/>
    5858        <Loaded Value="True"/>
    5959      </Unit1>
     
    6161        <Filename Value="UCompilator.pas"/>
    6262        <UnitName Value="UCompilator"/>
    63         <CursorPos X="24" Y="290"/>
    64         <TopLine Value="51"/>
     63        <CursorPos X="3" Y="238"/>
     64        <TopLine Value="223"/>
    6565        <EditorIndex Value="2"/>
    66         <UsageCount Value="28"/>
     66        <UsageCount Value="29"/>
    6767        <Loaded Value="True"/>
    6868      </Unit2>
     
    7373        <CursorPos X="1" Y="13"/>
    7474        <TopLine Value="20"/>
    75         <EditorIndex Value="6"/>
    76         <UsageCount Value="56"/>
     75        <EditorIndex Value="7"/>
     76        <UsageCount Value="57"/>
    7777        <Loaded Value="True"/>
    7878      </Unit3>
     
    8282        <CursorPos X="8" Y="7"/>
    8383        <TopLine Value="1"/>
    84         <UsageCount Value="56"/>
     84        <UsageCount Value="57"/>
    8585        <SyntaxHighlighter Value="None"/>
    8686      </Unit4>
     
    9191        <CursorPos X="16" Y="135"/>
    9292        <TopLine Value="65"/>
    93         <EditorIndex Value="10"/>
    94         <UsageCount Value="56"/>
     93        <EditorIndex Value="11"/>
     94        <UsageCount Value="57"/>
    9595        <Loaded Value="True"/>
    9696      </Unit5>
     
    122122      <Unit10>
    123123        <Filename Value="UVoidParser.pas"/>
    124         <IsPartOfProject Value="True"/>
    125124        <UnitName Value="UVoidParser"/>
    126125        <CursorPos X="30" Y="162"/>
    127126        <TopLine Value="140"/>
    128         <EditorIndex Value="5"/>
    129         <UsageCount Value="56"/>
     127        <EditorIndex Value="6"/>
     128        <UsageCount Value="57"/>
    130129        <Loaded Value="True"/>
    131130      </Unit10>
     
    163162        <TopLine Value="1"/>
    164163        <EditorIndex Value="1"/>
    165         <UsageCount Value="32"/>
     164        <UsageCount Value="33"/>
    166165        <Loaded Value="True"/>
    167166      </Unit15>
     
    178177        <CursorPos X="24" Y="53"/>
    179178        <TopLine Value="2"/>
    180         <EditorIndex Value="7"/>
    181         <UsageCount Value="31"/>
     179        <EditorIndex Value="8"/>
     180        <UsageCount Value="32"/>
    182181        <Loaded Value="True"/>
    183182      </Unit17>
     
    188187        <CursorPos X="24" Y="80"/>
    189188        <TopLine Value="73"/>
    190         <EditorIndex Value="9"/>
    191         <UsageCount Value="31"/>
     189        <EditorIndex Value="10"/>
     190        <UsageCount Value="32"/>
    192191        <Loaded Value="True"/>
    193192      </Unit18>
     
    198197        <CursorPos X="24" Y="57"/>
    199198        <TopLine Value="1"/>
    200         <EditorIndex Value="8"/>
    201         <UsageCount Value="31"/>
     199        <EditorIndex Value="9"/>
     200        <UsageCount Value="32"/>
    202201        <Loaded Value="True"/>
    203202      </Unit19>
     
    206205        <IsPartOfProject Value="True"/>
    207206        <UnitName Value="UGrammer"/>
    208         <CursorPos X="62" Y="20"/>
    209         <TopLine Value="1"/>
     207        <CursorPos X="15" Y="44"/>
     208        <TopLine Value="29"/>
    210209        <EditorIndex Value="3"/>
    211         <UsageCount Value="27"/>
     210        <UsageCount Value="28"/>
    212211        <Loaded Value="True"/>
    213212      </Unit20>
     
    222221        <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    223222        <UnitName Value="fgl"/>
    224         <CursorPos X="36" Y="356"/>
    225         <TopLine Value="341"/>
    226         <EditorIndex Value="4"/>
    227         <UsageCount Value="11"/>
     223        <CursorPos X="15" Y="124"/>
     224        <TopLine Value="111"/>
     225        <EditorIndex Value="5"/>
     226        <UsageCount Value="12"/>
    228227        <Loaded Value="True"/>
    229228      </Unit22>
     
    234233        <UsageCount Value="10"/>
    235234      </Unit23>
     235      <Unit24>
     236        <Filename Value="UTokenizer.pas"/>
     237        <IsPartOfProject Value="True"/>
     238        <UnitName Value="UTokenizer"/>
     239        <CursorPos X="1" Y="1"/>
     240        <TopLine Value="1"/>
     241        <EditorIndex Value="4"/>
     242        <UsageCount Value="21"/>
     243        <Loaded Value="True"/>
     244      </Unit24>
    236245    </Units>
    237246    <JumpHistory Count="30" HistoryIndex="29">
    238247      <Position1>
    239         <Filename Value="UGrammer.pas"/>
    240         <Caret Line="8" Column="25" TopLine="1"/>
     248        <Filename Value="UCompilator.pas"/>
     249        <Caret Line="155" Column="24" TopLine="140"/>
    241250      </Position1>
    242251      <Position2>
    243252        <Filename Value="UGrammer.pas"/>
    244         <Caret Line="25" Column="28" TopLine="10"/>
     253        <Caret Line="40" Column="39" TopLine="20"/>
    245254      </Position2>
    246255      <Position3>
    247         <Filename Value="UGrammer.pas"/>
    248         <Caret Line="20" Column="33" TopLine="5"/>
     256        <Filename Value="UCompilator.pas"/>
     257        <Caret Line="167" Column="16" TopLine="152"/>
    249258      </Position3>
    250259      <Position4>
    251260        <Filename Value="UGrammer.pas"/>
    252         <Caret Line="37" Column="1" TopLine="9"/>
     261        <Caret Line="21" Column="27" TopLine="7"/>
    253262      </Position4>
    254263      <Position5>
    255264        <Filename Value="UGrammer.pas"/>
    256         <Caret Line="62" Column="12" TopLine="36"/>
     265        <Caret Line="62" Column="29" TopLine="45"/>
    257266      </Position5>
    258267      <Position6>
    259268        <Filename Value="UGrammer.pas"/>
    260         <Caret Line="30" Column="43" TopLine="34"/>
     269        <Caret Line="57" Column="35" TopLine="42"/>
    261270      </Position6>
    262271      <Position7>
    263272        <Filename Value="UGrammer.pas"/>
    264         <Caret Line="63" Column="17" TopLine="37"/>
     273        <Caret Line="70" Column="1" TopLine="55"/>
    265274      </Position7>
    266275      <Position8>
    267         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    268         <Caret Line="799" Column="9" TopLine="795"/>
     276        <Filename Value="UCompilator.pas"/>
     277        <Caret Line="8" Column="58" TopLine="1"/>
    269278      </Position8>
    270279      <Position9>
    271         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    272         <Caret Line="320" Column="50" TopLine="313"/>
     280        <Filename Value="UCompilator.pas"/>
     281        <Caret Line="168" Column="25" TopLine="152"/>
    273282      </Position9>
    274283      <Position10>
    275         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    276         <Caret Line="712" Column="39" TopLine="697"/>
     284        <Filename Value="UCompilator.pas"/>
     285        <Caret Line="171" Column="34" TopLine="155"/>
    277286      </Position10>
    278287      <Position11>
    279         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    280         <Caret Line="715" Column="23" TopLine="697"/>
     288        <Filename Value="UCompilator.pas"/>
     289        <Caret Line="179" Column="34" TopLine="164"/>
    281290      </Position11>
    282291      <Position12>
    283         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    284         <Caret Line="730" Column="11" TopLine="715"/>
     292        <Filename Value="UCompilator.pas"/>
     293        <Caret Line="183" Column="7" TopLine="165"/>
    285294      </Position12>
    286295      <Position13>
    287         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    288         <Caret Line="725" Column="31" TopLine="715"/>
     296        <Filename Value="UCompilator.pas"/>
     297        <Caret Line="194" Column="1" TopLine="176"/>
    289298      </Position13>
    290299      <Position14>
    291         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    292         <Caret Line="825" Column="41" TopLine="810"/>
     300        <Filename Value="UCompilator.pas"/>
     301        <Caret Line="203" Column="18" TopLine="188"/>
    293302      </Position14>
    294303      <Position15>
    295         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    296         <Caret Line="1" Column="24" TopLine="1"/>
     304        <Filename Value="UCompilator.pas"/>
     305        <Caret Line="225" Column="1" TopLine="209"/>
    297306      </Position15>
    298307      <Position16>
    299         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    300         <Caret Line="42" Column="20" TopLine="27"/>
     308        <Filename Value="UCompilator.pas"/>
     309        <Caret Line="233" Column="1" TopLine="217"/>
    301310      </Position16>
    302311      <Position17>
    303         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    304         <Caret Line="43" Column="20" TopLine="27"/>
     312        <Filename Value="UCompilator.pas"/>
     313        <Caret Line="242" Column="34" TopLine="227"/>
    305314      </Position17>
    306315      <Position18>
    307         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    308         <Caret Line="96" Column="20" TopLine="81"/>
     316        <Filename Value="UCompilator.pas"/>
     317        <Caret Line="244" Column="1" TopLine="228"/>
    309318      </Position18>
    310319      <Position19>
    311         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    312         <Caret Line="127" Column="20" TopLine="112"/>
     320        <Filename Value="UCompilator.pas"/>
     321        <Caret Line="270" Column="1" TopLine="246"/>
    313322      </Position19>
    314323      <Position20>
    315         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    316         <Caret Line="158" Column="20" TopLine="143"/>
     324        <Filename Value="UCompilator.pas"/>
     325        <Caret Line="287" Column="34" TopLine="271"/>
    317326      </Position20>
    318327      <Position21>
    319         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    320         <Caret Line="237" Column="20" TopLine="222"/>
     328        <Filename Value="UGrammer.pas"/>
     329        <Caret Line="41" Column="25" TopLine="20"/>
    321330      </Position21>
    322331      <Position22>
    323         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    324         <Caret Line="338" Column="25" TopLine="323"/>
     332        <Filename Value="UGrammer.pas"/>
     333        <Caret Line="17" Column="65" TopLine="2"/>
    325334      </Position22>
    326335      <Position23>
    327         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    328         <Caret Line="342" Column="25" TopLine="323"/>
     336        <Filename Value="UCompilator.pas"/>
     337        <Caret Line="450" Column="1" TopLine="277"/>
    329338      </Position23>
    330339      <Position24>
    331         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    332         <Caret Line="349" Column="10" TopLine="323"/>
     340        <Filename Value="UCompilator.pas"/>
     341        <Caret Line="153" Column="15" TopLine="140"/>
    333342      </Position24>
    334343      <Position25>
    335         <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>
    336         <Caret Line="367" Column="12" TopLine="352"/>
     344        <Filename Value="UCompilator.pas"/>
     345        <Caret Line="204" Column="27" TopLine="189"/>
    337346      </Position25>
    338347      <Position26>
    339         <Filename Value="UGrammer.pas"/>
    340         <Caret Line="8" Column="20" TopLine="1"/>
     348        <Filename Value="UCompilator.pas"/>
     349        <Caret Line="277" Column="14" TopLine="258"/>
    341350      </Position26>
    342351      <Position27>
    343         <Filename Value="UModel.pas"/>
    344         <Caret Line="76" Column="25" TopLine="67"/>
     352        <Filename Value="UCompilator.pas"/>
     353        <Caret Line="169" Column="12" TopLine="153"/>
    345354      </Position27>
    346355      <Position28>
    347         <Filename Value="UModel.pas"/>
    348         <Caret Line="121" Column="23" TopLine="106"/>
     356        <Filename Value="UCompilator.pas"/>
     357        <Caret Line="192" Column="27" TopLine="163"/>
    349358      </Position28>
    350359      <Position29>
    351         <Filename Value="UModel.pas"/>
    352         <Caret Line="147" Column="31" TopLine="132"/>
     360        <Filename Value="UCompilator.pas"/>
     361        <Caret Line="201" Column="19" TopLine="185"/>
    353362      </Position29>
    354363      <Position30>
    355         <Filename Value="UModel.pas"/>
    356         <Caret Line="163" Column="26" TopLine="148"/>
     364        <Filename Value="UCompilator.pas"/>
     365        <Caret Line="209" Column="28" TopLine="194"/>
    357366      </Position30>
    358367    </JumpHistory>
  • branches/Void/project1.lpr

    r18 r19  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UMainForm, UOutputGenerator, LResources, UModel, UVoidParser,
    11   UModelViewer, UZ80Generator, UCGenerator, UPascalGenerator, UGrammer;
     10  Forms, UMainForm, UOutputGenerator, LResources, UModel, UModelViewer,
     11  UZ80Generator, UCGenerator, UPascalGenerator, UGrammer, UTokenizer;
    1212
    1313{$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
Note: See TracChangeset for help on using the changeset viewer.