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)
35 @Post_action: (Optional)
36 @Post_check: (Optional)
39 c) Potential commit construct
41 @Potential_commit_point_define: ...
45 d) Commit point define construct
47 @Commit_point_define_check: ...
54 @Commit_point_define: ...
55 @Potential_commit_point_label: ...
64 JAVA_UNICODE_ESCAPE = true;
67 PARSER_BEGIN(SpecParser)
68 package edu.uci.eecs.specCompiler.grammerParser;
70 import java.io.FileInputStream;
71 import java.io.FileNotFoundException;
72 import java.util.ArrayList;
73 import java.util.HashMap;
74 import java.util.HashSet;
76 import edu.uci.eecs.specCompiler.specExtraction.Construct;
77 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
78 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
79 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
80 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
81 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
82 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
84 public class SpecParser {
85 public static void main(String[] argvs)
86 throws ParseException, TokenMgrError {
88 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
89 SpecParser parser = new SpecParser(fis);
91 System.out.println("Parsing finished!");
92 } catch (FileNotFoundException e) {
97 PARSER_END(SpecParser)
111 // "#" comment for the specification
112 <"#" (~["\n", "\r"])* (["\n", "\r"])>
114 // "//" comment for the specification
115 <"//" (~["\n", "\r"])* (["\n", "\r"])>
120 /* Above are specification-only tokens */
129 <GLOBAL_DEFINE: "@Global_define:">
131 <INTERFACE_CLUSTER: "@Interface_cluster:">
133 <HAPPENS_BEFORE: "@Happens_before:">
135 <INTERFACE: "@Interface:">
137 <COMMIT_POINT_SET: "@Commit_point_set:">
139 <CONDITION: "@Condition:">
141 <HB_CONDITION: "@HB_condition:">
149 <POST_ACTION: "@Post_action:">
151 <POST_CHECK: "@Post_check:">
153 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
157 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
159 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
161 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
164 /* Specification & C/C++ shared tokens */
168 <#LETTER: ["a"-"z", "A"-"Z"]>
170 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
187 /* C/C++ only token*/
216 <GREATER_EQUALS: ">=">
220 <LOGICAL_EQUALS: "==">
240 ((~["\"","\\","\n","\r"])
242 ( ["n","t","b","r","f","\\","'","\""]
243 | ["0"-"7"] ( ["0"-"7"] )?
244 | ["0"-"3"] ["0"-"7"]
253 ((~["'","\\","\n","\r"])
255 (["n","t","b","r","f","\\","'","\""]
256 | ["0"-"7"] ( ["0"-"7"] )?
257 | ["0"-"3"] ["0"-"7"]
265 <DECIMAL_LITERAL> (["l","L"])?
266 | <HEX_LITERAL> (["l","L"])?
267 | <OCTAL_LITERAL> (["l","L"])?>
269 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
271 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
273 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
275 < FLOATING_POINT_LITERAL:
276 <DECIMAL_FLOATING_POINT_LITERAL>
277 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
279 < #DECIMAL_FLOATING_POINT_LITERAL:
280 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
281 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
282 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
283 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
285 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
287 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
288 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
289 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
291 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
300 LOOKAHEAD(3) res = Global_construct() |
301 LOOKAHEAD(3) res = Interface() |
302 LOOKAHEAD(3) res = Potential_commit_point_define() |
303 LOOKAHEAD(3) res = Commit_point_define() |
304 LOOKAHEAD(3) res = Commit_point_define_check()
308 System.out.println(res);
313 GlobalConstruct Global_construct() :
322 (code = Global_define())
323 { res = new GlobalConstruct(code); }
324 (Interface_clusters(res))?
325 (Happens_before(res))?
329 res.unfoldInterfaceCluster();
334 String C_CPP_CODE() :
341 text = new StringBuilder();
345 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
346 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
347 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
348 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
349 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
350 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR>
351 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
352 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
353 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
356 text.append(t.image);
357 if (t.image.equals(";") || t.image.equals("\\")
358 || t.image.equals("{") || t.image.equals("}"))
365 //System.out.println(text);
366 return text.toString();
370 String Global_define() :
375 <GLOBAL_DEFINE> (code = C_CPP_CODE())
381 ConditionalInterface Conditional_interface() :
383 String interfaceName, hbConditionLabel;
387 hbConditionLabel = "";
389 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
390 <IDENTIFIER>.image <CLOSE_BRACKET>)?
392 return new ConditionalInterface(interfaceName, hbConditionLabel);
396 void Interface_cluster(GlobalConstruct inst) :
399 ConditionalInterface condInterface;
402 (clusterName= <IDENTIFIER>.image)
403 <EQUALS> <OPEN_PAREN>
404 (condInterface = Conditional_interface()
405 { inst.addInterface2Cluster(clusterName, condInterface); }
407 (<COMMA> condInterface = Conditional_interface()
408 { inst.addInterface2Cluster(clusterName, condInterface); }
413 void Interface_clusters(GlobalConstruct inst) :
416 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
419 void Happens_before(GlobalConstruct inst) :
421 ConditionalInterface left, right;
426 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
427 { inst.addHBCondition(left, right); }
431 InterfaceConstruct Interface() :
433 InterfaceConstruct res;
434 String interfaceName, condition, idCode, check, action, postAction,
435 postCheck, commitPoint, hbLabel, hbCondition;
436 ArrayList<String> commitPointSet;
437 HashMap<String, String> hbConditions;
448 commitPointSet = new ArrayList<String>();
449 hbConditions = new HashMap<String, String>();
453 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
455 (commitPoint = <IDENTIFIER>.image
456 { commitPointSet.add(commitPoint); }
459 (commitPoint = <IDENTIFIER>.image)
461 if (commitPointSet.contains(commitPoint)) {
462 throw new ParseException(interfaceName + " has" +
463 "duplicate commit point labels");
465 commitPointSet.add(commitPoint);
469 (<CONDITION> (condition = C_CPP_CODE()))?
472 (hbLabel = <IDENTIFIER>.image)
473 (hbCondition = C_CPP_CODE())
475 if (hbConditions.containsKey(hbLabel)) {
476 throw new ParseException(interfaceName + " has" +
477 "duplicate happens-before condtion labels");
479 hbConditions.put(hbLabel, hbCondition);
482 (<ID> (idCode = C_CPP_CODE()))?
483 (<CHECK> (check = C_CPP_CODE()))?
484 (<ACTION> (action = C_CPP_CODE()))?
485 (<POST_ACTION> (postAction = C_CPP_CODE()))?
486 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
490 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
491 hbConditions, idCode, check, action, postAction, postCheck);
496 PotentialCPDefineConstruct Potential_commit_point_define() :
498 PotentialCPDefineConstruct res;
499 String label, condition;
506 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
507 <LABEL> (label = <IDENTIFIER>.image)
511 res = new PotentialCPDefineConstruct(label, condition);
517 CPDefineConstruct Commit_point_define() :
519 CPDefineConstruct res;
520 String label, potentialCPLabel, condition;
527 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
528 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
529 <LABEL> (label = <IDENTIFIER>.image)
533 res = new CPDefineConstruct(label, potentialCPLabel, condition);
539 CPDefineCheckConstruct Commit_point_define_check() :
541 CPDefineCheckConstruct res;
542 String label, condition;
549 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
550 <LABEL> (label = <IDENTIFIER>.image)
554 res = new CPDefineCheckConstruct(label, condition);