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<String> 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: "/=">
442 ((~["\"","\\","\n","\r"])
444 ( ["n","t","b","r","f","\\","'","\""]
445 | ["0"-"7"] ( ["0"-"7"] )?
446 | ["0"-"3"] ["0"-"7"]
455 ((~["'","\\","\n","\r"])
457 (["n","t","b","r","f","\\","'","\""]
458 | ["0"-"7"] ( ["0"-"7"] )?
459 | ["0"-"3"] ["0"-"7"]
467 <DECIMAL_LITERAL> (["l","L"])?
468 | <HEX_LITERAL> (["l","L"])?
469 | <OCTAL_LITERAL> (["l","L"])?>
471 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
473 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
475 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
477 < FLOATING_POINT_LITERAL:
478 <DECIMAL_FLOATING_POINT_LITERAL>
479 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
481 < #DECIMAL_FLOATING_POINT_LITERAL:
482 (["0"-"9"])+ "." (["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"])?
485 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
487 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
489 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
490 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
491 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
493 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
495 < #SPACE: (" " | "\t")+>
497 < #TO_END_OF_LINE: (~["\n"])+>
500 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
502 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
516 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
518 name = ParseQualifiedName() {
519 if (!type.equals(""))
520 type = type + " " + name.fullName;
522 type = name.fullName;
525 ((str = "const".image {
526 if (!type.equals(""))
527 type = type + " " + str;
531 (str = <STAR>.image {
532 if (!type.equals(""))
533 type = type + " " + str;
538 if (!type.equals(""))
539 type = type + " " + str;
558 System.out.println(str);
563 System.out.println(func);
568 String ParameterizedName() :
574 (str = <IDENTIFIER>.image {res = str;})
575 ("<" str = Type() { res = res + "<" + str; }
576 ("," str = Type() { res = res + ", " + str; })* ">"
584 FunctionHeader FuncDecl() :
586 ArrayList<VariableDeclaration> templateList;
588 QualifiedName funcName;
589 ArrayList<VariableDeclaration> args;
593 (<STATIC> | <INLINE>)*
595 funcName = ParseQualifiedName()
596 args = FormalParamList()
598 FunctionHeader res = new FunctionHeader(ret, funcName, args);
599 //System.out.println(res);
604 QualifiedName ParseQualifiedName() :
606 String qualifiedName, str;
609 { qualifiedName = ""; }
610 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
611 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
614 QualifiedName res = new QualifiedName(qualifiedName);
615 //System.out.println(res);
620 ArrayList<VariableDeclaration> TemplateParamList() :
622 ArrayList<VariableDeclaration> params;
628 params = new ArrayList<VariableDeclaration>();
632 (type = <IDENTIFIER>.image
633 name = <IDENTIFIER>.image
635 params.add(new VariableDeclaration(type, name));
639 ("," type = <IDENTIFIER>.image
640 name = <IDENTIFIER>.image
642 params.add(new VariableDeclaration(type, name));
647 //System.out.println(params);
652 ArrayList<VariableDeclaration > FormalParamList() :
654 ArrayList<VariableDeclaration > typeParams;
655 VariableDeclaration varDecl;
659 typeParams = new ArrayList<VariableDeclaration >();
662 ((varDecl = TypeParam() {typeParams.add(varDecl);})
663 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
670 VariableDeclaration TypeParam() :
675 (type = Type()) (param = <IDENTIFIER>.image)
677 return new VariableDeclaration(type, param);
681 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
685 boolean newLine = false;
686 boolean inTemplate = false;
687 ArrayList<String> content;
694 content = new ArrayList<String>();
699 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
700 (t = <TEMPLATE> { inTemplate = true; })|
701 t = <STATIC> | t = <INLINE> |
706 if (headers != null) {
707 headers.add(header.substring(header.lastIndexOf(' ') + 1));
710 | t = <IDENTIFIER> | t = <POUND> |
711 (t = <OPEN_BRACE> { newLine = true; } ) |
712 (t = <CLOSE_BRACE> { newLine = true; } ) |
713 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
714 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
715 | t = <HB_SYMBOL> | t = <COMMA> |
716 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
717 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
719 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
720 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
721 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
722 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
723 t = <DOUBLELESSTHAN> |
724 t = <DOUBLEGREATERTHAN> |
725 t = <TRIPLEGREATERTHAN> |
730 t = <DIVIDE_EQUALS> |
736 (t = <SEMI_COLON> { newLine = true; } )
737 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
738 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
739 (t = <DEFINE> { newLine = true; } )
742 if (text.equals("")) {
745 text = text + " " + t.image;
757 if (content.size() == 0) {
765 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
768 ArrayList<String> code;
774 _constructs = constructs;
776 ((inst = ParseSpec() { _constructs.add(inst); }) |
777 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
781 Construct ParseSpec() :
787 LOOKAHEAD(2) res = Global_construct() |
788 LOOKAHEAD(2) res = Interface() |
789 LOOKAHEAD(2) res = Potential_commit_point_define() |
790 LOOKAHEAD(2) res = Commit_point_define() |
791 LOOKAHEAD(2) res = Commit_point_define_check() |
792 LOOKAHEAD(2) res = Entry_point() |
793 LOOKAHEAD(2) res = Class_begin() |
794 LOOKAHEAD(2) res = Class_end() |
795 LOOKAHEAD(2) res = Interface_define()
798 //System.out.println(res);
803 GlobalConstruct Global_construct() :
806 SequentialDefineSubConstruct code;
807 HashMap<String, String> options;
813 options = new HashMap<String, String>();
817 ((key = <IDENTIFIER>.image)
819 (value = <IDENTIFIER>.image)
821 if (options.containsKey(key)) {
822 throw new ParseException("Duplicate options!");
824 options.put(key, value);
829 (code = Global_define())
830 { res = new GlobalConstruct(_file, _content.size(), code, options); }
831 (Interface_clusters(res))?
832 (Happens_before(res))?
835 res.unfoldInterfaceCluster();
840 SequentialDefineSubConstruct Global_define() :
842 ArrayList<String> initVar, defineFunc, code, declareStruct;
843 ArrayList<ArrayList<String>> defineFuncs;
844 ArrayList<VariableDeclaration> declareVars;
845 ArrayList<ArrayList<String>> declareStructs;
846 VariableDeclaration declareVar;
851 declareVars = new ArrayList<VariableDeclaration>();
853 defineFuncs = new ArrayList<ArrayList<String>>();
854 declareStructs = new ArrayList<ArrayList<String>>();
857 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
858 declareStructs.add(declareStruct); }))*
859 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
860 declareVars.add(declareVar); } )*))?
861 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
862 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
864 SequentialDefineSubConstruct res = new
865 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
866 //System.out.println(res);
871 ConditionalInterface Conditional_interface() :
873 String interfaceName, hbConditionLabel;
877 hbConditionLabel = "";
879 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
880 <IDENTIFIER>.image <CLOSE_PAREN>)?
882 return new ConditionalInterface(interfaceName, hbConditionLabel);
886 void Interface_cluster(GlobalConstruct inst) :
889 ConditionalInterface condInterface;
892 (clusterName= <IDENTIFIER>.image)
893 <EQUALS> <OPEN_BRACE>
894 (condInterface = Conditional_interface()
895 { inst.addInterface2Cluster(clusterName, condInterface); }
897 (<COMMA> condInterface = Conditional_interface()
898 { inst.addInterface2Cluster(clusterName, condInterface); }
903 void Interface_clusters(GlobalConstruct inst) :
906 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
909 void Happens_before(GlobalConstruct inst) :
911 ConditionalInterface left, right;
916 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
917 { inst.addHBCondition(left, right); }
921 InterfaceConstruct Interface() :
923 InterfaceConstruct res;
924 String interfaceName, condition, idCode, check,
925 postCheck, commitPoint, hbLabel, hbCondition;
926 ArrayList<String> commitPointSet;
927 ArrayList<String> action, postAction;
928 HashMap<String, String> hbConditions;
929 ArrayList<String> content;
934 action = new ArrayList<String>();
939 commitPointSet = new ArrayList<String>();
940 hbConditions = new HashMap<String, String>();
941 postAction = new ArrayList<String>();
944 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
946 (commitPoint = <IDENTIFIER>.image
947 { commitPointSet.add(commitPoint); }
950 (commitPoint = <IDENTIFIER>.image)
952 if (commitPointSet.contains(commitPoint)) {
953 throw new ParseException(interfaceName + " has" +
954 "duplicate commit point labels");
956 commitPointSet.add(commitPoint);
960 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
963 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
964 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
966 if (hbConditions.containsKey(hbLabel)) {
967 throw new ParseException(interfaceName + " has" +
968 "duplicate happens-before condtion labels");
970 hbConditions.put(hbLabel, hbCondition);
973 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
974 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
975 (<ACTION> action = C_CPP_CODE(null))?
976 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
977 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
980 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
981 hbConditions, idCode, check, action, postAction, postCheck);
987 PotentialCPDefineConstruct Potential_commit_point_define() :
989 PotentialCPDefineConstruct res;
990 String label, condition;
991 ArrayList<String> content;
997 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
998 <LABEL> (label = <IDENTIFIER>.image)
1001 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
1007 CPDefineConstruct Commit_point_define() :
1009 CPDefineConstruct res;
1010 String label, potentialCPLabel, condition;
1011 ArrayList<String> content;
1017 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1018 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
1019 <LABEL> (label = <IDENTIFIER>.image)
1022 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
1028 CPDefineCheckConstruct Commit_point_define_check() :
1030 CPDefineCheckConstruct res;
1031 String label, condition;
1032 ArrayList<String> content;
1038 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1039 <LABEL> (label = <IDENTIFIER>.image)
1042 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
1047 EntryPointConstruct Entry_point() :
1055 return new EntryPointConstruct(_file, _content.size());
1059 ClassBeginConstruct Class_begin() :
1067 return new ClassBeginConstruct(_file, _content.size());
1071 ClassEndConstruct Class_end() :
1079 return new ClassEndConstruct(_file, _content.size());
1083 InterfaceDefineConstruct Interface_define() :
1089 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1092 return new InterfaceDefineConstruct(_file, _content.size(), name);