Question: Write a Python program using PLY to compute a Relational Algebra expression corresponding to an atomic formula in Datalog. Here are some sample atomic formulas
Write a Python program using PLY to compute a Relational Algebra expression corresponding to an atomic formula in Datalog. Here are some sample atomic formulas from Datalog:
employeejonesxy"john",xyxxz pxxxxx pxxy"john",xy studentxyz
As can be seen, an atomic formula in Datalog begins with a NAME token followed by a left parenthesis LPAREN followed by a list or arguments and ends with a right parenthesis RPAREN The list of arguments is COMMA separated and each of argument can be either a NUMBER constant assume positive integers or a STRING constant, or a NAME corresponding to a variable.
A sample run of the final program on a Terminal is shown below user input is shown in RED:
macbookpro:dlog raj$ python DLOG.py DLG: employeejonesxy"john",xyxxz renamexyzprojectxxxselectx"jones" and x and x"john" and xx and xx and xx and xxrenamexxxxxxxxxxemployee DLG: pxxxxx renamexprojectxselectxx and xx and xx and xxrenamexxxxxp DLG: pxxy"john",xy renamexyprojectxxselectx and x"john" and xx and xx and xxrenamexxxxxxxp DLG: studentxyz renamexyzstudent DLG: exit
Some notes:
STEP : Identify all tokens and define the lexical specifications in DLOGLexer.py This has already been done for you above!
STEP : Write a grammar specification for the Datalog atom. Then, write the Parser specification in DLOGParser.py
STEP : Write the main program DLOGpy that prompts the user, reads a Datalog atom from the keyboard, calls the parser which returns a data structure containing the details of the atom. While debugging, you may print this data structure to verify that the lexical analysis and parsing steps work.
STEP : Finally, write the code in DLOG.py to process the data structure and generate the Relational Algebra string. Print this string to terminal.
The conversion from Datalog atom to Relational Algebra string works as follows described in an insideout manner, with pxxy"john",xy as an example:
First produce a list of variables, x xn where n is the number of arguments in the Datalog atom and generate the following string:
renamexxxxxxxp
Next, generate the select conditions; There are two types of select conditions: one that correspond to the numeric or string constants in the Datalog atom and the other that correspond to repeated variable arguments in the Datalog atom. Combine all of the conditions with "and" and generate the following string:
selectx and x"john" and xx and xx and xxrenamexxxxxxxp
As you can see, there are two conditions that correspond to the constant arguments of the atom, two conditions to ensure that the instances of variable x are equated and one condition to ensure that the instances of the variable y are equated. Notice that this string builds on the string generated in the previous step.Next, generate the project list; this list consists of the unique variables in the Datalog atom in this case the unique variables are x and y; Generate the following string, again building on the previous string:
projectxxselectx and x"john" and xx and xx and xxrenamexxxxxxxp
In the final step, produce the rename list of unique variables from the original names of variables and generate the final string:
renamexyprojectxxselectx and x"john" and xx and xx and xxrenamexxxxxxxp
Notice that in the final example, studentxyz there are no constant arguments and no repeated variables resulting in an empty select condition. In this case produce only the final rename part as shown in the sample run. This is a special case and can be handled at the end.
Submit the following files: DLOG.py DLOGLexer.py and DLOGParser.py
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
