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

Session 0: Modularity Exercise Guide

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)
19 views4 pages

Session 0: Modularity Exercise Guide

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

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: [Link], 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.

[Link] 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).

[Link] 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

[Link] 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.

[Link] 4/4

Common questions

Powered by AI

To add support for the '*' operator, both the term and evaluation modules' interfaces and implementations need modification. The term module requires provisions to recognize '*' as a term, while the evaluation module must adjust logic to handle multiplication operations during stack manipulations .

The evaluation function involves creating a stack, extracting tokens from the stream, transforming them into terms, and handling constant and '+' terms accordingly. For constants, values are pushed onto the stack. If a '+' is encountered, two values are popped, summed, and the result pushed back. This continues until the expression is evaluated and displayed .

Several functionalities are necessary, including reading the input as a string, isolating elements separated by spaces (tokens), typing tokens to classify them as either numbers or operators, and using a data structure, specifically a stack, to store and compute expressions .

If the stream interface is modified, all modules dependent on it, particularly the evaluation module, require recompilation to ensure compatibility with the updated interface. Changes in defined interfaces can affect how other modules interact with the stream .

Dependencies between the modules include the stream module needing to provide tokens for the evaluation module, which relies on the term module for token interpretation. The stack module is fundamental to both the term and evaluation modules, providing the necessary data structure for expression computation .

The Makefile should define build targets for each module, specify their dependencies, and include rules for compiling and linking the project. It must also produce a final executable called 'evaluate'. Make adjustments for changes in source files or dependencies to ensure accurate builds .

Recompilation criteria include direct dependency on the changed interface, such as using modified data types or function signatures. Recompilation is necessary if a module directly imports, relies on behaviors, or uses features altered by the interface updates .

To compile the stream module manually, the necessary command typically involves using a C compiler (e.g., gcc) with the appropriate source files and any dependencies specified. Example: 'gcc -c stream.c' .

The stream module should include an abstract type for streams, a stream_open function to return a stream from a file, a stream_close function to terminate the stream, and a stream_get_token function to retrieve successive tokens from the stream until none are left (returns NULL).

The term module involves an abstract type to represent tokens (both integer constants and operators), a string_to_term function to convert strings to terms, and is_constant and is_plus functions to check if a term is a number or the plus operator, respectively. These functionalities aid in token classification and management .

You might also like