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.util.ArrayList;
75 import java.util.HashMap;
76 import java.util.HashSet;
78 import edu.uci.eecs.specCompiler.specExtraction.Construct;
79 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
80 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
81 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
82 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
83 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
84 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
85 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
86 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
88 public class SpecParser {
89 public static void main(String[] argvs)
90 throws ParseException, TokenMgrError {
92 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
93 SpecParser parser = new SpecParser(fis);
95 System.out.println("Parsing finished!");
96 } catch (FileNotFoundException e) {
101 PARSER_END(SpecParser)
115 // "#" comment for the specification
116 <"#" (~["\n", "\r"])* (["\n", "\r"])>
118 // "//" comment for the specification
119 <"//" (~["\n", "\r"])* (["\n", "\r"])>
124 /* Above are specification-only tokens */
133 <GLOBAL_DEFINE: "@Global_define:">
135 <INTERFACE_CLUSTER: "@Interface_cluster:">
137 <HAPPENS_BEFORE: "@Happens_before:">
139 <INTERFACE: "@Interface:">
141 <COMMIT_POINT_SET: "@Commit_point_set:">
143 <CONDITION: "@Condition:">
145 <HB_CONDITION: "@HB_condition:">
153 <DEFINEVAR: "@DefineVar:">
157 <POST_ACTION: "@Post_action:">
159 <POST_CHECK: "@Post_check:">
161 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
165 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
167 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
169 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
172 /* Specification & C/C++ shared tokens */
176 <#LETTER: ["a"-"z", "A"-"Z"]>
178 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
195 /* C/C++ only token*/
224 <GREATER_EQUALS: ">=">
228 <LOGICAL_EQUALS: "==">
248 ((~["\"","\\","\n","\r"])
250 ( ["n","t","b","r","f","\\","'","\""]
251 | ["0"-"7"] ( ["0"-"7"] )?
252 | ["0"-"3"] ["0"-"7"]
261 ((~["'","\\","\n","\r"])
263 (["n","t","b","r","f","\\","'","\""]
264 | ["0"-"7"] ( ["0"-"7"] )?
265 | ["0"-"3"] ["0"-"7"]
273 <DECIMAL_LITERAL> (["l","L"])?
274 | <HEX_LITERAL> (["l","L"])?
275 | <OCTAL_LITERAL> (["l","L"])?>
277 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
279 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
281 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
283 < FLOATING_POINT_LITERAL:
284 <DECIMAL_FLOATING_POINT_LITERAL>
285 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
287 < #DECIMAL_FLOATING_POINT_LITERAL:
288 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
289 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
290 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
291 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
293 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
295 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
296 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
297 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
299 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
308 LOOKAHEAD(3) res = Global_construct() |
309 LOOKAHEAD(3) res = Interface() |
310 LOOKAHEAD(3) res = Potential_commit_point_define() |
311 LOOKAHEAD(3) res = Commit_point_define() |
312 LOOKAHEAD(3) res = Commit_point_define_check()
316 System.out.println(res);
321 GlobalConstruct Global_construct() :
330 (code = Global_define())
331 { res = new GlobalConstruct(code); }
332 (Interface_clusters(res))?
333 (Happens_before(res))?
337 res.unfoldInterfaceCluster();
342 String C_CPP_CODE() :
349 text = new StringBuilder();
355 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
356 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
357 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
358 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
359 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
360 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
361 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
362 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
363 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
366 text.append(t.image);
367 if (t.image.equals(";") || t.image.equals("\\")
368 || t.image.equals("{") || t.image.equals("}"))
375 //System.out.println(text);
376 return text.toString();
380 String Global_define() :
385 <GLOBAL_DEFINE> (code = C_CPP_CODE())
391 ConditionalInterface Conditional_interface() :
393 String interfaceName, hbConditionLabel;
397 hbConditionLabel = "";
399 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
400 <IDENTIFIER>.image <CLOSE_BRACKET>)?
402 return new ConditionalInterface(interfaceName, hbConditionLabel);
406 void Interface_cluster(GlobalConstruct inst) :
409 ConditionalInterface condInterface;
412 (clusterName= <IDENTIFIER>.image)
413 <EQUALS> <OPEN_PAREN>
414 (condInterface = Conditional_interface()
415 { inst.addInterface2Cluster(clusterName, condInterface); }
417 (<COMMA> condInterface = Conditional_interface()
418 { inst.addInterface2Cluster(clusterName, condInterface); }
423 void Interface_clusters(GlobalConstruct inst) :
426 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
429 void Happens_before(GlobalConstruct inst) :
431 ConditionalInterface left, right;
436 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
437 { inst.addHBCondition(left, right); }
441 InterfaceConstruct Interface() :
443 InterfaceConstruct res;
444 String interfaceName, condition, idCode, check, postAction,
445 postCheck, commitPoint, hbLabel, hbCondition;
446 ActionSubConstruct action;
447 ArrayList<String> commitPointSet;
448 HashMap<String, String> hbConditions;
459 commitPointSet = new ArrayList<String>();
460 hbConditions = new HashMap<String, String>();
464 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
466 (commitPoint = <IDENTIFIER>.image
467 { commitPointSet.add(commitPoint); }
470 (commitPoint = <IDENTIFIER>.image)
472 if (commitPointSet.contains(commitPoint)) {
473 throw new ParseException(interfaceName + " has" +
474 "duplicate commit point labels");
476 commitPointSet.add(commitPoint);
480 (<CONDITION> (condition = C_CPP_CODE()))?
483 (hbLabel = <IDENTIFIER>.image)
484 (hbCondition = C_CPP_CODE())
486 if (hbConditions.containsKey(hbLabel)) {
487 throw new ParseException(interfaceName + " has" +
488 "duplicate happens-before condtion labels");
490 hbConditions.put(hbLabel, hbCondition);
493 (<ID> (idCode = C_CPP_CODE()))?
494 (<CHECK> (check = C_CPP_CODE()))?
496 (<POST_ACTION> (postAction = C_CPP_CODE()))?
497 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
501 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
502 hbConditions, idCode, check, action, postAction, postCheck);
507 ActionSubConstruct Action() :
509 String type, name, expr, defineVarStr, code;
510 ArrayList<DefineVar> defineVars;
514 defineVars = new ArrayList<DefineVar>();
517 (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
519 int eqIdx = defineVarStr.indexOf('=');
520 int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
521 type = defineVarStr.substring(0, typeEnd);
522 name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
523 expr = defineVarStr.substring(eqIdx + 2);
524 DefineVar defineVar = new DefineVar(type, name, expr);
525 defineVars.add(defineVar);
528 <CODE> (code = C_CPP_CODE())
530 ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
535 PotentialCPDefineConstruct Potential_commit_point_define() :
537 PotentialCPDefineConstruct res;
538 String label, condition;
545 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
546 <LABEL> (label = <IDENTIFIER>.image)
550 res = new PotentialCPDefineConstruct(label, condition);
556 CPDefineConstruct Commit_point_define() :
558 CPDefineConstruct res;
559 String label, potentialCPLabel, condition;
566 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
567 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
568 <LABEL> (label = <IDENTIFIER>.image)
572 res = new CPDefineConstruct(label, potentialCPLabel, condition);
578 CPDefineCheckConstruct Commit_point_define_check() :
580 CPDefineCheckConstruct res;
581 String label, condition;
588 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
589 <LABEL> (label = <IDENTIFIER>.image)
593 res = new CPDefineCheckConstruct(label, condition);