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();
52 public static VariableDeclaration parseDeclaration(String line)
53 throws ParseException {
54 InputStream input = new ByteArrayInputStream(line.getBytes());
55 UtilParser parser = new UtilParser(input);
56 return parser.Declaration();
59 public static String stringArray2String(ArrayList<String> content) {
60 StringBuilder sb = new StringBuilder();
61 if (content.size() == 1)
62 return content.get(0);
63 for (int i = 0; i < content.size(); i++) {
64 sb.append(content.get(i) + "\n");
69 PARSER_END(UtilParser)
86 /* Specification & C/C++ shared tokens */
94 <UNSIGNED: "unsigned">
96 <TEMPLATE: "template">
106 <#LETTER: ["a"-"z", "A"-"Z"]>
108 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
130 /* C/C++ only token*/
163 <GREATER_EQUALS: ">=">
167 <LOGICAL_EQUALS: "==">
183 <DOUBLELESSTHAN: "<<">
185 <DOUBLEGREATERTHAN: ">>">
187 <TRIPLEGREATERTHAN: ">>>">
195 <DIVIDE_EQUALS: "/=">
209 ((~["\"","\\","\n","\r"])
211 ( ["n","t","b","r","f","\\","'","\""]
212 | ["0"-"7"] ( ["0"-"7"] )?
213 | ["0"-"3"] ["0"-"7"]
222 ((~["'","\\","\n","\r"])
224 (["n","t","b","r","f","\\","'","\""]
225 | ["0"-"7"] ( ["0"-"7"] )?
226 | ["0"-"3"] ["0"-"7"]
234 <DECIMAL_LITERAL> (["l","L"])?
235 | <HEX_LITERAL> (["l","L"])?
236 | <OCTAL_LITERAL> (["l","L"])?>
238 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
240 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
242 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
244 < FLOATING_POINT_LITERAL:
245 <DECIMAL_FLOATING_POINT_LITERAL>
246 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
248 < #DECIMAL_FLOATING_POINT_LITERAL:
249 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
250 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
251 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
252 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
254 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
256 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
257 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
258 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
260 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
262 < #SPACE: (" " | "\t")+>
264 < #TO_END_OF_LINE: (~["\n"])+>
267 <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
269 <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
283 (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
285 name = ParseQualifiedName() {
286 if (!type.equals(""))
287 type = type + " " + name.fullName;
289 type = name.fullName;
292 ((str = <CONST>.image {
293 if (!type.equals(""))
294 type = type + " " + str;
298 (str = <STAR>.image {
299 if (!type.equals(""))
300 type = type + " " + str;
305 if (!type.equals(""))
306 type = type + " " + str;
325 System.out.println(str);
330 System.out.println(func);
335 String ParameterizedName() :
341 (str = <IDENTIFIER>.image {res = str;})
342 (<LESS_THAN> str = Type() { res = res + "<" + str; }
343 (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
351 FunctionHeader FuncDecl() :
354 QualifiedName funcName;
355 ArrayList<VariableDeclaration> args;
358 (<STATIC> | <INLINE>)*
360 funcName = ParseQualifiedName()
361 args = FormalParamList()
363 FunctionHeader res = new FunctionHeader(ret, funcName, args);
364 //System.out.println(res);
369 QualifiedName ParseQualifiedName() :
371 String qualifiedName, str;
374 { qualifiedName = ""; }
375 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
376 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
379 QualifiedName res = new QualifiedName(qualifiedName);
380 //System.out.println(res);
385 ArrayList<VariableDeclaration> TemplateParamList() :
387 ArrayList<VariableDeclaration> params;
393 params = new ArrayList<VariableDeclaration>();
397 (type = <IDENTIFIER>.image
398 name = <IDENTIFIER>.image
400 params.add(new VariableDeclaration(type, name));
404 (<COMMA> type = <IDENTIFIER>.image
405 name = <IDENTIFIER>.image
407 params.add(new VariableDeclaration(type, name));
412 //System.out.println(params);
417 ArrayList<VariableDeclaration > FormalParamList() :
419 ArrayList<VariableDeclaration > typeParams;
420 VariableDeclaration varDecl;
424 typeParams = new ArrayList<VariableDeclaration >();
427 ((varDecl = TypeParam() {typeParams.add(varDecl);})
428 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
435 VariableDeclaration Declaration() :
440 (type = Type()) (param = <IDENTIFIER>.image) <SEMI_COLON>
442 return new VariableDeclaration(type, param);
446 VariableDeclaration TypeParam() :
451 (type = Type()) (param = <IDENTIFIER>.image)
453 return new VariableDeclaration(type, param);