Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help with this ciding project!! In this project, you will write a lex yacc program that will convert a python code to c++code.

i need help with this ciding project!! image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
In this project, you will write a lex yacc program that will convert a python code to c++code. We do not want you to handle all the possible python codes. The input python codes can contain only assignment and if/else statements. Your project is to convert the given assignment and if/else statements into c++ language. The project contains syntax and semantic analysis. First try to code the syntax analysis part, test your code, then try to write the semantic analysis part. 1. Syntax Analysis Part In the syntax analysis, you should write lex and yacc code that will recognize and accept the given python code. 1.1. Assignment statement - The code will contain several assignment states. An assignment state has a form of: VAR = Operand Operator Operand Operator ... - VAR is a variable. Operand can be a variable name, integer, float or string value. Numbers can be positive or negative. Operators can be "+", "-", "/" and "*" - There can be infinite number of Operand and Operator - Example X=a+5+cc1214/X 1.2. If/else statement - The input code can contain if, if/else or if/elif/else statements. Also it can contain nested if statements (elif and else included) - The body of if/else statements can contain several assignment or if/else statements. - The body of the if/else states should start 1 tab inside. For each nested if, there must be an extra tab in the beginning. If it is not clear look at the example. - The comparison part in the if/elif can have only comparison. Thus, the input code will not contain "and" or "or" in the comparison. - Comparison can be "==", "!=", "" or ">=". - You can see an example in figure 1. Ifa>b:a=1elifa5b=a 2.2. If/else inconsistency - For each elif and else statement there must be an unclosed if before the statement. - An if-statement is accepted as closed when a statement with same indentation level comes except else or elif come statements. - Print an error message "if/else consistency in line x where x is the corresponding line. - To be able to catch the if/else inconsistency, you need to find when an if is closed or not - Examples with errors else: d=2 ifif==3c=3 d=2 \# in this line if is closed so the next else does not have an if else: c=2 Ifa==3:c=3else:c=2elif:c=1 2.3. Type inconsistency - You have already implemented type inconsistency (mismatch) in prelab 3. You will do the same thing in your project. - In prelab3, you have assumed that a variable will not have more than 1 type. But as you know, python uses dynamic type binding and the type of the variable can change through the program flow. - In the first example below, you can see the type of A is string initially. Then it is changed to integer in the next line. So the addition operation is valid in the example 1. On contrary, in example 2, the type of A is integer initially. And it is changed to string in the next line. So your program should give an error message for example 2. - Also In addition to the prelab 3, you should check the consistency in the comparison part. You cannot compare a string with an integer or a float. If the input code contains such a comparison, your program should detect it. - Print an error message "type inconsistency in line x where x is the corresponding line. - There is a case you should not consider in the project. Let's assume that we have code like below. In this case we do not know whether the program goes into if state or else state. Thus, we cannot decide the type of the variable A with running the program. Thus, you can assume that the type of a variable will not change in the if/eliflelse statements. So in your project do not try to solve cases like this. 3. Desired Output format 3.1. Type Declaration - In assignment 3, you have already put type declaration of variables in the head of the program. In the project you will upgrade it. - In python, a variable can have multiple types in the flow of the program since it uses dynamic type binding. Thus, a variable can be integer in the beginning, but it can be string in the end. - In c++, the type of the variable cannot change in the flow of the program. So somehow we have to solve this problem while converting a python code into ct+. - In project, a variable can be int, float or string. So you should keep the record of the types that a variable uses. Then, convert a variable into a typed-variable in c++ output. - For example, if a variable x is used as int, float and string in the input code, output should contain xint, xfloat and xstr. But if it is used only as int and string, then the output should only contain xint and xstr. - Each variable should be converted into a typed variable in the output and all typed-variables should be declared in the beginning of the main. - Order of the declaration should be in: int, float and string as it is seen in the example below. 3.2. Indentation and \{\} - Your code should be surrounded by the main function. - Each line should start with an extra tab. - Each if, else if and else body should start with "?" with the correct indentation level. - Each if, else if and else body should end with "?" with the correct indentation level. 4. Testing Your Code and Grading Policy You are given a set of valid input files and corresponding output files. Also, you are given a set of invalid input files that you should detect the inconsistency. Your project will be evaluated according to two points; syntax analysis and semantic analysis: - First, your program should have a grammar that will recognise the input. - Second, the correctness of your output for the given input file. Also, a part of your project grade will be given from a few large input files that contain all the cases. Those input files will not be provided to you. Thus, you should write a generic program not just for the given examples. 4.1 Valid input files - You should be able to create outputs that are exactly the same with the provided one. - You do not need to write onto a file. Use redirection if you wish. - The input files will not be large and each of them will contain only one of the cases in the project. - If your output file is exactly the same as the provided one, you will get a full grade. - So do not try to do everything at the same time. - Finish one part, test your code and if you are sure it is okay save this version. Then, pass to the next part. - We will compare the output files with diff command in terminal: - diff -s output1.txt outputStudent.bxt - If two files are same, you will see on the screen: - Files output1.txt and outputStudent.txt are identical - If not, you will see which lines are different. So, if we cannot see the *... are identical" message you will not get a grade from that input file 4.2 Invalid input files - If a file contains invalid code, then you should print only an error message. - The error message should be clear about the type of the error. - Do not just print "error" on the screen. 4.3 File format - In the project you will be dealing with a lot of whitespace. Some whitespace characters can vary on different operating systems like newline. - Use dos2unix command to be sure that all the files are in the same format. You can install it: sudo apt install dos2unix And use it: dos2unix input1.txt 4.5 Report Write a report that briefly explains in which input files you are successful in terms of syntax and semantic analysis. 5 SUBMISSION The files you should submit - Your lex file - Your yacc file - Your Makefile Your makefile should work correctly. And the first line of the Makefile should contain: all: lex yacc g++ lex.yy.c y.tab.c -II -o project It is essential to name your executable as "project" as it is seen above

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions

Question

What stereotypes are associated with lesbians and gay men?

Answered: 1 week ago

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago