From: Peizhao Ou <peizhaoo@uci.edu>
Date: Fri, 11 Oct 2013 07:44:27 +0000 (-0700)
Subject: add more, GlobalConstruct parsed
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=944b6af217212532a88e314df52a4cf5a99ed36a;p=cdsspec-compiler.git

add more, GlobalConstruct parsed
---

diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj
index 4a7d11a..a006e7b 100644
--- a/grammer/spec-compiler.jj
+++ b/grammer/spec-compiler.jj
@@ -70,7 +70,15 @@ package edu.uci.eecs.specCompiler.grammerParser;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 
-	class SpecParser {
+import edu.uci.eecs.specCompiler.specExtraction.Construct;
+import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
+
+	public class SpecParser {
 		public static void main(String[] argvs)
 		throws ParseException, TokenMgrError {
 			try {
@@ -280,28 +288,42 @@ TOKEN :
 	< #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
 }
 
-void Start() :
-{}
+Construct Start() :
+{
+	Construct res;	
+}
 {
 	(
-	LOOKAHEAD(3) Global_construct() |
-	LOOKAHEAD(3) Interface() |
-	LOOKAHEAD(3) Potential_commit_point_define() |
-	LOOKAHEAD(3) Commit_point_define() |
-	LOOKAHEAD(3) Commit_point_define_check()
+	LOOKAHEAD(3) res = Global_construct() |
+	LOOKAHEAD(3) res = Interface() |
+	LOOKAHEAD(3) res = Potential_commit_point_define() |
+	LOOKAHEAD(3) res = Commit_point_define() |
+	LOOKAHEAD(3) res = Commit_point_define_check()
 	)
 	<EOF>
+	{ return res; }
 }
 
-void Global_construct() :
+GlobalConstruct Global_construct() :
 {
+	GlobalConstruct res;
+	String code;
 }
 {
+	{ res = null; }
 	<HEAD>
 		<BEGIN> 
-			Global_define() (Interface_clusters())? (Happens_before())?
+			(code = Global_define())
+			{ res = new GlobalConstruct(code); }
+			(Interface_clusters(res))?
+			(Happens_before(res))?
 		<END>
 	<TAIL>
+	{
+		res.unfoldInterfaceCluster();
+		System.out.println(res);
+		return res;
+	}
 }
 
 String C_CPP_CODE() :
@@ -335,46 +357,78 @@ String C_CPP_CODE() :
 	}
 	)+
 	{
-		System.out.println(text);
+		//System.out.println(text);
 		return text.toString();
 	}
 }
 
-void Global_define() :
-{}
+String Global_define() :
+{
+	String code;	
+}
 {
-	<GLOBAL_DEFINE> C_CPP_CODE()
+	<GLOBAL_DEFINE> (code = C_CPP_CODE())
+	{
+		return code;
+	}
 }
 
-void Conditional_interface() :
-{}
+ConditionalInterface Conditional_interface() :
+{
+	String interfaceName, hbConditionLabel;
+}
 {
-	<IDENTIFIER> (<OPEN_BRACKET> <IDENTIFIER> <CLOSE_BRACKET>)*
+	{
+		hbConditionLabel = "";
+	}
+	interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
+	<IDENTIFIER>.image <CLOSE_BRACKET>)?
+	{
+		return new ConditionalInterface(interfaceName, hbConditionLabel);
+	}
 }
 
-void Interface_cluster() :
-{}
+void Interface_cluster(GlobalConstruct inst) :
+{
+	String clusterName;
+	ConditionalInterface condInterface;
+}
 {
-	<IDENTIFIER> <EQUALS> <OPEN_PAREN>
-		Conditional_interface() (<COMMA> Conditional_interface())*
+	(clusterName= <IDENTIFIER>.image)
+	<EQUALS> <OPEN_PAREN>
+		(condInterface = Conditional_interface()
+		{ inst.addInterface2Cluster(clusterName, condInterface); } 
+		)
+		(<COMMA> condInterface = Conditional_interface()
+		{ inst.addInterface2Cluster(clusterName, condInterface); } 
+		)*
 	<CLOSE_PAREN>
 }
 
