Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/GPDefSem.cs @ 9518

Last change on this file since 9518 was 9430, checked in by gkronber, 11 years ago

initial import of GPDL parser plugin

File size: 11.4 KB
Line 
1// GPDefSem.cs                                              HDO, 2006-08-28
2// -------------
3// Semantic evaluator for table-driven top-down parsing.
4// Generated by Coco-2 (PGT).
5//=====================================|========================================
6
7using System;
8using System.Text;
9using System.Collections;
10using System.Collections.Generic;
11
12using Lex = GPDefLex;
13using Syn = GPDefSyn;
14
15public class GPDefSem {
16
17
18  public const String MODULENAME = "GPDefSem";
19
20  public static void GPDefSemMethod(Utils.ModuleAction action, out String moduleName) {
21  //-----------------------------------|----------------------------------------
22    moduleName = MODULENAME;
23    switch (action) {
24      case Utils.ModuleAction.getModuleName:
25        return;
26      case Utils.ModuleAction.initModule:
27        break;
28      case Utils.ModuleAction.resetModule:
29        break;
30      case Utils.ModuleAction.cleanupModule:
31        return;
32    } // switch
33  } // GPDefSemMethod
34
35
36  // *** start of global SYN and SEM declarations from ATG ***
37 
38  static StringBuilder srcText;
39  public static HeuristicLab.Optimization.IProblem problem;
40
41  // *** end of global SYN and SEM declarations from ATG ***
42
43 
44  private static void P_source() {
45    Lex.GETsourceAttr();
46    GPDefSem.srcText = new StringBuilder();
47    for (; ; ) {
48     switch (GPDefLex.curCh) {
49       case Utils.EF:
50         Errors.SemError(GPDefLex.curLine, GPDefLex.curCol, "end of file in source text");
51         return;
52       case '<':
53         GPDefLex.NextCh();
54         if (GPDefLex.curCh == '<') {
55           GPDefSem.srcText.Append('<');
56           Errors.Warning(GPDefLex.curLine, GPDefLex.curCol, "non closed source text before?");
57           GPDefLex.NextCh();
58         }
59         GPDefSem.srcText.Append('<');
60         break;
61       case '>':
62         GPDefLex.NextCh();
63         if (GPDefLex.curCh == '>') {
64           GPDefLex.curCh = ' ';          // force GPDefLex to get next character
65           SourceReader.Handle(GPDefSem.srcText.ToString());
66           return;
67         }
68         GPDefSem.srcText.Append('>');
69         break;
70       default:
71         GPDefSem.srcText.Append(GPDefLex.curCh);
72         GPDefLex.NextCh();
73         break;
74     }
75    }
76
77  } // P_source
78
79
80  private static void NT_GPDef() {
81    string identStr = "";
82    RuleNode ruleNode = null;
83    GPDefNode gpDef = new GPDefNode();
84    NonTerminalNode ntNode = null;
85    FitnessFunctionNode fitnessFunNode = null;
86    TerminalNode tNode = null;
87    problem = null;
88
89    for (;;) {
90      switch (Syn.Interpret()) {
91        case 0:
92          return;
93        case 1:
94          Lex.GETidentAttr(out identStr);
95          break;
96        case 2: // SEM
97          gpDef.Name = identStr;
98          break;
99        case 3: // SEM
100          SourceReader.WithNext((src) => gpDef.ClassCodeNode = new CodeNode{SrcCode = src});
101          break;
102        case 4: // SEM
103          SourceReader.WithNext((src) => gpDef.InitCodeNode = new CodeNode{SrcCode = src});
104          break;
105        case 5:
106          NT_NonterminalDecl(out ntNode);
107          break;
108        case 6: // SEM
109          gpDef.NonTerminals.Add(ntNode);
110          break;
111        case 7:
112          NT_TerminalDecl(out tNode);
113          break;
114        case 8: // SEM
115          gpDef.Terminals.Add(tNode);
116          break;
117        case 9:
118          NT_RuleDef(out ruleNode);
119          break;
120        case 10: // SEM
121          gpDef.Rules.Add(ruleNode);
122          break;
123        case 11: // SEM
124          fitnessFunNode = new FitnessFunctionNode();
125          gpDef.FitnessFunctionNode = fitnessFunNode;
126
127          break;
128        case 12: // SEM
129          fitnessFunNode.Maximization = true;
130          break;
131        case 13: // SEM
132          fitnessFunNode.Maximization = false;
133          break;
134        case 14: // SEM
135          SourceReader.WithNext((src) => fitnessFunNode.SrcCode = src);
136          break;
137        case 15:
138          Lex.GETidentAttr(out identStr);
139          break;
140        case 16: // SEM
141          SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
142          var gen = new ProblemGenerator();
143          problem = gen.GenerateFromAst(gpDef);
144
145          break;
146      } // switch
147    } // for
148  } // NT_GPDef
149
150  private static void NT_SemAction(out RuleActionNode action) {
151    RuleActionNode myAction = null; action = null;
152    for (;;) {
153      switch (Syn.Interpret()) {
154        case 0:
155          return;
156        case 1: // SEM
157          myAction = new RuleActionNode();
158          SourceReader.WithNext((src) => {myAction.SrcCode = src;});
159          action = myAction;
160
161          break;
162      } // switch
163    } // for
164  } // NT_SemAction
165
166  private static void NT_NonterminalDecl(out NonTerminalNode ntNode) {
167    string identStr = ""; ntNode = null;
168    for (;;) {
169      switch (Syn.Interpret()) {
170        case 0:
171          return;
172        case 1:
173          Lex.GETidentAttr(out identStr);
174          break;
175        case 2: // SEM
176          var myNtNode = new NonTerminalNode();
177          ntNode = myNtNode;
178          myNtNode.Ident = identStr;
179          SourceReader.WithNext((src) => {myNtNode.FormalParameters = src;});
180
181          break;
182      } // switch
183    } // for
184  } // NT_NonterminalDecl
185
186  private static void NT_TerminalDecl(out TerminalNode tNode) {
187    string identStr = "";
188    tNode = null;
189    TerminalNode myTNode = null;
190    IEnumerable<ConstraintNode> constraints = null;
191
192    for (;;) {
193      switch (Syn.Interpret()) {
194        case 0:
195          return;
196        case 1:
197          Lex.GETidentAttr(out identStr);
198          break;
199        case 2: // SEM
200          myTNode = new TerminalNode();
201          tNode = myTNode;
202          myTNode.Ident = identStr;
203          SourceReader.WithNext((src) => {
204            myTNode.FormalParameters = src;
205            myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);
206          });
207         
208
209          break;
210        case 3:
211          NT_ConstraintDef(out constraints);
212          break;
213        case 4: // SEM
214          myTNode.Constraints = constraints;
215          break;
216      } // switch
217    } // for
218  } // NT_TerminalDecl
219
220  private static void NT_ConstraintDef(out IEnumerable<ConstraintNode> constraints) {
221    List<ConstraintNode> constraintsList = new List<ConstraintNode>();
222    ConstraintNode n = null;
223    constraints = null;
224
225    for (;;) {
226      switch (Syn.Interpret()) {
227        case 0:
228          return;
229        case 1:
230          NT_ConstraintRule(out n);
231          break;
232        case 2: // SEM
233          constraintsList.Add(n);
234          break;
235        case 3: // SEM
236          constraints = constraintsList;
237          break;
238      } // switch
239    } // for
240  } // NT_ConstraintDef
241
242  private static void NT_ConstraintRule(out ConstraintNode constraint) {
243    string identStr = null;
244    constraint = null;
245
246    for (;;) {
247      switch (Syn.Interpret()) {
248        case 0:
249          return;
250        case 1:
251          Lex.GETidentAttr(out identStr);
252          break;
253        case 2: // SEM
254          constraint = new ConstraintNode(identStr);
255          break;
256        case 3:
257          NT_SetDefinition(constraint);
258          break;
259        case 4: // SEM
260          SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
261          break;
262      } // switch
263    } // for
264  } // NT_ConstraintRule
265
266  private static void NT_SetDefinition(ConstraintNode constraint) {
267    for (;;) {
268      switch (Syn.Interpret()) {
269        case 0:
270          return;
271        case 1: // SEM
272          constraint.Type = ConstraintNodeType.Set;
273          break;
274        case 2: // SEM
275          SourceReader.WithNext((src) => {constraint.SetExpression = src;});
276          break;
277        case 3: // SEM
278          constraint.Type = ConstraintNodeType.Range;
279          break;
280        case 4: // SEM
281          SourceReader.WithNext((src) => {constraint.RangeMinExpression = src;});
282          break;
283        case 5: // SEM
284          SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
285          SourceReader.WithNext((src) => {constraint.RangeMaxExpression = src;});
286          break;
287      } // switch
288    } // for
289  } // NT_SetDefinition
290
291  private static void NT_RuleDef(out RuleNode rule) {
292    RuleExprNode expr = null;
293    string identStr = null;
294    RuleNode myRule = null;
295    rule = null;
296
297    for (;;) {
298      switch (Syn.Interpret()) {
299        case 0:
300          return;
301        case 1:
302          Lex.GETidentAttr(out identStr);
303          break;
304        case 2: // SEM
305          myRule = new RuleNode();
306          rule = myRule;
307          myRule.NtSymbol = identStr;
308
309          break;
310        case 3: // SEM
311          SourceReader.WithNext((src) => {myRule.LocalCode = src;});
312
313          break;
314        case 4:
315          NT_SynExpr(out expr);
316          break;
317        case 5: // SEM
318          myRule.RuleExpr = expr;
319
320          break;
321      } // switch
322    } // for
323  } // NT_RuleDef
324
325  private static void NT_SynExpr(out RuleExprNode expr) {
326    expr = null;
327    AlternativesNode alt = null;
328
329    for (;;) {
330      switch (Syn.Interpret()) {
331        case 0:
332          return;
333        case 1:
334          NT_SynTerm(out expr);
335          break;
336        case 2: // SEM
337          alt = new AlternativesNode();
338          alt.Add(expr);
339
340          break;
341        case 3:
342          NT_SynTerm(out expr);
343          break;
344        case 4: // SEM
345          alt.Add(expr); expr = alt;
346          break;
347      } // switch
348    } // for
349  } // NT_SynExpr
350
351  private static void NT_SynTerm(out RuleExprNode expr) {
352    SequenceNode seq = null; expr = null;
353    for (;;) {
354      switch (Syn.Interpret()) {
355        case 0:
356          return;
357        case 1:
358          NT_SynFact(out expr);
359          break;
360        case 2: // SEM
361          seq = new SequenceNode();
362          seq.Add(expr);
363
364          break;
365        case 3:
366          NT_SynFact(out expr);
367          break;
368        case 4: // SEM
369          seq.Add(expr); expr = seq;
370          break;
371      } // switch
372    } // for
373  } // NT_SynTerm
374
375  private static void NT_SynFact(out RuleExprNode expr) {
376    string identStr = "";
377    RuleActionNode action = null;
378    expr = null;
379
380    for (;;) {
381      switch (Syn.Interpret()) {
382        case 0:
383          return;
384        case 1:
385          Lex.GETidentAttr(out identStr);
386          break;
387        case 2: // SEM
388          var callNode = new CallSymbolNode{Ident = identStr};
389          SourceReader.WithNext((src) => {callNode.ActualParameter = src;});
390          expr = callNode;
391
392          break;
393        case 3:
394          NT_SemAction(out action);
395          break;
396        case 4: // SEM
397          expr = action;
398          break;
399      } // switch
400    } // for
401  } // NT_SynFact
402
403
404  public delegate void PragmaMethod();
405  public static PragmaMethod[] pragmaMethods = {
406      new PragmaMethod(P_source)
407    };
408
409
410  public static void StartSem() {
411  //-----------------------------------|----------------------------------------
412    for (;;) {
413      switch (Syn.Interpret()) {
414        case 0:
415          return;
416        case 1:
417          NT_GPDef();
418          break;
419      } // switch
420    } // for
421  } // StartSem
422
423   
424} // GPDefSem
425
426// End of GPDefSem.cs
427//=====================================|========================================
Note: See TracBrowser for help on using the repository browser.