From 1d4b1988dce853be3a027adca7e5cd0d61b4855a Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Wed, 23 Oct 2013 04:09:23 -0700 Subject: [PATCH] lots of changes --- grammer/pre_scanner.jj | 52 ++ grammer/spec-compiler.jj | 490 ---------- grammer/spec.txt | 201 +--- grammer/spec_compiler.jj | 857 ++++++++++++++++++ run-javacc.sh | 22 +- .../codeGenerator/CodeGenerator.java | 72 +- .../codeGenerator/CodeVariables.java | 17 +- .../codeGenerator/SemanticsChecker.java | 40 +- .../CPDefineCheckConstruct.java | 10 +- .../specExtraction/CPDefineConstruct.java | 10 +- .../specExtraction/Construct.java | 20 +- .../specExtraction/EntryPointConstruct.java | 6 + .../specExtraction/GlobalConstruct.java | 44 +- .../specExtraction/InterfaceConstruct.java | 13 +- .../InterfaceDefineConstruct.java | 9 +- .../PotentialCPDefineConstruct.java | 10 +- .../specExtraction/SpecConstruct.java | 56 -- .../specExtraction/SpecConstructInfo.java | 5 - .../specExtraction/SpecExtractor.java | 141 +-- .../specExtraction/SpecNotMatchException.java | 9 - test.c | 16 - test.cc | 0 22 files changed, 1083 insertions(+), 1017 deletions(-) create mode 100644 grammer/pre_scanner.jj delete mode 100644 grammer/spec-compiler.jj create mode 100644 grammer/spec_compiler.jj delete mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SpecConstruct.java delete mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SpecConstructInfo.java delete mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SpecNotMatchException.java delete mode 100644 test.c create mode 100644 test.cc diff --git a/grammer/pre_scanner.jj b/grammer/pre_scanner.jj new file mode 100644 index 0000000..301ae88 --- /dev/null +++ b/grammer/pre_scanner.jj @@ -0,0 +1,52 @@ +/* pre_scan.jj Process the backslash at the end of line */ + +options { + STATIC = false; + JAVA_UNICODE_ESCAPE = true; +} + +PARSER_BEGIN(PreScanner) +package edu.uci.eecs.specCompiler.grammerParser.preScanner; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.util.ArrayList; + + public class PreScanner { + public static void main(String[] argvs) + throws ParseException, TokenMgrError { + try { + FileInputStream fis = new FileInputStream("./grammer/spec.txt"); + PreScanner preScanner = new PreScanner(fis); + String code = preScanner.ProcessEndBackslash(); + System.out.println(code); + System.out.println("Finished!"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +PARSER_END(PreScanner) + +SKIP : { + <"\\\n"> +} + +TOKEN : { + +} + +String ProcessEndBackslash() : +{ + StringBuilder sb; + String str; +} +{ + { sb = new StringBuilder(); } + (str = .image { sb.append(str); } )* + { + return sb.toString(); + } +} diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj deleted file mode 100644 index c048141..0000000 --- a/grammer/spec-compiler.jj +++ /dev/null @@ -1,490 +0,0 @@ -/* spec-compiler.jj Grammer definition for the specification */ - - -/* - SPEC constructs: - Each construct should be embraced by /DOUBLE_STAR ... STAR/ annotation. - Within there, any line beginning with a "#" is a comment of the annotation. - Each constrcut should begin with @Begin and end with @End. Otherwise, the - annotation would be considered as normal comments of the source. - - a) Global construct - @Begin - @Options: - # If LANG is not define, it's C++ by default. C does not support class - # and template, so if it's defined as C, we should also have a explicit - # entry point. - LANG = C; - @Global_define: - @DeclareVar: - @InitVar: - @DefineFunc: - ... - @Interface_cluster: - ... - @Happens-before: - ... - @End - - b) Interface construct - @Begin - @Interface: ... - @Commit_point_set: - IDENTIFIER | IDENTIFIER ... - @Condition: ... (Optional) - @HB_Condition: - IDENTIFIER :: - @HB_Condition: ... - @ID: ... (Optional, use default ID) - @Check: (Optional) - ... - @Action: (Optional) - # Type here must be a pointer - @DefineVar: Type var1 = SomeExpression (Optional) - @Code (Optional) - ... - @Post_action: (Optional) - @Post_check: (Optional) - @End - - c) Potential commit construct - @Begin - @Potential_commit_point_define: ... - @Label: ... - @End - - d) Commit point define construct - @Begin - @Commit_point_define_check: ... - @Label: ... - @End - - OR - - @Begin - @Commit_point_define: ... - @Potential_commit_point_label: ... - @Label: ... - @End - - e) Entry point construct - @Begin - @Entry_point - @End - - f) Interface define construct - @Begin - @Interface_define: - @End -*/ - - - -options { - STATIC = false; - JAVA_UNICODE_ESCAPE = true; -} - -PARSER_BEGIN(SpecParser) -package edu.uci.eecs.specCompiler.grammerParser; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.ByteArrayInputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; - -import edu.uci.eecs.specCompiler.specExtraction.Construct; -import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct; -import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct; -import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct; -import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct; -import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct; -import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; -import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct; -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; - - public class SpecParser { - public static void main(String[] argvs) - throws ParseException, TokenMgrError { - try { - FileInputStream fis = new FileInputStream("./grammer/spec.txt"); - SpecParser parser = new SpecParser(fis); - parser.ParseSpec(); - System.out.println("Parsing finished!"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - public static Construct parseSpec(String text) - throws ParseException, TokenMgrError { - InputStream input = new ByteArrayInputStream(text.getBytes()); - SpecParser parser = new SpecParser(input); - return parser.Parse(); - } - - - } -PARSER_END(SpecParser) - -< IN_COMMENT > SKIP : { < ~[] > } - -< IN_COMMENT, IN_SPEC > SKIP : { - "*/": DEFAULT -} - -SKIP : { - "/*": IN_COMMENT -} - -SKIP : { - "/**": IN_SPEC -} - - TOKEN : { - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -| - -} - -SKIP : -{ - " " -| - "\n" -| - "\r" -| - "\r\n" -| - "\t" -| - // "#" comment for the specification - <"#" (~["\n", "\r"])* (["\n", "\r"])> -| - // "//" comment for the specification - <"//" (~["\n", "\r"])* (["\n", "\r"])> -} - - 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"])+ > -} - -String Type() : -{ - String type; - String str; -} -{ - { type = ""; } - ("const" - { type = "const"; } - )? - (("struct" { type = type + " struct"; })? - (str = .image { - if (!type.equals("")) - type = type + " " + str; - else - type = str; - })) - ((str = "const".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; - } -} - -ArrayList FormalParamList() : -{ - ArrayList typeParams; -} -{ - { - typeParams = new ArrayList(); - } - (TypeParam(typeParams) ( TypeParam(typeParams))*)? - { - System.out.println(typeParams); - return typeParams; - } -} - -void TypeParam(ArrayList typeParams) : -{ - String type, param; -} -{ - (type = Type()) (param = .image) - { - typeParams.add(type); - typeParams.add(param); - } -} - -void ParseSpec() : -{} -{ - C_CPP_CODE()