/* util.jj Grammer definition for utility functions */ options { STATIC = false; JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(UtilParser) package edu.uci.eecs.specCompiler.grammerParser.utilParser; import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader; import edu.uci.eecs.specCompiler.specExtraction.QualifiedName; import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.util.ArrayList; public class UtilParser { public static void main(String[] argvs) throws ParseException, TokenMgrError { try { File f = new File("./grammer/spec1.txt"); FileInputStream fis = new FileInputStream(f); UtilParser parser = new UtilParser(fis); //parser.Test(); System.out.println("Parsing finished!"); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static ArrayList getTemplateArg(String line) throws ParseException { InputStream input = new ByteArrayInputStream(line.getBytes()); UtilParser parser = new UtilParser(input); return parser.TemplateParamList(); } public static FunctionHeader parseFuncHeader(String line) throws ParseException { InputStream input = new ByteArrayInputStream(line.getBytes()); UtilParser parser = new UtilParser(input); return parser.FuncDecl(); } public static String stringArray2String(ArrayList content) { StringBuilder sb = new StringBuilder(); if (content.size() == 1) return content.get(0); for (int i = 0; i < content.size(); i++) { sb.append(content.get(i) + "\n"); } return sb.toString(); } } PARSER_END(UtilParser) SKIP : { " " | "\n" | "\r" | "\r\n" | "\t" } TOKEN : { /* Specification & C/C++ shared tokens */ // Reserved keywords | | | | | | | | <#DIGIT: ["0"-"9"]> | <#LETTER: ["a"-"z", "A"-"Z"]> | | "_") ( | | "_")*> | | | | | | | | "> | | /* C/C++ only token*/ | | | | | | | | | | | | | | | "> | ="> | | | | | | | | >"> | >>"> | | | | | < INTEGER_LITERAL: (["l","L"])? | (["l","L"])? | (["l","L"])?> | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > | < FLOATING_POINT_LITERAL: | > | < #DECIMAL_FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"]> | < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < #HEXADECIMAL_FLOATING_POINT_LITERAL: "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? (["f","F","d","D"])? | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ (["f","F","d","D"])?> | < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ > | < #SPACE: (" " | "\t")+> | < #TO_END_OF_LINE: (~["\n"])+> | /* Macro token */ )? "include" ( | "<" ( | )+ ">")> | )? > } String Type() : { String type; String str; QualifiedName name; } { { type = ""; } ( { type = "const"; } )? (((str = .image | str = .image | str = .image) { type = type + " " + str; })? ( name = ParseQualifiedName() { if (!type.equals("")) type = type + " " + name.fullName; else type = name.fullName; }) ) ((str = .image { if (!type.equals("")) type = type + " " + str; else type = str; }) | (str = .image { if (!type.equals("")) type = type + " " + str; else type = str; }) | (str = .image { if (!type.equals("")) type = type + " " + str; else type = str; }) )* { return type; } } void Test() : { String str; FunctionHeader func; } { /* str = Type() { System.out.println(str); } */ func = FuncDecl() { System.out.println(func); } } String ParameterizedName() : { String res = ""; String str; } { (str = .image {res = str;}) ( str = Type() { res = res + "<" + str; } ( str = Type() { res = res + ", " + str; })* { res = res + ">"; } )? { return res; } } FunctionHeader FuncDecl() : { String ret; QualifiedName funcName; ArrayList args; } { ( | )* ret = Type() funcName = ParseQualifiedName() args = FormalParamList() { FunctionHeader res = new FunctionHeader(ret, funcName, args); //System.out.println(res); return res; } } QualifiedName ParseQualifiedName() : { String qualifiedName, str; } { { qualifiedName = ""; } (str = ParameterizedName() { qualifiedName = qualifiedName + str; } ) ( (str = ParameterizedName() { qualifiedName = qualifiedName + "::" + str; } ))* { QualifiedName res = new QualifiedName(qualifiedName); //System.out.println(res); return res; } } ArrayList TemplateParamList() : { ArrayList params; String type; String name; } { { params = new ArrayList(); }