X-Git-Url: http://plrg.eecs.uci.edu/git/?p=cdsspec-compiler.git;a=blobdiff_plain;f=grammer%2Fspec-compiler.jj;h=2238268a43ac644a62e490703ea226c6d3060f2f;hp=bfcd6b99825b7c67e90facd45552b29ddae044a3;hb=570d67c87af73f80a04e584ffacf2c395c570450;hpb=b1e52c7b681a0bc2704509b63a8907b3004901b3 diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj index bfcd6b9..2238268 100644 --- a/grammer/spec-compiler.jj +++ b/grammer/spec-compiler.jj @@ -24,12 +24,15 @@ @Commit_point_set: IDENTIFIER | IDENTIFIER ... @Condition: ... (Optional) - @HB_Condition: ... + @HB_Condition: + IDENTIFIER :: @HB_Condition: ... @ID: ... (Optional, use default ID) @Check: (Optional) ... @Action: (Optional) + @DefineVar: Type var1 = SomeExpression (Optional) + @Code (Optional) ... @Post_action: (Optional) @Post_check: (Optional) @@ -68,19 +71,43 @@ package edu.uci.eecs.specCompiler.grammerParser; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +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; +import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar; - class SpecParser { + public class SpecParser { public static void main(String[] argvs) throws ParseException, TokenMgrError { 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(); } } + + public static Construct parseSpec(String text) + throws ParseException, TokenMgrError { + InputStream input = new ByteArrayInputStream(text.getBytes()); + SpecParser parser = new SpecParser(input); + return parser.Parse(); + } + + } PARSER_END(SpecParser) @@ -133,6 +160,10 @@ TOKEN : | +| + +| + | | @@ -178,6 +209,8 @@ TOKEN : | +| + | | @@ -200,10 +233,26 @@ TOKEN : | "> +| + ="> +| + +| + +| + | | +| + | | @@ -263,28 +312,44 @@ TOKEN : < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ > } -void Start() : -{} +Construct Parse() : +{ + 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() ) + { + //System.out.println(res); + return res; + } } -void Global_construct() : +GlobalConstruct Global_construct() : { + GlobalConstruct res; + String code; } { + { res = null; } - Global_define() (Interface_clusters())? (Happens_before())? + (code = Global_define()) + { res = new GlobalConstruct(code); } + (Interface_clusters(res))? + (Happens_before(res))? + { + res.unfoldInterfaceCluster(); + return res; + } } String C_CPP_CODE() : @@ -297,108 +362,251 @@ 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 = | t = | t = | t = | t = | t = | t = | t = ) { text.append(t.image); - text.append("\n"); + if (t.image.equals(";") || t.image.equals("\\") + || t.image.equals("{") || t.image.equals("}")) + text.append("\n"); + else + text.append(" "); } )+ { - System.out.println(text); + //System.out.println(text); return text.toString(); } } -void Global_define() : -{} +String Global_define() : { - C_CPP_CODE() + String code; +} +{ + (code = C_CPP_CODE()) + { + return code; + } } -void Conditional_interface() : -{} +ConditionalInterface Conditional_interface() : { - ( )* + String interfaceName, hbConditionLabel; +} +{ + { + hbConditionLabel = ""; + } + interfaceName = .image ( hbConditionLabel = + .image )? + { + return new ConditionalInterface(interfaceName, hbConditionLabel); + } } -void Interface_cluster() : -{} +void Interface_cluster(GlobalConstruct inst) : { - - Conditional_interface() ( Conditional_interface())* + String clusterName; + ConditionalInterface condInterface; +} +{ + (clusterName= .image) + + (condInterface = Conditional_interface() + { inst.addInterface2Cluster(clusterName, condInterface); } + ) + ( condInterface = Conditional_interface() + { inst.addInterface2Cluster(clusterName, condInterface); } + )* } -void Interface_clusters() : +void Interface_clusters(GlobalConstruct inst) : {} { - (Interface_cluster())+ + (Interface_cluster(inst))+ } -void Happens_before() : -{} +void Happens_before(GlobalConstruct inst) : +{ + ConditionalInterface left, right; +} { - (Conditional_interface() Conditional_interface())+ + + ( + left = Conditional_interface() right = Conditional_interface() + { inst.addHBCondition(left, right); } + )+ } -void Interface() : -{} +InterfaceConstruct Interface() : { + InterfaceConstruct res; + String interfaceName, condition, idCode, check, postAction, + postCheck, commitPoint, hbLabel, hbCondition; + ActionSubConstruct action; + ArrayList commitPointSet; + HashMap hbConditions; +} +{ + { + res = null; + action = null; + condition = ""; + idCode = ""; + check = ""; + postAction = ""; + postCheck = ""; + commitPointSet = new ArrayList(); + hbConditions = new HashMap(); + } - - ( )* - ( C_CPP_CODE())? - ( C_CPP_CODE())* - ( C_CPP_CODE())? - ( C_CPP_CODE())? - ( C_CPP_CODE())? - ( C_CPP_CODE())? - ( C_CPP_CODE())? + (interfaceName = .image) + + (commitPoint = .image + { commitPointSet.add(commitPoint); } + ) + ( + (commitPoint = .image) + { + if (commitPointSet.contains(commitPoint)) { + throw new ParseException(interfaceName + " has" + + "duplicate commit point labels"); + } + commitPointSet.add(commitPoint); + } + )* + + ( (condition = C_CPP_CODE()))? + ( + + (hbLabel = .image) + (hbCondition = C_CPP_CODE()) + { + if (hbConditions.containsKey(hbLabel)) { + throw new ParseException(interfaceName + " has" + + "duplicate happens-before condtion labels"); + } + hbConditions.put(hbLabel, hbCondition); + } + )* + ( (idCode = C_CPP_CODE()))? + ( (check = C_CPP_CODE()))? + (action = Action())? + ( (postAction = C_CPP_CODE()))? + ( (postCheck = C_CPP_CODE()))? + { + res = new InterfaceConstruct(interfaceName, commitPointSet, condition, + hbConditions, idCode, check, action, postAction, postCheck); + return res; + } } -void Potential_commit_point_define() : -{} +ActionSubConstruct Action() : { + String type, name, expr, defineVarStr, code; + ArrayList defineVars; +} +{ + { + defineVars = new ArrayList(); + code = ""; + } + + ( + ( + ( (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; + String label, condition; +} +{ + + { res = null; } - C_CPP_CODE() -