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
31 b) Interface construct
35 IDENTIFIER | IDENTIFIER ...
36 @Condition: ... (Optional)
38 IDENTIFIER :: <C_CPP_Condition>
40 @ID: ... (Optional, use default ID)
45 @Post_action: (Optional)
46 @Post_check: (Optional)
49 c) Potential commit construct
51 @Potential_commit_point_define: ...
55 d) Commit point define construct
57 @Commit_point_define_check: ...
64 @Commit_point_define: ...
65 @Potential_commit_point_label: ...
69 e) Entry point construct
74 f) Interface define construct
76 @Interface_define: <Interface_Name>
79 g) Interface declare & define construct
81 @Interface_decl_define: <Interface_Name>
83 IDENTIFIER | IDENTIFIER ...
84 @Condition: ... (Optional)
86 IDENTIFIER :: <C_CPP_Condition>
88 @ID: ... (Optional, use default ID)
93 @Post_action: (Optional)
94 @Post_check: (Optional)
103 JAVA_UNICODE_ESCAPE = true;
106 PARSER_BEGIN(SpecParser)
107 package edu.uci.eecs.specCompiler.grammerParser;
109 import java.io.FileInputStream;
110 import java.io.FileNotFoundException;
111 import java.io.InputStream;
112 import java.io.ByteArrayInputStream;
114 import java.util.ArrayList;
115 import java.util.HashMap;
116 import java.util.HashSet;
118 import edu.uci.eecs.specCompiler.specExtraction.Construct;
119 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
120 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
121 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
122 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
123 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
124 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
125 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
126 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
127 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
128 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
129 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
130 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
131 import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
132 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
133 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
135 public class SpecParser {
136 private static ArrayList<String> _content;
137 private static File _file;
138 private static ArrayList<Construct> _constructs;
141 public static void main(String[] argvs)
142 throws ParseException, TokenMgrError {
144 File f = new File("./grammer/spec.txt");
145 FileInputStream fis = new FileInputStream(f);
146 SpecParser parser = new SpecParser(fis);
148 ArrayList<String> content = new ArrayList<String>();
149 ArrayList<Construct> constructs = new ArrayList<Construct>();
150 ArrayList<String> headers = new ArrayList<String>();
151 parser.Parse(f, content, constructs, headers);
152 for (int i = 0; i < content.size(); i++) {
153 System.out.println(content.get(i));
156 for (int i = 0; i < constructs.size(); i++) {
157 System.out.println(constructs.get(i));
162 System.out.println("Parsing finished!");
163 } catch (FileNotFoundException e) {
168 public static SourceFileInfo ParseFile(File f)
169 throws ParseException, TokenMgrError {
171 InputStream input = new FileInputStream(f);
172 SpecParser parser = new SpecParser(input);
173 ArrayList<String> content = new ArrayList<String>(),
174 headers = new ArrayList<String>();
175 ArrayList<Construct> constructs = new ArrayList<Construct>();
176 parser.Parse(f, content, constructs, headers);
177 return new SourceFileInfo(f, content, headers, constructs);
178 } catch (FileNotFoundException e) {
184 public static ArrayList<VariableDeclaration> getTemplateArg(String line)
185 throws ParseException {
186 InputStream input = new ByteArrayInputStream(line.getBytes());
187 SpecParser parser = new SpecParser(input);
188 return parser.TemplateParamList();
191 public static FunctionHeader parseFuncHeader(String line)
192 throws ParseException {
193 InputStream input = new ByteArrayInputStream(line.getBytes());
194 SpecParser parser = new SpecParser(input);
195 return parser.FuncDecl();
199 public static String stringArray2String(ArrayList<String> content) {
200 StringBuilder sb = new StringBuilder();
201 if (content.size() == 1)
202 return content.get(0);
203 for (int i = 0; i < content.size(); i++) {
204 sb.append(content.get(i) + "\n");
206 return sb.toString();
210 PARSER_END(SpecParser)
228 "/**" : IN_POTENTIAL_SPEC
231 <IN_POTENTIAL_SPEC> TOKEN : {
232 <BEGIN: "@Begin"> : IN_SPEC
244 // "//" comment for the specification
245 <"//" (~["\n", "\r"])* (["\n", "\r"])>
248 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
252 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
256 // "#" comment for the specification
257 <"#" (~["\n", "\r"])* (["\n", "\r"])>
264 <OPTIONS: "@Options:">
266 <GLOBAL_DEFINE: "@Global_define:">
268 <DECLARE_STRUCT: "@DeclareStruct:">
270 <DECLARE_VAR: "@DeclareVar:">
272 <INIT_VAR: "@InitVar:">
274 <DEFINE_FUNC: "@DefineFunc:">
276 <INTERFACE_CLUSTER: "@Interface_cluster:">
278 <HAPPENS_BEFORE: "@Happens_before:">
280 <INTERFACE: "@Interface:">
282 <COMMIT_POINT_SET: "@Commit_point_set:">
284 <ENTRY_POINT: "@Entry_point">
286 <CLASS_BEGIN: "@Class_begin">
288 <CLASS_END: "@Class_end">
290 <INTERFACE_DEFINE: "@Interface_define:">
292 <CONDITION: "@Condition:">
294 <HB_CONDITION: "@HB_condition:">
304 <POST_ACTION: "@Post_action:">
306 <POST_CHECK: "@Post_check:">
308 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
312 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
314 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
316 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
320 <IN_SPEC, DEFAULT> TOKEN :
322 /* Specification & C/C++ shared tokens */
330 <UNSIGNED: "unsigned">
332 <TEMPLATE: "template">
340 <#LETTER: ["a"-"z", "A"-"Z"]>
342 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
364 /* C/C++ only token*/
395 <GREATER_EQUALS: ">=">
399 <LOGICAL_EQUALS: "==">
415 <DOUBLELESSTHAN: "<<">
417 <DOUBLEGREATERTHAN: ">>">
419 <TRIPLEGREATERTHAN: ">>>">
427 <DIVIDE_EQUALS: "/=">
441 ((~["\"","\\","\n","\r"])
443 ( ["n","t","b","r","f","\\","'","\""]
444 | ["0"-"7"] ( ["0"-"7"] )?
445 | ["0"-"3"] ["0"-"7"]
454 ((~["'","\\","\n","\r"])
456 (["n","t","b","r","f","\\","'","\""]
457 | ["0"-"7"] ( ["0"-"7"] )?
458 | ["0"-"3"] ["0"-"7"]
466 <DECIMAL_LITERAL> (["l","L"])?
467 | <HEX_LITERAL> (["l","L"])?
468 | <OCTAL_LITERAL> (["l","L"])?>
470 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
472 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
474 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
476 < FLOATING_POINT_LITERAL:
477 <DECIMAL_FLOATING_POINT_LITERAL>
478 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
480 < #DECIMAL_FLOATING_POINT_LITERAL:
481 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
482 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
483 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
484 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
486 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
488 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
489 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
490 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
492 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
494 < #SPACE: (" " | "\t")+>
496 < #TO_END_OF_LINE: (~["\n"])+>
499 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
501 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
515 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
517 name = ParseQualifiedName() {
518 if (!type.equals(""))
519 type = type + " " + name.fullName;
521 type = name.fullName;
524 ((str = "const".image {
525 if (!type.equals(""))
526 type = type + " " + str;
530 (str = <STAR>.image {
531 if (!type.equals(""))
532 type = type + " " + str;
537 if (!type.equals(""))
538 type = type + " " + str;
557 System.out.println(str);
562 System.out.println(func);
567 String ParameterizedName() :
573 (str = <IDENTIFIER>.image {res = str;})
574 ("<" str = Type() { res = res + "<" + str; }
575 ("," str = Type() { res = res + ", " + str; })* ">"
583 FunctionHeader FuncDecl() :
586 QualifiedName funcName;
587 ArrayList<VariableDeclaration> args;
590 (<STATIC> | <INLINE>)*
592 funcName = ParseQualifiedName()
593 args = FormalParamList()
595 FunctionHeader res = new FunctionHeader(ret, funcName, args);
596 //System.out.println(res);
601 QualifiedName ParseQualifiedName() :
603 String qualifiedName, str;
606 { qualifiedName = ""; }
607 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
608 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
611 QualifiedName res = new QualifiedName(qualifiedName);
612 //System.out.println(res);
617 ArrayList<VariableDeclaration> TemplateParamList() :
619 ArrayList<VariableDeclaration> params;
625 params = new ArrayList<VariableDeclaration>();
629 (type = <IDENTIFIER>.image
630 name = <IDENTIFIER>.image
632 params.add(new VariableDeclaration(type, name));
636 ("," type = <IDENTIFIER>.image
637 name = <IDENTIFIER>.image
639 params.add(new VariableDeclaration(type, name));
644 //System.out.println(params);
649 ArrayList<VariableDeclaration > FormalParamList() :
651 ArrayList<VariableDeclaration > typeParams;
652 VariableDeclaration varDecl;
656 typeParams = new ArrayList<VariableDeclaration >();
659 ((varDecl = TypeParam() {typeParams.add(varDecl);})
660 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
667 VariableDeclaration TypeParam() :
672 (type = Type()) (param = <IDENTIFIER>.image)
674 return new VariableDeclaration(type, param);
678 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
682 boolean newLine = false;
683 boolean inTemplate = false;
684 ArrayList<String> content;
691 content = new ArrayList<String>();
696 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
697 (t = <TEMPLATE> { inTemplate = true; })|
698 t = <STATIC> | t = <INLINE> |
703 if (headers != null) {
704 headers.add(header.substring(header.lastIndexOf(' ') + 1));
707 | t = <IDENTIFIER> | t = <POUND> |
708 (t = <OPEN_BRACE> { newLine = true; } ) |
709 (t = <CLOSE_BRACE> { newLine = true; } ) |
710 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
711 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
712 | t = <HB_SYMBOL> | t = <COMMA> |
713 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
714 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
716 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
717 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
718 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
719 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
720 t = <DOUBLELESSTHAN> |
721 t = <DOUBLEGREATERTHAN> |
722 t = <TRIPLEGREATERTHAN> |
727 t = <DIVIDE_EQUALS> |
733 (t = <SEMI_COLON> { newLine = true; } )
734 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
735 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
736 (t = <DEFINE> { newLine = true; } )
739 if (text.equals("")) {
742 text = text + " " + t.image;
754 if (content.size() == 0) {
762 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
765 ArrayList<String> code;
771 _constructs = constructs;
773 ((inst = ParseSpec() { _constructs.add(inst); }) |
774 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
778 Construct ParseSpec() :
784 LOOKAHEAD(2) res = Global_construct() |
785 LOOKAHEAD(2) res = Interface() |
786 LOOKAHEAD(2) res = Potential_commit_point_define() |
787 LOOKAHEAD(2) res = Commit_point_define() |
788 LOOKAHEAD(2) res = Commit_point_define_check() |
789 LOOKAHEAD(2) res = Entry_point() |
790 LOOKAHEAD(2) res = Class_begin() |
791 LOOKAHEAD(2) res = Class_end() |
792 LOOKAHEAD(2) res = Interface_define()
795 //System.out.println(res);
800 GlobalConstruct Global_construct() :
803 SequentialDefineSubConstruct code;
804 HashMap<String, String> options;
810 options = new HashMap<String, String>();
814 ((key = <IDENTIFIER>.image)
816 (value = <IDENTIFIER>.image)
818 if (options.containsKey(key)) {
819 throw new ParseException("Duplicate options!");
821 options.put(key, value);
826 (code = Global_define())
827 { res = new GlobalConstruct(_file, _content.size(), code, options); }
828 (Interface_clusters(res))?
829 (Happens_before(res))?
832 res.unfoldInterfaceCluster();
837 SequentialDefineSubConstruct Global_define() :
839 ArrayList<String> initVar, defineFunc, code, declareStruct;
840 ArrayList<ArrayList<String>> defineFuncs;
841 ArrayList<VariableDeclaration> declareVars;
842 ArrayList<ArrayList<String>> declareStructs;
843 VariableDeclaration declareVar;
848 declareVars = new ArrayList<VariableDeclaration>();
850 defineFuncs = new ArrayList<ArrayList<String>>();
851 declareStructs = new ArrayList<ArrayList<String>>();
854 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
855 declareStructs.add(declareStruct); }))*
856 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
857 declareVars.add(declareVar); } )*))?
858 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
859 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
861 SequentialDefineSubConstruct res = new
862 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
863 //System.out.println(res);
868 ConditionalInterface Conditional_interface() :
870 String interfaceName, hbConditionLabel;
874 hbConditionLabel = "";
876 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
877 <IDENTIFIER>.image <CLOSE_PAREN>)?
879 return new ConditionalInterface(interfaceName, hbConditionLabel);
883 void Interface_cluster(GlobalConstruct inst) :
886 ConditionalInterface condInterface;
889 (clusterName= <IDENTIFIER>.image)
890 <EQUALS> <OPEN_BRACE>
891 (condInterface = Conditional_interface()
892 { inst.addInterface2Cluster(clusterName, condInterface); }
894 (<COMMA> condInterface = Conditional_interface()
895 { inst.addInterface2Cluster(clusterName, condInterface); }
900 void Interface_clusters(GlobalConstruct inst) :
903 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
906 void Happens_before(GlobalConstruct inst) :
908 ConditionalInterface left, right;
913 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
914 { inst.addHBCondition(left, right); }
918 InterfaceConstruct Interface() :
920 InterfaceConstruct res;
921 String interfaceName, condition, idCode, check,
922 postCheck, commitPoint, hbLabel, hbCondition;
923 ArrayList<String> commitPointSet;
924 ArrayList<String> action, postAction;
925 HashMap<String, String> hbConditions;
926 ArrayList<String> content;
931 action = new ArrayList<String>();
936 commitPointSet = new ArrayList<String>();
937 hbConditions = new HashMap<String, String>();
938 postAction = new ArrayList<String>();
941 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
943 (commitPoint = <IDENTIFIER>.image
944 { commitPointSet.add(commitPoint); }
947 (commitPoint = <IDENTIFIER>.image)
949 if (commitPointSet.contains(commitPoint)) {
950 throw new ParseException(interfaceName + " has" +
951 "duplicate commit point labels");
953 commitPointSet.add(commitPoint);
957 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
960 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
961 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
963 if (hbConditions.containsKey(hbLabel)) {
964 throw new ParseException(interfaceName + " has" +
965 "duplicate happens-before condtion labels");
967 hbConditions.put(hbLabel, hbCondition);
970 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
971 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
972 (<ACTION> action = C_CPP_CODE(null))?
973 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
974 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
977 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
978 hbConditions, idCode, check, action, postAction, postCheck);
984 PotentialCPDefineConstruct Potential_commit_point_define() :
986 PotentialCPDefineConstruct res;
987 String label, condition;
988 ArrayList<String> content;
994 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
995 <LABEL> (label = <IDENTIFIER>.image)
998 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
1004 CPDefineConstruct Commit_point_define() :
1006 CPDefineConstruct res;
1007 String label, potentialCPLabel, condition;
1008 ArrayList<String> content;
1014 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1015 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
1016 <LABEL> (label = <IDENTIFIER>.image)
1019 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
1025 CPDefineCheckConstruct Commit_point_define_check() :
1027 CPDefineCheckConstruct res;
1028 String label, condition;
1029 ArrayList<String> content;
1035 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1036 <LABEL> (label = <IDENTIFIER>.image)
1039 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
1044 EntryPointConstruct Entry_point() :
1052 return new EntryPointConstruct(_file, _content.size());
1056 ClassBeginConstruct Class_begin() :
1064 return new ClassBeginConstruct(_file, _content.size());
1068 ClassEndConstruct Class_end() :
1076 return new ClassEndConstruct(_file, _content.size());
1080 InterfaceDefineConstruct Interface_define() :
1086 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1089 return new InterfaceDefineConstruct(_file, _content.size(), name);