$30
Assignment 1
This assignment is done individually or by a group of 2 students (each group submits only 1
copy of the assignment)
Goal :
1. Learn how to write and use makefile: http://www.delorie.com/djgpp/doc/ug/larger/makefiles.html
2. Acquaint yourself with flex and bison.
Specifications
You will extend calc.l and calc.y to parse programs whose syntax is defined below.
Prog à main() {Stmts}
Stmts à ε | Stmt; Stmts
Stmt à Id = E | Id *= E | Id += E | print E
E à Integer | Id | E * E | E + E | (-Integer)
Integer à digit+
Prog is a program that contains one main function.
Stmts is empty or a sequence of statements separated using ;
Integer is either a positive integer, or a negative integer enclosed in “(“ and “)”
Id is an identifier, which is a sequence of one or more lower-case letters or digits. In addition, Id should
start with a lower-case letters. For example, x, x1, xy are identifiers, but 1x and A are not.
Expression E is an integer, an identifier, or an infix arithmetic expression with operators "*" and "+".
These two operators are left associative (e.g., 1 + 2 + 3 is equivalent to (1 + 2) + 3). "*" has higher
precedence than “+”.
Id = E assigns the value of an expression E to the variable Id, Id *= E assigns Id * E to Id, and Id += E
assigns Id + E to Id. println E outputs the value of the expression E. Assume that Id is already assigned a
value prior to the execution of Id *= E and Id += E.
If there is any syntax error, you are expected to interpret the program until the statement where you find
the error. Also, your error message must contain the line number where the error was found.
Tokens may be separated by any number of white spaces, tabs, or new lines.
Compile your program:
flex –l calc.l
bison -dv calc.y
gcc -o calc calc.tab.c lex.yy.c –lfl
Execution (example):.
./calc < input
Where input is the name of the input file
Example Programs (Note that the test cases used in grading may be different from the examples)
Program 1:
main() {x = 3;}
Output:
Program 2:
main() {x = 3; print x;}
Output:
3
Program 3:
main() {x = 3; x = 5 + 4; print x;}
Output:
9
Program 4:
main() {x = 3* 4; print x;}
Output:
12
Program 5:
main() {print 1 + 2 + 4;}
Output:
7
Program 6:
main() {x = 0; x = 1-2; }
Output:
Lexical error: -
Parsing error: line 1
Program 7:
main() {
1x = 3;
}
Output:
Lexical error: 1x
Parsing error: line 2
Program 8:
main() {
x = 3;
print x;
}
Output:
3
Program 9:
x = 0;
Output:
Parsing error: line 1
Program 10:
main() {x = 3; x += 5 + 4; print x;}
Output:
12
Program 11:
main() {x = 3; x = (-5) + 3; print x;}
Output:
-2
Program 12:
main() {x = 3; y = 2; print x; print y;}
Output:
3
2
Submission guideline
• Each group please submit only ONE copy of the assignment (one group member submit the
assignment)
• Please hand in your source code and a Makefile electronically through
mycourses.binghamton.edu (please do not submit .o or executable code). You must make sure
that your code compiles and runs correctly on bingsuns.binghamton.edu or
remote.cs.binghamton.edu. The Makefile must give the executable code the name calc
• Write a README file (text file, do not submit a .doc file) which contains
§ The name and the email address of group members
§ Whether your code was tested on bingsuns or remote.cs.
§ How to execute your program.
§ (Optional) Briefly describe your algorithm or anything special about your submission that the
TA should take note of.
• Place all your files under one directory with a unique name (such as p1-[userid] for assignment 1,
e.g. p1-pyang).
• Tar the contents of this directory using the following command.
tar –cvf [directory_name].tar [directory_name]
E.g. tar -cvf p1-pyang.tar p1-pyang/
• Use the mycourses to upload the tared file you created above.
Grading guideline
Readme, correct executable names: 4’
Correct makefile: 8’
Correctness: 88’
Academic Honesty:
All students should follow Student Academic Honesty Code
(https://www.binghamton.edu/watson/about/honesty-policy.pdf).
All forms of cheating will be treated with utmost seriousness. You may discuss the problems with other
students, however, you must write your OWN codes and solutions. Discussing solutions to the problem is
NOT acceptable. Copying an assignment from another student or allowing another student to copy your
work may lead to an automatic F for this course. If you borrow small parts of code/text from Internet, you
must acknowledge this in your submission. Also, you must clearly understand and be able to explain the
material. Copying entire material or large parts of such material from the Internet will be considered
academic dishonesty. Moss will be used to detect plagiarism in programming assignments. You need
ensure that your code and documentation are protected and not accessible to other students. Use chmod
700 command to change the permissions of your working directories before you start working on the
assignments. If you have any questions about whether an act of collaboration may be treated as academic
dishonesty, please consult the instructor before you collaborate.