Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2026 changed ATG to Coco/R syntax and use Coco/R (C#) to generate scanner and parser for GPDL

File size: 15.1 KB
Line 
1$namespace=HeuristicLab.Problems.GPDL
2
3using System.Text;
4using System.Collections.Generic;
5using IEnumerableConstraintNode = System.Collections.Generic.IEnumerable<ConstraintNode>;
6
7COMPILER GPDef
8  static StringBuilder srcText;
9  public static HeuristicLab.Optimization.IProblem problem;
10  static string lastSrc;
11 
12CHARACTERS
13  letter = 'A'..'Z' + 'a'..'z'.
14  digit = '0'..'9'.
15
16TOKENS
17  ident = letter {letter | digit}  .
18
19COMMENTS
20  FROM "/*" TO "*/" NESTED
21
22IGNORE '\t' + '\r' + '\n'
23
24
25PRODUCTIONS
26
27  SourceCode<out string src> =                                                    (. src = ""; .)
28   "<<"                                                                           (. int beg = la.pos; .)
29   {ANY}                                                                          (. int end = la.pos; .)
30   ">>"                                                                           (. if(end>beg) src = scanner.buffer.GetString(beg, end); .)
31  .
32
33  GPDef =                                                                         (.
34                                                                                    string identStr = "";
35                                                                                    RuleNode ruleNode = null;
36                                                                                    GPDefNode gpDef = new GPDefNode();
37                                                                                    NonTerminalNode ntNode = null;
38                                                                                    FitnessFunctionNode fitnessFunNode = null;
39                                                                                    TerminalNode tNode = null;
40                                                                                    problem = null;
41                                                                                    string src = "";
42                                                                                  .)
43    "PROBLEM" ident                                                               (. gpDef.Name = t.val; .)
44    ["CODE" SourceCode<out src>                                                   (. gpDef.ClassCodeNode = new CodeNode{SrcCode = src}; .)
45    ]
46    ["INIT" SourceCode<out src>                                                   (. gpDef.InitCodeNode = new CodeNode{SrcCode = src}; .)
47    ]
48
49    "NONTERMINALS" { NonterminalDecl<out ntNode>                                  (. gpDef.NonTerminals.Add(ntNode); .)
50    }
51    "TERMINALS" { TerminalDecl<out tNode>                                         (. gpDef.Terminals.Add(tNode); .)
52    }
53    "RULES" { RuleDef<out ruleNode>                                               (. gpDef.Rules.Add(ruleNode); .)
54    }
55                                                                                  (. fitnessFunNode = new FitnessFunctionNode();
56                                                                                         gpDef.FitnessFunctionNode = fitnessFunNode;
57                                                                                  .)
58    ("MAXIMIZE"                                                                   (. fitnessFunNode.Maximization = true; .)
59    | "MINIMIZE"                                                                  (. fitnessFunNode.Maximization = false; .)
60    )
61     SourceCode<out src>                                                          (. fitnessFunNode.SrcCode = src; .)
62    "END" ident                                                                   (.
63                                                                                    // SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
64                                                                                    var gen = new ProblemGenerator();
65                                                                                    problem = gen.GenerateFromAst(gpDef);
66                                                                                  .)
67    '.'
68  .
69
70
71  /******************************************************/
72  SemAction<out RuleActionNode action> =                                          (. RuleActionNode myAction = null; action = null; string src = ""; .)
73    "SEM" SourceCode<out src>                                                     (.
74                                                                                    myAction = new RuleActionNode();
75                                                                                    myAction.SrcCode = src;
76                                                                                    action = myAction;
77                                                                                  .)
78  .
79
80  /******************************************************/
81  NonterminalDecl<out NonTerminalNode ntNode> =                                   (. string identStr = ""; ntNode = null; string src = ""; .)
82    ident                                                                         (. identStr = t.val; .)
83    SourceCode<out src>                                                           (.
84                                                                                    var myNtNode = new NonTerminalNode();
85                                                                                    ntNode = myNtNode;
86                                                                                    myNtNode.Ident = identStr;
87                                                                                    myNtNode.FormalParameters = src;
88                                                                                  .)
89    '.'
90  .
91
92  /******************************************************/
93  TerminalDecl<out TerminalNode tNode> =                                          (.
94                                                                                    string identStr = "";
95                                                                                    tNode = null;
96                                                                                    TerminalNode myTNode = null;
97                                                                                    IEnumerableConstraintNode constraints = null;
98                                                                                    string src = "";
99                                                                                  .)
100  ident                                                                           (. identStr = t.val; .)
101  SourceCode<out src>                                                             (.
102                                                                                         myTNode = new TerminalNode();
103                                                                                         tNode = myTNode;
104                                                                                         myTNode.Ident = identStr;
105                                                                                         myTNode.FormalParameters = src;
106                                                                                         myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);
107                                                                                  .)
108  [ "CONSTRAINTS" ConstraintDef<out constraints>                                  (. myTNode.Constraints = constraints; .)
109  ]
110  '.'
111  .
112
113
114  /******************************************************/
115  ConstraintDef<out IEnumerableConstraintNode constraints>  =                   (.
116                                                                                    List<ConstraintNode> constraintsList = new List<ConstraintNode>();
117                                                                                    ConstraintNode n = null;
118                                                                                    constraints = null;
119                                                                                  .)
120    { ConstraintRule<out n>                                                       (. constraintsList.Add(n); .)
121    }
122                                                                                  (. constraints = constraintsList; .)
123  .
124
125  /******************************************************/
126  ConstraintRule<out ConstraintNode constraint> =                                 (.
127                                                                                    string identStr = null;
128                                                                                    constraint = null;
129                                                                                  .)
130    ident                                                                         (. constraint = new ConstraintNode(t.val); .)
131    "IN" SetDefinition<constraint>                                                // (. SourceReader.Handle(GPDefSem.srcText.ToString()); .) // flush the source reader
132  .
133
134  /******************************************************/
135  SetDefinition<ConstraintNode constraint> =                                      (. string src = ""; .)
136    ("SET"                                                                        (. constraint.Type = ConstraintNodeType.Set; .)
137     SourceCode<out src>                                                          (. constraint.SetExpression = src; .)
138     |
139     "RANGE"                                                                      (. constraint.Type = ConstraintNodeType.Range; .)
140     SourceCode<out src>                                                          (. constraint.RangeMinExpression = src; .)
141     ".." SourceCode<out src>                                                     (. // SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
142                                                                                    constraint.RangeMaxExpression = src; .)
143    )
144  .
145
146  /******************************************************/
147  RuleDef<out RuleNode rule> =                                                    (.
148                                                                                    RuleExprNode expr = null;
149                                                                                    string identStr = null;
150                                                                                    RuleNode myRule = null;
151                                                                                    rule = null;
152                                                                                   string src = "";
153                                                                                  .)
154  ident                                                                           (. identStr = t.val; .)
155  SourceCode<out src> '='                                                         (.
156                                                                                      myRule = new RuleNode();
157                                                                                      rule = myRule;
158                                                                                      myRule.NtSymbol = identStr;
159                                                                                  .)
160  ["LOCAL" SourceCode<out src>                                                    (.
161                                                                                    myRule.LocalCode = src;
162                                                                                  .)
163  ]
164  SynExpr<out expr> '.'                                                          (.
165                                                                                    myRule.RuleExpr = expr;
166                                                                                 .)
167  .
168
169  /******************************************************/
170  SynExpr<out RuleExprNode expr > =                                               (.
171                                                                                    expr = null;
172                                                                                    AlternativesNode alt = null;
173                                                                                  .)
174  SynTerm<out expr>                                                               (.
175                                                                                    alt = new AlternativesNode();
176                                                                                    alt.Add(expr);
177                                                                                  .)
178  {
179    '|' SynTerm<out expr>                                                         (. alt.Add(expr); expr = alt; .)
180  }
181  .
182
183  /******************************************************/
184  SynTerm<out RuleExprNode expr> =                                               (. SequenceNode seq = null; expr = null; .)
185  SynFact<out expr>                                                              (.
186                                                                                   seq = new SequenceNode();
187                                                                                   seq.Add(expr);
188                                                                                 .)
189  {
190    SynFact<out expr>                                                            (. seq.Add(expr); expr = seq; .)
191  }
192  .
193
194  /******************************************************/
195  SynFact<out RuleExprNode expr> =                                               (.
196                                                                                   string identStr = "";
197                                                                                   RuleActionNode action = null;
198                                                                                   expr = null;
199                                                                                   string src = "";
200                                                                                 .)
201    (ident                                                                        (. identStr = t.val; .)
202     SourceCode<out src>                                                          (.
203                                                                                    var callNode = new CallSymbolNode{Ident = identStr};
204                                                                                    callNode.ActualParameter = src;
205                                                                                    expr = callNode;
206                                                                                 .)
207// not implemented   | "EPS"
208// not implemented   | '(' SynExpr<ref rules, mBuilder> ')'
209// not implemented   | '[' SynExpr<ref rules, mBuilder> ']'
210// not implemented   | '{' SynExpr<ref rules, mBuilder> '}'
211     | SemAction<out action>                                                      (. expr = action; .)
212    )
213  .
214
215END GPDef.
Note: See TracBrowser for help on using the repository browser.