Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/Parser.cs @ 9727

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

#2026 attributes for NT- and T-symbols are optional (incorrectly translated from Coco-2 yesterday)

File size: 9.3 KB
Line 
1using System.Text;
2using System.Collections.Generic;
3using IEnumerableConstraintNode = System.Collections.Generic.IEnumerable<ConstraintNode>;
4
5
6
7using System;
8
9namespace HeuristicLab.Problems.GPDL {
10
11
12
13public class Parser {
14  public const int _EOF = 0;
15  public const int _ident = 1;
16  public const int maxT = 23;
17
18  const bool T = true;
19  const bool x = false;
20  const int minErrDist = 2;
21 
22  public Scanner scanner;
23  public Errors  errors;
24
25  public Token t;    // last recognized token
26  public Token la;   // lookahead token
27  int errDist = minErrDist;
28
29public static HeuristicLab.Optimization.IProblem problem;
30 
31
32
33  public Parser(Scanner scanner) {
34    this.scanner = scanner;
35    errors = new Errors();
36  }
37
38  void SynErr (int n) {
39    if (errDist >= minErrDist) errors.SynErr(la.line, la.col, n);
40    errDist = 0;
41  }
42
43  public void SemErr (string msg) {
44    if (errDist >= minErrDist) errors.SemErr(t.line, t.col, msg);
45    errDist = 0;
46  }
47 
48  void Get () {
49    for (;;) {
50      t = la;
51      la = scanner.Scan();
52      if (la.kind <= maxT) { ++errDist; break; }
53
54      la = t;
55    }
56  }
57 
58  void Expect (int n) {
59    if (la.kind==n) Get(); else { SynErr(n); }
60  }
61 
62  bool StartOf (int s) {
63    return set[s, la.kind];
64  }
65 
66  void ExpectWeak (int n, int follow) {
67    if (la.kind == n) Get();
68    else {
69      SynErr(n);
70      while (!StartOf(follow)) Get();
71    }
72  }
73
74
75  bool WeakSeparator(int n, int syFol, int repFol) {
76    int kind = la.kind;
77    if (kind == n) {Get(); return true;}
78    else if (StartOf(repFol)) {return false;}
79    else {
80      SynErr(n);
81      while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) {
82        Get();
83        kind = la.kind;
84      }
85      return StartOf(syFol);
86    }
87  }
88
89 
90  void SourceCode(out string src) {
91    src = "";
92    Expect(2);
93    int beg = la.pos;
94    while (StartOf(1)) {
95      Get();
96    }
97    int end = la.pos;
98    Expect(3);
99    if(end>beg) src = scanner.buffer.GetString(beg, end);
100  }
101
102  void GPDef() {
103    RuleNode ruleNode = null;
104    GPDefNode gpDef = new GPDefNode();
105    NonTerminalNode ntNode = null;
106    FitnessFunctionNode fitnessFunNode = null;
107    TerminalNode tNode = null;
108    problem = null;
109    string src = "";
110   
111    Expect(4);
112    Expect(1);
113    gpDef.Name = t.val;
114    if (la.kind == 5) {
115      Get();
116      SourceCode(out src);
117      gpDef.ClassCodeNode = new CodeNode{SrcCode = src};
118    }
119    if (la.kind == 6) {
120      Get();
121      SourceCode(out src);
122      gpDef.InitCodeNode = new CodeNode{SrcCode = src};
123    }
124    Expect(7);
125    while (la.kind == 1) {
126      NonterminalDecl(out ntNode);
127      gpDef.NonTerminals.Add(ntNode);
128    }
129    Expect(8);
130    while (la.kind == 1) {
131      TerminalDecl(out tNode);
132      gpDef.Terminals.Add(tNode);
133    }
134    Expect(9);
135    while (la.kind == 1) {
136      RuleDef(out ruleNode);
137      gpDef.Rules.Add(ruleNode);
138    }
139    fitnessFunNode = new FitnessFunctionNode();
140       gpDef.FitnessFunctionNode = fitnessFunNode;
141   
142    if (la.kind == 10) {
143      Get();
144      fitnessFunNode.Maximization = true;
145    } else if (la.kind == 11) {
146      Get();
147      fitnessFunNode.Maximization = false;
148    } else SynErr(24);
149    SourceCode(out src);
150    fitnessFunNode.SrcCode = src;
151    Expect(12);
152    Expect(1);
153    var gen = new ProblemGenerator();
154    problem = gen.GenerateFromAst(gpDef);
155   
156    Expect(13);
157  }
158
159  void NonterminalDecl(out NonTerminalNode ntNode) {
160    string identStr = ""; ntNode = null; string src = "";
161    Expect(1);
162    identStr = t.val;
163    if (la.kind == 2) {
164      SourceCode(out src);
165    }
166    var myNtNode = new NonTerminalNode();
167    ntNode = myNtNode;
168    myNtNode.Ident = identStr;
169    myNtNode.FormalParameters = src;
170   
171    Expect(13);
172  }
173
174  void TerminalDecl(out TerminalNode tNode) {
175    string identStr = "";
176    tNode = null;
177    TerminalNode myTNode = null;
178    IEnumerableConstraintNode constraints = null;
179    string src = "";
180   
181    Expect(1);
182    identStr = t.val;
183    if (la.kind == 2) {
184      SourceCode(out src);
185    }
186    myTNode = new TerminalNode();
187    tNode = myTNode;
188    myTNode.Ident = identStr;
189    myTNode.FormalParameters = src;
190    myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);
191   
192    if (la.kind == 15) {
193      Get();
194      ConstraintDef(out constraints);
195      myTNode.Constraints = constraints;
196    }
197    Expect(13);
198  }
199
200  void RuleDef(out RuleNode rule) {
201    RuleExprNode expr = null;
202    string identStr = null;
203    RuleNode myRule = null;
204    rule = null;
205    string src = "";
206   
207    Expect(1);
208    identStr = t.val;
209    if (la.kind == 2) {
210      SourceCode(out src);
211    }
212    Expect(20);
213    myRule = new RuleNode();
214    rule = myRule;
215    myRule.NtSymbol = identStr;
216   
217    if (la.kind == 21) {
218      Get();
219      SourceCode(out src);
220      myRule.LocalCode = src;
221     
222    }
223    SynExpr(out expr);
224    Expect(13);
225    myRule.RuleExpr = expr;
226   
227  }
228
229  void SemAction(out RuleActionNode action) {
230    RuleActionNode myAction = null; action = null; string src = "";
231    Expect(14);
232    SourceCode(out src);
233    myAction = new RuleActionNode();
234    myAction.SrcCode = src;
235    action = myAction;
236   
237  }
238
239  void ConstraintDef(out IEnumerableConstraintNode constraints) {
240    List<ConstraintNode> constraintsList = new List<ConstraintNode>();
241    ConstraintNode n = null;
242    constraints = null;
243   
244    while (la.kind == 1) {
245      ConstraintRule(out n);
246      constraintsList.Add(n);
247    }
248    constraints = constraintsList;
249  }
250
251  void ConstraintRule(out ConstraintNode constraint) {
252    constraint = null;
253   
254    Expect(1);
255    constraint = new ConstraintNode(t.val);
256    Expect(16);
257    SetDefinition(constraint);
258  }
259
260  void SetDefinition(ConstraintNode constraint) {
261    string src = "";
262    if (la.kind == 17) {
263      Get();
264      constraint.Type = ConstraintNodeType.Set;
265      SourceCode(out src);
266      constraint.SetExpression = src;
267    } else if (la.kind == 18) {
268      Get();
269      constraint.Type = ConstraintNodeType.Range;
270      SourceCode(out src);
271      constraint.RangeMinExpression = src;
272      Expect(19);
273      SourceCode(out src);
274      constraint.RangeMaxExpression = src;
275    } else SynErr(25);
276  }
277
278  void SynExpr(out RuleExprNode expr ) {
279    expr = null;
280    AlternativesNode alt = null;
281   
282    SynTerm(out expr);
283    alt = new AlternativesNode();
284    alt.Add(expr);
285   
286    while (la.kind == 22) {
287      Get();
288      SynTerm(out expr);
289      alt.Add(expr); expr = alt;
290    }
291  }
292
293  void SynTerm(out RuleExprNode expr) {
294    SequenceNode seq = null; expr = null;
295    SynFact(out expr);
296    seq = new SequenceNode();
297    seq.Add(expr);
298   
299    while (la.kind == 1 || la.kind == 14) {
300      SynFact(out expr);
301      seq.Add(expr); expr = seq;
302    }
303  }
304
305  void SynFact(out RuleExprNode expr) {
306    string identStr = "";
307    RuleActionNode action = null;
308    expr = null;
309    string src = "";
310   
311    if (la.kind == 1) {
312      Get();
313      identStr = t.val;
314      if (la.kind == 2) {
315        SourceCode(out src);
316      }
317      var callNode = new CallSymbolNode{Ident = identStr};
318      callNode.ActualParameter = src;
319      expr = callNode;
320     
321    } else if (la.kind == 14) {
322      SemAction(out action);
323      expr = action;
324    } else SynErr(26);
325  }
326
327
328
329  public void Parse() {
330    la = new Token();
331    la.val = "";   
332    Get();
333    GPDef();
334    Expect(0);
335
336  }
337 
338  static readonly bool[,] set = {
339    {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x},
340    {x,T,T,x, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, x}
341
342  };
343} // end Parser
344
345
346public class Errors {
347  public int count = 0;                                    // number of errors detected
348  public System.IO.TextWriter errorStream = Console.Out;   // error messages go to this stream
349  public string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
350
351  public virtual void SynErr (int line, int col, int n) {
352    string s;
353    switch (n) {
354      case 0: s = "EOF expected"; break;
355      case 1: s = "ident expected"; break;
356      case 2: s = "\"<<\" expected"; break;
357      case 3: s = "\">>\" expected"; break;
358      case 4: s = "\"PROBLEM\" expected"; break;
359      case 5: s = "\"CODE\" expected"; break;
360      case 6: s = "\"INIT\" expected"; break;
361      case 7: s = "\"NONTERMINALS\" expected"; break;
362      case 8: s = "\"TERMINALS\" expected"; break;
363      case 9: s = "\"RULES\" expected"; break;
364      case 10: s = "\"MAXIMIZE\" expected"; break;
365      case 11: s = "\"MINIMIZE\" expected"; break;
366      case 12: s = "\"END\" expected"; break;
367      case 13: s = "\".\" expected"; break;
368      case 14: s = "\"SEM\" expected"; break;
369      case 15: s = "\"CONSTRAINTS\" expected"; break;
370      case 16: s = "\"IN\" expected"; break;
371      case 17: s = "\"SET\" expected"; break;
372      case 18: s = "\"RANGE\" expected"; break;
373      case 19: s = "\"..\" expected"; break;
374      case 20: s = "\"=\" expected"; break;
375      case 21: s = "\"LOCAL\" expected"; break;
376      case 22: s = "\"|\" expected"; break;
377      case 23: s = "??? expected"; break;
378      case 24: s = "invalid GPDef"; break;
379      case 25: s = "invalid SetDefinition"; break;
380      case 26: s = "invalid SynFact"; break;
381
382      default: s = "error " + n; break;
383    }
384    errorStream.WriteLine(errMsgFormat, line, col, s);
385    count++;
386  }
387
388  public virtual void SemErr (int line, int col, string s) {
389    errorStream.WriteLine(errMsgFormat, line, col, s);
390    count++;
391  }
392 
393  public virtual void SemErr (string s) {
394    errorStream.WriteLine(s);
395    count++;
396  }
397 
398  public virtual void Warning (int line, int col, string s) {
399    errorStream.WriteLine(errMsgFormat, line, col, s);
400  }
401 
402  public virtual void Warning(string s) {
403    errorStream.WriteLine(s);
404  }
405} // Errors
406
407
408public class FatalError: Exception {
409  public FatalError(string m): base(m) {}
410}
411}
Note: See TracBrowser for help on using the repository browser.