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;
117 import java.util.Arrays;
119 import edu.uci.eecs.specCompiler.specExtraction.Construct;
120 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
121 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
122 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
123 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
124 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
125 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
126 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
127 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
128 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
129 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
130 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
131 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
132 import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
133 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
134 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
136 public class SpecParser {
137 private static ArrayList<String> _content;
138 private static File _file;
139 private static ArrayList<Construct> _constructs;
142 public static void main(String[] argvs)
143 throws ParseException, TokenMgrError {
145 File f = new File("./grammer/spec1.txt");
146 FileInputStream fis = new FileInputStream(f);
147 SpecParser parser = new SpecParser(fis);
149 ArrayList<String> content = new ArrayList<String>();
150 ArrayList<Construct> constructs = new ArrayList<Construct>();
151 ArrayList<String> headers = new ArrayList<String>();
152 parser.Parse(f, content, constructs, headers);
153 for (int i = 0; i < content.size(); i++) {
154 System.out.println(content.get(i));
157 for (int i = 0; i < constructs.size(); i++) {
158 System.out.println(constructs.get(i));
163 System.out.println("Parsing finished!");
164 } catch (FileNotFoundException e) {
169 public static SourceFileInfo ParseFile(File f)
170 throws ParseException, TokenMgrError {
172 InputStream input = new FileInputStream(f);
173 SpecParser parser = new SpecParser(input);
174 ArrayList<String> content = new ArrayList<String>(),
175 headers = new ArrayList<String>();
176 ArrayList<Construct> constructs = new ArrayList<Construct>();
177 parser.Parse(f, content, constructs, headers);
178 return new SourceFileInfo(f, content, headers, constructs);
179 } catch (FileNotFoundException e) {
186 private static ArrayList<String> breakLines(String all) {
187 String lines[] = all.split("[\\r\\n]+");
188 return new ArrayList<String>(Arrays.asList(lines));
192 public static ArrayList<VariableDeclaration> getTemplateArg(String line)
193 throws ParseException {
194 InputStream input = new ByteArrayInputStream(line.getBytes());
195 SpecParser parser = new SpecParser(input);
196 return parser.TemplateParamList();
199 public static FunctionHeader parseFuncHeader(String line)
200 throws ParseException {
201 InputStream input = new ByteArrayInputStream(line.getBytes());
202 SpecParser parser = new SpecParser(input);
203 return parser.FuncDecl();
207 public static String stringArray2String(ArrayList<String> content) {
208 StringBuilder sb = new StringBuilder();
209 if (content.size() == 1)
210 return content.get(0);
211 for (int i = 0; i < content.size(); i++) {
212 sb.append(content.get(i) + "\n");
214 return sb.toString();
218 boolean spaceSeparator(Token t) {
267 PARSER_END(SpecParser)
271 <IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
285 "/**" : IN_POTENTIAL_SPEC
288 <IN_POTENTIAL_SPEC> TOKEN : {
289 <BEGIN: "@Begin"> : IN_SPEC
305 // "//" comment for the specification
306 <"//" (~["\n", "\r"])* (["\n", "\r"])>
309 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
313 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
317 // "#" comment for the specification
318 <"#" (~["\n", "\r"])* (["\n", "\r"])>
325 <OPTIONS: "@Options:">
327 <GLOBAL_DEFINE: "@Global_define:">
329 <DECLARE_STRUCT: "@DeclareStruct:">
331 <DECLARE_VAR: "@DeclareVar:">
333 <INIT_VAR: "@InitVar:">
335 <DEFINE_FUNC: "@DefineFunc:">
337 <INTERFACE_CLUSTER: "@Interface_cluster:">
339 <HAPPENS_BEFORE: "@Happens_before:">
341 <INTERFACE: "@Interface:">
343 <COMMIT_POINT_SET: "@Commit_point_set:">
345 <ENTRY_POINT: "@Entry_point">
347 <CLASS_BEGIN: "@Class_begin">
349 <CLASS_END: "@Class_end">
351 <INTERFACE_DEFINE: "@Interface_define:">
353 <CONDITION: "@Condition:">
355 <HB_CONDITION: "@HB_condition:">
365 <POST_ACTION: "@Post_action:">
367 <POST_CHECK: "@Post_check:">
369 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
373 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
375 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
377 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
383 /* Specification & C/C++ shared tokens */
391 <UNSIGNED: "unsigned">
393 <TEMPLATE: "template">
403 <#LETTER: ["a"-"z", "A"-"Z"]>
405 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
427 /* C/C++ only token*/
460 <GREATER_EQUALS: ">=">
464 <LOGICAL_EQUALS: "==">
480 <DOUBLELESSTHAN: "<<">
482 <DOUBLEGREATERTHAN: ">>">
484 <TRIPLEGREATERTHAN: ">>>">
492 <DIVIDE_EQUALS: "/=">
506 ((~["\"","\\","\n","\r"])
508 ( ["n","t","b","r","f","\\","'","\""]
509 | ["0"-"7"] ( ["0"-"7"] )?
510 | ["0"-"3"] ["0"-"7"]
519 ((~["'","\\","\n","\r"])
521 (["n","t","b","r","f","\\","'","\""]
522 | ["0"-"7"] ( ["0"-"7"] )?
523 | ["0"-"3"] ["0"-"7"]
531 <DECIMAL_LITERAL> (["l","L"])?
532 | <HEX_LITERAL> (["l","L"])?
533 | <OCTAL_LITERAL> (["l","L"])?>
535 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
537 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
539 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
541 < FLOATING_POINT_LITERAL:
542 <DECIMAL_FLOATING_POINT_LITERAL>
543 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
545 < #DECIMAL_FLOATING_POINT_LITERAL:
546 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
547 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
548 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
549 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
551 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
553 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
554 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
555 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
557 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
559 < #SPACE: (" " | "\t")+>
561 < #TO_END_OF_LINE: (~["\n"])+>
564 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
566 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
580 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
582 name = ParseQualifiedName() {
583 if (!type.equals(""))
584 type = type + " " + name.fullName;
586 type = name.fullName;
589 ((str = <CONST>.image {
590 if (!type.equals(""))
591 type = type + " " + str;
595 (str = <STAR>.image {
596 if (!type.equals(""))
597 type = type + " " + str;
602 if (!type.equals(""))
603 type = type + " " + str;
614 String ParameterizedName() :
620 (str = <IDENTIFIER>.image {res = str;})
621 (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
622 (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
630 FunctionHeader FuncDecl() :
633 QualifiedName funcName;
634 ArrayList<VariableDeclaration> args;
637 (<STATIC> | <INLINE>)*
639 funcName = ParseQualifiedName()
640 args = FormalParamList()
642 FunctionHeader res = new FunctionHeader(ret, funcName, args);
643 //System.out.println(res);
648 QualifiedName ParseQualifiedName() :
650 String qualifiedName, str;
653 { qualifiedName = ""; }
654 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
655 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
658 QualifiedName res = new QualifiedName(qualifiedName);
659 //System.out.println(res);
664 ArrayList<VariableDeclaration> TemplateParamList() :
666 ArrayList<VariableDeclaration> params;
672 params = new ArrayList<VariableDeclaration>();
676 (type = <IDENTIFIER>.image
677 name = <IDENTIFIER>.image
679 params.add(new VariableDeclaration(type, name));
683 (<COMMA> type = <IDENTIFIER>.image
684 name = <IDENTIFIER>.image
686 params.add(new VariableDeclaration(type, name));
691 //System.out.println(params);
696 ArrayList<VariableDeclaration > FormalParamList() :
698 ArrayList<VariableDeclaration > typeParams;
699 VariableDeclaration varDecl;
703 typeParams = new ArrayList<VariableDeclaration >();
706 ((varDecl = TypeParam() {typeParams.add(varDecl);})
707 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
714 VariableDeclaration TypeParam() :
719 (type = Type()) (param = <IDENTIFIER>.image)
721 return new VariableDeclaration(type, param);
727 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
731 boolean newLine = false;
732 boolean newSpace = true;
733 boolean inTemplate = false;
734 boolean inForLoop = false;
735 ArrayList<String> content;
742 content = new ArrayList<String>();
747 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
748 (t = <TEMPLATE> { inTemplate = true; })|
749 t = <STATIC> | t = <INLINE> |
750 (t = <FOR> { inForLoop = true; })|
755 if (headers != null) {
756 headers.add(header.substring(header.lastIndexOf(' ') + 1));
759 | t = <IDENTIFIER> | t = <POUND> |
760 (t = <OPEN_BRACE> { newLine = true; } ) |
761 (t = <CLOSE_BRACE> { newLine = true; inForLoop = false;} ) |
762 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
763 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
764 | t = <HB_SYMBOL> | t = <COMMA> |
765 t = <DOT> | t = <STAR> | t = <DOLLAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
766 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
768 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
769 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
770 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
771 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
772 t = <DOUBLELESSTHAN> |
773 t = <DOUBLEGREATERTHAN> |
774 t = <TRIPLEGREATERTHAN> |
779 t = <DIVIDE_EQUALS> |
785 (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
786 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
787 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
788 (t = <DEFINE> { newLine = true; } )
791 if (text.equals("")) {
795 text = text + " " + t.image;
797 if (newSpace && spaceSeparator(t)) {
798 text = text + " " + t.image;
800 text = text + t.image;
801 if (spaceSeparator(t))
815 if (content.size() == 0) {
823 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
833 _constructs = constructs;
834 sb = new StringBuilder();
839 _constructs.add(inst);
842 //((code = C_CPP_CODE(headers)) { _content.addAll(code); })
844 flushSB = OriginalCode(sb)
847 sb = new StringBuilder();
852 // For the last piece of code
854 _content.add(sb.toString());
859 // If true, there's a new line and sb should be flushed
860 boolean OriginalCode(StringBuilder sb) :
867 if (!str.equals("\n")) {
871 _content.add(sb.toString());
877 Construct ParseSpec() :
883 LOOKAHEAD(2) res = Global_construct() |
884 LOOKAHEAD(2) res = Interface() |
885 LOOKAHEAD(2) res = Potential_commit_point_define() |
886 LOOKAHEAD(2) res = Commit_point_define() |
887 LOOKAHEAD(2) res = Commit_point_define_check() |
888 LOOKAHEAD(2) res = Entry_point() |
889 LOOKAHEAD(2) res = Class_begin() |
890 LOOKAHEAD(2) res = Class_end() |
891 LOOKAHEAD(2) res = Interface_define()
894 //System.out.println(res);
899 GlobalConstruct Global_construct() :
902 SequentialDefineSubConstruct code;
903 HashMap<String, String> options;
909 options = new HashMap<String, String>();
913 ((key = <IDENTIFIER>.image)
915 (value = <IDENTIFIER>.image)
917 if (options.containsKey(key)) {
918 throw new ParseException("Duplicate options!");
920 options.put(key, value);
925 (code = Global_define())
926 { res = new GlobalConstruct(_file, _content.size(), code, options); }
927 (Interface_clusters(res))?
928 (Happens_before(res))?
931 res.unfoldInterfaceCluster();
936 SequentialDefineSubConstruct Global_define() :
938 ArrayList<String> initVar, defineFunc, code, declareStruct;
939 ArrayList<ArrayList<String>> defineFuncs;
940 ArrayList<VariableDeclaration> declareVars;
941 ArrayList<ArrayList<String>> declareStructs;
942 VariableDeclaration declareVar;
947 declareVars = new ArrayList<VariableDeclaration>();
949 defineFuncs = new ArrayList<ArrayList<String>>();
950 declareStructs = new ArrayList<ArrayList<String>>();
953 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
954 declareStructs.add(declareStruct); }))*
955 (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
956 declareVars.add(declareVar); } )*))?
957 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
958 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
960 SequentialDefineSubConstruct res = new
961 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
962 //System.out.println(res);
967 ConditionalInterface Conditional_interface() :
969 String interfaceName, hbConditionLabel;
973 hbConditionLabel = "";
975 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
976 <IDENTIFIER>.image <CLOSE_PAREN>)?
978 return new ConditionalInterface(interfaceName, hbConditionLabel);
982 void Interface_cluster(GlobalConstruct inst) :
985 ConditionalInterface condInterface;
988 (clusterName= <IDENTIFIER>.image)
989 <EQUALS> <OPEN_BRACE>
990 (condInterface = Conditional_interface()
991 { inst.addInterface2Cluster(clusterName, condInterface); }
993 (<COMMA> condInterface = Conditional_interface()
994 { inst.addInterface2Cluster(clusterName, condInterface); }
999 void Interface_clusters(GlobalConstruct inst) :
1002 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
1005 void Happens_before(GlobalConstruct inst) :
1007 ConditionalInterface left, right;
1012 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
1013 { inst.addHBCondition(left, right); }
1017 InterfaceConstruct Interface() :
1019 InterfaceConstruct res;
1020 String interfaceName, condition, idCode, check,
1021 postCheck, commitPoint, hbLabel, hbCondition;
1022 ArrayList<String> commitPointSet;
1023 ArrayList<String> action, postAction;
1024 HashMap<String, String> hbConditions;
1025 ArrayList<String> content;
1030 action = new ArrayList<String>();
1035 commitPointSet = new ArrayList<String>();
1036 hbConditions = new HashMap<String, String>();
1037 postAction = new ArrayList<String>();
1040 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
1042 (commitPoint = <IDENTIFIER>.image
1043 { commitPointSet.add(commitPoint); }
1046 (commitPoint = <IDENTIFIER>.image)
1048 if (commitPointSet.contains(commitPoint)) {
1049 throw new ParseException(interfaceName + " has" +
1050 "duplicate commit point labels");
1052 commitPointSet.add(commitPoint);
1056 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
1059 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
1060 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
1062 if (hbConditions.containsKey(hbLabel)) {
1063 throw new ParseException(interfaceName + " has" +
1064 "duplicate happens-before condtion labels");
1066 hbConditions.put(hbLabel, hbCondition);
1069 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
1070 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
1071 (<ACTION> action = C_CPP_CODE(null))?
1072 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
1073 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
1076 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
1077 hbConditions, idCode, check, action, postAction, postCheck);
1083 PotentialCPDefineConstruct Potential_commit_point_define() :
1085 PotentialCPDefineConstruct res;
1086 String label, condition;
1087 ArrayList<String> content;
1093 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1094 <LABEL> (label = <IDENTIFIER>.image)
1097 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
1103 CPDefineConstruct Commit_point_define() :
1105 CPDefineConstruct res;
1106 String label, potentialCPLabel, condition;
1107 ArrayList<String> content;
1113 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1114 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
1115 <LABEL> (label = <IDENTIFIER>.image)
1118 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
1124 CPDefineCheckConstruct Commit_point_define_check() :
1126 CPDefineCheckConstruct res;
1127 String label, condition;
1128 ArrayList<String> content;
1134 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1135 <LABEL> (label = <IDENTIFIER>.image)
1138 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
1143 EntryPointConstruct Entry_point() :
1151 return new EntryPointConstruct(_file, _content.size());
1155 ClassBeginConstruct Class_begin() :
1163 return new ClassBeginConstruct(_file, _content.size());
1167 ClassEndConstruct Class_end() :
1175 return new ClassEndConstruct(_file, _content.size());
1179 InterfaceDefineConstruct Interface_define() :
1185 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1188 return new InterfaceDefineConstruct(_file, _content.size(), name);