Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/02/13 18:11:26 (11 years ago)
Author:
gkronber
Message:

#2026 added rudimentary error checking to GPDL parser ATG. Added GPL license headers to all files. Created a first set of unit tests.

Location:
branches/HeuristicLab.Problems.GPDL
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL

    • Property svn:ignore
      •  

        old new  
        11_ReSharper.HeuristicLab.Problems.GPDL
        22*.suo
         3bin
         4*.user
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/GPDef.atg

    r9727 r9842  
     1/* HeuristicLab
     2 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 *
     4 * This file is part of HeuristicLab.
     5 *
     6 * HeuristicLab is free software: you can redistribute it and/or modify
     7 * it under the terms of the GNU General Public License as published by
     8 * the Free Software Foundation, either version 3 of the License, or
     9 * (at your option) any later version.
     10 *
     11 * HeuristicLab is distributed in the hope that it will be useful,
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 * GNU General Public License for more details.
     15 *
     16 * You should have received a copy of the GNU General Public License
     17 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     18 */
     19
     20// Coco/R grammar for the GPDL compiler
     21
    122$namespace=HeuristicLab.Problems.GPDL
    223
    324using System.Text;
    425using System.Collections.Generic;
     26using System.Linq;
    527using IEnumerableConstraintNode = System.Collections.Generic.IEnumerable<ConstraintNode>;
    628
     
    4466    ]
    4567
    46     "NONTERMINALS" { NonterminalDecl<out ntNode>                                  (. gpDef.NonTerminals.Add(ntNode); .)
    47     }
    48     "TERMINALS" { TerminalDecl<out tNode>                                         (. gpDef.Terminals.Add(tNode); .)
    49     }
    50     "RULES" { RuleDef<out ruleNode>                                               (. gpDef.Rules.Add(ruleNode); .)
    51     }
     68    "NONTERMINALS" { NonterminalDecl<out ntNode>                                  (. if(gpDef.IsSymbolDefined(ntNode)) {
     69                                                                                       SemErr("Duplicate non-terminal symbol: " + ntNode.Ident);
     70                                                                                     } else {
     71                                                                                       gpDef.NonTerminals.Add(ntNode);
     72                                                                                     }
     73                                                                                  .)
     74    }                                                                             (. if(!gpDef.NonTerminals.Any())
     75                                                                                       SemErr("No non-terminal symbols defined.");
     76                                                                                  .)
     77    "TERMINALS" { TerminalDecl<out tNode>                                         (. if(gpDef.IsSymbolDefined(tNode)) {
     78                                                                                       SemErr("Duplicate terminal symbol: " + tNode.Ident);
     79                                                                                     } else {
     80                                                                                       gpDef.Terminals.Add(tNode);
     81                                                                                     }
     82                                                                                  .)
     83    }                                                                             (. if(!gpDef.Terminals.Any())
     84                                                                                       SemErr("No terminal symbols defined.");
     85                                                                                  .)
     86    "RULES" { RuleDef<out ruleNode>                                               (.
     87                                                                                     if(!gpDef.IsNonTerminalDefined(ruleNode.NtSymbol)) {
     88                                                                                       SemErr("Non-terminal symbol " + ruleNode.NtSymbol + " is not defined in the NONTERMINALS section.");
     89                                                                                     } else if(gpDef.IsRuleDefined(ruleNode)) {
     90                                                                                       SemErr("Duplicate rule definition for non-terminal symbol " + ruleNode.NtSymbol);
     91                                                                                     } else {
     92                                                                                       gpDef.Rules.Add(ruleNode);
     93                                                                                     }
     94                                                                                   .)
     95    }                                                                             (.
     96                                                                                     var undefinedNT = gpDef.UndefinedNonTerminals();
     97                                                                                     if(!string.IsNullOrEmpty(undefinedNT)) {
     98                                                                                       SemErr("Rules missing in the RULES section for: " + undefinedNT);
     99                                                                                     }
     100                                                                                  .)
    52101                                                                                  (. fitnessFunNode = new FitnessFunctionNode();
    53                                                                                          gpDef.FitnessFunctionNode = fitnessFunNode;
     102                                                                                     gpDef.FitnessFunctionNode = fitnessFunNode;
    54103                                                                                  .)
    55104    ("MAXIMIZE"                                                                   (. fitnessFunNode.Maximization = true; .)
     
    57106    )
    58107     SourceCode<out src>                                                          (. fitnessFunNode.SrcCode = src; .)
     108                                                                                  (. if(errors.count > 0) throw new FatalError("Syntactic or semantic errors found."); .)
    59109    "END" ident                                                                   (.
    60                                                                                     // SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
    61110                                                                                    var gen = new ProblemGenerator();
    62111                                                                                    problem = gen.GenerateFromAst(gpDef);
     
    125174                                                                                  .)
    126175    ident                                                                         (. constraint = new ConstraintNode(t.val); .)
    127     "IN" SetDefinition<constraint>                                                // (. SourceReader.Handle(GPDefSem.srcText.ToString()); .) // flush the source reader
     176    "IN" SetDefinition<constraint>                                               
    128177  .
    129178
     
    135184     "RANGE"                                                                      (. constraint.Type = ConstraintNodeType.Range; .)
    136185     SourceCode<out src>                                                          (. constraint.RangeMinExpression = src; .)
    137      ".." SourceCode<out src>                                                     (. // SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
    138                                                                                     constraint.RangeMaxExpression = src; .)
     186     ".." SourceCode<out src>                                                     (. constraint.RangeMaxExpression = src; .)
    139187    )
    140188  .
     
    142190  /******************************************************/
    143191  RuleDef<out RuleNode rule> =                                                    (.
    144                                                                                     RuleExprNode expr = null;
    145                                                                                     string identStr = null;
    146                                                                                     RuleNode myRule = null;
    147                                                                                     rule = null;
    148                                                                                    string src = "";
     192                                                                                     RuleExprNode expr = null;
     193                                                                                     string identStr = null;
     194                                                                                     RuleNode myRule = null;
     195                                                                                     rule = null;
     196                                                                                     string src = "";
    149197                                                                                  .)
    150198  ident                                                                           (. identStr = t.val; .)
    151199  [ SourceCode<out src> ] '='                                                     (.
    152                                                                                       myRule = new RuleNode();
    153                                                                                       rule = myRule;
    154                                                                                       myRule.NtSymbol = identStr;
     200                                                                                     myRule = new RuleNode();
     201                                                                                     rule = myRule;
     202                                                                                     myRule.NtSymbol = identStr;
    155203                                                                                  .)
    156204  ["LOCAL" SourceCode<out src>                                                    (.
    157                                                                                     myRule.LocalCode = src;
     205                                                                                     myRule.LocalCode = src;
    158206                                                                                  .)
    159207  ]
     
    205253// not implemented   | '[' SynExpr<ref rules, mBuilder> ']'
    206254// not implemented   | '{' SynExpr<ref rules, mBuilder> '}'
    207      | SemAction<out action>                                                      (. expr = action; .)
     255     | SemAction<out action>                                                     (. expr = action; .)
    208256    )
    209257  .
Note: See TracChangeset for help on using the changeset viewer.