Code Examples
A repository of 155 code examples for BeepBeep
ArithmeticBuilder.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package dsl;
19 
20 import java.util.ArrayDeque;
21 
22 import ca.uqac.lif.bullwinkle.BnfParser.InvalidGrammarException;
23 import ca.uqac.lif.bullwinkle.Builds;
24 import ca.uqac.lif.cep.dsl.GrammarObjectBuilder;
25 import ca.uqac.lif.cep.functions.Constant;
26 import ca.uqac.lif.cep.functions.Function;
27 import ca.uqac.lif.cep.functions.FunctionTree;
28 import ca.uqac.lif.cep.util.Numbers;
29 
30 /**
31  * Use a {@link ca.uqac.lif.cep.dsl.GrammarObjectBuilder GrammarObjectBuilder}
32  * to parse and evaluate a string in Polish notation.
33  * @author Sylvain HallĂ©
34  * @difficulty Medium
35  */
36 public class ArithmeticBuilder extends GrammarObjectBuilder<Function>
37 {
38  public static void main(String[] args) throws BuildException
39  {
40  //&
41  ArithmeticBuilder builder = new ArithmeticBuilder();
42  Function f = builder.build("+ 3 - 4 5");
43  Object[] value = new Object[1];
44  f.evaluate(new Object[]{}, value);
45  System.out.println(value[0]);
46  //&
47  }
48 
49  ///
50  public ArithmeticBuilder()
51  {
52  super();
53  try
54  {
55  setGrammar("<exp> := <add> | <sbt> | <num>;\n"
56  + "<add> := + <exp> <exp>;\n"
57  + "<sbt> := - <exp> <exp>;\n"
58  + "<num> := ^[0-9]+;");
59  }
60  catch (InvalidGrammarException e)
61  {
62  // Do nothing
63  }
64  }
65  ///
66 
67  @Builds(rule="<num>")
68  //*
69  public void handleNum(ArrayDeque<Object> stack)
70  {
71  String s_num = (String) stack.pop();
72  Number n_num = Float.parseFloat(s_num);
73  Constant c = new Constant(n_num);
74  stack.push(c);
75  }
76  //*
77 
78  //%
79  @Builds(rule="<add>")
80  public void handleAdd(ArrayDeque<Object> stack)
81  {
82  Function f2 = (Function) stack.pop();
83  Function f1 = (Function) stack.pop();
84  stack.pop(); // To remove the "+" symbol
85  stack.push(new FunctionTree(Numbers.addition, f1, f2));
86  }
87  //%
88 
89  //!
90  @Builds(rule="<sbt>")
91  public void handleSbt(ArrayDeque<Object> stack)
92  {
93  Function f2 = (Function) stack.pop();
94  Function f1 = (Function) stack.pop();
95  stack.pop(); // To remove the "-" symbol
96  stack.push(new FunctionTree(Numbers.subtraction, f1, f2));
97  }
98  //!
99 
100 }
Use a GrammarObjectBuilder to parse and evaluate a string in Polish notation.