Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/GPDef.atg @ 10049

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

#2026 created a separate plugin for the classes for the GPDL code generation backend. To remove references to HL code from the core compiler code.

File size: 18.6 KB
Line 
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
24using System.Text;
25using System.Collections.Generic;
26using System.Linq;
27using IEnumerableConstraintNode = System.Collections.Generic.IEnumerable<ConstraintNode>;
28
29COMPILER GPDef
30public GPDefNode AbstractSyntaxTree { get; private set; }
31
32CHARACTERS
33  letter = 'A'..'Z' + 'a'..'z'.
34  digit = '0'..'9'.
35
36TOKENS
37  ident = letter {letter | digit}  .
38
39COMMENTS
40  FROM "/*" TO "*/" NESTED
41
42IGNORE '\t' + '\r' + '\n'
43
44
45PRODUCTIONS
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                                                                                    string src = "";
60                                                                                  .)
61    "PROBLEM" ident                                                               (. gpDef.Name = t.val; .)
62    ["CODE" SourceCode<out src>                                                   (. gpDef.ClassCodeNode = new CodeNode{SrcCode = src}; .)
63    ]
64    ["INIT" SourceCode<out src>                                                   (. gpDef.InitCodeNode = new CodeNode{SrcCode = src}; .)
65    ]
66
67    "NONTERMINALS" { NonterminalDecl<out ntNode>                                  (. if(gpDef.IsSymbolDefined(ntNode)) {
68                                                                                       SemErr("Duplicate non-terminal symbol: " + ntNode.Ident);
69                                                                                     } else {
70                                                                                       gpDef.NonTerminals.Add(ntNode);
71                                                                                     }
72                                                                                  .)
73    }                                                                             (. if(!gpDef.NonTerminals.Any())
74                                                                                       SemErr("No non-terminal symbols defined.");
75                                                                                  .)
76    "TERMINALS" { TerminalDecl<out tNode>                                         (. if(gpDef.IsSymbolDefined(tNode)) {
77                                                                                       SemErr("Duplicate terminal symbol: " + tNode.Ident);
78                                                                                     } else {
79                                                                                       gpDef.Terminals.Add(tNode);
80                                                                                     }
81                                                                                  .)
82    }                                                                             (. if(!gpDef.Terminals.Any())
83                                                                                       SemErr("No terminal symbols defined.");
84                                                                                  .)
85    "RULES" { RuleDef<out ruleNode>                                               (.
86                                                                                     if(!gpDef.IsNonTerminalDefined(ruleNode.NtSymbol)) {
87                                                                                       SemErr("Non-terminal symbol " + ruleNode.NtSymbol + " is not defined in the NONTERMINALS section.");
88                                                                                     } else if(gpDef.IsRuleDefined(ruleNode)) {
89                                                                                       SemErr("Duplicate rule definition for non-terminal symbol " + ruleNode.NtSymbol);
90                                                                                     } else {
91                                                                                       gpDef.Rules.Add(ruleNode);
92                                                                                     }
93                                                                                   .)
94    }                                                                             (.
95                                                                                     var undefinedNT = gpDef.UndefinedNonTerminals();
96                                                                                     if(!string.IsNullOrEmpty(undefinedNT)) {
97                                                                                       SemErr("Rules missing in the RULES section for: " + undefinedNT);
98                                                                                     }
99                                                                                  .)
100                                                                                  (. fitnessFunNode = new FitnessFunctionNode();
101                                                                                     gpDef.FitnessFunctionNode = fitnessFunNode;
102                                                                                  .)
103    ("MAXIMIZE"                                                                   (. fitnessFunNode.Maximization = true; .)
104    | "MINIMIZE"                                                                  (. fitnessFunNode.Maximization = false; .)
105    )
106     SourceCode<out src>                                                          (. fitnessFunNode.SrcCode = src; .)
107                                                                                  (. if(errors.count > 0) throw new FatalError("Syntactic or semantic errors found."); .)
108    "END" ident                                                                   (.
109                                                                                    AbstractSyntaxTree = gpDef;
110                                                                                  .)
111    '.'
112  .
113
114
115  /******************************************************/
116  SemAction<out RuleActionNode action> =                                          (. RuleActionNode myAction = null; action = null; string src = ""; .)
117    "SEM" SourceCode<out src>                                                     (.
118                                                                                    myAction = new RuleActionNode();
119                                                                                    myAction.SrcCode = src;
120                                                                                    action = myAction;
121                                                                                  .)
122  .
123
124  /******************************************************/
125  NonterminalDecl<out NonTerminalNode ntNode> =                                   (. string identStr = ""; ntNode = null; string src = ""; .)
126    ident                                                                         (. identStr = t.val; .)
127    [ SourceCode<out src> ]                                                       (.
128                                                                                    var myNtNode = new NonTerminalNode();
129                                                                                    ntNode = myNtNode;
130                                                                                    myNtNode.Ident = identStr;
131                                                                                    myNtNode.FormalParameters = src;
132                                                                                  .)
133    '.'
134  .
135
136  /******************************************************/
137  TerminalDecl<out TerminalNode tNode> =                                          (.
138                                                                                    string identStr = "";
139                                                                                    tNode = null;
140                                                                                    TerminalNode myTNode = null;
141                                                                                    IEnumerableConstraintNode constraints = new List<ConstraintNode>();
142                                                                                    string src = "";
143                                                                                  .)
144  ident                                                                           (. identStr = t.val; .)
145  [ SourceCode<out src> ]                                                         (.
146                                                                                         myTNode = new TerminalNode();
147                                                                                         tNode = myTNode;
148                                                                                         myTNode.Ident = identStr;
149                                                                                         myTNode.FormalParameters = src;
150                                                                                         myTNode.FieldDefinitions = Util.ExtractFormalParameters(src);
151                                                                                  .)
152  [ "CONSTRAINTS" ConstraintDef<out constraints>
153  ]                                                                               (. myTNode.Constraints = constraints; .)
154  '.'
155  .
156
157
158  /******************************************************/
159  ConstraintDef<out IEnumerableConstraintNode constraints>  =                   (.
160                                                                                    List<ConstraintNode> constraintsList = new List<ConstraintNode>();
161                                                                                    ConstraintNode n = null;
162                                                                                    constraints = null;
163                                                                                  .)
164    { ConstraintRule<out n>                                                       (. constraintsList.Add(n); .)
165    }
166                                                                                  (. constraints = constraintsList; .)
167  .
168
169  /******************************************************/
170  ConstraintRule<out ConstraintNode constraint> =                                 (.
171                                                                                    constraint = null;
172                                                                                  .)
173    ident                                                                         (. constraint = new ConstraintNode(t.val); .)
174    "IN" SetDefinition<constraint>                                               
175  .
176
177  /******************************************************/
178  SetDefinition<ConstraintNode constraint> =                                      (. string src = ""; .)
179    ("SET"                                                                        (. constraint.Type = ConstraintNodeType.Set; .)
180     SourceCode<out src>                                                          (. constraint.SetExpression = src; .)
181     |
182     "RANGE"                                                                      (. constraint.Type = ConstraintNodeType.Range; .)
183     SourceCode<out src>                                                          (. constraint.RangeMinExpression = src; .)
184     ".." SourceCode<out src>                                                     (. constraint.RangeMaxExpression = src; .)
185    )
186  .
187
188  /******************************************************/
189  RuleDef<out RuleNode rule> =                                                    (.
190                                                                                     RuleExprNode expr = null;
191                                                                                     string identStr = null;
192                                                                                     RuleNode myRule = null;
193                                                                                     rule = null;
194                                                                                     string src = "";
195                                                                                  .)
196  ident                                                                           (. identStr = t.val; .)
197  [ SourceCode<out src> ] '='                                                     (.
198                                                                                     myRule = new RuleNode();
199                                                                                     rule = myRule;
200                                                                                     myRule.NtSymbol = identStr;
201                                                                                  .)
202  ["LOCAL" SourceCode<out src>                                                    (.
203                                                                                     myRule.LocalCode = src;
204                                                                                  .)
205  ]
206  SynExpr<out expr> '.'                                                          (.
207                                                                                    myRule.RuleExpr = expr;
208                                                                                 .)
209  .
210
211  /******************************************************/
212  SynExpr<out RuleExprNode expr > =                                               (.
213                                                                                    expr = null;
214                                                                                    AlternativesNode alt = null;
215                                                                                  .)
216  SynTerm<out expr>                                                               (.
217                                                                                    alt = new AlternativesNode();
218                                                                                    alt.Add(expr);
219                                                                                  .)
220  {
221    '|' SynTerm<out expr>                                                         (. alt.Add(expr); expr = alt; .)
222  }
223  .
224
225  /******************************************************/
226  SynTerm<out RuleExprNode expr> =                                               (. SequenceNode seq = null; expr = null; .)
227  SynFact<out expr>                                                              (.
228                                                                                   seq = new SequenceNode();
229                                                                                   seq.Add(expr);
230                                                                                 .)
231  {
232    SynFact<out expr>                                                            (. seq.Add(expr); expr = seq; .)
233  }
234  .
235
236  /******************************************************/
237  SynFact<out RuleExprNode expr> =                                               (.
238                                                                                   string identStr = "";
239                                                                                   RuleActionNode action = null;
240                                                                                   expr = null;
241                                                                                   string src = "";
242                                                                                 .)
243    (ident                                                                       (. identStr = t.val; .)
244     [ SourceCode<out src> ]                                                     (.
245                                                                                    var callNode = new CallSymbolNode{Ident = identStr};
246                                                                                    callNode.ActualParameter = src;
247                                                                                    expr = callNode;
248                                                                                 .)
249// not implemented   | "EPS"
250// not implemented   | '(' SynExpr<ref rules, mBuilder> ')'
251// not implemented   | '[' SynExpr<ref rules, mBuilder> ']'
252// not implemented   | '{' SynExpr<ref rules, mBuilder> '}'
253     | SemAction<out action>                                                     (. expr = action; .)
254    )
255  .
256
257END GPDef.
Note: See TracBrowser for help on using the repository browser.