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>
84 JAVA_UNICODE_ESCAPE = true;
87 PARSER_BEGIN(SpecParser)
88 package edu.uci.eecs.specCompiler.grammerParser;
90 import java.io.FileInputStream;
91 import java.io.FileNotFoundException;
92 import java.io.InputStream;
93 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.SequentialDefineSubConstruct;
107 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
108 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
109 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
110 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
111 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
112 import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
113 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
114 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
116 public class SpecParser {
117 private static ArrayList<String> _content;
118 private static File _file;
119 private static ArrayList<Construct> _constructs;
122 public static void main(String[] argvs)
123 throws ParseException, TokenMgrError {
125 File f = new File("./grammer/spec.txt");
126 FileInputStream fis = new FileInputStream(f);
127 SpecParser parser = new SpecParser(fis);
129 ArrayList<String> content = new ArrayList<String>();
130 ArrayList<Construct> constructs = new ArrayList<Construct>();
131 ArrayList<String> headers = new ArrayList<String>();
132 parser.Parse(f, content, constructs, headers);
133 for (int i = 0; i < content.size(); i++) {
134 System.out.println(content.get(i));
137 for (int i = 0; i < constructs.size(); i++) {
138 System.out.println(constructs.get(i));
143 System.out.println("Parsing finished!");
144 } catch (FileNotFoundException e) {
149 public static SourceFileInfo ParseFile(File f)
150 throws ParseException, TokenMgrError {
152 InputStream input = new FileInputStream(f);
153 SpecParser parser = new SpecParser(input);
154 ArrayList<String> content = new ArrayList<String>(),
155 headers = new ArrayList<String>();
156 ArrayList<Construct> constructs = new ArrayList<Construct>();
157 parser.Parse(f, content, constructs, headers);
158 return new SourceFileInfo(f, content, headers, constructs);
159 } catch (FileNotFoundException e) {
165 public static ArrayList<String> getTemplateArg(String line)
166 throws ParseException {
167 InputStream input = new ByteArrayInputStream(line.getBytes());
168 SpecParser parser = new SpecParser(input);
169 return parser.TemplateParamList();
172 public static FunctionHeader parseFuncHeader(String line)
173 throws ParseException {
174 InputStream input = new ByteArrayInputStream(line.getBytes());
175 SpecParser parser = new SpecParser(input);
176 return parser.FuncDecl();
180 public static String stringArray2String(ArrayList<String> content) {
181 StringBuilder sb = new StringBuilder();
182 if (content.size() == 1)
183 return content.get(0);
184 for (int i = 0; i < content.size(); i++) {
185 sb.append(content.get(i) + "\n");
187 return sb.toString();
191 PARSER_END(SpecParser)
209 "/**" : IN_POTENTIAL_SPEC
212 <IN_POTENTIAL_SPEC> TOKEN : {
213 <BEGIN: "@Begin"> : IN_SPEC
225 // "//" comment for the specification
226 <"//" (~["\n", "\r"])* (["\n", "\r"])>
229 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
233 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
237 // "#" comment for the specification
238 <"#" (~["\n", "\r"])* (["\n", "\r"])>
245 <OPTIONS: "@Options:">
247 <GLOBAL_DEFINE: "@Global_define:">
249 <DECLARE_STRUCT: "@DeclareStruct:">
251 <DECLARE_VAR: "@DeclareVar:">
253 <INIT_VAR: "@InitVar:">
255 <DEFINE_FUNC: "@DefineFunc:">
257 <INTERFACE_CLUSTER: "@Interface_cluster:">
259 <HAPPENS_BEFORE: "@Happens_before:">
261 <INTERFACE: "@Interface:">
263 <COMMIT_POINT_SET: "@Commit_point_set:">
265 <ENTRY_POINT: "@Entry_point">
267 <CLASS_BEGIN: "@Class_begin">
269 <CLASS_END: "@Class_end">
271 <INTERFACE_DEFINE: "@Interface_define:">
273 <CONDITION: "@Condition:">
275 <HB_CONDITION: "@HB_condition:">
285 <POST_ACTION: "@Post_action:">
287 <POST_CHECK: "@Post_check:">
289 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
293 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
295 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
297 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
301 <IN_SPEC, DEFAULT> TOKEN :
303 /* Specification & C/C++ shared tokens */
311 <UNSIGNED: "unsigned">
313 <TEMPLATE: "template">
321 <#LETTER: ["a"-"z", "A"-"Z"]>
323 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
345 /* C/C++ only token*/
376 <GREATER_EQUALS: ">=">
380 <LOGICAL_EQUALS: "==">
400 ((~["\"","\\","\n","\r"])
402 ( ["n","t","b","r","f","\\","'","\""]
403 | ["0"-"7"] ( ["0"-"7"] )?
404 | ["0"-"3"] ["0"-"7"]
413 ((~["'","\\","\n","\r"])
415 (["n","t","b","r","f","\\","'","\""]
416 | ["0"-"7"] ( ["0"-"7"] )?
417 | ["0"-"3"] ["0"-"7"]
425 <DECIMAL_LITERAL> (["l","L"])?
426 | <HEX_LITERAL> (["l","L"])?
427 | <OCTAL_LITERAL> (["l","L"])?>
429 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
431 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
433 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
435 < FLOATING_POINT_LITERAL:
436 <DECIMAL_FLOATING_POINT_LITERAL>
437 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
439 < #DECIMAL_FLOATING_POINT_LITERAL:
440 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
441 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
442 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
443 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
445 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
447 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
448 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
449 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
451 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
453 < #SPACE: (" " | "\t")+>
455 < #TO_END_OF_LINE: (~["\n"])+>
458 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
460 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
474 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
476 name = ParseQualifiedName() {
477 if (!type.equals(""))
478 type = type + " " + name.fullName;
480 type = name.fullName;
483 ((str = "const".image {
484 if (!type.equals(""))
485 type = type + " " + str;
489 (str = <STAR>.image {
490 if (!type.equals(""))
491 type = type + " " + str;
496 if (!type.equals(""))
497 type = type + " " + str;
516 System.out.println(str);
521 System.out.println(func);
526 String ParameterizedName() :
532 (str = <IDENTIFIER>.image {res = str;})
533 ("<" str = Type() { res = res + "<" + str; }
534 ("," str = Type() { res = res + ", " + str; })* ">"
542 FunctionHeader FuncDecl() :
545 QualifiedName funcName;
546 ArrayList<VariableDeclaration> args;
549 (<STATIC> | <INLINE>)*
551 funcName = ParseQualifiedName()
552 args = FormalParamList()
554 FunctionHeader res = new FunctionHeader(ret, funcName, args);
555 //System.out.println(res);
560 QualifiedName ParseQualifiedName() :
562 String qualifiedName, str;
565 { qualifiedName = ""; }
566 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
567 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
570 QualifiedName res = new QualifiedName(qualifiedName);
571 //System.out.println(res);
576 ArrayList<String> TemplateParamList() :
578 ArrayList<String> params;
583 params = new ArrayList<String>();
587 (str = <IDENTIFIER>.image
588 str = <IDENTIFIER>.image {params.add(str);})
590 ("," str = <IDENTIFIER>.image
591 str = <IDENTIFIER>.image {params.add(str);})*
594 //System.out.println(params);
599 ArrayList<VariableDeclaration > FormalParamList() :
601 ArrayList<VariableDeclaration > typeParams;
602 VariableDeclaration varDecl;
606 typeParams = new ArrayList<VariableDeclaration >();
609 ((varDecl = TypeParam() {typeParams.add(varDecl);})
610 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
617 VariableDeclaration TypeParam() :
622 (type = Type()) (param = <IDENTIFIER>.image)
624 return new VariableDeclaration(type, param);
628 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
632 boolean newLine = false;
633 boolean inTemplate = false;
634 ArrayList<String> content;
641 content = new ArrayList<String>();
646 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
647 (t = <TEMPLATE> { inTemplate = true; })|
648 t = <STATIC> | t = <INLINE> |
653 if (headers != null) {
654 headers.add(header.substring(header.lastIndexOf(' ') + 1));
657 | t = <IDENTIFIER> | t = <POUND> |
658 (t = <OPEN_BRACE> { newLine = true; } ) |
659 (t = <CLOSE_BRACE> { newLine = true; } ) |
660 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
661 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
662 | t = <HB_SYMBOL> | t = <COMMA> |
663 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
664 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
666 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
667 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
668 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
669 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
670 (t = <SEMI_COLON> { newLine = true; } )
671 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
672 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
673 (t = <DEFINE> { newLine = true; } )
676 if (text.equals("")) {
679 text = text + " " + t.image;
691 if (content.size() == 0) {
699 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
702 ArrayList<String> code;
708 _constructs = constructs;
710 ((inst = ParseSpec() { _constructs.add(inst); }) |
711 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
715 Construct ParseSpec() :
721 LOOKAHEAD(2) res = Global_construct() |
722 LOOKAHEAD(2) res = Interface() |
723 LOOKAHEAD(2) res = Potential_commit_point_define() |
724 LOOKAHEAD(2) res = Commit_point_define() |
725 LOOKAHEAD(2) res = Commit_point_define_check() |
726 LOOKAHEAD(2) res = Entry_point() |
727 LOOKAHEAD(2) res = Class_begin() |
728 LOOKAHEAD(2) res = Class_end() |
729 LOOKAHEAD(2) res = Interface_define()
732 //System.out.println(res);
737 GlobalConstruct Global_construct() :
740 SequentialDefineSubConstruct code;
741 HashMap<String, String> options;
747 options = new HashMap<String, String>();
751 ((key = <IDENTIFIER>.image)
753 (value = <IDENTIFIER>.image)
755 if (options.containsKey(key)) {
756 throw new ParseException("Duplicate options!");
758 options.put(key, value);
763 (code = Global_define())
764 { res = new GlobalConstruct(_file, _content.size(), code, options); }
765 (Interface_clusters(res))?
766 (Happens_before(res))?
769 res.unfoldInterfaceCluster();
774 SequentialDefineSubConstruct Global_define() :
776 ArrayList<String> initVar, defineFunc, code, declareStruct;
777 ArrayList<ArrayList<String>> defineFuncs;
778 ArrayList<VariableDeclaration> declareVars;
779 ArrayList<ArrayList<String>> declareStructs;
780 VariableDeclaration declareVar;
785 declareVars = new ArrayList<VariableDeclaration>();
787 defineFuncs = new ArrayList<ArrayList<String>>();
788 declareStructs = new ArrayList<ArrayList<String>>();
791 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
792 declareStructs.add(declareStruct); }))*
793 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
794 declareVars.add(declareVar); } )*))?
795 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
796 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
798 SequentialDefineSubConstruct res = new
799 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
800 //System.out.println(res);
805 ConditionalInterface Conditional_interface() :
807 String interfaceName, hbConditionLabel;
811 hbConditionLabel = "";
813 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
814 <IDENTIFIER>.image <CLOSE_PAREN>)?
816 return new ConditionalInterface(interfaceName, hbConditionLabel);
820 void Interface_cluster(GlobalConstruct inst) :
823 ConditionalInterface condInterface;
826 (clusterName= <IDENTIFIER>.image)
827 <EQUALS> <OPEN_BRACE>
828 (condInterface = Conditional_interface()
829 { inst.addInterface2Cluster(clusterName, condInterface); }
831 (<COMMA> condInterface = Conditional_interface()
832 { inst.addInterface2Cluster(clusterName, condInterface); }
837 void Interface_clusters(GlobalConstruct inst) :
840 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
843 void Happens_before(GlobalConstruct inst) :
845 ConditionalInterface left, right;
850 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
851 { inst.addHBCondition(left, right); }
855 InterfaceConstruct Interface() :
857 InterfaceConstruct res;
858 String interfaceName, condition, idCode, check,
859 postCheck, commitPoint, hbLabel, hbCondition;
860 ArrayList<String> commitPointSet;
861 ArrayList<String> action, postAction;
862 HashMap<String, String> hbConditions;
863 ArrayList<String> content;
868 action = new ArrayList<String>();
873 commitPointSet = new ArrayList<String>();
874 hbConditions = new HashMap<String, String>();
875 postAction = new ArrayList<String>();
878 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
880 (commitPoint = <IDENTIFIER>.image
881 { commitPointSet.add(commitPoint); }
884 (commitPoint = <IDENTIFIER>.image)
886 if (commitPointSet.contains(commitPoint)) {
887 throw new ParseException(interfaceName + " has" +
888 "duplicate commit point labels");
890 commitPointSet.add(commitPoint);
894 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
897 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
898 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
900 if (hbConditions.containsKey(hbLabel)) {
901 throw new ParseException(interfaceName + " has" +
902 "duplicate happens-before condtion labels");
904 hbConditions.put(hbLabel, hbCondition);
907 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
908 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
909 (<ACTION> action = C_CPP_CODE(null))?
910 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
911 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
914 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
915 hbConditions, idCode, check, action, postAction, postCheck);
921 PotentialCPDefineConstruct Potential_commit_point_define() :
923 PotentialCPDefineConstruct res;
924 String label, condition;
925 ArrayList<String> content;
931 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
932 <LABEL> (label = <IDENTIFIER>.image)
935 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
941 CPDefineConstruct Commit_point_define() :
943 CPDefineConstruct res;
944 String label, potentialCPLabel, condition;
945 ArrayList<String> content;
951 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
952 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
953 <LABEL> (label = <IDENTIFIER>.image)
956 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
962 CPDefineCheckConstruct Commit_point_define_check() :
964 CPDefineCheckConstruct res;
965 String label, condition;
966 ArrayList<String> content;
972 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
973 <LABEL> (label = <IDENTIFIER>.image)
976 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
981 EntryPointConstruct Entry_point() :
989 return new EntryPointConstruct(_file, _content.size());
993 ClassBeginConstruct Class_begin() :
1001 return new ClassBeginConstruct(_file, _content.size());
1005 ClassEndConstruct Class_end() :
1013 return new ClassEndConstruct(_file, _content.size());
1017 InterfaceDefineConstruct Interface_define() :
1023 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1026 return new InterfaceDefineConstruct(_file, _content.size(), name);