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.
21 b) Interface construct
25 IDENTIFIER | IDENTIFIER ...
26 @Condition: ... (Optional)
28 IDENTIFIER :: <C_CPP_Condition>
30 @ID: ... (Optional, use default ID)
34 @DefineVar: Type var1 = SomeExpression (Optional)
37 @Post_action: (Optional)
38 @Post_check: (Optional)
41 c) Potential commit construct
43 @Potential_commit_point_define: ...
47 d) Commit point define construct
49 @Commit_point_define_check: ...
56 @Commit_point_define: ...
57 @Potential_commit_point_label: ...
66 JAVA_UNICODE_ESCAPE = true;
69 PARSER_BEGIN(SpecParser)
70 package edu.uci.eecs.specCompiler.grammerParser;
72 import java.io.FileInputStream;
73 import java.io.FileNotFoundException;
74 import java.io.InputStream;
75 import java.io.ByteArrayInputStream;
76 import java.util.ArrayList;
77 import java.util.HashMap;
78 import java.util.HashSet;
80 import edu.uci.eecs.specCompiler.specExtraction.Construct;
81 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
82 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
83 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
84 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
85 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
86 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
87 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
88 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
90 public class SpecParser {
91 public static void main(String[] argvs)
92 throws ParseException, TokenMgrError {
94 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
95 SpecParser parser = new SpecParser(fis);
97 System.out.println("Parsing finished!");
98 } catch (FileNotFoundException e) {
103 public static Construct parseSpec(String text)
104 throws ParseException, TokenMgrError {
105 InputStream input = new ByteArrayInputStream(text.getBytes());
106 SpecParser parser = new SpecParser(input);
107 return parser.Parse();
112 PARSER_END(SpecParser)
126 // "#" comment for the specification
127 <"#" (~["\n", "\r"])* (["\n", "\r"])>
129 // "//" comment for the specification
130 <"//" (~["\n", "\r"])* (["\n", "\r"])>
135 /* Above are specification-only tokens */
144 <GLOBAL_DEFINE: "@Global_define:">
146 <INTERFACE_CLUSTER: "@Interface_cluster:">
148 <HAPPENS_BEFORE: "@Happens_before:">
150 <INTERFACE: "@Interface:">
152 <COMMIT_POINT_SET: "@Commit_point_set:">
154 <CONDITION: "@Condition:">
156 <HB_CONDITION: "@HB_condition:">
164 <DEFINEVAR: "@DefineVar:">
168 <POST_ACTION: "@Post_action:">
170 <POST_CHECK: "@Post_check:">
172 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
176 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
178 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
180 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
183 /* Specification & C/C++ shared tokens */
187 <#LETTER: ["a"-"z", "A"-"Z"]>
189 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
206 /* C/C++ only token*/
237 <GREATER_EQUALS: ">=">
241 <LOGICAL_EQUALS: "==">
261 ((~["\"","\\","\n","\r"])
263 ( ["n","t","b","r","f","\\","'","\""]
264 | ["0"-"7"] ( ["0"-"7"] )?
265 | ["0"-"3"] ["0"-"7"]
274 ((~["'","\\","\n","\r"])
276 (["n","t","b","r","f","\\","'","\""]
277 | ["0"-"7"] ( ["0"-"7"] )?
278 | ["0"-"3"] ["0"-"7"]
286 <DECIMAL_LITERAL> (["l","L"])?
287 | <HEX_LITERAL> (["l","L"])?
288 | <OCTAL_LITERAL> (["l","L"])?>
290 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
292 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
294 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
296 < FLOATING_POINT_LITERAL:
297 <DECIMAL_FLOATING_POINT_LITERAL>
298 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
300 < #DECIMAL_FLOATING_POINT_LITERAL:
301 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
302 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
303 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
304 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
306 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
308 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
309 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
310 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
312 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
321 LOOKAHEAD(3) res = Global_construct() |
322 LOOKAHEAD(3) res = Interface() |
323 LOOKAHEAD(3) res = Potential_commit_point_define() |
324 LOOKAHEAD(3) res = Commit_point_define() |
325 LOOKAHEAD(3) res = Commit_point_define_check()
329 //System.out.println(res);
334 GlobalConstruct Global_construct() :
343 (code = Global_define())
344 { res = new GlobalConstruct(code); }
345 (Interface_clusters(res))?
346 (Happens_before(res))?
350 res.unfoldInterfaceCluster();
355 String C_CPP_CODE() :
362 text = new StringBuilder();
368 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
369 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
370 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
371 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
372 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
373 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
374 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
375 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
376 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
379 text.append(t.image);
380 if (t.image.equals(";") || t.image.equals("\\")
381 || t.image.equals("{") || t.image.equals("}"))
388 //System.out.println(text);
389 return text.toString();
393 String Global_define() :
398 <GLOBAL_DEFINE> (code = C_CPP_CODE())
404 ConditionalInterface Conditional_interface() :
406 String interfaceName, hbConditionLabel;
410 hbConditionLabel = "";
412 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
413 <IDENTIFIER>.image <CLOSE_BRACKET>)?
415 return new ConditionalInterface(interfaceName, hbConditionLabel);
419 void Interface_cluster(GlobalConstruct inst) :
422 ConditionalInterface condInterface;
425 (clusterName= <IDENTIFIER>.image)
426 <EQUALS> <OPEN_PAREN>
427 (condInterface = Conditional_interface()
428 { inst.addInterface2Cluster(clusterName, condInterface); }
430 (<COMMA> condInterface = Conditional_interface()
431 { inst.addInterface2Cluster(clusterName, condInterface); }
436 void Interface_clusters(GlobalConstruct inst) :
439 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
442 void Happens_before(GlobalConstruct inst) :
444 ConditionalInterface left, right;
449 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
450 { inst.addHBCondition(left, right); }
454 InterfaceConstruct Interface() :
456 InterfaceConstruct res;
457 String interfaceName, condition, idCode, check, postAction,
458 postCheck, commitPoint, hbLabel, hbCondition;
459 ActionSubConstruct action;
460 ArrayList<String> commitPointSet;
461 HashMap<String, String> hbConditions;
472 commitPointSet = new ArrayList<String>();
473 hbConditions = new HashMap<String, String>();
477 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
479 (commitPoint = <IDENTIFIER>.image
480 { commitPointSet.add(commitPoint); }
483 (commitPoint = <IDENTIFIER>.image)
485 if (commitPointSet.contains(commitPoint)) {
486 throw new ParseException(interfaceName + " has" +
487 "duplicate commit point labels");
489 commitPointSet.add(commitPoint);
493 (<CONDITION> (condition = C_CPP_CODE()))?
496 (hbLabel = <IDENTIFIER>.image)
497 (hbCondition = C_CPP_CODE())
499 if (hbConditions.containsKey(hbLabel)) {
500 throw new ParseException(interfaceName + " has" +
501 "duplicate happens-before condtion labels");
503 hbConditions.put(hbLabel, hbCondition);
506 (<ID> (idCode = C_CPP_CODE()))?
507 (<CHECK> (check = C_CPP_CODE()))?
509 (<POST_ACTION> (postAction = C_CPP_CODE()))?
510 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
514 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
515 hbConditions, idCode, check, action, postAction, postCheck);
520 ActionSubConstruct Action() :
522 String type, name, expr, defineVarStr, code;
523 ArrayList<DefineVar> defineVars;
527 defineVars = new ArrayList<DefineVar>();
533 (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
535 int eqIdx = defineVarStr.indexOf('=');
536 int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
537 type = defineVarStr.substring(0, typeEnd);
538 name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
539 expr = defineVarStr.substring(eqIdx + 2);
540 DefineVar defineVar = new DefineVar(type, name, expr);
541 defineVars.add(defineVar);
542 })* (<CODE> (code = C_CPP_CODE()))? )
546 ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
551 PotentialCPDefineConstruct Potential_commit_point_define() :
553 PotentialCPDefineConstruct res;
554 String label, condition;
561 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
562 <LABEL> (label = <IDENTIFIER>.image)
566 res = new PotentialCPDefineConstruct(label, condition);
572 CPDefineConstruct Commit_point_define() :
574 CPDefineConstruct res;
575 String label, potentialCPLabel, condition;
582 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
583 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
584 <LABEL> (label = <IDENTIFIER>.image)
588 res = new CPDefineConstruct(label, potentialCPLabel, condition);
594 CPDefineCheckConstruct Commit_point_define_check() :
596 CPDefineCheckConstruct res;
597 String label, condition;
604 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
605 <LABEL> (label = <IDENTIFIER>.image)
609 res = new CPDefineCheckConstruct(label, condition);