1 /* util.jj Grammer definition for utility functions */
5 JAVA_UNICODE_ESCAPE = true;
8 PARSER_BEGIN(UtilParser)
9 package edu.uci.eecs.specCompiler.grammerParser.utilParser;
10 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
11 import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
12 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.InputStream;
17 import java.io.ByteArrayInputStream;
19 import java.util.ArrayList;
21 public class UtilParser {
22 public static void main(String[] argvs)
23 throws ParseException, TokenMgrError {
25 File f = new File("./grammer/spec1.txt");
26 FileInputStream fis = new FileInputStream(f);
27 UtilParser parser = new UtilParser(fis);
30 System.out.println("Parsing finished!");
31 } catch (FileNotFoundException e) {
36 public static ArrayList<VariableDeclaration> getTemplateArg(String line)
37 throws ParseException {
38 InputStream input = new ByteArrayInputStream(line.getBytes());
39 UtilParser parser = new UtilParser(input);
40 return parser.TemplateParamList();
43 public static FunctionHeader parseFuncHeader(String line)
44 throws ParseException {
45 InputStream input = new ByteArrayInputStream(line.getBytes());
46 UtilParser parser = new UtilParser(input);
47 return parser.FuncDecl();
51 public static String stringArray2String(ArrayList<String> content) {
52 StringBuilder sb = new StringBuilder();
53 if (content.size() == 1)
54 return content.get(0);
55 for (int i = 0; i < content.size(); i++) {
56 sb.append(content.get(i) + "\n");
61 PARSER_END(UtilParser)
78 /* Specification & C/C++ shared tokens */
86 <UNSIGNED: "unsigned">
88 <TEMPLATE: "template">
98 <#LETTER: ["a"-"z", "A"-"Z"]>
100 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
122 /* C/C++ only token*/
155 <GREATER_EQUALS: ">=">
159 <LOGICAL_EQUALS: "==">
175 <DOUBLELESSTHAN: "<<">
177 <DOUBLEGREATERTHAN: ">>">
179 <TRIPLEGREATERTHAN: ">>>">
187 <DIVIDE_EQUALS: "/=">
201 ((~["\"","\\","\n","\r"])
203 ( ["n","t","b","r","f","\\","'","\""]
204 | ["0"-"7"] ( ["0"-"7"] )?
205 | ["0"-"3"] ["0"-"7"]
214 ((~["'","\\","\n","\r"])
216 (["n","t","b","r","f","\\","'","\""]
217 | ["0"-"7"] ( ["0"-"7"] )?
218 | ["0"-"3"] ["0"-"7"]
226 <DECIMAL_LITERAL> (["l","L"])?
227 | <HEX_LITERAL> (["l","L"])?
228 | <OCTAL_LITERAL> (["l","L"])?>
230 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
232 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
234 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
236 < FLOATING_POINT_LITERAL:
237 <DECIMAL_FLOATING_POINT_LITERAL>
238 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
240 < #DECIMAL_FLOATING_POINT_LITERAL:
241 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
242 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
243 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
244 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
246 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
248 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
249 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
250 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
252 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
254 < #SPACE: (" " | "\t")+>
256 < #TO_END_OF_LINE: (~["\n"])+>
259 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
261 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
275 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
277 name = ParseQualifiedName() {
278 if (!type.equals(""))
279 type = type + " " + name.fullName;
281 type = name.fullName;
284 ((str = <CONST>.image {
285 if (!type.equals(""))
286 type = type + " " + str;
290 (str = <STAR>.image {
291 if (!type.equals(""))
292 type = type + " " + str;
297 if (!type.equals(""))
298 type = type + " " + str;
317 System.out.println(str);
322 System.out.println(func);
327 String ParameterizedName() :
333 (str = <IDENTIFIER>.image {res = str;})
334 (<LESS_THAN> str = Type() { res = res + "<" + str; }
335 (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
343 FunctionHeader FuncDecl() :
346 QualifiedName funcName;
347 ArrayList<VariableDeclaration> args;
350 (<STATIC> | <INLINE>)*
352 funcName = ParseQualifiedName()
353 args = FormalParamList()
355 FunctionHeader res = new FunctionHeader(ret, funcName, args);
356 //System.out.println(res);
361 QualifiedName ParseQualifiedName() :
363 String qualifiedName, str;
366 { qualifiedName = ""; }
367 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
368 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
371 QualifiedName res = new QualifiedName(qualifiedName);
372 //System.out.println(res);
377 ArrayList<VariableDeclaration> TemplateParamList() :
379 ArrayList<VariableDeclaration> params;
385 params = new ArrayList<VariableDeclaration>();
389 (type = <IDENTIFIER>.image
390 name = <IDENTIFIER>.image
392 params.add(new VariableDeclaration(type, name));
396 (<COMMA> type = <IDENTIFIER>.image
397 name = <IDENTIFIER>.image
399 params.add(new VariableDeclaration(type, name));
404 //System.out.println(params);
409 ArrayList<VariableDeclaration > FormalParamList() :
411 ArrayList<VariableDeclaration > typeParams;
412 VariableDeclaration varDecl;
416 typeParams = new ArrayList<VariableDeclaration >();
419 ((varDecl = TypeParam() {typeParams.add(varDecl);})
420 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
427 VariableDeclaration TypeParam() :
432 (type = Type()) (param = <IDENTIFIER>.image)
434 return new VariableDeclaration(type, param);