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
29 b) Interface construct
33 IDENTIFIER | IDENTIFIER ...
34 @Condition: ... (Optional)
36 IDENTIFIER :: <C_CPP_Condition>
38 @ID: ... (Optional, use default ID)
42 # Type here must be a pointer
43 @DefineVar: Type var1 = SomeExpression (Optional)
46 @Post_action: (Optional)
47 @Post_check: (Optional)
50 c) Potential commit construct
52 @Potential_commit_point_define: ...
56 d) Commit point define construct
58 @Commit_point_define_check: ...
65 @Commit_point_define: ...
66 @Potential_commit_point_label: ...
70 e) Entry point construct
75 f) Interface define construct
77 @Interface_define: <Interface_Name>
85 JAVA_UNICODE_ESCAPE = true;
88 PARSER_BEGIN(SpecParser)
89 package edu.uci.eecs.specCompiler.grammerParser;
91 import java.io.FileInputStream;
92 import java.io.FileNotFoundException;
93 import java.io.InputStream;
94 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.ActionSubConstruct;
107 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
108 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
109 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
110 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
112 public class SpecParser {
113 public static void main(String[] argvs)
114 throws ParseException, TokenMgrError {
116 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
117 SpecParser parser = new SpecParser(fis);
119 System.out.println("Parsing finished!");
120 } catch (FileNotFoundException e) {
125 public static Construct parseSpec(String text)
126 throws ParseException, TokenMgrError {
127 InputStream input = new ByteArrayInputStream(text.getBytes());
128 SpecParser parser = new SpecParser(input);
129 return parser.Parse();
134 PARSER_END(SpecParser)
136 < IN_COMMENT > SKIP : { < ~[] > }
138 < IN_COMMENT, IN_SPEC > SKIP : {
155 <OPTIONS: "@Options:">
157 <GLOBAL_DEFINE: "@Global_define:">
159 <DECLARE_VAR: "@DeclareVar:">
161 <INIT_VAR: "@InitVar:">
163 <DEFINE_FUNC: "@DefineFunc:">
165 <INTERFACE_CLUSTER: "@Interface_cluster:">
167 <HAPPENS_BEFORE: "@Happens_before:">
169 <INTERFACE: "@Interface:">
171 <COMMIT_POINT_SET: "@Commit_point_set:">
173 <ENTRY_POINT: "@Entry_point">
175 <INTERFACE_DEFINE: "@Interface_define:">
177 <CONDITION: "@Condition:">
179 <HB_CONDITION: "@HB_condition:">
187 <DEFINEVAR: "@DefineVar:">
191 <POST_ACTION: "@Post_action:">
193 <POST_CHECK: "@Post_check:">
195 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
199 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
201 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
203 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
218 // "#" comment for the specification
219 <"#" (~["\n", "\r"])* (["\n", "\r"])>
221 // "//" comment for the specification
222 <"//" (~["\n", "\r"])* (["\n", "\r"])>
225 <IN_SPEC, DEFAULT> TOKEN :
227 /* Specification & C/C++ shared tokens */
233 <TYPENAME: "typename">
237 <#LETTER: ["a"-"z", "A"-"Z"]>
239 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
256 /* C/C++ only token*/
287 <GREATER_EQUALS: ">=">
291 <LOGICAL_EQUALS: "==">
311 ((~["\"","\\","\n","\r"])
313 ( ["n","t","b","r","f","\\","'","\""]
314 | ["0"-"7"] ( ["0"-"7"] )?
315 | ["0"-"3"] ["0"-"7"]
324 ((~["'","\\","\n","\r"])
326 (["n","t","b","r","f","\\","'","\""]
327 | ["0"-"7"] ( ["0"-"7"] )?
328 | ["0"-"3"] ["0"-"7"]
336 <DECIMAL_LITERAL> (["l","L"])?
337 | <HEX_LITERAL> (["l","L"])?
338 | <OCTAL_LITERAL> (["l","L"])?>
340 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
342 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
344 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
346 < FLOATING_POINT_LITERAL:
347 <DECIMAL_FLOATING_POINT_LITERAL>
348 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
350 < #DECIMAL_FLOATING_POINT_LITERAL:
351 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
352 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
353 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
354 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
356 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
358 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
359 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
360 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
362 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
375 (("struct" { type = type + " struct"; })?
376 (str = <IDENTIFIER>.image {
377 if (!type.equals(""))
378 type = type + " " + str;
382 ((str = "const".image {
383 if (!type.equals(""))
384 type = type + " " + str;
388 (str = <STAR>.image {
389 if (!type.equals(""))
390 type = type + " " + str;
395 if (!type.equals(""))
396 type = type + " " + str;
406 ArrayList<String> FormalParamList() :
408 ArrayList<String> typeParams;
412 typeParams = new ArrayList<String>();
414 (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
416 System.out.println(typeParams);
421 void TypeParam(ArrayList<String> typeParams) :
426 (type = Type()) (param = <IDENTIFIER>.image)
428 typeParams.add(type);
429 typeParams.add(param);
436 <BEGIN> <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE() <LABEL> <END>
439 String C_CPP_CODE() :
446 text = new StringBuilder();
452 t = <CONST> | t = <STRUCT> |
453 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
454 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
455 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
456 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
457 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
458 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
459 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
460 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
461 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
464 text.append(t.image);
465 if (t.image.equals(";") || t.image.equals("\\")
466 || t.image.equals("{") || t.image.equals("}"))
473 //System.out.println(text);
474 return text.toString();