Exercice Session 0 - Modularity
Exercice Session 0 - Modularity
2. Exercice on Modularity
We want to write a program that evaluates arithmetic expressions written
in PostFix notation (a.k.a Reverse Polish Notation), such as
3 4 + 5 +
read the input as a string and isolate the elements separated by spaces,
these are the tokens.
type (classify) a token: is it a number or an operator? Use a new type
term to store the token semantics.
a convenient data structure to store the terms and compute the
expression. In Reverse Polish Notation, a stack is known to be well
adapted.
https://moodle.unistra.fr/pluginfile.php/2392035/mod_resource/content/6/ex-modularity.html 1/4
2/22/24, 12:47 PM Exercice Session 0 - Modularity
stack create(void);
bool is_empty(stack s);
void push(stack * s, int val);
int pop(stack * s);
https://moodle.unistra.fr/pluginfile.php/2392035/mod_resource/content/6/ex-modularity.html 2/4
2/22/24, 12:47 PM Exercice Session 0 - Modularity
module?
3. Assuming that all the modules have been compiled, what command
should be typed manually to edit the links?
4. The stream interface is modified. Which module(s) should be
recompiled?
5. The stream implementation is modified. Which module(s) should be
recompiled?
6. Write the specifications as comments (using Doxygen syntax) for
functions in stack_uncommented.h . Once done, rename the file to stack.h .
7. Write the interfaces for the stream , term and evaluation modules.
3.2. Part 2
create a stack Q
while (we can get a token TOKEN from stream)
do
make a term from TOKEN
if TERM is a constant value VAL
then
https://moodle.unistra.fr/pluginfile.php/2392035/mod_resource/content/6/ex-modularity.html 3/4
2/22/24, 12:47 PM Exercice Session 0 - Modularity
push VAL on Q
endif
if TERM is '+'
then
for i = 0 to 1
pop TOKi from Q
if Q is empty
then
display "Syntax Error in expression" and exit
endif
endfor
push TOK0+TOK1 to Q
endif
done
pop result from Q
print result
https://moodle.unistra.fr/pluginfile.php/2392035/mod_resource/content/6/ex-modularity.html 4/4