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 |
|
---|
22 | $namespace=HeuristicLab.Problems.GPDL
|
---|
23 |
|
---|
24 | using System.Text;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Linq;
|
---|
27 | using IEnumerableConstraintNode = System.Collections.Generic.IEnumerable<ConstraintNode>;
|
---|
28 |
|
---|
29 | COMPILER GPDef
|
---|
30 | public HeuristicLab.Optimization.ISingleObjectiveHeuristicOptimizationProblem problem;
|
---|
31 |
|
---|
32 | CHARACTERS
|
---|
33 | letter = 'A'..'Z' + 'a'..'z'.
|
---|
34 | digit = '0'..'9'.
|
---|
35 |
|
---|
36 | TOKENS
|
---|
37 | ident = letter {letter | digit} .
|
---|
38 |
|
---|
39 | COMMENTS
|
---|
40 | FROM "/*" TO "*/" NESTED
|
---|
41 |
|
---|
42 | IGNORE '\t' + '\r' + '\n'
|
---|
43 |
|
---|
44 |
|
---|
45 | PRODUCTIONS
|
---|
46 |
|
---|
47 | SourceCode<out string src> = (. src = ""; .)
|
---|
48 | "<<" (. int beg = la.pos; .)
|
---|
49 | {ANY} (. int end = la.pos; .)
|
---|
50 | ">>" (. if(end>beg) src = scanner.buffer.GetString(beg, end); .)
|
---|
51 | .
|
---|
52 |
|
---|
53 | GPDef = (.
|
---|
54 | RuleNode ruleNode = null;
|
---|
55 | GPDefNode gpDef = new GPDefNode();
|
---|
56 | NonTerminalNode ntNode = null;
|
---|
57 | FitnessFunctionNode fitnessFunNode = null;
|
---|
58 | TerminalNode tNode = null;
|
---|
59 | problem = null;
|
---|
60 | string src = "";
|
---|
61 | .)
|
---|
62 | "PROBLEM" ident (. gpDef.Name = t.val; .)
|
---|
63 | ["CODE" SourceCode<out src> (. gpDef.ClassCodeNode = new CodeNode{SrcCode = src}; .)
|
---|
64 | ]
|
---|
65 | ["INIT" SourceCode<out src> (. gpDef.InitCodeNode = new CodeNode{SrcCode = src}; .)
|
---|
66 | ]
|
---|
67 |
|
---|
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 | .)
|
---|
101 | (. fitnessFunNode = new FitnessFunctionNode();
|
---|
102 | gpDef.FitnessFunctionNode = fitnessFunNode;
|
---|
103 | .)
|
---|
104 | ("MAXIMIZE" (. fitnessFunNode.Maximization = true; .)
|
---|
105 | | "MINIMIZE" (. fitnessFunNode.Maximization = false; .)
|
---|
106 | )
|
---|
107 | SourceCode<out src> (. fitnessFunNode.SrcCode = src; .)
|
---|
108 | (. if(errors.count > 0) throw new FatalError("Syntactic or semantic errors found."); .)
|
---|
109 | "END" ident (.
|
---|
110 | var gen = new ProblemGenerator();
|
---|
111 | problem = gen.GenerateFromAst(gpDef);
|
---|
112 | .)
|
---|
113 | '.'
|
---|
114 | .
|
---|
115 |
|
---|
116 |
|
---|
117 | /******************************************************/
|
---|
118 | SemAction<out RuleActionNode action> = (. RuleActionNode myAction = null; action = null; string src = ""; .)
|
---|
119 | "SEM" SourceCode<out src> (.
|
---|
120 | myAction = new RuleActionNode();
|
---|
121 | myAction.SrcCode = src;
|
---|
122 | action = myAction;
|
---|
123 | .)
|
---|
124 | .
|
---|
125 |
|
---|
126 | /******************************************************/
|
---|
127 | NonterminalDecl<out NonTerminalNode ntNode> = (. string identStr = ""; ntNode = null; string src = ""; .)
|
---|
128 | ident (. identStr = t.val; .)
|
---|
129 | [ SourceCode<out src> ] (.
|
---|
130 | var myNtNode = new NonTerminalNode();
|
---|
131 | ntNode = myNtNode;
|
---|
132 | myNtNode.Ident = identStr;
|
---|
133 | myNtNode.FormalParameters = src;
|
---|
134 | .)
|
---|
135 | '.'
|
---|
136 | .
|
---|
137 |
|
---|
138 | /******************************************************/
|
---|
139 | TerminalDecl<out TerminalNode tNode> = (.
|
---|
140 | string identStr = "";
|
---|
141 | tNode = null;
|
---|
142 | TerminalNode myTNode = null;
|
---|
143 | IEnumerableConstraintNode constraints = new List<ConstraintNode>();
|
---|
144 | string src = "";
|
---|
145 | .)
|
---|
146 | ident (. identStr = t.val; .)
|
---|
147 | [ SourceCode<out src> ] (.
|
---|
148 | myTNode = new TerminalNode();
|
---|
149 | tNode = myTNode;
|
---|
150 | myTNode.Ident = identStr;
|
---|
151 | myTNode.FormalParameters = src;
|
---|
152 | myTNode.FieldDefinitions = Util.ExtractFormalParameters(src);
|
---|
153 | .)
|
---|
154 | [ "CONSTRAINTS" ConstraintDef<out constraints>
|
---|
155 | ] (. myTNode.Constraints = constraints; .)
|
---|
156 | '.'
|
---|
157 | .
|
---|
158 |
|
---|
159 |
|
---|
160 | /******************************************************/
|
---|
161 | ConstraintDef<out IEnumerableConstraintNode constraints> = (.
|
---|
162 | List<ConstraintNode> constraintsList = new List<ConstraintNode>();
|
---|
163 | ConstraintNode n = null;
|
---|
164 | constraints = null;
|
---|
165 | .)
|
---|
166 | { ConstraintRule<out n> (. constraintsList.Add(n); .)
|
---|
167 | }
|
---|
168 | (. constraints = constraintsList; .)
|
---|
169 | .
|
---|
170 |
|
---|
171 | /******************************************************/
|
---|
172 | ConstraintRule<out ConstraintNode constraint> = (.
|
---|
173 | constraint = null;
|
---|
174 | .)
|
---|
175 | ident (. constraint = new ConstraintNode(t.val); .)
|
---|
176 | "IN" SetDefinition<constraint>
|
---|
177 | .
|
---|
178 |
|
---|
179 | /******************************************************/
|
---|
180 | SetDefinition<ConstraintNode constraint> = (. string src = ""; .)
|
---|
181 | ("SET" (. constraint.Type = ConstraintNodeType.Set; .)
|
---|
182 | SourceCode<out src> (. constraint.SetExpression = src; .)
|
---|
183 | |
|
---|
184 | "RANGE" (. constraint.Type = ConstraintNodeType.Range; .)
|
---|
185 | SourceCode<out src> (. constraint.RangeMinExpression = src; .)
|
---|
186 | ".." SourceCode<out src> (. constraint.RangeMaxExpression = src; .)
|
---|
187 | )
|
---|
188 | .
|
---|
189 |
|
---|
190 | /******************************************************/
|
---|
191 | RuleDef<out RuleNode rule> = (.
|
---|
192 | RuleExprNode expr = null;
|
---|
193 | string identStr = null;
|
---|
194 | RuleNode myRule = null;
|
---|
195 | rule = null;
|
---|
196 | string src = "";
|
---|
197 | .)
|
---|
198 | ident (. identStr = t.val; .)
|
---|
199 | [ SourceCode<out src> ] '=' (.
|
---|
200 | myRule = new RuleNode();
|
---|
201 | rule = myRule;
|
---|
202 | myRule.NtSymbol = identStr;
|
---|
203 | .)
|
---|
204 | ["LOCAL" SourceCode<out src> (.
|
---|
205 | myRule.LocalCode = src;
|
---|
206 | .)
|
---|
207 | ]
|
---|
208 | SynExpr<out expr> '.' (.
|
---|
209 | myRule.RuleExpr = expr;
|
---|
210 | .)
|
---|
211 | .
|
---|
212 |
|
---|
213 | /******************************************************/
|
---|
214 | SynExpr<out RuleExprNode expr > = (.
|
---|
215 | expr = null;
|
---|
216 | AlternativesNode alt = null;
|
---|
217 | .)
|
---|
218 | SynTerm<out expr> (.
|
---|
219 | alt = new AlternativesNode();
|
---|
220 | alt.Add(expr);
|
---|
221 | .)
|
---|
222 | {
|
---|
223 | '|' SynTerm<out expr> (. alt.Add(expr); expr = alt; .)
|
---|
224 | }
|
---|
225 | .
|
---|
226 |
|
---|
227 | /******************************************************/
|
---|
228 | SynTerm<out RuleExprNode expr> = (. SequenceNode seq = null; expr = null; .)
|
---|
229 | SynFact<out expr> (.
|
---|
230 | seq = new SequenceNode();
|
---|
231 | seq.Add(expr);
|
---|
232 | .)
|
---|
233 | {
|
---|
234 | SynFact<out expr> (. seq.Add(expr); expr = seq; .)
|
---|
235 | }
|
---|
236 | .
|
---|
237 |
|
---|
238 | /******************************************************/
|
---|
239 | SynFact<out RuleExprNode expr> = (.
|
---|
240 | string identStr = "";
|
---|
241 | RuleActionNode action = null;
|
---|
242 | expr = null;
|
---|
243 | string src = "";
|
---|
244 | .)
|
---|
245 | (ident (. identStr = t.val; .)
|
---|
246 | [ SourceCode<out src> ] (.
|
---|
247 | var callNode = new CallSymbolNode{Ident = identStr};
|
---|
248 | callNode.ActualParameter = src;
|
---|
249 | expr = callNode;
|
---|
250 | .)
|
---|
251 | // not implemented | "EPS"
|
---|
252 | // not implemented | '(' SynExpr<ref rules, mBuilder> ')'
|
---|
253 | // not implemented | '[' SynExpr<ref rules, mBuilder> ']'
|
---|
254 | // not implemented | '{' SynExpr<ref rules, mBuilder> '}'
|
---|
255 | | SemAction<out action> (. expr = action; .)
|
---|
256 | )
|
---|
257 | .
|
---|
258 |
|
---|
259 | END GPDef.
|
---|