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 {
< #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() :
}
)+
{
- 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>
(<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()
<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; }
}
--- /dev/null
+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 + ")";
+ }
+}
--- /dev/null
+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();
+ }
+}