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;
73 import edu.uci.eecs.specCompiler.specExtraction.Construct;
74 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
75 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
76 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
77 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
78 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
79 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
81 public class SpecParser {
82 public static void main(String[] argvs)
83 throws ParseException, TokenMgrError {
85 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
86 SpecParser parser = new SpecParser(fis);
88 System.out.println("Parsing finished!");
89 } catch (FileNotFoundException e) {
94 PARSER_END(SpecParser)
108 // "#" comment for the specification
109 <"#" (~["\n", "\r"])* (["\n", "\r"])>
111 // "//" comment for the specification
112 <"//" (~["\n", "\r"])* (["\n", "\r"])>
117 /* Above are specification-only tokens */
126 <GLOBAL_DEFINE: "@Global_define:">
128 <INTERFACE_CLUSTER: "@Interface_cluster:">
130 <HAPPENS_BEFORE: "@Happens_before:">
132 <INTERFACE: "@Interface:">
134 <COMMIT_POINT_SET: "@Commit_point_set:">
136 <CONDITION: "@Condition:">
138 <HB_CONDITION: "@HB_condition:">
146 <POST_ACTION: "@Post_action:">
148 <POST_CHECK: "@Post_check:">
150 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
154 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
156 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
158 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
161 /* Specification & C/C++ shared tokens */
165 <#LETTER: ["a"-"z", "A"-"Z"]>
167 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
184 /* C/C++ only token*/
213 <GREATER_EQUALS: ">=">
217 <LOGICAL_EQUALS: "==">
237 ((~["\"","\\","\n","\r"])
239 ( ["n","t","b","r","f","\\","'","\""]
240 | ["0"-"7"] ( ["0"-"7"] )?
241 | ["0"-"3"] ["0"-"7"]
250 ((~["'","\\","\n","\r"])
252 (["n","t","b","r","f","\\","'","\""]
253 | ["0"-"7"] ( ["0"-"7"] )?
254 | ["0"-"3"] ["0"-"7"]
262 <DECIMAL_LITERAL> (["l","L"])?
263 | <HEX_LITERAL> (["l","L"])?
264 | <OCTAL_LITERAL> (["l","L"])?>
266 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
268 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
270 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
272 < FLOATING_POINT_LITERAL:
273 <DECIMAL_FLOATING_POINT_LITERAL>
274 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
276 < #DECIMAL_FLOATING_POINT_LITERAL:
277 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
278 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
279 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
280 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
282 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
284 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
285 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
286 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
288 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
297 LOOKAHEAD(3) res = Global_construct() |
298 LOOKAHEAD(3) res = Interface() |
299 LOOKAHEAD(3) res = Potential_commit_point_define() |
300 LOOKAHEAD(3) res = Commit_point_define() |
301 LOOKAHEAD(3) res = Commit_point_define_check()
307 GlobalConstruct Global_construct() :
316 (code = Global_define())
317 { res = new GlobalConstruct(code); }
318 (Interface_clusters(res))?
319 (Happens_before(res))?
323 res.unfoldInterfaceCluster();
324 System.out.println(res);
329 String C_CPP_CODE() :
336 text = new StringBuilder();
340 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
341 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
342 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
343 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
344 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
345 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR>
346 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
347 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
348 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
351 text.append(t.image);
352 if (t.image.equals(";") || t.image.equals("\\")
353 || t.image.equals("{") || t.image.equals("}"))
360 //System.out.println(text);
361 return text.toString();
365 String Global_define() :
370 <GLOBAL_DEFINE> (code = C_CPP_CODE())
376 ConditionalInterface Conditional_interface() :
378 String interfaceName, hbConditionLabel;
382 hbConditionLabel = "";
384 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
385 <IDENTIFIER>.image <CLOSE_BRACKET>)?
387 return new ConditionalInterface(interfaceName, hbConditionLabel);
391 void Interface_cluster(GlobalConstruct inst) :
394 ConditionalInterface condInterface;
397 (clusterName= <IDENTIFIER>.image)
398 <EQUALS> <OPEN_PAREN>
399 (condInterface = Conditional_interface()
400 { inst.addInterface2Cluster(clusterName, condInterface); }
402 (<COMMA> condInterface = Conditional_interface()
403 { inst.addInterface2Cluster(clusterName, condInterface); }
408 void Interface_clusters(GlobalConstruct inst) :
411 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
414 void Happens_before(GlobalConstruct inst) :
416 ConditionalInterface left, right;
421 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
422 { inst.addHBCondition(left, right); }
426 InterfaceConstruct Interface() :
428 InterfaceConstruct res;
434 <INTERFACE> <IDENTIFIER>
435 <COMMIT_POINT_SET> <IDENTIFIER> (<OR> <IDENTIFIER>)*
436 (<CONDITION> C_CPP_CODE())?
437 (<HB_CONDITION> <IDENTIFIER> C_CPP_CODE())*
439 (<CHECK> C_CPP_CODE())?
440 (<ACTION> C_CPP_CODE())?
441 (<POST_ACTION> C_CPP_CODE())?
442 (<POST_CHECK> C_CPP_CODE())?
448 PotentialCPDefineConstruct Potential_commit_point_define() :
450 PotentialCPDefineConstruct res;
457 <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
465 CPDefineConstruct Commit_point_define() :
467 CPDefineConstruct res;
474 <COMMIT_POINT_DEFINE> C_CPP_CODE()
475 <POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
483 CPDefineCheckConstruct Commit_point_define_check() :
485 CPDefineCheckConstruct res;
492 <COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()