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 for (int i = 0; i < content.size(); i++) {
183 sb.append(content.get(i) + "\n");
185 return sb.toString();
189 PARSER_END(SpecParser)
207 "/**" : IN_POTENTIAL_SPEC
210 <IN_POTENTIAL_SPEC> TOKEN : {
211 <BEGIN: "@Begin"> : IN_SPEC
223 // "//" comment for the specification
224 <"//" (~["\n", "\r"])* (["\n", "\r"])>
227 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
231 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
235 // "#" comment for the specification
236 <"#" (~["\n", "\r"])* (["\n", "\r"])>
243 <OPTIONS: "@Options:">
245 <GLOBAL_DEFINE: "@Global_define:">
247 <DECLARE_STRUCT: "@DeclareStruct:">
249 <DECLARE_VAR: "@DeclareVar:">
251 <INIT_VAR: "@InitVar:">
253 <DEFINE_FUNC: "@DefineFunc:">
255 <INTERFACE_CLUSTER: "@Interface_cluster:">
257 <HAPPENS_BEFORE: "@Happens_before:">
259 <INTERFACE: "@Interface:">
261 <COMMIT_POINT_SET: "@Commit_point_set:">
263 <ENTRY_POINT: "@Entry_point">
265 <CLASS_BEGIN: "@Class_begin">
267 <CLASS_END: "@Class_end">
269 <INTERFACE_DEFINE: "@Interface_define:">
271 <CONDITION: "@Condition:">
273 <HB_CONDITION: "@HB_condition:">
283 <POST_ACTION: "@Post_action:">
285 <POST_CHECK: "@Post_check:">
287 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
291 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
293 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
295 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
299 <IN_SPEC, DEFAULT> TOKEN :
301 /* Specification & C/C++ shared tokens */
309 <UNSIGNED: "unsigned">
311 <TEMPLATE: "template">
315 <#LETTER: ["a"-"z", "A"-"Z"]>
317 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
339 /* C/C++ only token*/
370 <GREATER_EQUALS: ">=">
374 <LOGICAL_EQUALS: "==">
394 ((~["\"","\\","\n","\r"])
396 ( ["n","t","b","r","f","\\","'","\""]
397 | ["0"-"7"] ( ["0"-"7"] )?
398 | ["0"-"3"] ["0"-"7"]
407 ((~["'","\\","\n","\r"])
409 (["n","t","b","r","f","\\","'","\""]
410 | ["0"-"7"] ( ["0"-"7"] )?
411 | ["0"-"3"] ["0"-"7"]
419 <DECIMAL_LITERAL> (["l","L"])?
420 | <HEX_LITERAL> (["l","L"])?
421 | <OCTAL_LITERAL> (["l","L"])?>
423 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
425 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
427 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
429 < FLOATING_POINT_LITERAL:
430 <DECIMAL_FLOATING_POINT_LITERAL>
431 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
433 < #DECIMAL_FLOATING_POINT_LITERAL:
434 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
435 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
436 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
437 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
439 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
441 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
442 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
443 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
445 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
447 < #SPACE: (" " | "\t")+>
449 < #TO_END_OF_LINE: (~["\n"])+>
452 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
454 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
468 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
470 name = ParseQualifiedName() {
471 if (!type.equals(""))
472 type = type + " " + name.fullName;
474 type = name.fullName;
477 ((str = "const".image {
478 if (!type.equals(""))
479 type = type + " " + str;
483 (str = <STAR>.image {
484 if (!type.equals(""))
485 type = type + " " + str;
490 if (!type.equals(""))
491 type = type + " " + str;
510 System.out.println(str);
515 System.out.println(func);
521 String ParameterizedName() :
527 (str = <IDENTIFIER>.image {res = str;})
528 ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
529 ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
537 FunctionHeader FuncDecl() :
540 QualifiedName funcName;
541 ArrayList<VariableDeclaration> args;
545 funcName = ParseQualifiedName()
546 args = FormalParamList()
548 FunctionHeader res = new FunctionHeader(ret, funcName, args);
549 //System.out.println(res);
554 QualifiedName ParseQualifiedName() :
556 String qualifiedName, str;
559 { qualifiedName = ""; }
560 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
561 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
564 QualifiedName res = new QualifiedName(qualifiedName);
565 //System.out.println(res);
570 ArrayList<String> TemplateParamList() :
572 ArrayList<String> params;
577 params = new ArrayList<String>();
581 (str = <IDENTIFIER>.image
582 str = <IDENTIFIER>.image {params.add(str);})
584 ("," str = <IDENTIFIER>.image
585 str = <IDENTIFIER>.image {params.add(str);})*
588 //System.out.println(params);
593 ArrayList<VariableDeclaration > FormalParamList() :
595 ArrayList<VariableDeclaration > typeParams;
596 VariableDeclaration varDecl;
600 typeParams = new ArrayList<VariableDeclaration >();
603 ((varDecl = TypeParam() {typeParams.add(varDecl);})
604 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
611 VariableDeclaration TypeParam() :
616 (type = Type()) (param = <IDENTIFIER>.image)
618 return new VariableDeclaration(type, param);
622 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
626 boolean newLine = false;
627 boolean inTemplate = false;
628 ArrayList<String> content;
635 content = new ArrayList<String>();
640 t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
641 (t = <TEMPLATE> { inTemplate = true; })|
646 if (headers != null) {
647 headers.add(header.substring(header.lastIndexOf(' ') + 1));
650 | t = <IDENTIFIER> | t = <POUND> |
651 (t = <OPEN_BRACE> { newLine = true; } ) |
652 (t = <CLOSE_BRACE> { newLine = true; } ) |
653 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
654 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
655 | t = <HB_SYMBOL> | t = <COMMA> |
656 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
657 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
659 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
660 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
661 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
662 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
663 (t = <SEMI_COLON> { newLine = true; } )
664 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
665 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
666 (t = <DEFINE> { newLine = true; } )
669 text = text + " " + t.image;
679 if (content.size() == 0) {
687 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
690 ArrayList<String> code;
696 _constructs = constructs;
698 ((inst = ParseSpec() { _constructs.add(inst); }) |
699 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
703 Construct ParseSpec() :
709 LOOKAHEAD(2) res = Global_construct() |
710 LOOKAHEAD(2) res = Interface() |
711 LOOKAHEAD(2) res = Potential_commit_point_define() |
712 LOOKAHEAD(2) res = Commit_point_define() |
713 LOOKAHEAD(2) res = Commit_point_define_check() |
714 LOOKAHEAD(2) res = Entry_point() |
715 LOOKAHEAD(2) res = Class_begin() |
716 LOOKAHEAD(2) res = Class_end() |
717 LOOKAHEAD(2) res = Interface_define()
720 //System.out.println(res);
725 GlobalConstruct Global_construct() :
728 SequentialDefineSubConstruct code;
729 HashMap<String, String> options;
735 options = new HashMap<String, String>();
739 ((key = <IDENTIFIER>.image)
741 (value = <IDENTIFIER>.image)
743 if (options.containsKey(key)) {
744 throw new ParseException("Duplicate options!");
746 options.put(key, value);
751 (code = Global_define())
752 { res = new GlobalConstruct(_file, _content.size(), code, options); }
753 (Interface_clusters(res))?
754 (Happens_before(res))?
757 res.unfoldInterfaceCluster();
762 SequentialDefineSubConstruct Global_define() :
764 ArrayList<String> initVar, defineFunc, code, declareStruct;
765 ArrayList<ArrayList<String>> defineFuncs;
766 ArrayList<VariableDeclaration> declareVars;
767 ArrayList<ArrayList<String>> declareStructs;
768 VariableDeclaration declareVar;
773 declareVars = new ArrayList<VariableDeclaration>();
775 defineFuncs = new ArrayList<ArrayList<String>>();
776 declareStructs = new ArrayList<ArrayList<String>>();
779 (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
780 declareStructs.add(declareStruct); }))*
781 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
782 declareVars.add(declareVar); } )*))?
783 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
784 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
786 SequentialDefineSubConstruct res = new
787 SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
788 //System.out.println(res);
793 ConditionalInterface Conditional_interface() :
795 String interfaceName, hbConditionLabel;
799 hbConditionLabel = "";
801 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
802 <IDENTIFIER>.image <CLOSE_BRACKET>)?
804 return new ConditionalInterface(interfaceName, hbConditionLabel);
808 void Interface_cluster(GlobalConstruct inst) :
811 ConditionalInterface condInterface;
814 (clusterName= <IDENTIFIER>.image)
815 <EQUALS> <OPEN_PAREN>
816 (condInterface = Conditional_interface()
817 { inst.addInterface2Cluster(clusterName, condInterface); }
819 (<COMMA> condInterface = Conditional_interface()
820 { inst.addInterface2Cluster(clusterName, condInterface); }
825 void Interface_clusters(GlobalConstruct inst) :
828 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
831 void Happens_before(GlobalConstruct inst) :
833 ConditionalInterface left, right;
838 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
839 { inst.addHBCondition(left, right); }
843 InterfaceConstruct Interface() :
845 InterfaceConstruct res;
846 String interfaceName, condition, idCode, check,
847 postCheck, commitPoint, hbLabel, hbCondition;
848 ArrayList<String> commitPointSet;
849 ArrayList<String> action, postAction;
850 HashMap<String, String> hbConditions;
851 ArrayList<String> content;
856 action = new ArrayList<String>();
861 commitPointSet = new ArrayList<String>();
862 hbConditions = new HashMap<String, String>();
863 postAction = new ArrayList<String>();
866 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
868 (commitPoint = <IDENTIFIER>.image
869 { commitPointSet.add(commitPoint); }
872 (commitPoint = <IDENTIFIER>.image)
874 if (commitPointSet.contains(commitPoint)) {
875 throw new ParseException(interfaceName + " has" +
876 "duplicate commit point labels");
878 commitPointSet.add(commitPoint);
882 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
885 (hbLabel = <IDENTIFIER>.image)
886 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
888 if (hbConditions.containsKey(hbLabel)) {
889 throw new ParseException(interfaceName + " has" +
890 "duplicate happens-before condtion labels");
892 hbConditions.put(hbLabel, hbCondition);
895 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
896 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
897 (<ACTION> action = C_CPP_CODE(null))?
898 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
899 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
902 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
903 hbConditions, idCode, check, action, postAction, postCheck);
909 PotentialCPDefineConstruct Potential_commit_point_define() :
911 PotentialCPDefineConstruct res;
912 String label, condition;
913 ArrayList<String> content;
919 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
920 <LABEL> (label = <IDENTIFIER>.image)
923 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
929 CPDefineConstruct Commit_point_define() :
931 CPDefineConstruct res;
932 String label, potentialCPLabel, condition;
933 ArrayList<String> content;
939 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
940 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
941 <LABEL> (label = <IDENTIFIER>.image)
944 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
950 CPDefineCheckConstruct Commit_point_define_check() :
952 CPDefineCheckConstruct res;
953 String label, condition;
954 ArrayList<String> content;
960 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
961 <LABEL> (label = <IDENTIFIER>.image)
964 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
969 EntryPointConstruct Entry_point() :
977 return new EntryPointConstruct(_file, _content.size());
981 ClassBeginConstruct Class_begin() :
989 return new ClassBeginConstruct(_file, _content.size());
993 ClassEndConstruct Class_end() :
1001 return new ClassEndConstruct(_file, _content.size());
1005 InterfaceDefineConstruct Interface_define() :
1011 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
1014 return new InterfaceDefineConstruct(_file, _content.size(), name);