CSc 4340/6340, Introduction to Compilers
Spring 2012
PROJECT - Phase I

Phase I: (Due: 12 February, 2012 - Sunday)
Electronic submission under phase1

Using JFlex and JCup tools, build the lexical analyser and parser for the DatalogQ Query language. Wrap the parser within an interactive program that behaves as the screen capture shown below. The query listing should be generated from the data structure that is built within the parser (and not simply a printing of the query file - the listing should be indented as shown in the output). Please override the toString() method for the various data structure classes - to make it easy to print the query rules.

[raj@tinman phase1]$ more q1
answer(F,M,L) :- 
  employee(F,M,L,S,_,_,_,_,_,5),
  works_on(S,P,H),
  projects('ProductX',P,_,_),
  H >= 10.
$
[raj@tinman phase1]$ more q2
aperson(A) :- actor(T,A).
r(A,D) :- actor(T,A), director(T,D).
answer(D) :- r([*]:aperson(*),D).
$
[raj@tinman phase1]$ more q3
aperson(A) :- actor(T,A).
r(A,D) :- actor(T,A), director(T,D).
answer(D) :- r([*,*,*]:aperson(*,*,*,X),D).
$
[raj@tinman phase1]$ more q4
answer(A1,A2) :- actor([*]:actor(*,A2),A1), actor([*]:actor(*,A1),A2), A1 < A2.
$
[raj@tinman phase1]$ java DLOG db
type "help;" for usage...
Message: Database Provided: Database Directory is ./db
DLOG> @q1;
----------INPUT QUERY----------------
answer(F,M,L) :- 
  employee(F,M,L,S,_,_,_,_,_,5),
  works_on(S,P,H),
  projects('ProductX',P,_,_),
  H >= 10.
------------------------------------
DLOG> @q2;
----------INPUT QUERY----------------
aperson(A) :- 
  actor(T,A).
r(A,D) :- 
  actor(T,A),
  director(T,D).
answer(D) :- 
  r([*]:aperson(*),D).
------------------------------------
DLOG> @q3;
----------INPUT QUERY----------------
aperson(A) :- 
  actor(T,A).
r(A,D) :- 
  actor(T,A),
  director(T,D).
answer(D) :- 
  r([*,*,*]:aperson(*,*,*,X),D).
------------------------------------
DLOG> @q4;
----------INPUT QUERY----------------
answer(A1,A2) :- 
  actor([*]:actor(*,A2),A1),
  actor([*]:actor(*,A1),A2),
  A1 < A2.
------------------------------------
DLOG> exit;
Exiting...
[raj@tinman phase1]$ 

The program should also build a data structure to capture the essential information in a query. Here is my design of the data structure:

public class Program {
  private Vector<Rule> rules;
}

public class Rule {
  private Predicate headPredicate;
  private Vector<Predicate> bodyPredicates;
  private Vector<Predicate> regularBodyPredicates;
  private Vector<Predicate> comparisonBodyPredicates;
}

public class Predicate {
  private String predName;
  private Vector<Argument> arguments;
  private boolean isNegated;
  private boolean isComparison;
  private Argument leftOperand;
  private String comparisonOperator;
  private Argument rightOperand;
}

public class Argument {
  private boolean isConstant = false; // true if constant argument
  private boolean isUnderscore = false; // true if _ argument
  private String argDataType; // data type for argument : "NUMBER" or "STRING"
  private String argName; // name of argument - if variable or if "*" or "#"
  private String argValue; // value of argument in case of constant argument
  private boolean isComplex; // true for complex arguments
  private Predicate complexPredicate; // stores predicate after : in complex argument; may be null
  private int numberOfStarsOrHashes; // Store the number of stars or hashes in complex argument
}

The following will replace the above class for Datalog (to be used by undergraduates):

public class Argument {
  private boolean isConstant = false; // true if constant argument
  private boolean isUnderscore = false; // true if _ argument
  private String argDataType; // data type for argument : "NUMBER" or "STRING"
  private String argName; // name of argument if variable
  private String argValue; // value of argument in case of constant argument
}