0% found this document useful (0 votes)
54 views4 pages

Exercice Session 0 - Modularity

This document describes an exercise to practice modular code structure. The goal is to write a program that evaluates arithmetic expressions in postfix notation. The required functionality is split across several modules: a stack module, a stream module to read input, a term module to classify tokens as numbers or operators, and an evaluation module that uses the stack to evaluate expressions. Questions are provided to guide implementing the modules and Makefile. Dependencies between modules and which need recompiling based on interface or implementation changes are explored.

Uploaded by

nniko6740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Exercice Session 0 - Modularity

This document describes an exercise to practice modular code structure. The goal is to write a program that evaluates arithmetic expressions in postfix notation. The required functionality is split across several modules: a stack module, a stream module to read input, a term module to classify tokens as numbers or operators, and an evaluation module that uses the stack to evaluate expressions. Questions are provided to guide implementing the modules and Makefile. Dependencies between modules and which need recompiling based on interface or implementation changes are explored.

Uploaded by

nniko6740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2/22/24, 12:47 PM Exercice Session 0 - Modularity

Exercice Session 0 - Modularity


1. Objective
This exercice aims at learning how to structure code so that a project
shows a decent modularity.
This exercice uses C as support language and Makefile to operate
separate compilation.
Download the files to start with: modularity-students.tar.gz, unarchive,
read the subject and answer the questions.

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 +

which is evaluated as (3+4)+5 = 12 .

We identify several required functionalities to develop:

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.

To do this, we split the needed functionalities in several modules.

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

2.1. stack module

Recall the interface of a module for stacks:

typedef struct stack_t * stack;

stack create(void);
bool is_empty(stack s);
void push(stack * s, int val);
int pop(stack * s);

In addition to the stack module, we have the following modules:

2.2. stream module

an abstract type for streams ;


a stream_open function which takes a character string as an argument
and returns a stream corresponding to the file indicated by the string ;
a stream_close function which takes a stream as an argument and closes
it ;
a stream_get_token function, which takes a stream as an argument and
returns a string corresponding to the next word in the input, or NULL if
no more word could be extracted ;

2.3. term module

an abstract type to represent the meaning of a token: integer constant


or plus operator;
a string_to_term function which takes a string of characters as an
argument and returns the term.
a function is_constant which takes a term as input and returns true if it
is an integer constant ;
a function is_plus which takes a term as input and returns true if it is
the plus operator ;
a val_constant function which takes a term as input and returns the
corresponding integer if it is a constant (undefined otherwise).

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

2.4. evaluation module

an eval function which takes a stream as an argument, evaluates the


stream expression using a stack and returns the integer corresponding
to the evaluation;
a main function which calls eval on the file corresponding to the first
argument passed on the command line and displays the result of the
evaluation on the standard output.
3. Questions
3.1. Part 1

1. What are the dependencies between the modules?


2. Which command must be written manually to compile the stream

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

1. Write the implementation of the evaluation module. The algorithm in


pseudo-code goes like:

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

2. Write the Makefile corresponding to the project. Don't forget to write a


target to produce the final executable, which we'll call evaluate .
3. We want to add management of the * (times) operator. Which files
need to be modified beforehand? Distinguish between interface and
implementation.

https://moodle.unistra.fr/pluginfile.php/2392035/mod_resource/content/6/ex-modularity.html 4/4

You might also like