COMPILER GPDef SEM << static StringBuilder srcText; public static HeuristicLab.Optimization.IProblem problem; >> CHARACTER SETS letter = 'A'..'Z' + 'a'..'z'. digit = '0'..'9'. whiteSpace = CHR(9) + EOL IGNORE. /* ' ' ignored by default */ COMMENTS FROM '/*' TO '*/' NESTED. KEYWORDS 'PROBLEM'. 'END'. 'EPS'. 'LOCAL'. 'NONTERMINALS'. 'RULES'. 'SEM'. 'MAXIMIZE'. 'MINIMIZE'. 'TERMINALS'. 'CONSTRAINTS'. 'INIT'. 'MUTATE'. 'CODE'. 'IN'. 'SET'. 'RANGE'. TOKENS '='. '|'. '.'. '('. ')'. '['. ']'. '{'. '}'. '..'. TOKEN CLASSES ident<> = letter {letter | digit} LEX << identStr = tokenStr; >> . PRAGMAS source = '<<' SEM << GPDefSem.srcText = new StringBuilder(); for (; ; ) { switch (GPDefLex.curCh) { case Utils.EF: Errors.SemError(GPDefLex.curLine, GPDefLex.curCol, "end of file in source text"); return; case '<': GPDefLex.NextCh(); if (GPDefLex.curCh == '<') { GPDefSem.srcText.Append('<'); Errors.Warning(GPDefLex.curLine, GPDefLex.curCol, "non closed source text before?"); GPDefLex.NextCh(); } GPDefSem.srcText.Append('<'); break; case '>': GPDefLex.NextCh(); if (GPDefLex.curCh == '>') { GPDefLex.curCh = ' '; // force GPDefLex to get next character SourceReader.Handle(GPDefSem.srcText.ToString()); return; } GPDefSem.srcText.Append('>'); break; default: GPDefSem.srcText.Append(GPDefLex.curCh); GPDefLex.NextCh(); break; } } >> . NONTERMINALS GPDef. NonterminalDecl<>. TerminalDecl<>. RuleDef<>. SynExpr<>. SynTerm<>. SynFact<>. SemAction<>. ConstraintDef< constraints>>. ConstraintRule<>. SetDefinition<>. RULES GPDef = LOCAL << string identStr = ""; RuleNode ruleNode = null; GPDefNode gpDef = new GPDefNode(); NonTerminalNode ntNode = null; FitnessFunctionNode fitnessFunNode = null; TerminalNode tNode = null; problem = null; >> 'PROBLEM' ident<> SEM << gpDef.Name = identStr; >> ['CODE' /* SourceText */ SEM << SourceReader.WithNext((src) => gpDef.ClassCodeNode = new CodeNode{SrcCode = src}); >> ] ['INIT' /* SourceText */ SEM << SourceReader.WithNext((src) => gpDef.InitCodeNode = new CodeNode{SrcCode = src}); >> ] 'NONTERMINALS' { NonterminalDecl<> SEM << gpDef.NonTerminals.Add(ntNode); >> } 'TERMINALS' { TerminalDecl<> SEM << gpDef.Terminals.Add(tNode); >> } 'RULES' { RuleDef<> SEM << gpDef.Rules.Add(ruleNode); >> } SEM << fitnessFunNode = new FitnessFunctionNode(); gpDef.FitnessFunctionNode = fitnessFunNode; >> ('MAXIMIZE' SEM << fitnessFunNode.Maximization = true; >> | 'MINIMIZE' SEM << fitnessFunNode.Maximization = false; >> ) /* SourceText */ SEM << SourceReader.WithNext((src) => fitnessFunNode.SrcCode = src); >> 'END' ident<> '.' SEM << SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader var gen = new ProblemGenerator(); problem = gen.GenerateFromAst(gpDef); >> . /******************************************************/ SemAction<> = LOCAL << RuleActionNode myAction = null; action = null; >> 'SEM' /* SourceText */ SEM << myAction = new RuleActionNode(); SourceReader.WithNext((src) => {myAction.SrcCode = src;}); action = myAction; >> . /******************************************************/ NonterminalDecl<> = LOCAL << string identStr = ""; ntNode = null;>> ident<> /* FormalAttrList */ SEM << var myNtNode = new NonTerminalNode(); ntNode = myNtNode; myNtNode.Ident = identStr; SourceReader.WithNext((src) => {myNtNode.FormalParameters = src;}); >> '.' . /******************************************************/ TerminalDecl<> = LOCAL << string identStr = ""; tNode = null; TerminalNode myTNode = null; IEnumerable constraints = null; >> ident<> /* FormalAttrList */ SEM << myTNode = new TerminalNode(); tNode = myTNode; myTNode.Ident = identStr; SourceReader.WithNext((src) => { myTNode.FormalParameters = src; myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src); }); >> [ 'CONSTRAINTS' ConstraintDef<> SEM << myTNode.Constraints = constraints; >> ] '.' . /******************************************************/ ConstraintDef< constraints>> = LOCAL << List constraintsList = new List(); ConstraintNode n = null; constraints = null; >> { ConstraintRule<> SEM << constraintsList.Add(n); >> } SEM << constraints = constraintsList; >> . /******************************************************/ ConstraintRule<> = LOCAL << string identStr = null; constraint = null; >> ident<> SEM << constraint = new ConstraintNode(identStr); >> 'IN' SetDefinition<> SEM << SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader >> . /******************************************************/ SetDefinition<> = 'SET' SEM << constraint.Type = ConstraintNodeType.Set; >> /* SourceText */ SEM << SourceReader.WithNext((src) => {constraint.SetExpression = src;}); >> | 'RANGE' SEM << constraint.Type = ConstraintNodeType.Range; >> /* SourceText */ SEM << SourceReader.WithNext((src) => {constraint.RangeMinExpression = src;}); >> '..' /* SourceText */ SEM << SourceReader.Handle(GPDefSem.srcText.ToString()); // flush the source reader SourceReader.WithNext((src) => {constraint.RangeMaxExpression = src;}); >> . /******************************************************/ RuleDef<> = LOCAL << RuleExprNode expr = null; string identStr = null; RuleNode myRule = null; rule = null; >> ident<> /* FormalAttrList */ '=' SEM << myRule = new RuleNode(); rule = myRule; myRule.NtSymbol = identStr; >> ['LOCAL' /* SourceText */ SEM << SourceReader.WithNext((src) => {myRule.LocalCode = src;}); >> ] SynExpr<> '.' SEM << myRule.RuleExpr = expr; >> . /******************************************************/ SynExpr<> = LOCAL << expr = null; AlternativesNode alt = null; >> SynTerm<> SEM << alt = new AlternativesNode(); alt.Add(expr); >> { '|' SynTerm<> SEM << alt.Add(expr); expr = alt; >> } . /******************************************************/ SynTerm<> = LOCAL << SequenceNode seq = null; expr = null; >> SynFact<> SEM << seq = new SequenceNode(); seq.Add(expr); >> { SynFact<> SEM << seq.Add(expr); expr = seq; >> } . /******************************************************/ SynFact<> = LOCAL << string identStr = ""; RuleActionNode action = null; expr = null; >> ident<> /* ActualAttrList */ SEM << var callNode = new CallSymbolNode{Ident = identStr}; SourceReader.WithNext((src) => {callNode.ActualParameter = src;}); expr = callNode; >> -- | 'EPS' -- | '(' SynExpr<> ')' -- | '[' SynExpr<> ']' -- | '{' SynExpr<> '}' | SemAction<> SEM << expr = action; >> . END GPDef.