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.
14 # If LANG is not define, it's C++ by default. C does not support class
15 # and template, so if it's defined as C, we should also have a explicit
29 b) Interface construct
33 IDENTIFIER | IDENTIFIER ...
34 @Condition: ... (Optional)
36 IDENTIFIER :: <C_CPP_Condition>
38 @ID: ... (Optional, use default ID)
42 # Type here must be a pointer
43 @DefineVar: Type var1 = SomeExpression (Optional)
46 @Post_action: (Optional)
47 @Post_check: (Optional)
50 c) Potential commit construct
52 @Potential_commit_point_define: ...
56 d) Commit point define construct
58 @Commit_point_define_check: ...
65 @Commit_point_define: ...
66 @Potential_commit_point_label: ...
70 e) Entry point construct
75 f) Interface define construct
77 @Interface_define: <Interface_Name>
85 JAVA_UNICODE_ESCAPE = true;
88 PARSER_BEGIN(SpecParser)
89 package edu.uci.eecs.specCompiler.grammerParser;
91 import java.io.FileInputStream;
92 import java.io.FileNotFoundException;
93 import java.io.InputStream;
94 import java.io.ByteArrayInputStream;
95 import java.util.ArrayList;
96 import java.util.HashMap;
97 import java.util.HashSet;
99 import edu.uci.eecs.specCompiler.specExtraction.Construct;
100 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
101 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
102 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
103 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
104 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
105 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
106 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
107 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
108 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
109 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
110 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
112 public class SpecParser {
113 public static void main(String[] argvs)
114 throws ParseException, TokenMgrError {
116 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
117 SpecParser parser = new SpecParser(fis);
119 System.out.println("Parsing finished!");
120 ArrayList<String> typeParams = parser.FormalParamList();
121 } catch (FileNotFoundException e) {
126 public static Construct parseSpec(String text)
127 throws ParseException, TokenMgrError {
128 InputStream input = new ByteArrayInputStream(text.getBytes());
129 SpecParser parser = new SpecParser(input);
130 return parser.Parse();
135 PARSER_END(SpecParser)
139 < IN_COMMENT > SKIP : { < ~[] > }
141 < IN_COMMENT > SKIP : {
145 <IN_COMMENT> TOKEN : {
162 // "#" comment for the specification
163 <"#" (~["\n", "\r"])* (["\n", "\r"])>
165 // "//" comment for the specification
166 <"//" (~["\n", "\r"])* (["\n", "\r"])>
169 <IN_COMMENT, DEFAULT> TOKEN :
171 /* Above are specification-only tokens */
172 <HEAD: "/**" : IN_COMMENT>
174 <TAIL: "*/" : DEFAULT>
180 <OPTIONS: "@Options:">
182 <GLOBAL_DEFINE: "@Global_define:">
184 <DECLARE_VAR: "@DeclareVar:">
186 <INIT_VAR: "@InitVar:">
188 <DEFINE_FUNC: "@DefineFunc:">
190 <INTERFACE_CLUSTER: "@Interface_cluster:">
192 <HAPPENS_BEFORE: "@Happens_before:">
194 <INTERFACE: "@Interface:">
196 <COMMIT_POINT_SET: "@Commit_point_set:">
198 <ENTRY_POINT: "@Entry_point">
200 <INTERFACE_DEFINE: "@Interface_define:">
202 <CONDITION: "@Condition:">
204 <HB_CONDITION: "@HB_condition:">
212 <DEFINEVAR: "@DefineVar:">
216 <POST_ACTION: "@Post_action:">
218 <POST_CHECK: "@Post_check:">
220 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
224 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
226 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
228 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
231 /* Specification & C/C++ shared tokens */
239 <TYPENAME: "typename">
245 <#LETTER: ["a"-"z", "A"-"Z"]>
247 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
264 /* C/C++ only token*/
295 <GREATER_EQUALS: ">=">
299 <LOGICAL_EQUALS: "==">
319 ((~["\"","\\","\n","\r"])
321 ( ["n","t","b","r","f","\\","'","\""]
322 | ["0"-"7"] ( ["0"-"7"] )?
323 | ["0"-"3"] ["0"-"7"]
332 ((~["'","\\","\n","\r"])
334 (["n","t","b","r","f","\\","'","\""]
335 | ["0"-"7"] ( ["0"-"7"] )?
336 | ["0"-"3"] ["0"-"7"]
344 <DECIMAL_LITERAL> (["l","L"])?
345 | <HEX_LITERAL> (["l","L"])?
346 | <OCTAL_LITERAL> (["l","L"])?>
348 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
350 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
352 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
354 < FLOATING_POINT_LITERAL:
355 <DECIMAL_FLOATING_POINT_LITERAL>
356 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
358 < #DECIMAL_FLOATING_POINT_LITERAL:
359 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
360 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
361 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
362 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
364 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
366 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
367 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
368 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
370 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
383 (("struct" { type = type + " struct"; })?
384 (str = <IDENTIFIER>.image {
385 if (!type.equals(""))
386 type = type + " " + str;
390 ((str = "const".image {
391 if (!type.equals(""))
392 type = type + " " + str;
396 (str = <STAR>.image {
397 if (!type.equals(""))
398 type = type + " " + str;
403 if (!type.equals(""))
404 type = type + " " + str;
414 ArrayList<String> FormalParamList() :
416 ArrayList<String> typeParams;
420 typeParams = new ArrayList<String>();
422 (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
424 System.out.println(typeParams);
429 void TypeParam(ArrayList<String> typeParams) :
434 (type = Type()) (param = <IDENTIFIER>.image)
436 typeParams.add(type);
437 typeParams.add(param);
447 LOOKAHEAD(3) res = Global_construct() |
448 LOOKAHEAD(3) res = Interface() |
449 LOOKAHEAD(3) res = Potential_commit_point_define() |
450 LOOKAHEAD(3) res = Commit_point_define() |
451 LOOKAHEAD(3) res = Commit_point_define_check() |
452 LOOKAHEAD(3) res = Entry_point() |
453 LOOKAHEAD(3) res = Interface_define()
457 //System.out.println(res);
462 GlobalConstruct Global_construct() :
465 SequentialDefineSubConstruct code;
466 HashMap<String, String> options;
472 options = new HashMap<String, String>();
477 ((key = <IDENTIFIER>.image)
479 (value = <IDENTIFIER>.image)
481 if (options.containsKey(key)) {
482 throw new ParseException("Duplicate options!");
484 options.put(key, value);
489 (code = Global_define())
490 { res = new GlobalConstruct(code, options); }
491 (Interface_clusters(res))?
492 (Happens_before(res))?
496 res.unfoldInterfaceCluster();
501 String C_CPP_CODE() :
508 text = new StringBuilder();
514 t = <CONST> | t = <STRUCT> |
515 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
516 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
517 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
518 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
519 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
520 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
521 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
522 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
523 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
526 text.append(t.image);
527 if (t.image.equals(";") || t.image.equals("\\")
528 || t.image.equals("{") || t.image.equals("}"))
535 //System.out.println(text);
536 return text.toString();
540 SequentialDefineSubConstruct Global_define() :
542 String declareVar, initVar, defineFunc;
551 (<DECLARE_VAR> (declareVar = C_CPP_CODE()))?
552 (<INIT_VAR> (initVar = C_CPP_CODE()))?
553 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE()))?
555 SequentialDefineSubConstruct res = new SequentialDefineSubConstruct(declareVar, initVar, defineFunc);
556 //System.out.println(res);
561 ConditionalInterface Conditional_interface() :
563 String interfaceName, hbConditionLabel;
567 hbConditionLabel = "";
569 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
570 <IDENTIFIER>.image <CLOSE_BRACKET>)?
572 return new ConditionalInterface(interfaceName, hbConditionLabel);
576 void Interface_cluster(GlobalConstruct inst) :
579 ConditionalInterface condInterface;
582 (clusterName= <IDENTIFIER>.image)
583 <EQUALS> <OPEN_PAREN>
584 (condInterface = Conditional_interface()
585 { inst.addInterface2Cluster(clusterName, condInterface); }
587 (<COMMA> condInterface = Conditional_interface()
588 { inst.addInterface2Cluster(clusterName, condInterface); }
593 void Interface_clusters(GlobalConstruct inst) :
596 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
599 void Happens_before(GlobalConstruct inst) :
601 ConditionalInterface left, right;
606 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
607 { inst.addHBCondition(left, right); }
611 InterfaceConstruct Interface() :
613 InterfaceConstruct res;
614 String interfaceName, condition, idCode, check, postAction,
615 postCheck, commitPoint, hbLabel, hbCondition;
616 ActionSubConstruct action;
617 ArrayList<String> commitPointSet;
618 HashMap<String, String> hbConditions;
629 commitPointSet = new ArrayList<String>();
630 hbConditions = new HashMap<String, String>();
634 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
636 (commitPoint = <IDENTIFIER>.image
637 { commitPointSet.add(commitPoint); }
640 (commitPoint = <IDENTIFIER>.image)
642 if (commitPointSet.contains(commitPoint)) {
643 throw new ParseException(interfaceName + " has" +
644 "duplicate commit point labels");
646 commitPointSet.add(commitPoint);
650 (<CONDITION> (condition = C_CPP_CODE()))?
653 (hbLabel = <IDENTIFIER>.image)
654 (hbCondition = C_CPP_CODE())
656 if (hbConditions.containsKey(hbLabel)) {
657 throw new ParseException(interfaceName + " has" +
658 "duplicate happens-before condtion labels");
660 hbConditions.put(hbLabel, hbCondition);
663 (<ID> (idCode = C_CPP_CODE()))?
664 (<CHECK> (check = C_CPP_CODE()))?
666 (<POST_ACTION> (postAction = C_CPP_CODE()))?
667 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
671 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
672 hbConditions, idCode, check, action, postAction, postCheck);
677 ActionSubConstruct Action() :
679 String type, name, expr, defineVarStr, code;
680 ArrayList<DefineVar> defineVars;
684 defineVars = new ArrayList<DefineVar>();
690 (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
692 int eqIdx = defineVarStr.indexOf('=');
693 int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
694 type = defineVarStr.substring(0, typeEnd);
695 name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
696 expr = defineVarStr.substring(eqIdx + 2);
697 DefineVar defineVar = new DefineVar(type, name, expr);
698 defineVars.add(defineVar);
699 })* (<CODE> (code = C_CPP_CODE()))? )
703 ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
708 PotentialCPDefineConstruct Potential_commit_point_define() :
710 PotentialCPDefineConstruct res;
711 String label, condition;
718 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
719 <LABEL> (label = <IDENTIFIER>.image)
723 res = new PotentialCPDefineConstruct(label, condition);
729 CPDefineConstruct Commit_point_define() :
731 CPDefineConstruct res;
732 String label, potentialCPLabel, condition;
739 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
740 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
741 <LABEL> (label = <IDENTIFIER>.image)
745 res = new CPDefineConstruct(label, potentialCPLabel, condition);
751 CPDefineCheckConstruct Commit_point_define_check() :
753 CPDefineCheckConstruct res;
754 String label, condition;
761 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
762 <LABEL> (label = <IDENTIFIER>.image)
766 res = new CPDefineCheckConstruct(label, condition);
771 EntryPointConstruct Entry_point() :
781 return new EntryPointConstruct();
785 InterfaceDefineConstruct Interface_define() :
792 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
796 return new InterfaceDefineConstruct(name);