This exercise will get you set up with ANTLR using a VM and then have you modify an existing ANTLR file. You may work with others if you find it helpful, but each student must submit their own solution.
vmboxes
. You can use the passwd
command at the terminal prompt to change it.
antlr4 -Dlanguage=Python3 adder.g4Note the 4 in this command!
add
command; that should still take
and produce an integer. (The two have different types so you can get
experience with multiple types in this sort of application.) For example,
ave 3.5, 4.8, 2 add 3, 5, 2 ave 8would print something like
Average: 3.43333333 Sum: 10 Average: 8.0There is no need to control the number of decimal places printed for
ave
.
The only file you should need to modify is adder.g4
. In a
perfect world you would rename this file to something more general than
"adder", but it is probably simpler if you do not.
Feel free to ask for help with any Python questions you might have in addition to help with ANTLR and installation.
list[float]
; that is, to write a production which
returns a list of the numbers after the average
command.
At the end, you would compute the sum and count from this list.
Another way is to set up the productions to return the pair (float,
int)
where the first number is the floating-point sum of the
values and the second is the count. A possible third way is to use a
global variable as shown in
interp.g4
.
However, if you do this, only use the global to store one of the values;
a key part of this assignment is returning and using values computed by
productions as shown in the original adder.g4
.
Submit an evidence document showing your solution working on relevant
cases. You should include easy-to-compute results, more extensive results,
and a case with a single value. Include results for both
the ave
and add
cases. Submit this as a
PDF. In addition, submit any ANTLR and Python files you
modify. Do not submit files that are generated by ANTLR.