Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Benchmark Generator/HeuristicLab.BenchmarkGenerator/3.3/ExpressionGrammar.cs @ 12833

Last change on this file since 12833 was 10088, checked in by bburlacu, 11 years ago

#2083: Removed old project and rewritten the Benchmark Generator as a HeuristicLab plugin. Added new functions and visualization capability.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using Irony.Interpreter;
24using Irony.Interpreter.Ast;
25using Irony.Parsing;
26
27namespace HeuristicLab.BenchmarkGenerator {
28  public class ExpressionGrammar : InterpretedLanguageGrammar {
29    public ExpressionGrammar() : this(caseSensitive: false) { }
30
31    public ExpressionGrammar(bool caseSensitive)
32      : base(caseSensitive) {
33
34      var comma = ToTerm(",");
35      // Define terminals
36      var number = new NumberLiteral("Number") {
37        DefaultIntTypes = new[] { TypeCode.Double },
38        //        DefaultIntTypes = new[] { TypeCode.Int32, TypeCode.Int64 },
39        DefaultFloatType = TypeCode.Double
40      };
41      var identifier = new IdentifierTerminal("Identifier");
42
43      // Define non-terminals
44      var term = new NonTerminal("Term");
45      var binExpr = new NonTerminal("BinExpr", typeof(BinaryOperationNode));
46      var binOp = new NonTerminal("BinOp", "operator");
47      var unExpr = new NonTerminal("UnExpr", typeof(UnaryOperationNode));
48      var unOp = new NonTerminal("UnOp", "operator");
49      var expr = new NonTerminal("Expr");
50      var argList = new NonTerminal("ArgList", typeof(ExpressionListNode));
51      var functionCall = new NonTerminal("FunctionCall", typeof(FunctionCallNode));
52      var parExpr = new NonTerminal("ParExpr");
53      var statement = new NonTerminal("Statement");
54      var assignmentStmt = new NonTerminal("AssignmentStmt", typeof(AssignmentNode));
55      var assignmentOp = new NonTerminal("AssignmentOp", "assignment operator");
56      var program = new NonTerminal("Program", typeof(StatementListNode));
57
58      // BNF rules
59      expr.Rule = term | unExpr | binExpr;
60      term.Rule = number | parExpr | identifier | functionCall;
61      parExpr.Rule = "(" + expr + ")";
62      unExpr.Rule = unOp + term;
63      unOp.Rule = ToTerm("+") | "-";
64      binExpr.Rule = expr + binOp + expr;
65      binOp.Rule = ToTerm("+") | "-" | "*" | "/" | "^"; // add, sub, mul, div, pow
66      argList.Rule = MakeStarRule(argList, comma, expr);
67      functionCall.Rule = identifier + PreferShiftHere() + "(" + argList + ")";
68      assignmentStmt.Rule = identifier + assignmentOp + expr;
69      assignmentOp.Rule = ToTerm("=");
70      statement.Rule = assignmentStmt | expr | Empty;
71      program.Rule = MakePlusRule(program, NewLine, statement);
72      this.Root = program;
73
74      // Caption templates
75      functionCall.NodeCaptionTemplate = "call #{0}(...)";
76
77      // Operators precedence
78      RegisterOperators(1, "+", "-");
79      RegisterOperators(2, "*", "/");
80      RegisterOperators(3, Associativity.Right, "^");
81
82      // Punctuation and braces
83      MarkPunctuation("(", ")", "[", "]");
84      RegisterBracePair("(", ")");
85      RegisterBracePair("[", "]");
86      // Mark transient because these don't have corresponding AST nodes
87      MarkTransient(term, expr, statement, binOp, unOp, assignmentOp, parExpr);
88      // Language flags                         
89      LanguageFlags = LanguageFlags.NewLineBeforeEOF | LanguageFlags.CreateAst;
90    }
91
92    public override LanguageRuntime CreateRuntime(LanguageData data) {
93      return new CustomLanguageRuntime(data);
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.