1 /* spec-compiler.jj Grammer definition for the specification */
6 Each construct should be embraced by /DOUBLE_STAR ... STAR/ annotation.
7 Within there, any line beginning with a "#" is a comment of the annotation.
8 Each constrcut should begin with @Begin and end with @End. Otherwise, the
9 annotation would be considered as normal comments of the source.
24 b) Interface construct
28 IDENTIFIER | IDENTIFIER ...
29 @Condition: ... (Optional)
31 IDENTIFIER :: <C_CPP_Condition>
33 @ID: ... (Optional, use default ID)
37 @DefineVar: Type var1 = SomeExpression (Optional)
40 @Post_action: (Optional)
41 @Post_check: (Optional)
44 c) Potential commit construct
46 @Potential_commit_point_define: ...
50 d) Commit point define construct
52 @Commit_point_define_check: ...
59 @Commit_point_define: ...
60 @Potential_commit_point_label: ...
69 JAVA_UNICODE_ESCAPE = true;
72 PARSER_BEGIN(SpecParser)
73 package edu.uci.eecs.specCompiler.grammerParser;
75 import java.io.FileInputStream;
76 import java.io.FileNotFoundException;
77 import java.io.InputStream;
78 import java.io.ByteArrayInputStream;
79 import java.util.ArrayList;
80 import java.util.HashMap;
81 import java.util.HashSet;
83 import edu.uci.eecs.specCompiler.specExtraction.Construct;
84 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
85 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
86 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
87 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
88 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
89 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
90 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
91 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
92 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
94 public class SpecParser {
95 public static void main(String[] argvs)
96 throws ParseException, TokenMgrError {
98 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
99 SpecParser parser = new SpecParser(fis);
101 System.out.println("Parsing finished!");
102 } catch (FileNotFoundException e) {
107 public static Construct parseSpec(String text)
108 throws ParseException, TokenMgrError {
109 InputStream input = new ByteArrayInputStream(text.getBytes());
110 SpecParser parser = new SpecParser(input);
111 return parser.Parse();
116 PARSER_END(SpecParser)
130 // "#" comment for the specification
131 <"#" (~["\n", "\r"])* (["\n", "\r"])>
133 // "//" comment for the specification
134 <"//" (~["\n", "\r"])* (["\n", "\r"])>
139 /* Above are specification-only tokens */
148 <GLOBAL_DEFINE: "@Global_define:">
150 <DECLARE_VAR: "@DeclareVar:">
152 <INIT_VAR: "@InitVar:">
154 <DEFINE_FUNC: "@DefineFunc:">
156 <INTERFACE_CLUSTER: "@Interface_cluster:">
158 <HAPPENS_BEFORE: "@Happens_before:">
160 <INTERFACE: "@Interface:">
162 <COMMIT_POINT_SET: "@Commit_point_set:">
164 <CONDITION: "@Condition:">
166 <HB_CONDITION: "@HB_condition:">
174 <DEFINEVAR: "@DefineVar:">
178 <POST_ACTION: "@Post_action:">
180 <POST_CHECK: "@Post_check:">
182 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
186 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
188 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
190 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
193 /* Specification & C/C++ shared tokens */
197 <#LETTER: ["a"-"z", "A"-"Z"]>
199 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
216 /* C/C++ only token*/
247 <GREATER_EQUALS: ">=">
251 <LOGICAL_EQUALS: "==">
271 ((~["\"","\\","\n","\r"])
273 ( ["n","t","b","r","f","\\","'","\""]
274 | ["0"-"7"] ( ["0"-"7"] )?
275 | ["0"-"3"] ["0"-"7"]
284 ((~["'","\\","\n","\r"])
286 (["n","t","b","r","f","\\","'","\""]
287 | ["0"-"7"] ( ["0"-"7"] )?
288 | ["0"-"3"] ["0"-"7"]
296 <DECIMAL_LITERAL> (["l","L"])?
297 | <HEX_LITERAL> (["l","L"])?
298 | <OCTAL_LITERAL> (["l","L"])?>
300 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
302 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
304 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
306 < FLOATING_POINT_LITERAL:
307 <DECIMAL_FLOATING_POINT_LITERAL>
308 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
310 < #DECIMAL_FLOATING_POINT_LITERAL:
311 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
312 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
313 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
314 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
316 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
318 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
319 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
320 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
322 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
331 LOOKAHEAD(3) res = Global_construct() |
332 LOOKAHEAD(3) res = Interface() |
333 LOOKAHEAD(3) res = Potential_commit_point_define() |
334 LOOKAHEAD(3) res = Commit_point_define() |
335 LOOKAHEAD(3) res = Commit_point_define_check()
339 //System.out.println(res);
344 GlobalConstruct Global_construct() :
347 SequentialDefineSubConstruct code;
353 (code = Global_define())
354 { res = new GlobalConstruct(code); }
355 (Interface_clusters(res))?
356 (Happens_before(res))?
360 res.unfoldInterfaceCluster();
365 String C_CPP_CODE() :
372 text = new StringBuilder();
378 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
379 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
380 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
381 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
382 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
383 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
384 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
385 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
386 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
389 text.append(t.image);
390 if (t.image.equals(";") || t.image.equals("\\")
391 || t.image.equals("{") || t.image.equals("}"))
398 //System.out.println(text);
399 return text.toString();
403 SequentialDefineSubConstruct Global_define() :
405 String declareVar, initVar, defineFunc;
414 (<DECLARE_VAR> (declareVar = C_CPP_CODE()))?
415 (<INIT_VAR> (initVar = C_CPP_CODE()))?
416 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE()))?
418 SequentialDefineSubConstruct res = new SequentialDefineSubConstruct(declareVar, initVar, defineFunc);
419 //System.out.println(res);
424 ConditionalInterface Conditional_interface() :
426 String interfaceName, hbConditionLabel;
430 hbConditionLabel = "";
432 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
433 <IDENTIFIER>.image <CLOSE_BRACKET>)?
435 return new ConditionalInterface(interfaceName, hbConditionLabel);
439 void Interface_cluster(GlobalConstruct inst) :
442 ConditionalInterface condInterface;
445 (clusterName= <IDENTIFIER>.image)
446 <EQUALS> <OPEN_PAREN>
447 (condInterface = Conditional_interface()
448 { inst.addInterface2Cluster(clusterName, condInterface); }
450 (<COMMA> condInterface = Conditional_interface()
451 { inst.addInterface2Cluster(clusterName, condInterface); }
456 void Interface_clusters(GlobalConstruct inst) :
459 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
462 void Happens_before(GlobalConstruct inst) :
464 ConditionalInterface left, right;
469 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
470 { inst.addHBCondition(left, right); }
474 InterfaceConstruct Interface() :
476 InterfaceConstruct res;
477 String interfaceName, condition, idCode, check, postAction,
478 postCheck, commitPoint, hbLabel, hbCondition;
479 ActionSubConstruct action;
480 ArrayList<String> commitPointSet;
481 HashMap<String, String> hbConditions;
492 commitPointSet = new ArrayList<String>();
493 hbConditions = new HashMap<String, String>();
497 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
499 (commitPoint = <IDENTIFIER>.image
500 { commitPointSet.add(commitPoint); }
503 (commitPoint = <IDENTIFIER>.image)
505 if (commitPointSet.contains(commitPoint)) {
506 throw new ParseException(interfaceName + " has" +
507 "duplicate commit point labels");
509 commitPointSet.add(commitPoint);
513 (<CONDITION> (condition = C_CPP_CODE()))?
516 (hbLabel = <IDENTIFIER>.image)
517 (hbCondition = C_CPP_CODE())
519 if (hbConditions.containsKey(hbLabel)) {
520 throw new ParseException(interfaceName + " has" +
521 "duplicate happens-before condtion labels");
523 hbConditions.put(hbLabel, hbCondition);
526 (<ID> (idCode = C_CPP_CODE()))?
527 (<CHECK> (check = C_CPP_CODE()))?
529 (<POST_ACTION> (postAction = C_CPP_CODE()))?
530 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
534 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
535 hbConditions, idCode, check, action, postAction, postCheck);
540 ActionSubConstruct Action() :
542 String type, name, expr, defineVarStr, code;
543 ArrayList<DefineVar> defineVars;
547 defineVars = new ArrayList<DefineVar>();
553 (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
555 int eqIdx = defineVarStr.indexOf('=');
556 int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
557 type = defineVarStr.substring(0, typeEnd);
558 name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
559 expr = defineVarStr.substring(eqIdx + 2);
560 DefineVar defineVar = new DefineVar(type, name, expr);
561 defineVars.add(defineVar);
562 })* (<CODE> (code = C_CPP_CODE()))? )
566 ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
571 PotentialCPDefineConstruct Potential_commit_point_define() :
573 PotentialCPDefineConstruct res;
574 String label, condition;
581 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
582 <LABEL> (label = <IDENTIFIER>.image)
586 res = new PotentialCPDefineConstruct(label, condition);
592 CPDefineConstruct Commit_point_define() :
594 CPDefineConstruct res;
595 String label, potentialCPLabel, condition;
602 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
603 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
604 <LABEL> (label = <IDENTIFIER>.image)
608 res = new CPDefineConstruct(label, potentialCPLabel, condition);
614 CPDefineCheckConstruct Commit_point_define_check() :
616 CPDefineCheckConstruct res;
617 String label, condition;
624 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
625 <LABEL> (label = <IDENTIFIER>.image)
629 res = new CPDefineCheckConstruct(label, condition);