1 | COMPILER GPDef
|
---|
2 | SEM <<
|
---|
3 | static StringBuilder srcText;
|
---|
4 | public static HeuristicLab.Optimization.IProblem problem;
|
---|
5 | >>
|
---|
6 |
|
---|
7 |
|
---|
8 | CHARACTER SETS
|
---|
9 | letter = 'A'..'Z' + 'a'..'z'.
|
---|
10 | digit = '0'..'9'.
|
---|
11 | whiteSpace = CHR(9) + EOL IGNORE. /* ' ' ignored by default */
|
---|
12 |
|
---|
13 | COMMENTS
|
---|
14 | FROM '/*' TO '*/' NESTED.
|
---|
15 |
|
---|
16 | KEYWORDS
|
---|
17 | 'PROBLEM'. 'END'. 'EPS'.
|
---|
18 | 'LOCAL'. 'NONTERMINALS'. 'RULES'.
|
---|
19 | 'SEM'. 'MAXIMIZE'. 'MINIMIZE'. 'TERMINALS'. 'CONSTRAINTS'. 'INIT'. 'MUTATE'. 'CODE'.
|
---|
20 | 'IN'. 'SET'. 'RANGE'.
|
---|
21 |
|
---|
22 | TOKENS
|
---|
23 | '='. '|'. '.'. '('. ')'. '['. ']'. '{'. '}'. '..'.
|
---|
24 |
|
---|
25 | TOKEN CLASSES
|
---|
26 | ident<<out string identStr>> =
|
---|
27 | letter {letter | digit} LEX << identStr = tokenStr; >>
|
---|
28 | .
|
---|
29 |
|
---|
30 | PRAGMAS
|
---|
31 | source = '<<' SEM << GPDefSem.srcText = new StringBuilder();
|
---|
32 | for (; ; ) {
|
---|
33 | switch (GPDefLex.curCh) {
|
---|
34 | case Utils.EF:
|
---|
35 | Errors.SemError(GPDefLex.curLine, GPDefLex.curCol, "end of file in source text");
|
---|
36 | return;
|
---|
37 | case '<':
|
---|
38 | GPDefLex.NextCh();
|
---|
39 | if (GPDefLex.curCh == '<') {
|
---|
40 | GPDefSem.srcText.Append('<');
|
---|
41 | Errors.Warning(GPDefLex.curLine, GPDefLex.curCol, "non closed source text before?");
|
---|
42 | GPDefLex.NextCh();
|
---|
43 | }
|
---|
44 | GPDefSem.srcText.Append('<');
|
---|
45 | break;
|
---|
46 | case '>':
|
---|
47 | GPDefLex.NextCh();
|
---|
48 | if (GPDefLex.curCh == '>') {
|
---|
49 | GPDefLex.curCh = ' '; // force GPDefLex to get next character
|
---|
50 | SourceReader.Handle(GPDefSem.srcText.ToString());
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | GPDefSem.srcText.Append('>');
|
---|
54 | break;
|
---|
55 | default:
|
---|
56 | GPDefSem.srcText.Append(GPDefLex.curCh);
|
---|
57 | GPDefLex.NextCh();
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | >> .
|
---|
62 | NONTERMINALS
|
---|
63 | GPDef.
|
---|
64 | NonterminalDecl<<out NonTerminalNode ntNode>>.
|
---|
65 | TerminalDecl<<out TerminalNode tNode>>.
|
---|
66 | RuleDef<<out RuleNode rNode>>.
|
---|
67 | SynExpr<<out RuleExprNode expr>>.
|
---|
68 | SynTerm<<out RuleExprNode expr>>.
|
---|
69 | SynFact<<out RuleExprNode expr>>.
|
---|
70 | SemAction<<out RuleActionNode action>>.
|
---|
71 | ConstraintDef<<out IEnumerable<ConstraintNode> constraints>>.
|
---|
72 | ConstraintRule<<out ConstraintNode constraint>>.
|
---|
73 | SetDefinition<<ConstraintNode constraint>>.
|
---|
74 |
|
---|
75 | RULES
|
---|
76 | GPDef = LOCAL <<
|
---|
77 | string identStr = "";
|
---|
78 | RuleNode ruleNode = null;
|
---|
79 | GPDefNode gpDef = new GPDefNode();
|
---|
80 | NonTerminalNode ntNode = null;
|
---|
81 | FitnessFunctionNode fitnessFunNode = null;
|
---|
82 | TerminalNode tNode = null;
|
---|
83 | problem = null;
|
---|
84 | >>
|
---|
85 | 'PROBLEM' ident<<out identStr>> SEM << gpDef.Name = identStr; >>
|
---|
86 | ['CODE' /* SourceText */ SEM << SourceReader.WithNext((src) => gpDef.ClassCodeNode = new CodeNode{SrcCode = src}); >>
|
---|
87 | ]
|
---|
88 | ['INIT' /* SourceText */ SEM << SourceReader.WithNext((src) => gpDef.InitCodeNode = new CodeNode{SrcCode = src}); >>
|
---|
89 | ]
|
---|
90 |
|
---|
91 | 'NONTERMINALS' { NonterminalDecl<<out ntNode>> SEM << gpDef.NonTerminals.Add(ntNode); >>
|
---|
92 | }
|
---|
93 | 'TERMINALS' { TerminalDecl<<out tNode>> SEM << gpDef.Terminals.Add(tNode); >>
|
---|
94 | }
|
---|
95 | 'RULES' { RuleDef<<out ruleNode>> SEM << gpDef.Rules.Add(ruleNode); >>
|
---|
96 | }
|
---|
97 | SEM << fitnessFunNode = new FitnessFunctionNode();
|
---|
98 | gpDef.FitnessFunctionNode = fitnessFunNode;
|
---|
99 | >>
|
---|
100 | ('MAXIMIZE' SEM << fitnessFunNode.Maximization = true; >>
|
---|
101 | | 'MINIMIZE' SEM << fitnessFunNode.Maximization = false; >>
|
---|
102 | )
|
---|
103 | /* SourceText */ SEM << SourceReader.WithNext((src) => fitnessFunNode.SrcCode = src); >>
|
---|
104 | 'END' ident<<out identStr>> '.' SEM <<
|
---|
105 | SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
|
---|
106 | var gen = new ProblemGenerator();
|
---|
107 | problem = gen.GenerateFromAst(gpDef);
|
---|
108 | >>
|
---|
109 | .
|
---|
110 |
|
---|
111 |
|
---|
112 | /******************************************************/
|
---|
113 | SemAction<<out RuleActionNode action>> = LOCAL << RuleActionNode myAction = null; action = null; >>
|
---|
114 | 'SEM' /* SourceText */ SEM <<
|
---|
115 | myAction = new RuleActionNode();
|
---|
116 | SourceReader.WithNext((src) => {myAction.SrcCode = src;});
|
---|
117 | action = myAction;
|
---|
118 | >>
|
---|
119 | .
|
---|
120 |
|
---|
121 | /******************************************************/
|
---|
122 | NonterminalDecl<<out NonTerminalNode ntNode>> = LOCAL << string identStr = ""; ntNode = null;>>
|
---|
123 | ident<<out identStr>> /* FormalAttrList */ SEM <<
|
---|
124 | var myNtNode = new NonTerminalNode();
|
---|
125 | ntNode = myNtNode;
|
---|
126 | myNtNode.Ident = identStr;
|
---|
127 | SourceReader.WithNext((src) => {myNtNode.FormalParameters = src;});
|
---|
128 | >>
|
---|
129 | '.'
|
---|
130 | .
|
---|
131 |
|
---|
132 | /******************************************************/
|
---|
133 | TerminalDecl<<out TerminalNode tNode>> = LOCAL <<
|
---|
134 | string identStr = "";
|
---|
135 | tNode = null;
|
---|
136 | TerminalNode myTNode = null;
|
---|
137 | IEnumerable<ConstraintNode> constraints = null;
|
---|
138 | >>
|
---|
139 | ident<<out identStr>> /* FormalAttrList */ SEM <<
|
---|
140 | myTNode = new TerminalNode();
|
---|
141 | tNode = myTNode;
|
---|
142 | myTNode.Ident = identStr;
|
---|
143 | SourceReader.WithNext((src) => {
|
---|
144 | myTNode.FormalParameters = src;
|
---|
145 | myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);
|
---|
146 | });
|
---|
147 |
|
---|
148 | >>
|
---|
149 | [ 'CONSTRAINTS' ConstraintDef<<out constraints>> SEM << myTNode.Constraints = constraints; >>
|
---|
150 | ]
|
---|
151 | '.'
|
---|
152 | .
|
---|
153 |
|
---|
154 |
|
---|
155 | /******************************************************/
|
---|
156 | ConstraintDef<<out IEnumerable<ConstraintNode> constraints>> = LOCAL <<
|
---|
157 | List<ConstraintNode> constraintsList = new List<ConstraintNode>();
|
---|
158 | ConstraintNode n = null;
|
---|
159 | constraints = null;
|
---|
160 | >>
|
---|
161 | { ConstraintRule<<out n>> SEM << constraintsList.Add(n); >>
|
---|
162 | }
|
---|
163 | SEM << constraints = constraintsList; >>
|
---|
164 | .
|
---|
165 |
|
---|
166 | /******************************************************/
|
---|
167 | ConstraintRule<<out ConstraintNode constraint>> = LOCAL <<
|
---|
168 | string identStr = null;
|
---|
169 | constraint = null;
|
---|
170 | >>
|
---|
171 | ident<<out identStr>> SEM << constraint = new ConstraintNode(identStr); >>
|
---|
172 | 'IN' SetDefinition<<constraint>> SEM << SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader >>
|
---|
173 | .
|
---|
174 |
|
---|
175 | /******************************************************/
|
---|
176 | SetDefinition<<ConstraintNode constraint>> =
|
---|
177 | 'SET' SEM << constraint.Type = ConstraintNodeType.Set; >>
|
---|
178 | /* SourceText */ SEM << SourceReader.WithNext((src) => {constraint.SetExpression = src;}); >>
|
---|
179 | |
|
---|
180 | 'RANGE' SEM << constraint.Type = ConstraintNodeType.Range; >>
|
---|
181 | /* SourceText */ SEM << SourceReader.WithNext((src) => {constraint.RangeMinExpression = src;}); >>
|
---|
182 | '..' /* SourceText */ SEM << SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader
|
---|
183 | SourceReader.WithNext((src) => {constraint.RangeMaxExpression = src;}); >>
|
---|
184 | .
|
---|
185 |
|
---|
186 | /******************************************************/
|
---|
187 | RuleDef<<out RuleNode rule>> = LOCAL <<
|
---|
188 | RuleExprNode expr = null;
|
---|
189 | string identStr = null;
|
---|
190 | RuleNode myRule = null;
|
---|
191 | rule = null;
|
---|
192 | >>
|
---|
193 | ident<<out identStr>> /* FormalAttrList */ '=' SEM <<
|
---|
194 | myRule = new RuleNode();
|
---|
195 | rule = myRule;
|
---|
196 | myRule.NtSymbol = identStr;
|
---|
197 | >>
|
---|
198 | ['LOCAL' /* SourceText */ SEM <<
|
---|
199 | SourceReader.WithNext((src) => {myRule.LocalCode = src;});
|
---|
200 | >>
|
---|
201 | ]
|
---|
202 | SynExpr<<out expr>> '.' SEM <<
|
---|
203 | myRule.RuleExpr = expr;
|
---|
204 | >>
|
---|
205 | .
|
---|
206 |
|
---|
207 | /******************************************************/
|
---|
208 | SynExpr<<out RuleExprNode expr >> = LOCAL <<
|
---|
209 | expr = null;
|
---|
210 | AlternativesNode alt = null;
|
---|
211 | >>
|
---|
212 | SynTerm<<out expr>> SEM <<
|
---|
213 | alt = new AlternativesNode();
|
---|
214 | alt.Add(expr);
|
---|
215 | >>
|
---|
216 | {
|
---|
217 | '|' SynTerm<<out expr>> SEM << alt.Add(expr); expr = alt; >>
|
---|
218 | }
|
---|
219 | .
|
---|
220 |
|
---|
221 | /******************************************************/
|
---|
222 | SynTerm<<out RuleExprNode expr>> = LOCAL << SequenceNode seq = null; expr = null; >>
|
---|
223 | SynFact<<out expr>> SEM <<
|
---|
224 | seq = new SequenceNode();
|
---|
225 | seq.Add(expr);
|
---|
226 | >>
|
---|
227 | {
|
---|
228 | SynFact<<out expr>> SEM << seq.Add(expr); expr = seq; >>
|
---|
229 | }
|
---|
230 | .
|
---|
231 |
|
---|
232 | /******************************************************/
|
---|
233 | SynFact<<out RuleExprNode expr>> = LOCAL <<
|
---|
234 | string identStr = "";
|
---|
235 | RuleActionNode action = null;
|
---|
236 | expr = null;
|
---|
237 | >>
|
---|
238 | ident<<out identStr>> /* ActualAttrList */ SEM <<
|
---|
239 | var callNode = new CallSymbolNode{Ident = identStr};
|
---|
240 | SourceReader.WithNext((src) => {callNode.ActualParameter = src;});
|
---|
241 | expr = callNode;
|
---|
242 | >>
|
---|
243 | -- | 'EPS'
|
---|
244 | -- | '(' SynExpr<<ref rules, mBuilder>> ')'
|
---|
245 | -- | '[' SynExpr<<ref rules, mBuilder>> ']'
|
---|
246 | -- | '{' SynExpr<<ref rules, mBuilder>> '}'
|
---|
247 | | SemAction<<out action>> SEM << expr = action; >>
|
---|
248 | .
|
---|
249 |
|
---|
250 | END GPDef.
|
---|