-void Interface_clusters() :
+void Interface_clusters(GlobalConstruct inst) :
 {}
 {
-	<INTERFACE_CLUSTER> (Interface_cluster())+
+	<INTERFACE_CLUSTER> (Interface_cluster(inst))+
 }
 
-void Happens_before() :
-{}
+void Happens_before(GlobalConstruct inst) :
+{
+	ConditionalInterface left, right;	
+}
 {
-	<HAPPENS_BEFORE> (Conditional_interface() <HB_SYMBOL> Conditional_interface())+
+	<HAPPENS_BEFORE> 
+	(
+	left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
+	{ inst.addHBCondition(left, right); }
+	)+
 }
 
-void Interface() :
-{}
+InterfaceConstruct Interface() :
+{
+	InterfaceConstruct res;	
+}
 {
+	{ res = null; }
 	<HEAD> 
 		<BEGIN>
 			<INTERFACE> <IDENTIFIER>
@@ -388,23 +442,33 @@ void Interface() :
 			(<POST_CHECK> C_CPP_CODE())?
 		<END>
 	<TAIL>
+	{ return res; }
 }
 
-void Potential_commit_point_define() :
-{}
+PotentialCPDefineConstruct Potential_commit_point_define() :
 {
+	PotentialCPDefineConstruct res;	
+}
+{
+
+	{ res = null; }
 	<HEAD> 
 		<BEGIN>
 			<POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
 			<LABEL> <IDENTIFIER>
 		<END>
 	<TAIL>
+	{ return res; }
 }
 
 
-void Commit_point_define() :
-{}
+CPDefineConstruct Commit_point_define() :
+{
+	CPDefineConstruct res;	
+}
 {
+
+	{ res = null; }
 	<HEAD> 
 		<BEGIN>
 			<COMMIT_POINT_DEFINE> C_CPP_CODE()
@@ -412,16 +476,22 @@ void Commit_point_define() :
 			<LABEL> <IDENTIFIER>
 		<END>
 	<TAIL>
+	{ return res; }
 }
 
 
