From 5fe4d211752fa05df28c1e2b2e17b69492d3a40b Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Thu, 24 Oct 2013 18:07:26 -0700 Subject: [PATCH] small change --- grammer/spec.txt | 16 +- grammer/spec_compiler.jj | 164 +++++++++++++++++- notes/generated_code_examples.txt | 46 +++-- .../codeGenerator/CodeGenerator.java | 26 +-- .../codeGenerator/CodeVariables.java | 1 + .../specExtraction/ClassBeginConstruct.java | 13 ++ .../specExtraction/ClassEndConstruct.java | 13 ++ .../specExtraction/FunctionHeader.java | 23 +++ .../specExtraction/IDExtractor.java | 120 +++++++++++++ .../specExtraction/ParserUtils.java | 26 +++ .../specExtraction/SpecExtractor.java | 49 +++--- test.cc | 45 +++-- test.h | 12 +- 13 files changed, 450 insertions(+), 104 deletions(-) create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/ClassBeginConstruct.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/ClassEndConstruct.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/FunctionHeader.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/IDExtractor.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java diff --git a/grammer/spec.txt b/grammer/spec.txt index 8b8beb3..5fba648 100644 --- a/grammer/spec.txt +++ b/grammer/spec.txt @@ -1,14 +1,2 @@ -int main() { - struct pair *p; - p -> x = 2 - + 3 - 3; - /** - @Begin - @Potential_commit_point_define: - __ATOMIC_RET__ == true - @Label: - Enqueue_Success_Point - @End - */ - return 0; -} +Class* //A::B::id(const char * ch_ptr, int a) +//template < TypeK k, TypeV v> diff --git a/grammer/spec_compiler.jj b/grammer/spec_compiler.jj index 57956dc..440007a 100644 --- a/grammer/spec_compiler.jj +++ b/grammer/spec_compiler.jj @@ -109,21 +109,34 @@ import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar; import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct; import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct; +import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader; public class SpecParser { private static ArrayList _content; private static File _file; private static ArrayList _constructs; + public static void main(String[] argvs) throws ParseException, TokenMgrError { try { + String line = "int* A::B::id(const char * ch_ptr, int a)"; + System.out.println(parseFuncHeader(line)); + File f = new File("./grammer/spec.txt"); FileInputStream fis = new FileInputStream(f); SpecParser parser = new SpecParser(fis); + /** ArrayList content = new ArrayList(); ArrayList constructs = new ArrayList(); parser.Parse(f, content, constructs); + for (int i = 0; i < content.size(); i++) { + System.out.println(content.get(i)); + } + */ + parser.Test(); System.out.println("Parsing finished!"); } catch (FileNotFoundException e) { e.printStackTrace(); @@ -141,6 +154,21 @@ import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct; } } + public static ArrayList getTemplateArg(String line) + throws ParseException { + InputStream input = new ByteArrayInputStream(line.getBytes()); + SpecParser parser = new SpecParser(input); + return parser.TemplateParamList(); + } + + public static FunctionHeader parseFuncHeader(String line) + throws ParseException { + InputStream input = new ByteArrayInputStream(line.getBytes()); + SpecParser parser = new SpecParser(input); + return parser.FuncDecl(); + } + + public static String stringArray2String(ArrayList content) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < content.size(); i++) { @@ -223,6 +251,10 @@ SKIP : { | +| + +| + | | @@ -264,7 +296,9 @@ SKIP : { | | - + +| + | <#DIGIT: ["0"-"9"]> | @@ -421,13 +455,22 @@ String Type() : ("const" { type = "const"; } )? - (("struct" { type = type + " struct"; })? - (str = .image { + (((str = .image | str = .image) { type = type + " " + str; })? + ( + str = QualifiedName() { + if (!type.equals("")) + type = type + " " + str; + else + type = str; + }) + ( + str = ParameterizedName() { if (!type.equals("")) type = type + " " + str; else type = str; - })) + }) + ) ((str = "const".image { if (!type.equals("")) type = type + " " + str; @@ -452,6 +495,82 @@ String Type() : } } +void Test() : +{} +{ + Type() + //FuncDecl() +} + +String ParameterizedName() : +{ + String res = ""; + String str; +} +{ + (str = .image {res = str;}) + ("<" str = .image { res = res + "<" + str; } + ("," str = .image { res = res + ", " + str; })* ">" + { res = res + ">"; } + )? + { + return res; + } +} + +FunctionHeader FuncDecl() : +{ + String ret, qualifiedName, bareName; + ArrayList args; +} +{ + ret = Type() + qualifiedName = QualifiedName() + bareName = .image + args = FormalParamList() + { + FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args); + //System.out.println(res); + return res; + } +} + +String QualifiedName() : +{ + String qualifiedName, str; +} +{ + { qualifiedName = ""; } + (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName + + str + "::"; } ))* + { + return qualifiedName; + } +} + +ArrayList TemplateParamList() : +{ + ArrayList params; + String str; +} +{ + { + params = new ArrayList(); + } +