You are on page 1of 3

Department of Computer Science

University of Puerto Rico at Arecibo

CCOM 4005 – Data Structures


Fall 2023-2024
Project #2 – Fully-Parenthesized Expression Evaluator

Introduction
It is a non-trivial task for a computer system to evaluate complex expressions in infix
form, such as 12+34*(10+5)–3*4, because the precedence rule would require certain
operations to be performed before others (which is why prefix and postfix expressions
are easier to evaluate). A simple method to resolve this complexity is to require all
operations to be fully parenthesized. That is, every pair of operands and their operator
must be enclosed by parentheses. The previous expression would be written as
((12+(34*(10+5)))–(3*4)).

Task
You will create a program to evaluate fully-parenthesized expressions and store the
results in variables, where those variables may be used in subsequent expressions.
The expressions will be read from an input file and once all of the expressions have
been evaluated the program will print a table that shows the final value of each variable.

Technical Details
Input format
The input to your program will be a file named expressions.txt that consists of an
unknown number of assignment statements, each in one of two possible forms:
variable = integer
variable : expression

An “expression” is a fully-parenthesized expression made up of operators (+, –, *, /,


where / represents integer division) and integer variables, with no blank spaces used to
separate each component (called a token) of the expression. Each expression can be
up to 60 characters long. You may assume that there will be at most 15 variables
(uppercase letters) in the input file, and that a variable is used only after it has a value.

How to evaluate
How do you evaluate a fully-parenthesized expression? Using several stacks! Your
program must scan the expression from left to right and process each character from
the expression as follows:

Dr. Juan O. López Gerena CCOM 4005 Project 2 Specs Page 1 of 3


• If the next character is a letter (representing a variable), it must be an operand
whose value will be used, so push it onto an operand stack. (Why use a stack
here? Can’t we simply put it in a variable or an array?)
• If the next character is an operator, we need to save it because an operation can
only be performed once we have two operands. So, push it onto an operator
stack. (Again, why do we use a stack here?)
• If the next character is a left parenthesis ‘(‘, left bracket ‘[‘, or left brace ‘{‘, save it
onto the third stack (enclosure stack) until the matching right
parenthesis/bracket/brace is read.
• If the next character is a right parenthesis/bracket/brace, then we have reached
the end of the innermost expression, so pop an element from the enclosure stack
so see if there’s a match (e.g. ‘(‘ with ‘)’, or ‘[‘ with ‘]’). If there’s a match, pop two
elements from the operand stack, pop the operator stack, perform the operation,
and then push the result onto the operand stack. If there’s no match, the
expression is invalid.

At the end of the expression, the enclosure stack and the operator stack must be empty
and the operand stack must contain a single value, which the value of the expression.
Otherwise, the expression is invalid.

Sample Input File


L=5
W=10
A:(L*W)
X=100
L:[X-W]
L:(A-X)
J:([X-(A/W])*L)
J:([X-(A/W)]*L)
W:A+X
L:((L+A)*W)

Sample Output
L:(A-X)) is invalid
J:([X-(A/W])*L is invalid
W:A+X is invalid

The final symbol table is:

variable value
----------------
L 1400
W 10
A 50
X 100
J 8550

Dr. Juan O. López Gerena CCOM 4005 Project 2 Specs Page 2 of 3


Documentation & Comments
You must properly document and comment your code! It’s important that within your
methods your code is well commented so that your intention is clear. This includes
block comments that may take 2-4 lines explaining what the next several lines intend to
do; hopefully the code you have seen so far this semester has provided good examples
of that. This is an important part of your formation as a future professional in the
computer science/engineering industry. You will lose points if you don’t provide
adequate comments explaining your logic, and you must also document the class
indicating what it is/does. If you’re unsure whether you have enough comments, I
encourage you to meet with me during office hours.

Submission
Your submission must consist of five files:
1. StackADT.h (as provided in class, without any changes)
2. LinkedStack.h and/or ArrayStack.h (as provided in class; no changes)
3. ExpressionEvaluator.cpp (main program)

The final date to submit your program will be Friday, Nov 17, 2023 at 11:59pm. The
name of your zip file must follow the following format: P2_4005_nnnnnnnn_231.zip, where
nnnnnnnnn should be replaced with your student id number. There will be a 20 point
penalty for every day that the project is late.

Academic Integrity
Do NOT share your code! You may discuss design/implementation
strategies, but if projects are too similar for it to be a coincidence, all parties
involved will receive a grade of 0. Don’t cheat yourself out of a learning
experience; seek help if you need it.

Final comments
The specifications of a project are the first, and arguably the most important, part of a
software development project. Therefore, it’s crucial that you read these specifications
thoroughly so that you understand what is being asked of you. These are skills that
you will need to succeed in your professional career, so it’s imperative that you start
applying and improving them now. If your program runs successfully, but does not
adhere to the specifications, it is of no use. Before you submit your project, review
these specifications one last time and make sure you meet all of the requirements that
have been imposed.

If your code does not compile properly, your grade will be 0, NO


EXCEPTIONS!

Dr. Juan O. López Gerena CCOM 4005 Project 2 Specs Page 3 of 3

You might also like