From 809787d83de53a464887c48b0a083d4903e0734e Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Mon, 14 Oct 2013 16:19:07 -0700 Subject: [PATCH] add more parsing --- grammer/spec-compiler.jj | 53 ++++++++++++++++--- .../specExtraction/ActionSubConstruct.java | 43 +++++++++++++++ .../specExtraction/InterfaceConstruct.java | 5 +- .../specExtraction/SpecExtractor.java | 16 +++++- 4 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj index ca38b5f..85aabff 100644 --- a/grammer/spec-compiler.jj +++ b/grammer/spec-compiler.jj @@ -31,6 +31,8 @@ @Check: (Optional) ... @Action: (Optional) + @DefineVar: Type var1 = SomeExpression (Optional) + @Code (Optional) ... @Post_action: (Optional) @Post_check: (Optional) @@ -80,6 +82,8 @@ 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; +import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar; public class SpecParser { public static void main(String[] argvs) @@ -87,7 +91,7 @@ import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; try { FileInputStream fis = new FileInputStream("./grammer/spec.txt"); SpecParser parser = new SpecParser(fis); - parser.Start(); + parser.Parse(); System.out.println("Parsing finished!"); } catch (FileNotFoundException e) { e.printStackTrace(); @@ -145,6 +149,10 @@ TOKEN : | +| + +| + | | @@ -291,7 +299,7 @@ TOKEN : < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ > } -Construct Start() : +Construct Parse() : { Construct res; } @@ -341,13 +349,15 @@ String C_CPP_CODE() : text = new StringBuilder(); t = new Token(); } - (( + ( + //LOOKAHEAD(2) + ( t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | - t = | t = | t = | t = | t = + t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = | t = @@ -431,18 +441,19 @@ void Happens_before(GlobalConstruct inst) : InterfaceConstruct Interface() : { InterfaceConstruct res; - String interfaceName, condition, idCode, check, action, postAction, + String interfaceName, condition, idCode, check, postAction, postCheck, commitPoint, hbLabel, hbCondition; + ActionSubConstruct action; ArrayList commitPointSet; HashMap hbConditions; } { { res = null; + action = null; condition = ""; idCode = ""; check = ""; - action = ""; postAction = ""; postCheck = ""; commitPointSet = new ArrayList(); @@ -481,7 +492,7 @@ InterfaceConstruct Interface() : )* ( (idCode = C_CPP_CODE()))? ( (check = C_CPP_CODE()))? - ( (action = C_CPP_CODE()))? + (action = Action())? ( (postAction = C_CPP_CODE()))? ( (postCheck = C_CPP_CODE()))? @@ -493,6 +504,34 @@ InterfaceConstruct Interface() : } } +ActionSubConstruct Action() : +{ + String type, name, expr, defineVarStr, code; + ArrayList defineVars; +} +{ + { + defineVars = new ArrayList(); + } + + ( (defineVarStr = C_CPP_CODE()) + { + int eqIdx = defineVarStr.indexOf('='); + int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2); + type = defineVarStr.substring(0, typeEnd); + name = defineVarStr.substring(typeEnd + 1, eqIdx - 1); + expr = defineVarStr.substring(eqIdx + 2); + DefineVar defineVar = new DefineVar(type, name, expr); + defineVars.add(defineVar); + } + )* + (code = C_CPP_CODE()) + { + ActionSubConstruct res = new ActionSubConstruct(defineVars, code); + return res; + } +} + PotentialCPDefineConstruct Potential_commit_point_define() : { PotentialCPDefineConstruct res; diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java new file mode 100644 index 0000000..b8b8689 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java @@ -0,0 +1,43 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.util.ArrayList; + +public class ActionSubConstruct { + public static class DefineVar { + public final String varType; + public final String varName; + public final String varExpr; + + public DefineVar(String varType, String varName, String varExpr) { + this.varType = varType; + this.varName = varName; + this.varExpr = varExpr; + } + + public String toString() { + return varType + " " + varName + " = " + varExpr; + } + } + + public final ArrayList defineVars; + public final String code; + + public ActionSubConstruct(ArrayList defineVars, String code) { + this.code = code; + this.defineVars = defineVars; + } + + public void addDefineVar(DefineVar defineVar) { + defineVars.add(defineVar); + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("@Action:\n"); + for (DefineVar defineVar : defineVars) { + sb.append("\t@DefineVar: " + defineVar + "\n"); + } + sb.append("\t@Code: " + code); + return sb.toString(); + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java index c301c4e..6af7197 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java @@ -10,13 +10,13 @@ public class InterfaceConstruct extends Construct { public final HashMap hbConditions; public final String idCode; public final String check; - public final String action; + public final ActionSubConstruct action; public final String postAction; public final String postCheck; public InterfaceConstruct(String name, ArrayList commitPointSet, String condition, HashMap hbConditions, String idCode, - String check, String action, String postAction, String postCheck) { + String check, ActionSubConstruct action, String postAction, String postCheck) { this.name = name; this.commitPointSet = commitPointSet; this.condition = condition; @@ -46,7 +46,6 @@ public class InterfaceConstruct extends Construct { sb.append("@ID: "); sb.append(idCode + "\n"); sb.append("@Check: " + check + "\n"); - sb.append("@Action:\n"); sb.append(action + "\n"); sb.append("@Post_action:\n"); sb.append(postAction + "\n"); diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java index dc24b28..e1fae7e 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java @@ -61,6 +61,7 @@ public class SpecExtractor { if (trimedLine.endsWith("*/")) { _endLineNum = reader.getLineNumber(); foundHead = false; + //Constrcut inst = SpecParser System.out.println("Spec<" + specIndex + "> Begin: " + _beginLine + " End: " + _endLineNum); @@ -83,7 +84,7 @@ public class SpecExtractor { specText = new StringBuilder(); } } - } + } // At the end we can only find the head "/**" but no tail found if (foundHead) { String msg = "In file \"" + file.getAbsolutePath() + "\", line: " @@ -97,6 +98,19 @@ public class SpecExtractor { } } + private String readInterfaceDecl(LineNumberReader reader) throws IOException { + String res = "", curLine; + while ((curLine = reader.readLine()) != null) { + int braceIdx = curLine.indexOf(')'); + if (braceIdx == -1) { + res = res + " " + curLine; + } else { + res = res + curLine.substring(0, braceIdx + 1); + } + } + return res; + } + private String trimSpace(String line) { int i, j; char ch; -- 2.34.1