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