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>>
284 ((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) {
285 type = type.equals("") ? type + str : type + " " + str;
288 name = ParseQualifiedName() {
289 if (!type.equals(""))
290 type = type + " " + name.fullName;
292 type = name.fullName;
295 ((str = <CONST>.image {
296 if (!type.equals(""))
297 type = type + " " + str;
301 (str = <STAR>.image {
302 if (!type.equals(""))
303 type = type + " " + str;
308 if (!type.equals(""))
309 type = type + " " + str;
328 System.out.println(str);
333 System.out.println(func);
338 String ParameterizedName() :
344 (str = <IDENTIFIER>.image {res = str;})
345 (<LESS_THAN> str = Type() { res = res + "<" + str; }
346 (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
354 FunctionHeader FuncDecl() :
357 QualifiedName funcName;
358 ArrayList<VariableDeclaration> args;
361 (<STATIC> | <INLINE>)*
363 funcName = ParseQualifiedName()
364 args = FormalParamList()
366 FunctionHeader res = new FunctionHeader(ret, funcName, args);
367 //System.out.println(res);
372 QualifiedName ParseQualifiedName() :
374 String qualifiedName, str;
377 { qualifiedName = ""; }
378 (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
379 ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
382 QualifiedName res = new QualifiedName(qualifiedName);
383 //System.out.println(res);
388 ArrayList<VariableDeclaration> TemplateParamList() :
390 ArrayList<VariableDeclaration> params;
396 params = new ArrayList<VariableDeclaration>();
400 (type = <IDENTIFIER>.image
401 name = <IDENTIFIER>.image
403 params.add(new VariableDeclaration(type, name));
407 (<COMMA> type = <IDENTIFIER>.image
408 name = <IDENTIFIER>.image
410 params.add(new VariableDeclaration(type, name));
415 //System.out.println(params);
420 ArrayList<VariableDeclaration > FormalParamList() :
422 ArrayList<VariableDeclaration > typeParams;
423 VariableDeclaration varDecl;
427 typeParams = new ArrayList<VariableDeclaration >();
430 ((varDecl = TypeParam() {typeParams.add(varDecl);})
431 ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
438 VariableDeclaration Declaration() :
443 (type = Type()) (param = <IDENTIFIER>.image) <SEMI_COLON>
445 return new VariableDeclaration(type, param);
449 VariableDeclaration TypeParam() :
454 (type = Type()) (param = <IDENTIFIER>.image)
456 return new VariableDeclaration(type, param);