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
30 b) Interface construct
34 IDENTIFIER | IDENTIFIER ...
35 @Condition: ... (Optional)
37 IDENTIFIER :: <C_CPP_Condition>
39 @ID: ... (Optional, use default ID)
44 @Post_action: (Optional)
45 @Post_check: (Optional)
48 c) Potential commit construct
50 @Potential_commit_point_define: ...
54 d) Commit point define construct
56 @Commit_point_define_check: ...
63 @Commit_point_define: ...
64 @Potential_commit_point_label: ...
68 e) Entry point construct
73 f) Interface define construct
75 @Interface_define: <Interface_Name>
83 JAVA_UNICODE_ESCAPE = true;
86 PARSER_BEGIN(SpecParser)
87 package edu.uci.eecs.specCompiler.grammerParser;
89 import java.io.FileInputStream;
90 import java.io.FileNotFoundException;
91 import java.io.InputStream;
92 import java.io.ByteArrayInputStream;
94 import java.util.ArrayList;
95 import java.util.HashMap;
96 import java.util.HashSet;
98 import edu.uci.eecs.specCompiler.specExtraction.Construct;
99 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
100 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
101 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
102 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
103 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
104 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
105 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
106 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
107 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
108 import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
109 import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
110 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
111 import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
112 import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
113 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
115 public class SpecParser {
116 private static ArrayList<String> _content;
117 private static File _file;
118 private static ArrayList<Construct> _constructs;
121 public static void main(String[] argvs)
122 throws ParseException, TokenMgrError {
124 File f = new File("./grammer/spec.txt");
125 FileInputStream fis = new FileInputStream(f);
126 SpecParser parser = new SpecParser(fis);
128 ArrayList<String> content = new ArrayList<String>();
129 ArrayList<Construct> constructs = new ArrayList<Construct>();
130 ArrayList<String> headers = new ArrayList<String>();
131 parser.Parse(f, content, constructs, headers);
132 for (int i = 0; i < content.size(); i++) {
133 System.out.println(content.get(i));
136 for (int i = 0; i < constructs.size(); i++) {
137 System.out.println(constructs.get(i));
141 System.out.println("Parsing finished!");
142 } catch (FileNotFoundException e) {
147 public static SourceFileInfo ParseFile(File f)
148 throws ParseException, TokenMgrError {
150 InputStream input = new FileInputStream(f);
151 SpecParser parser = new SpecParser(input);
152 ArrayList<String> content = new ArrayList<String>(),
153 headers = new ArrayList<String>();
154 ArrayList<Construct> constructs = new ArrayList<Construct>();
155 parser.Parse(f, content, constructs, headers);
156 return new SourceFileInfo(f, content, headers, constructs);
157 } catch (FileNotFoundException e) {
163 public static ArrayList<String> getTemplateArg(String line)
164 throws ParseException {
165 InputStream input = new ByteArrayInputStream(line.getBytes());
166 SpecParser parser = new SpecParser(input);
167 return parser.TemplateParamList();
170 public static FunctionHeader parseFuncHeader(String line)
171 throws ParseException {
172 InputStream input = new ByteArrayInputStream(line.getBytes());
173 SpecParser parser = new SpecParser(input);
174 return parser.FuncDecl();
178 public static String stringArray2String(ArrayList<String> content) {
179 StringBuilder sb = new StringBuilder();
180 for (int i = 0; i < content.size(); i++) {
181 sb.append(content.get(i) + "\n");
183 return sb.toString();
187 PARSER_END(SpecParser)
205 "/**" : IN_POTENTIAL_SPEC
208 <IN_POTENTIAL_SPEC> TOKEN : {
209 <BEGIN: "@Begin"> : IN_SPEC
221 // "//" comment for the specification
222 <"//" (~["\n", "\r"])* (["\n", "\r"])>
225 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
229 <IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
233 // "#" comment for the specification
234 <"#" (~["\n", "\r"])* (["\n", "\r"])>
241 <OPTIONS: "@Options:">
243 <GLOBAL_DEFINE: "@Global_define:">
245 <DECLARE_VAR: "@DeclareVar:">
247 <INIT_VAR: "@InitVar:">
249 <DEFINE_FUNC: "@DefineFunc:">
251 <INTERFACE_CLUSTER: "@Interface_cluster:">
253 <HAPPENS_BEFORE: "@Happens_before:">
255 <INTERFACE: "@Interface:">
257 <COMMIT_POINT_SET: "@Commit_point_set:">
259 <ENTRY_POINT: "@Entry_point">
261 <CLASS_BEGIN: "@Class_begin">
263 <CLASS_END: "@Class_end">
265 <INTERFACE_DEFINE: "@Interface_define:">
267 <CONDITION: "@Condition:">
269 <HB_CONDITION: "@HB_condition:">
279 <POST_ACTION: "@Post_action:">
281 <POST_CHECK: "@Post_check:">
283 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
287 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
289 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
291 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
295 <IN_SPEC, DEFAULT> TOKEN :
297 /* Specification & C/C++ shared tokens */
305 <TEMPLATE: "template">
309 <#LETTER: ["a"-"z", "A"-"Z"]>
311 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
333 /* C/C++ only token*/
364 <GREATER_EQUALS: ">=">
368 <LOGICAL_EQUALS: "==">
388 ((~["\"","\\","\n","\r"])
390 ( ["n","t","b","r","f","\\","'","\""]
391 | ["0"-"7"] ( ["0"-"7"] )?
392 | ["0"-"3"] ["0"-"7"]
401 ((~["'","\\","\n","\r"])
403 (["n","t","b","r","f","\\","'","\""]
404 | ["0"-"7"] ( ["0"-"7"] )?
405 | ["0"-"3"] ["0"-"7"]
413 <DECIMAL_LITERAL> (["l","L"])?
414 | <HEX_LITERAL> (["l","L"])?
415 | <OCTAL_LITERAL> (["l","L"])?>
417 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
419 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
421 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
423 < FLOATING_POINT_LITERAL:
424 <DECIMAL_FLOATING_POINT_LITERAL>
425 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
427 < #DECIMAL_FLOATING_POINT_LITERAL:
428 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
429 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
430 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
431 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
433 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
435 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
436 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
437 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
439 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
441 < #SPACE: (" " | "\t")+>
443 < #TO_END_OF_LINE: (~["\n"])+>
446 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
448 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
462 (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })?
464 name = ParseQualifiedName() {
465 if (!type.equals(""))
466 type = type + " " + name.fullName;
468 type = name.fullName;
471 ((str = "const".image {
472 if (!type.equals(""))
473 type = type + " " + str;
477 (str = <STAR>.image {
478 if (!type.equals(""))
479 type = type + " " + str;
484 if (!type.equals(""))
485 type = type + " " + str;
504 System.out.println(str);
509 System.out.println(func);
513 String ParameterizedName() :
519 (str = <IDENTIFIER>.image {res = str;})
520 ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
521 ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
529 FunctionHeader FuncDecl() :
532 QualifiedName funcName;
533 ArrayList<VariableDeclaration> args;
537 funcName = ParseQualifiedName()
538 args = FormalParamList()
540 FunctionHeader res = new FunctionHeader(ret, funcName, args);
541 //System.out.println(res);
546 QualifiedName ParseQualifiedName() :
548 String qualifiedName, str;
551 { qualifiedName = ""; }
552 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
553 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
556 QualifiedName res = new QualifiedName(qualifiedName);
557 //System.out.println(res);
562 ArrayList<String> TemplateParamList() :
564 ArrayList<String> params;
569 params = new ArrayList<String>();
573 (str = <IDENTIFIER>.image
574 str = <IDENTIFIER>.image {params.add(str);})
576 ("," str = <IDENTIFIER>.image
577 str = <IDENTIFIER>.image {params.add(str);})*
580 //System.out.println(params);
585 ArrayList<VariableDeclaration > FormalParamList() :
587 ArrayList<VariableDeclaration > typeParams;
588 VariableDeclaration varDecl;
592 typeParams = new ArrayList<VariableDeclaration >();
595 ((varDecl = TypeParam() {typeParams.add(varDecl);})
596 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
603 VariableDeclaration TypeParam() :
608 (type = Type()) (param = <IDENTIFIER>.image)
610 return new VariableDeclaration(type, param);
614 ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
618 boolean newLine = false;
619 boolean inTemplate = false;
620 ArrayList<String> content;
627 content = new ArrayList<String>();
632 t = <CONST> | t = <STRUCT> | t = <CLASS> |
633 (t = <TEMPLATE> { inTemplate = true; })|
634 (header = <INCLUDE>.image {
635 if (headers != null) {
636 headers.add(header.substring(header.lastIndexOf(' ') + 1));
639 | t = <IDENTIFIER> | t = <POUND> |
640 (t = <OPEN_BRACE> { newLine = true; } ) |
641 (t = <CLOSE_BRACE> { newLine = true; } ) |
642 t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
643 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
644 | t = <HB_SYMBOL> | t = <COMMA> |
645 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
646 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
648 (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
649 t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
650 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
651 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
652 (t = <SEMI_COLON> { newLine = true; } )
653 | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
654 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
655 (t = <DEFINE> { newLine = true; } )
658 text = text + " " + t.image;
673 void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
676 ArrayList<String> code;
682 _constructs = constructs;
684 ((inst = ParseSpec() { _constructs.add(inst); }) |
685 ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
689 Construct ParseSpec() :
695 LOOKAHEAD(2) res = Global_construct() |
696 LOOKAHEAD(2) res = Interface() |
697 LOOKAHEAD(2) res = Potential_commit_point_define() |
698 LOOKAHEAD(2) res = Commit_point_define() |
699 LOOKAHEAD(2) res = Commit_point_define_check() |
700 LOOKAHEAD(2) res = Entry_point() |
701 LOOKAHEAD(2) res = Class_begin() |
702 LOOKAHEAD(2) res = Class_end() |
703 LOOKAHEAD(2) res = Interface_define()
706 //System.out.println(res);
711 GlobalConstruct Global_construct() :
714 SequentialDefineSubConstruct code;
715 HashMap<String, String> options;
721 options = new HashMap<String, String>();
725 ((key = <IDENTIFIER>.image)
727 (value = <IDENTIFIER>.image)
729 if (options.containsKey(key)) {
730 throw new ParseException("Duplicate options!");
732 options.put(key, value);
737 (code = Global_define())
738 { res = new GlobalConstruct(_file, _content.size(), code, options); }
739 (Interface_clusters(res))?
740 (Happens_before(res))?
743 res.unfoldInterfaceCluster();
748 SequentialDefineSubConstruct Global_define() :
750 ArrayList<String> initVar, defineFunc, code;
751 ArrayList<ArrayList<String>> defineFuncs;
752 ArrayList<VariableDeclaration> declareVars;
753 VariableDeclaration declareVar;
760 defineFuncs = new ArrayList<ArrayList<String>>();
763 (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
764 declareVars.add(declareVar); } )*))?
765 (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
766 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
768 SequentialDefineSubConstruct res = new
769 SequentialDefineSubConstruct(declareVars, initVar, defineFuncs);
770 //System.out.println(res);
775 ConditionalInterface Conditional_interface() :
777 String interfaceName, hbConditionLabel;
781 hbConditionLabel = "";
783 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
784 <IDENTIFIER>.image <CLOSE_BRACKET>)?
786 return new ConditionalInterface(interfaceName, hbConditionLabel);
790 void Interface_cluster(GlobalConstruct inst) :
793 ConditionalInterface condInterface;
796 (clusterName= <IDENTIFIER>.image)
797 <EQUALS> <OPEN_PAREN>
798 (condInterface = Conditional_interface()
799 { inst.addInterface2Cluster(clusterName, condInterface); }
801 (<COMMA> condInterface = Conditional_interface()
802 { inst.addInterface2Cluster(clusterName, condInterface); }
807 void Interface_clusters(GlobalConstruct inst) :
810 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
813 void Happens_before(GlobalConstruct inst) :
815 ConditionalInterface left, right;
820 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
821 { inst.addHBCondition(left, right); }
825 InterfaceConstruct Interface() :
827 InterfaceConstruct res;
828 String interfaceName, condition, idCode, check,
829 postCheck, commitPoint, hbLabel, hbCondition;
830 ArrayList<String> commitPointSet;
831 ArrayList<String> action, postAction;
832 HashMap<String, String> hbConditions;
833 ArrayList<String> content;
843 commitPointSet = new ArrayList<String>();
844 hbConditions = new HashMap<String, String>();
850 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
852 (commitPoint = <IDENTIFIER>.image
853 { commitPointSet.add(commitPoint); }
856 (commitPoint = <IDENTIFIER>.image)
858 if (commitPointSet.contains(commitPoint)) {
859 throw new ParseException(interfaceName + " has" +
860 "duplicate commit point labels");
862 commitPointSet.add(commitPoint);
866 (<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
869 (hbLabel = <IDENTIFIER>.image)
870 (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
872 if (hbConditions.containsKey(hbLabel)) {
873 throw new ParseException(interfaceName + " has" +
874 "duplicate happens-before condtion labels");
876 hbConditions.put(hbLabel, hbCondition);
879 (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
880 (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
881 (<ACTION> action = C_CPP_CODE(null))?
882 (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
883 (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
886 res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
887 hbConditions, idCode, check, action, postAction, postCheck);
893 PotentialCPDefineConstruct Potential_commit_point_define() :
895 PotentialCPDefineConstruct res;
896 String label, condition;
897 ArrayList<String> content;
903 <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
904 <LABEL> (label = <IDENTIFIER>.image)
907 res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
913 CPDefineConstruct Commit_point_define() :
915 CPDefineConstruct res;
916 String label, potentialCPLabel, condition;
917 ArrayList<String> content;
923 <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
924 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
925 <LABEL> (label = <IDENTIFIER>.image)
928 res = new CPDefineConstruct(_file, _content.size(), label, potentialCPLabel, condition);
934 CPDefineCheckConstruct Commit_point_define_check() :
936 CPDefineCheckConstruct res;
937 String label, condition;
938 ArrayList<String> content;
944 <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
945 <LABEL> (label = <IDENTIFIER>.image)
948 res = new CPDefineCheckConstruct(_file, _content.size(), label, condition);
953 EntryPointConstruct Entry_point() :
961 return new EntryPointConstruct(_file, _content.size());
965 ClassBeginConstruct Class_begin() :
973 return new ClassBeginConstruct(_file, _content.size());
977 ClassEndConstruct Class_end() :
985 return new ClassEndConstruct(_file, _content.size());
989 InterfaceDefineConstruct Interface_define() :
995 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
998 return new InterfaceDefineConstruct(_file, _content.size(), name);