$29
• Introduction
In this homework you will implement a tool which includes a simple semantic analyzer for MailScript language. Detailed information about this programming language was given in the second homework document. You can check it for more information. The tool will rst check if a given MailScript program has any syntax error, grammatically. If there are no syntax errors, the tool will perform some semantic checks for simple cases and if these checks are passed, it will generate noti cations for certain statements. Read the rest of the document for more information.
• Parser and Scanner
The scanner and the parser which you can use to implement this homework is provided to you. The semantic analysis will require you to implement an attribute grammar. You can start from the scanner/parser les provided, or of course, you can write your own versions of scanner and parser from scratch.
• Semantic Rules
Your semantic analyzer should start by performing an analysis for the following seman-tic rules. For each violation of these rules, your semantic analyzer must print out an error message. Note that, after printing out an error message, your semantic analyzer must not terminate and keep working to nd further violations, if there exists any.
3.1 Undeclared Variables
A variable has to be set to a certain value before it can be accessed in a statement.
This is done by using the set keyword. An example set statement would be:
set toBeSent ("How are you?")
1
If a variable is being called before initialization, the program is supposed to generate an error along with the line information and the identi er. For example, for this program:
Mail from john.doe@mail.com:
send [Message] to [(user5@mail.com)]
send ["I have an early class."] to [(NameInfo, diane@mail.com)] end Mail
The following should be printed out:
ERROR at line 2: Message is undefined
ERROR at line 3: NameInfo is undefined
3.2 Printing Errors
Errors will be printed in the order they appear in the program. That means, the errors with the earliest line number will be printed rst. If two or more errors occur on the same line, the errors will also be printed in the order that they are seen (left to right). For the program below:
Mail from ben@mail.com:
send (Message) to [(User1, user1@x.com), (User2, user2@x.com)] send ("Hello") to [(jane@mail.com), (User3, user3@x.com)]
end Mail
The output should be:
ERROR at line 2: Message is undefined
ERROR at line 2: User1 is undefined
ERROR at line 2: User2 is undefined
ERROR at line 3: User3 is undefined
Please note that the line number given is of the line in which the undeclared variable appears.
• Noti cations
After your semantic analyzer performs the semantic checks mentioned in Section 3 and if no errors are found in the end, it will move onto the second phase of the assignment, which is generating noti cations. Please keep in mind that noti cations will
2
only be generated if the program contains no errors. Once an e-mail is sent or it is scheduled to be sent, a noti cation should be printed to specify the actions that have been taken. There are two types of noti cations: send noti cations and schedule noti cations.
4.1 Send Noti cation
Once an e-mail is sent, a noti cation that speci es the sender address, the message, and the recipient should be printed out. The format will be as given below:
E-mail sent from X to Y: "Message"
Some important points that should be taken into consideration are:
A new noti cation should be generated for each unique recipient in the recipient list.
The sender’s information is only limited to their e-mail address (stylized as X above). On the other hand, the recipient can also have their name information given along with their e-mail. If the recipient’s name is present in the recipient object, their name should be printed out instead of their e-mail address (stylized as Y). If only their e-mail address is present, then Y should be their e-mail address. For the code below:
Mail from nick@mail.com:
send ["You re welcome."] to [("Luisa", luisa@mail.com), (anne@mail.com)]
end Mail
The output should be:
E-mail sent from nick@mail.com to Luisa: "You re welcome."
E-mail sent from nick@mail.com to anne@mail.com: "You re welcome."
The recipient list can contain the same e-mail more than once. It can appear with di erent names or with no name provided at all. It is crucial that you print only ONE noti cation for a single e-mail address, no matter how many times it is repeated in the recipients list of a single send statement. In this case, the rst (leftmost) one of the recipient objects (that share an e-mail address) will be taken into consideration. Once again, if the rst recipient object contains a name, the name will be printed as Y. If the said recipient object does not contain a name, then the e-mail address will be printed as Y. For example:
Mail from kate@mail.com:
send ["Welcome."] to [(mike@mail.com), ("Mike", mike@mail.com)] send ["Hi."] to [("Joseph", joe@mail.com), ("Joe", joe@mail.com)]
end Mail
3
The output will be:
E-mail sent from kate@mail.com to mike@mail.com: "Welcome."
E-mail sent from kate@mail.com to Joseph: "Hi."
On the second line, even though a name for the same user is provided in the second recipient object (Mike), we will only take the rst recipient object into consideration. That is why mike@mail.com is printed, instead of Mike. On the third line, once again the rst recipient object is taken into consideration and the name Joseph is printed instead of Joe.
Please note that the message or the recipient’s name can be given as a variable and not a string. In this case, the variable’s value should be printed out, not the variable’s name. For example:
Mail from ian@mail.com:
set newMail ("Welcome.")
set newUser ("Courtney")
send [newMail] to [(newUser, courtney@mail.com)] end Mail
The output will be:
E-mail sent from ian@mail.com to Courtney: "Welcome."
A variable name could be set to di erent values throughout the program. Re-gardless of the location of these set statements (they could be in a mail block or out of a mail block), the variable’s value will be set by the last set statement that is seen before it is accessed. An example of this could be found in the program given in Section 5 (variable named "Outgoing").
4.2 Schedule Noti cation
Schedule noti cations are similar to send noti cations with one key di erence between them: schedule noti cations are supposed to be printed after the entire program has been executed. That means, all the send noti cations will be printed in the order they are given in the code. Then, after the program’s nished, the schedule noti cations will be printed in chronological order:
E-mails with the earliest date and time should be printed rst. You can assume that date and time objects will be given in correct format. When we are testing your code, invalid date and time objects (such as 35/18/2021, 58:92) will not be used.
If two or more e-mails are scheduled to be sent on the same date and time, the e-mail that appears rst in the code (the statement with the lowest line number), should be printed rst.
4
The scheduled date (stylized as DATE) will not be printed as it is. Instead, it will be converted to the Month Day, Year format. The time object will stay the same (stylized as TIME). The examples to this conversion is given below:
17/02/2021 -> February 17, 2021
06/08/1997 -> August 6, 1997
The format of a schedule noti cation is given below:
E-mail scheduled to be sent from X on DATE, TIME to Y: "Message"
For example, if the input is the code given below:
Mail from root@mail.com:
schedule @ [08/12/1365, 00:00]:
send ["1"] to [("A", a@mail.com)]
end schedule
schedule @ [08/12/1365, 12:05]:
send ["2"] to [("B", b@mail.com)]
end schedule
schedule @ [01/12/1365, 23:59]:
send ["3"] to [("C", b@mail.com)]
end schedule
end Mail
The output will be:
E-mail scheduled to be sent from root@mail.com on December 1, 1365, 23:59 to C: "3" E-mail scheduled to be sent from root@mail.com on December 8, 1365, 00:00 to A: "1" E-mail scheduled to be sent from root@mail.com on December 8, 1365 12:05 to B: "2"
4.3 Printing Noti cations
Some details about printing noti cations are clari ed further below:
1. Send statements will be printed in the order they are given. That means, the send noti cation for the statement with the earliest line number will be printed rst. If two or more send statements occur on the same line, the noti cations will be printed in the order that the recipients are seen (left to right). For the program given below:
5
Mail from root@mail.com:
send ["Hi."] to [("A", user1@mail.com), ("B", user2@mail.com)] send ["Bye."] to [("C", user3@mail.com), ("D", user4@mail.com)]
end Mail
The output should be:
E-mail sent from root@mail.com to A: "Hi."
E-mail sent from root@mail.com to B: "Hi."
E-mail sent from root@mail.com to C: "Bye."
E-mail sent from root@mail.com to D: "Bye."
2. A similar rule also applies to schedule noti cations. Two or more e-mails that are scheduled at the same date/time will be printed according to their line numbers. If they also share a line, the leftmost recipient’s noti cation will be printed rst. For the program given below:
Mail from root@mail.com: schedule @ [05/06/2021, 22:00]:
send ["Hi."] to [("A", user1@mail.com), ("B", user2@mail.com)] send ["Bye."] to [("C", user3@mail.com), ("D", user4@mail.com)]
end schedule end Mail
The output should be:
E-mail scheduled to be sent from root@mail.com on June 5, 2021, 22:00 to A: "Hi." E-mail scheduled to be sent from root@mail.com on June 5, 2021, 22:00 to B: "Hi." E-mail scheduled to be sent from root@mail.com on June 5, 2021, 22:00 to C: "Bye." E-mail scheduled to be sent from root@mail.com on June 5, 2021, 22:00 to D: "Bye."
• Example Programs and Outputs
1. If the program is not grammatically correct then like the second homework you have to print ERROR. For example, if we have the below program:
send ["This is an invalid program."] to [(cs305@mail.edu)]
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Then the output should be:
ERROR
2. If a program is grammatically correct but contains violations of the semantic rules then the output should display all the semantic errors. For example if we have the following program:
Mail from cs305@mail.com:
send ["Hello!"] to [("Daniel", daniel@mail.com), (username, mehmet@mail.com), (mehmet@mail.com)]
send ["Bye."] to [(gamze@mail.com)]
schedule @ [18/04/2022, 06:30]:
send [Message] to [("Beril", beril@mail.com.tr)] end schedule
end Mail
Mail from cs305@sabanciuniv.edu:
schedule @ [02/12/2021, 23:00]:
send ["Good morning!"] to [(ali@mail.com),
("Ferhat Yilmaz", ferhat@mail.com), ("Ali", ali@mail.com)] end schedule
send ["These are the files."] to [(user_45@mail.co.uk), (Name_2, user_45@mail.co.uk)]
end Mail
Then the output to the above program must be as follows:
ERROR at line 4: username is undefined
ERROR at line 9: Message is undefined
ERROR at line 23: Name_2 is undefined
3. If a program is grammatically correct and does not contain any violations of the semantic rules then the output should display the noti cations. For example if we have the following program:
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
set Outgoing ("This is a message.")
Mail from cs305@mail.com:
set Outgoing ("CS 305")
send ["Hello!"] to [("Daniel", daniel@mail.com), ("Ahmet", mehmet@mail.com), (mehmet@mail.com)]
schedule @ [02/12/2021, 12:00]:
send ["Good morning!"] to [(ali@mail.com),
("John Doe", john@mail.com), ("Ali", ali@mail.com)] end schedule
set newMessage ("Thank you.")
end Mail
Mail from cs305@mail.com:
set Name ("Omer")
schedule @ [28/11/2021, 04:00]:
send [Outgoing] to [(Name, omer@mail.com)] end schedule
send [newMessage] to [("u1", u1@x.com), (u2@x.com), (u1@x.com), (u1@x.com), (Name, u1@x.com)]
end Mail
Then the output to the above program must be as follows:
E-mail sent from cs305@mail.com to Daniel: "Hello!"
E-mail sent from cs305@mail.com to Ahmet: "Hello!"
E-mail sent from cs305@mail.com to u1: "Thank you." E-mail sent from cs305@mail.com to u2@x.com: "Thank you." E-mail is scheduled to be sent from cs305@mail.com on November 28, 2021, 04:00 to Omer: "CS 305"
E-mail is scheduled to be sent from cs305@mail.com on December 2, 2021, 12:00 to ali@mail.com: "Good morning." E-mail is scheduled to be sent from cs305@mail.com on December 2, 2021, 12:00 to John Doe: "Good morning."
8
• How to Submit
Submit your Bison le named as username-hw3.y, and ex le named as username-hw3.flx where username is your SU-Net username. You may use additional les, such as a header le. Please also upload those les.
We will compile your les by using the following commands:
flex username-hw3.flx
bison -d username-hw3.y
gcc -o username-hw3 lex.yy.c username-hw3.tab.c -lfl
So, make sure that these three commands are enough to produce the executable. If we assume that there is a MailScript le named test17.ms, we will try out your parser by using the following command line:
username-hw3 < test17.ms
• Notes
Important: Name your les as you are told and don’t zip them. [-10 points otherwise]
Important: Make sure you include the right le in your scanner and make sure you can compile your parser using the commands given in the Section 6. If we are not able to compile your code with those commands your grade will be zero for this homework.
Important: Since this homework is evaluated automatically make sure your output is exactly as it is supposed to be. Some of the points that we can think of are:
{ There should be no extra space at the beginning or at the end of a line.
{ There are exactly one space between each word in a line.
{ Make sure that the spellings are as it is given in the homework document.
{ We check in a case sensitive manner (e.g. "ERROR" 6= "error") { The format of the error messages and noti cations should be ex-
actly the same as it is supposed to be.
{ If you are not sure about your outputs you can compare your outputs with the outputs given by the golden.
9
No homework will be accepted if it is not submitted using SUCourse+.
You may get help from our TA or from your friends. However, you must im-plement the homework by yourself.
Start working on the homework immediately.
If you develop your code or create your test les on your own computer (not on ow.sabanciuniv.edu), there can be incompatibilities once you transfer them to ow.sabanciuniv.edu. Since the grading will be done automatically on the ow.sabanciuniv.edu, we strongly encourage you to do your development on ow.sabanciuniv.edu, or at least test your code on ow.sabanciuniv.edu before submitting it. If you prefer not to test your implementation on ow.sabanciuniv.edu, this means you accept to take the risks of incompatibility. Even if you may have spent hours on the homework, you can easily get 0 due to such incompatibilities.
LATE SUBMISSION POLICY:
Late submission is allowed subject to the following conditions:
{ Your homework grade will be decided by multiplying what you get from the test cases by a \submission time factor (STF)".
{ If you submit on time (i.e. before the deadline), your STF is 1. So, you don’t lose anything.
{ If you submit late, you will lose 0.01 of your STF for every 5 mins of delay. { We will not accept any homework later than 500 mins after the deadline. { SUCourse+’s timestamp will be used for STF computation.
{ If you submit multiple times, the last submission time will be used.
10