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