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 boolean spaceSeparator(Token t) {
259 PARSER_END(SpecParser)
277 "/**" : IN_POTENTIAL_SPEC
280 <IN_POTENTIAL_SPEC> TOKEN : {
281 <BEGIN: "@Begin"> : IN_SPEC
293 // "//" comment for the specification
294 <"//" (~["\n", "\r"])* (["\n", "\r"])>
297 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
301 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
305 // "#" comment for the specification
306 <"#" (~["\n", "\r"])* (["\n", "\r"])>
313 <OPTIONS: "@Options:">
315 <GLOBAL_DEFINE: "@Global_define:">
317 <DECLARE_STRUCT: "@DeclareStruct:">
319 <DECLARE_VAR: "@DeclareVar:">
321 <INIT_VAR: "@InitVar:">
323 <DEFINE_FUNC: "@DefineFunc:">
325 <INTERFACE_CLUSTER: "@Interface_cluster:">
327 <HAPPENS_BEFORE: "@Happens_before:">
329 <INTERFACE: "@Interface:">
331 <COMMIT_POINT_SET: "@Commit_point_set:">
333 <ENTRY_POINT: "@Entry_point">
335 <CLASS_BEGIN: "@Class_begin">
337 <CLASS_END: "@Class_end">
339 <INTERFACE_DEFINE: "@Interface_define:">
341 <CONDITION: "@Condition:">
343 <HB_CONDITION: "@HB_condition:">
353 <POST_ACTION: "@Post_action:">
355 <POST_CHECK: "@Post_check:">
357 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
361 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
363 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
365 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
369 <IN_SPEC, DEFAULT> TOKEN :
371 /* Specification & C/C++ shared tokens */
379 <UNSIGNED: "unsigned">
381 <TEMPLATE: "template">
391 <#LETTER: ["a"-"z", "A"-"Z"]>
393 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
415 /* C/C++ only token*/
446 <GREATER_EQUALS: ">=">
450 <LOGICAL_EQUALS: "==">
466 <DOUBLELESSTHAN: "<<">
468 <DOUBLEGREATERTHAN: ">>">
470 <TRIPLEGREATERTHAN: ">>>">
478 <DIVIDE_EQUALS: "/=">
492 ((~["\"","\\","\n","\r"])
494 ( ["n","t","b","r","f","\\","'","\""]
495 | ["0"-"7"] ( ["0"-"7"] )?
496 | ["0"-"3"] ["0"-"7"]
505 ((~["'","\\","\n","\r"])
507 (["n","t","b","r","f","\\","'","\""]
508 | ["0"-"7"] ( ["0"-"7"] )?
509 | ["0"-"3"] ["0"-"7"]
517 <DECIMAL_LITERAL> (["l","L"])?
518 | <HEX_LITERAL> (["l","L"])?
519 | <OCTAL_LITERAL> (["l","L"])?>
521 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
523 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
525 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
527 < FLOATING_POINT_LITERAL:
528 <DECIMAL_FLOATING_POINT_LITERAL>
529 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
531 < #DECIMAL_FLOATING_POINT_LITERAL:
532 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
533 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
534 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
535 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
537 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
539 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
540 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
541 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
543 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
545 < #SPACE: (" " | "\t")+>
547 < #TO_END_OF_LINE: (~["\n"])+>
550 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
552 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
566 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
568 name = ParseQualifiedName() {
569 if (!type.equals(""))
570 type = type + " " + name.fullName;
572 type = name.fullName;
575 ((str = "const".image {
576 if (!type.equals(""))
577 type = type + " " + str;
581 (str = <STAR>.image {
582 if (!type.equals(""))
583 type = type + " " + str;
588 if (!type.equals(""))
589 type = type + " " + str;
608 System.out.println(str);
613 System.out.println(func);
618 String ParameterizedName() :
624 (str = <IDENTIFIER>.image {res = str;})
625 ("<" str = Type() { res = res + "<" + str; }
626 ("," str = Type() { res = res + ", " + str; })* ">"
634 FunctionHeader FuncDecl() :
637 QualifiedName funcName;
638 ArrayList<VariableDeclaration> args;
641 (<STATIC> | <INLINE>)*
643 funcName = ParseQualifiedName()
644 args = FormalParamList()
646 FunctionHeader res = new FunctionHeader(ret, funcName, args);
647 //System.out.println(res);
652 QualifiedName ParseQualifiedName() :
654 String qualifiedName, str;
657 { qualifiedName = ""; }
658 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
659 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
662 QualifiedName res = new QualifiedName(qualifiedName);
663 //System.out.println(res);
668 ArrayList<VariableDeclaration> TemplateParamList() :
670 ArrayList<VariableDeclaration> params;
676 params = new ArrayList<VariableDeclaration>();
680 (type = <IDENTIFIER>.image
681 name = <IDENTIFIER>.image
683 params.add(new VariableDeclaration(type, name));
687 ("," type = <IDENTIFIER>.image
688 name = <IDENTIFIER>.image
690 params.add(new VariableDeclaration(type, name));
695 //System.out.println(params);
700 ArrayList<VariableDeclaration > FormalParamList() :
702 ArrayList<VariableDeclaration > typeParams;
703 VariableDeclaration varDecl;
707 typeParams = new ArrayList<VariableDeclaration >();
710 ((varDecl = TypeParam() {typeParams.add(varDecl);})
711 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
718 VariableDeclaration TypeParam() :
723 (type = Type()) (param = <IDENTIFIER>.image)
725 return new VariableDeclaration(type, param);
731 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
735 boolean newLine = false;
736 boolean newSpace = true;
737 boolean inTemplate = false;
738 boolean inForLoop = false;
739 ArrayList<String> content;
746 content = new ArrayList<String>();
751 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
752 (t = <TEMPLATE> { inTemplate = true; })|
753 t = <STATIC> | t = <INLINE> |
754 (t = <FOR> { inForLoop = true; })|
759 if (headers != null) {
760 headers.add(header.substring(header.lastIndexOf(' ') + 1));
763 | t = <IDENTIFIER> | t = <POUND> |
764 (t = <OPEN_BRACE> { newLine = true; } ) |
765 (t = <CLOSE_BRACE> { newLine = true; inForLoop = false;} ) |
766 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
767 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
768 | t = <HB_SYMBOL> | t = <COMMA> |
769 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
770 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
772 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
773 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
774 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
775 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
776 t = <DOUBLELESSTHAN> |
777 t = <DOUBLEGREATERTHAN> |
778 t = <TRIPLEGREATERTHAN> |
783 t = <DIVIDE_EQUALS> |
789 (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
790 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
791 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
792 (t = <DEFINE> { newLine = true; } )
795 if (text.equals("")) {
799 text = text + " " + t.image;
801 if (newSpace && spaceSeparator(t)) {
802 text = text + " " + t.image;
804 text = text + t.image;
805 if (spaceSeparator(t))
819 if (content.size() == 0) {
827 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
830 ArrayList<String> code;
836 _constructs = constructs;
838 ((inst = ParseSpec() { _constructs.add(inst); }) |
839 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
843 Construct ParseSpec() :
849 LOOKAHEAD(2) res = Global_construct() |
850 LOOKAHEAD(2) res = Interface() |
851 LOOKAHEAD(2) res = Potential_commit_point_define() |
852 LOOKAHEAD(2) res = Commit_point_define() |
853 LOOKAHEAD(2) res = Commit_point_define_check() |
854 LOOKAHEAD(2) res = Entry_point() |
855 LOOKAHEAD(2) res = Class_begin() |
856 LOOKAHEAD(2) res = Class_end() |
857 LOOKAHEAD(2) res = Interface_define()
860 //System.out.println(res);
865 GlobalConstruct Global_construct() :
868 SequentialDefineSubConstruct code;
869 HashMap<String, String> options;
875 options = new HashMap<String, String>();
879 ((key = <IDENTIFIER>.image)
881 (value = <IDENTIFIER>.image)
883 if (options.containsKey(key)) {
884 throw new ParseException("Duplicate options!");
886 options.put(key, value);
891 (code = Global_define())
892 { res = new GlobalConstruct(_file, _content.size(), code, options); }
893 (Interface_clusters(res))?
894 (Happens_before(res))?
897 res.unfoldInterfaceCluster();
902 SequentialDefineSubConstruct Global_define() :
904 ArrayList<String> initVar, defineFunc, code, declareStruct;
905 ArrayList<ArrayList<String>> defineFuncs;
906 ArrayList<VariableDeclaration> declareVars;
907 ArrayList<ArrayList<String>> declareStructs;
908 VariableDeclaration declareVar;
913 declareVars = new ArrayList<VariableDeclaration>();
915 defineFuncs = new ArrayList<ArrayList<String>>();
916 declareStructs = new ArrayList<ArrayList<String>>();
919 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
920 declareStructs.add(declareStruct); }))*
921 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
922 declareVars.add(declareVar); } )*))?
923 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
924 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
926 SequentialDefineSubConstruct res = new
927 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
928 //System.out.println(res);
933 ConditionalInterface Conditional_interface() :
935 String interfaceName, hbConditionLabel;
939 hbConditionLabel = "";
941 interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
942 <IDENTIFIER>.image <CLOSE_PAREN>)?
944 return new ConditionalInterface(interfaceName, hbConditionLabel);
948 void Interface_cluster(GlobalConstruct inst) :
951 ConditionalInterface condInterface;
954 (clusterName= <IDENTIFIER>.image)
955 <EQUALS> <OPEN_BRACE>
956 (condInterface = Conditional_interface()
957 { inst.addInterface2Cluster(clusterName, condInterface); }
959 (<COMMA> condInterface = Conditional_interface()
960 { inst.addInterface2Cluster(clusterName, condInterface); }
965 void Interface_clusters(GlobalConstruct inst) :
968 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
971 void Happens_before(GlobalConstruct inst) :
973 ConditionalInterface left, right;
978 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
979 { inst.addHBCondition(left, right); }
983 InterfaceConstruct Interface() :
985 InterfaceConstruct res;
986 String interfaceName, condition, idCode, check,
987 postCheck, commitPoint, hbLabel, hbCondition;
988 ArrayList<String> commitPointSet;
989 ArrayList<String> action, postAction;
990 HashMap<String, String> hbConditions;
991 ArrayList<String> content;
996 action = new ArrayList<String>();
1001 commitPointSet = new ArrayList<String>();
1002 hbConditions = new HashMap<String, String>();
1003 postAction = new ArrayList<String>();
1006 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
1008 (commitPoint = <IDENTIFIER>.image
1009 { commitPointSet.add(commitPoint); }
1012 (commitPoint = <IDENTIFIER>.image)
1014 if (commitPointSet.contains(commitPoint)) {
1015 throw new ParseException(interfaceName + " has" +
1016 "duplicate commit point labels");
1018 commitPointSet.add(commitPoint);
1022 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
1025 (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
1026 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
1028 if (hbConditions.containsKey(hbLabel)) {
1029 throw new ParseException(interfaceName + " has" +
1030 "duplicate happens-before condtion labels");
1032 hbConditions.put(hbLabel, hbCondition);
1035 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
1036 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
1037 (<ACTION> action = C_CPP_CODE(null))?
1038 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
1039 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
1042 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
1043 hbConditions, idCode, check, action, postAction, postCheck);
1049 PotentialCPDefineConstruct Potential_commit_point_define() :
1051 PotentialCPDefineConstruct res;
1052 String label, condition;
1053 ArrayList<String> content;
1059 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1060 <LABEL> (label = <IDENTIFIER>.image)
1063 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
1069 CPDefineConstruct Commit_point_define() :
1071 CPDefineConstruct res;
1072 String label, potentialCPLabel, condition;
1073 ArrayList<String> content;
1079 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1080 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
1081 <LABEL> (label = <IDENTIFIER>.image)
1084 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
1090 CPDefineCheckConstruct Commit_point_define_check() :
1092 CPDefineCheckConstruct res;
1093 String label, condition;
1094 ArrayList<String> content;
1100 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
1101 <LABEL> (label = <IDENTIFIER>.image)
1104 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
1109 EntryPointConstruct Entry_point() :
1117 return new EntryPointConstruct(_file, _content.size());
1121 ClassBeginConstruct Class_begin() :
1129 return new ClassBeginConstruct(_file, _content.size());
1133 ClassEndConstruct Class_end() :
1141 return new ClassEndConstruct(_file, _content.size());
1145 InterfaceDefineConstruct Interface_define() :
1151 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1154 return new InterfaceDefineConstruct(_file, _content.size(), name);