Associativity and Precedence of Operators Programming Worksheet

Homework 5 – CMSC 331 – Summer 2023Due Date July 7, 11:59 PM
The goal of this exercise is to acquaint yourself with two very powerful tools for the
generation of compilers: flex and bison. In this homework, files for a calculator application
are provided to you. The files are as the following list.
File
Description
calc.l
Contains the regular expression rules
calc.y
Contains the grammar rules
calc.h
Contains a data structure used by calculator
makefile
Contains the make commands to build the application
You need to transfer the files to a directory in your GL account. The address of GL account
is “gl.umbc.edu”. You need to use a FTP application to transfer the files.
The following command builds the calculator application:
[user@linux2] make
After building the application, you can run the executable file “calc” that is created in the
folder. The following example presents a session of running the calculator. At the prompt
of calculator user can enter any arithmetic expression and by pressing enter, the
calculator evaluates the arithmetic expression. The exit command is dot, and by typing
“?” character user can get help instructions on the calculator prompt.
[user@linux2] calc
>> 3 + 4
7
>> .
[user@linux2]
Your job is to modify the application to add logical operators as in the following table. All
operators will have left associativity.
1
Operator Meaning
Precedence
Description
&
Logical and Lowest
This is equivalent to && in C language. It is a
binary operator. If it evaluates to true, it
returns 1 and if evaluates to false it returns 0.
|
Logical or
Same as &
This is equivalent to || in C language. It is a
binary operator. If it evaluates to true, it
returns 1 and if evaluates to false it returns 0.
==
Equal
Higher than
& and |,
lower than >
and < It is a binary operator. If both sides are of equal values, it returns 1, otherwise it returns 0. >
Greater
than
Highest
It is a binary operator. If left operand has a
greater value than right operand, it returns 1,
otherwise it returns 0.
< Less than Same as >
It is a binary operator. If left operand has a
greater value than right operand, it returns 0,
otherwise it returns 1.
The following is an example test session that your application can be tested against. You
need to test your modifications for all five operators you have added.
>> a = 5
>> b = 8
>> a > b
0
>> a < b & a < b 1 >> a == 5 & 3 > 4 | a > 5
0
You need to submit your modified files, i.e. “calc.l” and “calc.y” through the submit utility
on your gl account. This means all your modifications are only in these two files. Your
files will be compiled with “calc.h” and the resulting executable will be tested for its output.
2
Hints:
● In this application, you do not need to modify the grammar rules to implement
associativity and precedence of operators.
● In the declaration section of “calc.y” you can define the precedence and
associativity of operators. You can define this information for your logical operators
in the same place and in the same way.
● Logical and (&) has a higher precedence than assignment (=) operator, and
Greater than (>) has a lower precedence than + and – operators.
Submission
Please note, you submit two files calc.y and calc.l on Blackboard and also to
your gl account using the following commands. We will run your code on the
gl machine. If you forget to submit to gl server, you’ll lose the points for that part.
Submission mechanism: Submission to gl server for this assignment happens through
submit utility in gl servers of UMBC. You need to be in the directory that contains your
files.
To submit your modified calc.y and calc.l files use this command:
submit cmsc331_donyaee proj5 calc.y calc.l
To resubmit (overwrite) a file:
submit cmsc331_donyaee proj5 calc.y calc.l
To list files that you have submitted:
submitls cmsc331_donyaee proj5
To delete the files you have submitted:
submitrm cmsc331_donyaee proj5 calc.y
There is a help file on using the submit utility at the following URL:
https://wiki.umbc.edu/pages/viewpage.action?pageId=105284083
3

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER