Code Examples
A repository of 155 code examples for BeepBeep
ArithmeticBuilderIncorrect.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  * Shows what happens when a
32  * {@link ca.uqac.lif.cep.dsl.GrammarObjectBuilder GrammarObjectBuilder}
33  * incorrectly manipulates its object stack.
34  * @author Sylvain HallĂ©
35  * @see ArithmeticBuilder
36  * @difficulty Medium
37  */
38 public class ArithmeticBuilderIncorrect extends GrammarObjectBuilder<Function>
39 {
40  public static void main(String[] args) throws BuildException
41  {
42  //&
44  Function f = builder.build("+ 3 - 4 5");
45  Object[] value = new Object[1];
46  f.evaluate(new Object[]{}, value);
47  System.out.println(value[0]);
48  //&
49  }
50 
51 
52  ///
54  {
55  super();
56  try
57  {
58  setGrammar("<exp> := <add> | <sbt> | <num>;\n"
59  + "<add> := + <exp> <exp>;\n"
60  + "<sbt> := - <exp> <exp>;\n"
61  + "<num> := ^[0-9]+;");
62  }
63  catch (InvalidGrammarException e)
64  {
65  // Do nothing
66  }
67  }
68  ///
69 
70  @Builds(rule="<num>")
71  //*
72  public void handleNum(ArrayDeque<Object> stack)
73  {
74  String s_num = (String) stack.pop();
75  Number n_num = Float.parseFloat(s_num);
76  Constant c = new Constant(n_num);
77  stack.push(c);
78  }
79  //*
80 
81  //%
82  @Builds(rule="<add>")
83  public void handleAdd(ArrayDeque<Object> stack)
84  {
85  Function f2 = (Function) stack.pop();
86  stack.pop(); // Incorrect! This line and the next should be swapped
87  Function f1 = (Function) stack.pop();
88  stack.push(new FunctionTree(Numbers.addition, f1, f2));
89  }
90  //%
91 
92  //!
93  @Builds(rule="<sbt>")
94  public void handleSbt(ArrayDeque<Object> stack)
95  {
96  Function f2 = (Function) stack.pop();
97  Function f1 = (Function) stack.pop();
98  stack.pop(); // To remove the "-" symbol
99  stack.push(new FunctionTree(Numbers.subtraction, f1, f2));
100  }
101  //!
102 
103 }
Shows what happens when a GrammarObjectBuilder incorrectly manipulates its object stack...