try {
FileInputStream fis = new FileInputStream("./grammer/spec.txt");
SpecParser parser = new SpecParser(fis);
- parser.Parse();
+ parser.ParseSpec();
System.out.println("Parsing finished!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PARSER_END(SpecParser)
-SKIP :
-{
- " "
-|
- "\n"
-|
- "\r"
-|
- "\r\n"
-|
- "\t"
-|
- // "#" comment for the specification
- <"#" (~["\n", "\r"])* (["\n", "\r"])>
-|
- // "//" comment for the specification
- <"//" (~["\n", "\r"])* (["\n", "\r"])>
+< IN_COMMENT > SKIP : { < ~[] > }
+
+< IN_COMMENT, IN_SPEC > SKIP : {
+ "*/": DEFAULT
}
-TOKEN :
-{
-/* Above are specification-only tokens */
- <HEAD: "/**">
-|
- <TAIL: "*/">
-|
+SKIP : {
+ "/*": IN_COMMENT
+}
+
+SKIP : {
+ "/**": IN_SPEC
+}
+
+<IN_SPEC> TOKEN : {
<BEGIN: "@Begin">
|
<END: "@End">
<COMMIT_POINT_DEFINE: "@Commit_point_define:">
|
<POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
+}
+SKIP :
+{
+ " "
+|
+ "\n"
+|
+ "\r"
+|
+ "\r\n"
+|
+ "\t"
+|
+ // "#" comment for the specification
+ <"#" (~["\n", "\r"])* (["\n", "\r"])>
+|
+ // "//" comment for the specification
+ <"//" (~["\n", "\r"])* (["\n", "\r"])>
+}
+<IN_SPEC, DEFAULT> TOKEN :
+{
/* Specification & C/C++ shared tokens */
+// Reserved keywords
+ <CONST: "const">
+|
+ <STRUCT: "struct">
+|
+ <TYPENAME: "typename">
|
<#DIGIT: ["0"-"9"]>
|
< #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
}
-Construct Parse() :
+String Type() :
{
- Construct res;
+ String type;
+ String str;
}
{
- (
- LOOKAHEAD(3) res = Global_construct() |
- LOOKAHEAD(3) res = Interface() |
- LOOKAHEAD(3) res = Potential_commit_point_define() |
- LOOKAHEAD(3) res = Commit_point_define() |
- LOOKAHEAD(3) res = Commit_point_define_check() |
- LOOKAHEAD(3) res = Entry_point() |
- LOOKAHEAD(3) res = Interface_define()
- )
- <EOF>
+ { type = ""; }
+ ("const"
+ { type = "const"; }
+ )?
+ (("struct" { type = type + " struct"; })?
+ (str = <IDENTIFIER>.image {
+ if (!type.equals(""))
+ type = type + " " + str;
+ else
+ type = str;
+ }))
+ ((str = "const".image {
+ if (!type.equals(""))
+ type = type + " " + str;
+ else
+ type = str;
+ }) |
+ (str = <STAR>.image {
+ if (!type.equals(""))
+ type = type + " " + str;
+ else
+ type = str;
+ }) |
+ (str = <AND>.image {
+ if (!type.equals(""))
+ type = type + " " + str;
+ else
+ type = str;
+ })
+ )*
{
- //System.out.println(res);
- return res;
+ return type;
}
}
-GlobalConstruct Global_construct() :
+ArrayList<String> FormalParamList() :
{
- GlobalConstruct res;
- SequentialDefineSubConstruct code;
- HashMap<String, String> options;
- String key, value;
+ ArrayList<String> typeParams;
}
{
{
- res = null;
- options = new HashMap<String, String>();
+ typeParams = new ArrayList<String>();
}
- <HEAD>
- <BEGIN>
- (<OPTIONS>
- ((key = <IDENTIFIER>.image)
- <EQUALS>
- (value = <IDENTIFIER>.image)
- {
- if (options.containsKey(key)) {
- throw new ParseException("Duplicate options!");
- }
- options.put(key, value);
- }
- <SEMI_COLON>
- )*
- )?
- (code = Global_define())
- { res = new GlobalConstruct(code, options); }
- (Interface_clusters(res))?
- (Happens_before(res))?
- <END>
- <TAIL>
+ (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
{
- res.unfoldInterfaceCluster();
- return res;
+ System.out.println(typeParams);
+ return typeParams;
}
}
+void TypeParam(ArrayList<String> typeParams) :
+{
+ String type, param;
+}
+{
+ (type = Type()) (param = <IDENTIFIER>.image)
+ {
+ typeParams.add(type);
+ typeParams.add(param);
+ }
+}
+
+void ParseSpec() :
+{}
+{
+ <BEGIN> <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE() <LABEL> <END>
+}
+
String C_CPP_CODE() :
{
StringBuilder text;
(
//LOOKAHEAD(2)
(
+ t = <CONST> | t = <STRUCT> |
t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
}
}
-SequentialDefineSubConstruct Global_define() :
-{
- String declareVar, initVar, defineFunc;
-}
-{
- {
- declareVar = "";
- initVar = "";
- defineFunc = "";
- }
- <GLOBAL_DEFINE>
- (<DECLARE_VAR> (declareVar = C_CPP_CODE()))?
- (<INIT_VAR> (initVar = C_CPP_CODE()))?
- (<DEFINE_FUNC> (defineFunc = C_CPP_CODE()))?
- {
- SequentialDefineSubConstruct res = new SequentialDefineSubConstruct(declareVar, initVar, defineFunc);
- //System.out.println(res);
- return res;
- }
-}
-
-ConditionalInterface Conditional_interface() :
-{
- String interfaceName, hbConditionLabel;
-}
-{
- {
- hbConditionLabel = "";
- }
- interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
- <IDENTIFIER>.image <CLOSE_BRACKET>)?
- {
- return new ConditionalInterface(interfaceName, hbConditionLabel);
- }
-}
-
-void Interface_cluster(GlobalConstruct inst) :
-{
- String clusterName;
- ConditionalInterface condInterface;
-}
-{
- (clusterName= <IDENTIFIER>.image)
- <EQUALS> <OPEN_PAREN>
- (condInterface = Conditional_interface()
- { inst.addInterface2Cluster(clusterName, condInterface); }
- )
- (<COMMA> condInterface = Conditional_interface()
- { inst.addInterface2Cluster(clusterName, condInterface); }
- )*
- <CLOSE_PAREN>
-}
-void Interface_clusters(GlobalConstruct inst) :
+void Comment() :
{}
{
- <INTERFACE_CLUSTER> (Interface_cluster(inst))+
-}
-
-void Happens_before(GlobalConstruct inst) :
-{
- ConditionalInterface left, right;
-}
-{
- <HAPPENS_BEFORE>
- (
- left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
- { inst.addHBCondition(left, right); }
- )+
-}
-
-InterfaceConstruct Interface() :
-{
- InterfaceConstruct res;
- String interfaceName, condition, idCode, check, postAction,
- postCheck, commitPoint, hbLabel, hbCondition;
- ActionSubConstruct action;
- ArrayList<String> commitPointSet;
- HashMap<String, String> hbConditions;
-}
-{
- {
- res = null;
- action = null;
- condition = "";
- idCode = "";
- check = "";
- postAction = "";
- postCheck = "";
- commitPointSet = new ArrayList<String>();
- hbConditions = new HashMap<String, String>();
- }
- <HEAD>
- <BEGIN>
- <INTERFACE> (interfaceName = <IDENTIFIER>.image)
- <COMMIT_POINT_SET>
- (commitPoint = <IDENTIFIER>.image
- { commitPointSet.add(commitPoint); }
- )
- (<OR>
- (commitPoint = <IDENTIFIER>.image)
- {
- if (commitPointSet.contains(commitPoint)) {
- throw new ParseException(interfaceName + " has" +
- "duplicate commit point labels");
- }
- commitPointSet.add(commitPoint);
- }
- )*
-
- (<CONDITION> (condition = C_CPP_CODE()))?
- (
- <HB_CONDITION>
- (hbLabel = <IDENTIFIER>.image)
- (hbCondition = C_CPP_CODE())
- {
- if (hbConditions.containsKey(hbLabel)) {
- throw new ParseException(interfaceName + " has" +
- "duplicate happens-before condtion labels");
- }
- hbConditions.put(hbLabel, hbCondition);
- }
- )*
- (<ID> (idCode = C_CPP_CODE()))?
- (<CHECK> (check = C_CPP_CODE()))?
- (action = Action())?
- (<POST_ACTION> (postAction = C_CPP_CODE()))?
- (<POST_CHECK> (postCheck = C_CPP_CODE()))?
- <END>
- <TAIL>
- {
- res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
- hbConditions, idCode, check, action, postAction, postCheck);
- return res;
- }
-}
-
-ActionSubConstruct Action() :
-{
- String type, name, expr, defineVarStr, code;
- ArrayList<DefineVar> defineVars;
-}
-{
- {
- defineVars = new ArrayList<DefineVar>();
- code = "";
- }
- <ACTION>
- (
- (
- (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
- {
- int eqIdx = defineVarStr.indexOf('=');
- int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
- type = defineVarStr.substring(0, typeEnd);
- name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
- expr = defineVarStr.substring(eqIdx + 2);
- DefineVar defineVar = new DefineVar(type, name, expr);
- defineVars.add(defineVar);
- })* (<CODE> (code = C_CPP_CODE()))? )
- )
-
- {
- ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
- return res;
- }
-}
-
-PotentialCPDefineConstruct Potential_commit_point_define() :
-{
- PotentialCPDefineConstruct res;
- String label, condition;
-}
-{
-
- { res = null; }
- <HEAD>
- <BEGIN>
- <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- <TAIL>
- {
- res = new PotentialCPDefineConstruct(label, condition);
- return res;
- }
-}
-
-
-CPDefineConstruct Commit_point_define() :
-{
- CPDefineConstruct res;
- String label, potentialCPLabel, condition;
-}
-{
-
- { res = null; }
- <HEAD>
- <BEGIN>
- <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
- <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- <TAIL>
- {
- res = new CPDefineConstruct(label, potentialCPLabel, condition);
- return res;
- }
+ C_CPP_CODE()
}
-
-CPDefineCheckConstruct Commit_point_define_check() :
-{
- CPDefineCheckConstruct res;
- String label, condition;
-}
-{
-
- { res = null; }
- <HEAD>
- <BEGIN>
- <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- <TAIL>
- {
- res = new CPDefineCheckConstruct(label, condition);
- return res;
- }
-}
-
-EntryPointConstruct Entry_point() :
+void Parse() :
{}
{
-
- <HEAD>
- <BEGIN>
- <ENTRY_POINT>
- <END>
- <TAIL>
- {
- return new EntryPointConstruct();
- }
-}
-
-InterfaceDefineConstruct Interface_define() :
-{
- String name;
-}
-{
- <HEAD>
- <BEGIN>
- <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
- <END>
- <TAIL>
- {
- return new InterfaceDefineConstruct(name);
- }
+ C_CPP_CODE() |
+ <EOF>
}
-
-