1 /* util.jj Grammer definition for utility functions */
5 JAVA_UNICODE_ESCAPE = true;
8 PARSER_BEGIN(UtilParser)
9 package edu.uci.eecs.utilParser;
10 import edu.uci.eecs.specExtraction.FunctionHeader;
11 import edu.uci.eecs.specExtraction.QualifiedName;
12 import edu.uci.eecs.specExtraction.VariableDeclaration;
13 //import edu.uci.eecs.specExtraction.WrongAnnotationException;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import java.io.InputStream;
18 import java.io.ByteArrayInputStream;
20 import java.util.ArrayList;
23 public class UtilParser {
24 public static void main(String[] argvs)
25 throws ParseException, TokenMgrError {
27 File f = new File("./grammer/spec1.txt");
28 FileInputStream fis = new FileInputStream(f);
29 UtilParser parser = new UtilParser(fis);
32 System.out.println("Parsing finished!");
33 } catch (FileNotFoundException e) {
38 public static ArrayList<VariableDeclaration> getTemplateArg(String line)
39 throws ParseException {
40 InputStream input = new ByteArrayInputStream(line.getBytes());
41 UtilParser parser = new UtilParser(input);
42 return parser.TemplateParamList();
45 public static FunctionHeader parseFuncHeader(String line)
46 throws ParseException {
47 InputStream input = new ByteArrayInputStream(line.getBytes());
48 UtilParser parser = new UtilParser(input);
49 return parser.FuncDecl();
53 public static String stringArray2String(ArrayList<String> content) {
54 StringBuilder sb = new StringBuilder();
55 if (content.size() == 1)
56 return content.get(0);
57 for (int i = 0; i < content.size(); i++) {
58 sb.append(content.get(i) + "\n");
63 PARSER_END(UtilParser)
80 /* Specification & C/C++ shared tokens */
88 <UNSIGNED: "unsigned">
90 <TEMPLATE: "template">
100 <#LETTER: ["a"-"z", "A"-"Z"]>
102 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
124 /* C/C++ only token*/
157 <GREATER_EQUALS: ">=">
161 <LOGICAL_EQUALS: "==">
177 <DOUBLELESSTHAN: "<<">
179 <DOUBLEGREATERTHAN: ">>">
181 <TRIPLEGREATERTHAN: ">>>">
189 <DIVIDE_EQUALS: "/=">
203 ((~["\"","\\","\n","\r"])
205 ( ["n","t","b","r","f","\\","'","\""]
206 | ["0"-"7"] ( ["0"-"7"] )?
207 | ["0"-"3"] ["0"-"7"]
216 ((~["'","\\","\n","\r"])
218 (["n","t","b","r","f","\\","'","\""]
219 | ["0"-"7"] ( ["0"-"7"] )?
220 | ["0"-"3"] ["0"-"7"]
228 <DECIMAL_LITERAL> (["l","L"])?
229 | <HEX_LITERAL> (["l","L"])?
230 | <OCTAL_LITERAL> (["l","L"])?>
232 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
234 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
236 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
238 < FLOATING_POINT_LITERAL:
239 <DECIMAL_FLOATING_POINT_LITERAL>
240 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
242 < #DECIMAL_FLOATING_POINT_LITERAL:
243 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
244 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
245 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
246 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
248 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
250 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
251 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
252 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
254 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
256 < #SPACE: (" " | "\t")+>
258 < #TO_END_OF_LINE: (~["\n"])+>
261 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
263 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
277 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
279 name = ParseQualifiedName() {
280 if (!type.equals(""))
281 type = type + " " + name.fullName;
283 type = name.fullName;
286 ((str = <CONST>.image {
287 if (!type.equals(""))
288 type = type + " " + str;
292 (str = <STAR>.image {
293 if (!type.equals(""))
294 type = type + " " + str;
299 if (!type.equals(""))
300 type = type + " " + str;
319 System.out.println(str);
324 System.out.println(func);
329 String ParameterizedName() :
335 (str = <IDENTIFIER>.image {res = str;})
336 (<LESS_THAN> str = Type() { res = res + "<" + str; }
337 (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
345 FunctionHeader FuncDecl() :
348 QualifiedName funcName;
349 ArrayList<VariableDeclaration> args;
352 (<STATIC> | <INLINE>)*
354 funcName = ParseQualifiedName()
355 args = FormalParamList()
357 FunctionHeader res = new FunctionHeader(ret, funcName, args);
358 //System.out.println(res);
363 QualifiedName ParseQualifiedName() :
365 String qualifiedName, str;
368 { qualifiedName = ""; }
369 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
370 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
373 QualifiedName res = new QualifiedName(qualifiedName);
374 //System.out.println(res);
379 ArrayList<VariableDeclaration> TemplateParamList() :
381 ArrayList<VariableDeclaration> params;
387 params = new ArrayList<VariableDeclaration>();
391 (type = <IDENTIFIER>.image
392 name = <IDENTIFIER>.image
394 params.add(new VariableDeclaration(type, name));
398 (<COMMA> type = <IDENTIFIER>.image
399 name = <IDENTIFIER>.image
401 params.add(new VariableDeclaration(type, name));
406 //System.out.println(params);
411 ArrayList<VariableDeclaration > FormalParamList() :
413 ArrayList<VariableDeclaration > typeParams;
414 VariableDeclaration varDecl;
418 typeParams = new ArrayList<VariableDeclaration >();
421 ((varDecl = TypeParam() {typeParams.add(varDecl);})
422 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
429 VariableDeclaration TypeParam() :
434 (type = Type()) (param = <IDENTIFIER>.image)
436 return new VariableDeclaration(type, param);