-void Commit_point_define_check() :
-{}
+CPDefineCheckConstruct Commit_point_define_check() :
 {
+	CPDefineCheckConstruct res;	
+}
+{
+
+	{ res = null; }
 	<HEAD> 
 		<BEGIN> 
 			<COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
 			<LABEL> <IDENTIFIER>
 		<END>
 	<TAIL>
+	{ return res; }
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java
new file mode 100644
index 0000000..9edd764
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java
@@ -0,0 +1,5 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+public class CPDefineCheckConstruct extends Construct {
+
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java
new file mode 100644
index 0000000..e351dc9
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java
@@ -0,0 +1,5 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+public class CPDefineConstruct extends Construct {
+
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ConditionalInterface.java b/src/edu/uci/eecs/specCompiler/specExtraction/ConditionalInterface.java
new file mode 100644
index 0000000..791a205
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/ConditionalInterface.java
@@ -0,0 +1,30 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+public class ConditionalInterface {
+	public final String interfaceName;
+	public final String hbConditionLabel;
+	
+	public ConditionalInterface(String interfaceName, String hbConditionLabel) {
+		this.interfaceName = interfaceName;
+		this.hbConditionLabel = hbConditionLabel;
+	}
+	
+	public boolean equals(ConditionalInterface other) {
+		if (!(other instanceof ConditionalInterface))
+			return false;
+		ConditionalInterface another = (ConditionalInterface) other;
+		return another.interfaceName.equals(interfaceName) && 
+				another.hbConditionLabel.equals(hbConditionLabel);
+	}
+	
+	public int hashCode() {
+		return interfaceName.hashCode() << 5 ^ hbConditionLabel.hashCode();
+	}
+	
+	public String toString() {
+		if (hbConditionLabel.equals(""))
+			return interfaceName + "(true)";
+		else
+			return interfaceName + "(" + hbConditionLabel + ")";
+	}
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/Construct.java b/src/edu/uci/eecs/specCompiler/specExtraction/Construct.java
new file mode 100644
index 0000000..4aa4fcf
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/Construct.java
@@ -0,0 +1,11 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+/**
+ * <p>
+ * This is just an abstract class for all the constructs.
+ * </p>
+ * @author peizhaoo
+ *
+ */
+abstract public class Construct {
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java
new file mode 100644
index 0000000..88c2bfb
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java
@@ -0,0 +1,83 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+import java.util.HashMap;
+import java.util.HashSet;
+
+public class GlobalConstruct extends Construct {
+	public final String code;
+	private final HashMap<String, HashSet<ConditionalInterface>> interfaceCluster;
+	private final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> originalHBRelations;
+	public final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbRelations;
+	
+	public GlobalConstruct(String code) {
+		this.code = code;
+		this.interfaceCluster = new HashMap<String, HashSet<ConditionalInterface>>();
+		this.originalHBRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
+		this.hbRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
+	}
+	
+	public void addInterface2Cluster(String clusterName, ConditionalInterface condInterface) {
+		if (!interfaceCluster.containsKey(clusterName)) {
+			interfaceCluster.put(clusterName, new HashSet<ConditionalInterface>());
+		}
+		HashSet<ConditionalInterface> set = interfaceCluster.get(clusterName);
+		set.add(condInterface);
+	}
+	
+	public void addHBCondition(ConditionalInterface left, ConditionalInterface right) {
+		if (!originalHBRelations.containsKey(left)) {
+			originalHBRelations.put(left, new HashSet<ConditionalInterface>());
+		}
+		HashSet<ConditionalInterface> set = originalHBRelations.get(left);
+		set.add(right);
+	}
+	
+	private void addUnfoldedHBCondition(ConditionalInterface left, ConditionalInterface right) {
+		if (!hbRelations.containsKey(left)) {
+			hbRelations.put(left, new HashSet<ConditionalInterface>());
+		}
+		HashSet<ConditionalInterface> set = hbRelations.get(left);
+		set.add(right);
+	}
+	
+	private HashSet<ConditionalInterface> getByName(ConditionalInterface condInterface) {
+		if (interfaceCluster.containsKey(condInterface.interfaceName))
+			return interfaceCluster.get(condInterface.interfaceName);
+		HashSet<ConditionalInterface> res = new HashSet<ConditionalInterface>();
+		res.add(condInterface);
+		return res;
+	}
+	
+	public void unfoldInterfaceCluster() {
+		for (ConditionalInterface left : originalHBRelations.keySet()) {
+			HashSet<ConditionalInterface> rights = originalHBRelations.get(left);
+			for (ConditionalInterface right : rights) {
+				HashSet<ConditionalInterface> leftCondInterfaces = getByName(left),
+						rightCondInterfaces = getByName(right);
+				for (ConditionalInterface l : leftCondInterfaces) {
+					for (ConditionalInterface r : rightCondInterfaces) {
+						addUnfoldedHBCondition(l, r);
+					}
+				}
+			}
+		}
+	}
+	
+	public String toString() {
+		StringBuilder sb = new StringBuilder();
+		sb.append("Code:\n");
+		sb.append(code);
+		
+		sb.append("Happens_before:\n");
+		for (ConditionalInterface left : hbRelations.keySet()) {
+			HashSet<ConditionalInterface> rights = hbRelations.get(left);
+			sb.append(left + " -> ");
+			for (ConditionalInterface right : rights) {
+				sb.append(right + " | ");
+			}
+			sb.append(".\n");
+		}
+		
+		return sb.toString();
+	}
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java
new file mode 100644
index 0000000..e18bc39
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java
@@ -0,0 +1,5 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+public class InterfaceConstruct extends Construct {
+
+}
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java
new file mode 100644
index 0000000..946697e
--- /dev/null
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java
@@ -0,0 +1,5 @@
+package edu.uci.eecs.specCompiler.specExtraction;
+
+public class PotentialCPDefineConstruct extends Construct {
+
+}