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;
622 System.out.println(str);
627 System.out.println(func);
632 String ParameterizedName() :
638 (str = <IDENTIFIER>.image {res = str;})
639 (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
640 (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
648 FunctionHeader FuncDecl() :
651 QualifiedName funcName;
652 ArrayList<VariableDeclaration> args;
655 (<STATIC> | <INLINE>)*
657 funcName = ParseQualifiedName()
658 args = FormalParamList()
660 FunctionHeader res = new FunctionHeader(ret, funcName, args);
661 //System.out.println(res);
666 QualifiedName ParseQualifiedName() :
668 String qualifiedName, str;
671 { qualifiedName = ""; }
672 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
673 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
676 QualifiedName res = new QualifiedName(qualifiedName);
677 //System.out.println(res);
682 ArrayList<VariableDeclaration> TemplateParamList() :
684 ArrayList<VariableDeclaration> params;
690 params = new ArrayList<VariableDeclaration>();
694 (type = <IDENTIFIER>.image
695 name = <IDENTIFIER>.image
697 params.add(new VariableDeclaration(type, name));
701 (<COMMA> type = <IDENTIFIER>.image
702 name = <IDENTIFIER>.image
704 params.add(new VariableDeclaration(type, name));
709 //System.out.println(params);
714 ArrayList<VariableDeclaration > FormalParamList() :
716 ArrayList<VariableDeclaration > typeParams;
717 VariableDeclaration varDecl;
721 typeParams = new ArrayList<VariableDeclaration >();
724 ((varDecl = TypeParam() {typeParams.add(varDecl);})
725 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
732 VariableDeclaration TypeParam() :
737 (type = Type()) (param = <IDENTIFIER>.image)
739 return new VariableDeclaration(type, param);
745 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
749 boolean newLine = false;
750 boolean newSpace = true;
751 boolean inTemplate = false;
752 boolean inForLoop = false;
753 ArrayList<String> content;
760 content = new ArrayList<String>();
765 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
766 (t = <TEMPLATE> { inTemplate = true; })|
767 t = <STATIC> | t = <INLINE> |
768 (t = <FOR> { inForLoop = true; })|
773 if (headers != null) {
774 headers.add(header.substring(header.lastIndexOf(' ') + 1));
777 | t = <IDENTIFIER> | t = <POUND> |
778 (t = <OPEN_BRACE> { newLine = true; } ) |
779 (t = <CLOSE_BRACE> { newLine = true; inForLoop = false;} ) |
780 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
781 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
782 | t = <HB_SYMBOL> | t = <COMMA> |
783 t = <DOT> | t = <STAR> | t = <DOLLAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
784 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
786 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
787 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
788 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
789 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
790 t = <DOUBLELESSTHAN> |
791 t = <DOUBLEGREATERTHAN> |
792 t = <TRIPLEGREATERTHAN> |
797 t = <DIVIDE_EQUALS> |
803 (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
804 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
805 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
806 (t = <DEFINE> { newLine = true; } )
809 if (text.equals("")) {
813 text = text + " " + t.image;
815 if (newSpace && spaceSeparator(t)) {
816 text = text + " " + t.image;
818 text = text + t.image;
819 if (spaceSeparator(t))
833 if (content.size() == 0) {
841 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
851 _constructs = constructs;
852 sb = new StringBuilder();
857 _constructs.add(inst);
860 //((code = C_CPP_CODE(headers)) { _content.addAll(code); })
862 flushSB = OriginalCode(sb)
865 sb = new StringBuilder();
870 // For the last piece of code
872 _content.add(sb.toString());
877 // If true, there's a new line and sb should be flushed
878 boolean OriginalCode(StringBuilder sb) :
885 if (!str.equals("\n")) {
889 _content.add(sb.toString());
895 Construct ParseSpec() :
901 LOOKAHEAD(2) res = Global_construct() |
902 LOOKAHEAD(2) res = Interface() |
903 LOOKAHEAD(2) res = Potential_commit_point_define() |
904 LOOKAHEAD(2) res = Commit_point_define() |
905 LOOKAHEAD(2) res = Commit_point_define_check() |
906 LOOKAHEAD(2) res = Entry_point() |
907 LOOKAHEAD(2) res = Class_begin() |
908 LOOKAHEAD(2) res = Class_end() |
909 LOOKAHEAD(2) res = Interface_define()
912 //System.out.println(res);
917 GlobalConstruct Global_construct() :
920 SequentialDefineSubConstruct code;
921 HashMap<String, String> options;
927 options = new HashMap<String, String>();
931 ((key = <IDENTIFIER>.image)
933 (value = <IDENTIFIER>.image)
935 if (options.containsKey(key)) {
936 throw new ParseException("Duplicate options!");
938 options.put(key, value);
943 (code = Global_define())
944 { res = new GlobalConstruct(_file, _content.size(), code, options); }
945 (Interface_clusters(res))?
946 (Happens_before(res))?
949 res.unfoldInterfaceCluster();
954 SequentialDefineSubConstruct Global_define() :
956 ArrayList<String> initVar, defineFunc, code, declareStruct;
957 ArrayList<ArrayList<String>> defineFuncs;
958 ArrayList<VariableDeclaration> declareVars;
959 ArrayList<ArrayList<String>> declareStructs;
960 VariableDeclaration declareVar;
965 declareVars = new ArrayList<VariableDeclaration>();
967 defineFuncs = new ArrayList<ArrayList<String>>();
968 declareStructs = new ArrayList<ArrayList<String>>();
971 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
972 declareStructs.add(declareStruct); }))*
973 (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
974 declareVars.add(declareVar); } )*))?
975 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
976 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
978 SequentialDefineSubConstruct res = new
979 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
980 //System.out.println(res);
985 ConditionalInterface Conditional_interface() :
987 String interfaceName, hbConditionLabel;
991 hbConditionLabel = "";
993 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
994 <IDENTIFIER>.image <CLOSE_PAREN>)?
996 return new ConditionalInterface(interfaceName, hbConditionLabel);
1000 void Interface_cluster(GlobalConstruct inst) :
1003 ConditionalInterface condInterface;
1006 (clusterName= <IDENTIFIER>.image)
1007 <EQUALS> <OPEN_BRACE>
1008 (condInterface = Conditional_interface()
1009 { inst.addInterface2Cluster(clusterName, condInterface); }
1011 (<COMMA> condInterface = Conditional_interface()
1012 { inst.addInterface2Cluster(clusterName, condInterface); }
1017 void Interface_clusters(GlobalConstruct inst) :
1020 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
1023 void Happens_before(GlobalConstruct inst) :
1025 ConditionalInterface left, right;
1030 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
1031 { inst.addHBCondition(left, right); }
1035 InterfaceConstruct Interface() :
1037 InterfaceConstruct res;
1038 String interfaceName, condition, idCode, check,
1039 postCheck, commitPoint, hbLabel, hbCondition;
1040 ArrayList<String> commitPointSet;
1041 ArrayList<String> action, postAction;
1042 HashMap<String, String> hbConditions;
1043 ArrayList<String> content;
1048 action = new ArrayList<String>();
1053 commitPointSet = new ArrayList<String>();
1054 hbConditions = new HashMap<String, String>();
1055 postAction = new ArrayList<String>();
1058 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
1060 (commitPoint = <IDENTIFIER>.image
1061 { commitPointSet.add(commitPoint); }
1064 (commitPoint = <IDENTIFIER>.image)
1066 if (commitPointSet.contains(commitPoint)) {
1067 throw new ParseException(interfaceName + " has" +
1068 "duplicate commit point labels");
1070 commitPointSet.add(commitPoint);
1074 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
1077 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
1078 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
1080 if (hbConditions.containsKey(hbLabel)) {
1081 throw new ParseException(interfaceName + " has" +
1082 "duplicate happens-before condtion labels");
1084 hbConditions.put(hbLabel, hbCondition);
1087 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
1088 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
1089 (<ACTION> action = C_CPP_CODE(null))?
1090 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
1091 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
1094 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
1095 hbConditions, idCode, check, action, postAction, postCheck);
1101 PotentialCPDefineConstruct Potential_commit_point_define() :
1103 PotentialCPDefineConstruct res;
1104 String label, condition;
1105 ArrayList<String> content;
1111 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1112 <LABEL> (label = <IDENTIFIER>.image)
1115 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
1121 CPDefineConstruct Commit_point_define() :
1123 CPDefineConstruct res;
1124 String label, potentialCPLabel, condition;
1125 ArrayList<String> content;
1131 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1132 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
1133 <LABEL> (label = <IDENTIFIER>.image)
1136 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
1142 CPDefineCheckConstruct Commit_point_define_check() :
1144 CPDefineCheckConstruct res;
1145 String label, condition;
1146 ArrayList<String> content;
1152 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1153 <LABEL> (label = <IDENTIFIER>.image)
1156 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
1161 EntryPointConstruct Entry_point() :
1169 return new EntryPointConstruct(_file, _content.size());
1173 ClassBeginConstruct Class_begin() :
1181 return new ClassBeginConstruct(_file, _content.size());
1185 ClassEndConstruct Class_end() :
1193 return new ClassEndConstruct(_file, _content.size());
1197 InterfaceDefineConstruct Interface_define() :
1203 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1206 return new InterfaceDefineConstruct(_file, _content.size(), name);