diff --git a/.gitignore b/.gitignore index c1a9cb2..5bf12f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ ### CS323 Template ### #### Ignore generated #### src/main/java/generated +out/ *.tar *.tar.gz diff --git a/Splc.g4 b/Splc.g4 index ebc957b..abb0833 100644 --- a/Splc.g4 +++ b/Splc.g4 @@ -16,6 +16,7 @@ program: globalDef* EOF; globalDef : specifier Identifier LPAREN funcArgs RPAREN LBRACE statement* RBRACE // function definition + | specifier Identifier LPAREN funcArgs RPAREN SEMI // function declaration | specifier varDec (ASSIGN expression)? SEMI // global variable definition | specifier SEMI // global struct declaration ; diff --git a/out/production/CS323-Compilers-2025F-Projects/Main.class b/out/production/CS323-Compilers-2025F-Projects/Main.class deleted file mode 100644 index 1b4cb0d..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/Main.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader$1.class b/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader$1.class deleted file mode 100644 index bab8be7..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader$1.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader.class b/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader.class deleted file mode 100644 index 12ed8ed..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Grader.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/framework/project2/MissingSymbolError.class b/out/production/CS323-Compilers-2025F-Projects/framework/project2/MissingSymbolError.class deleted file mode 100644 index 8f7ee2a..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/framework/project2/MissingSymbolError.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Utils.class b/out/production/CS323-Compilers-2025F-Projects/framework/project2/Utils.class deleted file mode 100644 index 2c31121..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/framework/project2/Utils.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.interp b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.interp deleted file mode 100644 index c487a61..0000000 --- a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.interp +++ /dev/null @@ -1,100 +0,0 @@ -token literal names: -null -'int' -'char' -'struct' -'return' -'if' -'else' -'while' -'=' -'+' -'-' -'*' -'/' -'%' -'<' -'<=' -'>' -'>=' -'==' -'!=' -'&&' -'||' -'!' -'++' -'--' -'.' -'->' -'&' -';' -',' -'(' -')' -'{' -'}' -'[' -']' -null -null -null -null -null -null - -token symbolic names: -null -INT -CHAR -STRUCT -RETURN -IF -ELSE -WHILE -ASSIGN -PLUS -MINUS -STAR -DIV -MOD -LT -LE -GT -GE -EQ -NEQ -AND -OR -NOT -INC -DEC -DOT -ARROW -AMP -SEMI -COMMA -LPAREN -RPAREN -LBRACE -RBRACE -LBRACK -RBRACK -Identifier -Number -Char -WS -LINE_COMMENT -BLOCK_COMMENT - -rule names: -program -globalDef -specifier -varDec -funcArgs -statement -expression - - -atn: -[4, 1, 41, 219, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 1, 0, 5, 0, 16, 8, 0, 10, 0, 12, 0, 19, 9, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 30, 8, 1, 10, 1, 12, 1, 33, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 41, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 48, 8, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 5, 2, 61, 8, 2, 10, 2, 12, 2, 64, 9, 2, 1, 2, 3, 2, 67, 8, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 3, 3, 77, 8, 3, 1, 3, 1, 3, 1, 3, 1, 3, 5, 3, 83, 8, 3, 10, 3, 12, 3, 86, 9, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 5, 4, 94, 8, 4, 10, 4, 12, 4, 97, 9, 4, 3, 4, 99, 8, 4, 1, 5, 1, 5, 5, 5, 103, 8, 5, 10, 5, 12, 5, 106, 9, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 113, 8, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 124, 8, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 139, 8, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 5, 6, 154, 8, 6, 10, 6, 12, 6, 157, 9, 6, 3, 6, 159, 8, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 176, 8, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 5, 6, 214, 8, 6, 10, 6, 12, 6, 217, 9, 6, 1, 6, 0, 2, 6, 12, 7, 0, 2, 4, 6, 8, 10, 12, 0, 4, 1, 0, 11, 13, 1, 0, 9, 10, 1, 0, 14, 17, 1, 0, 18, 19, 258, 0, 17, 1, 0, 0, 0, 2, 47, 1, 0, 0, 0, 4, 66, 1, 0, 0, 0, 6, 76, 1, 0, 0, 0, 8, 98, 1, 0, 0, 0, 10, 138, 1, 0, 0, 0, 12, 175, 1, 0, 0, 0, 14, 16, 3, 2, 1, 0, 15, 14, 1, 0, 0, 0, 16, 19, 1, 0, 0, 0, 17, 15, 1, 0, 0, 0, 17, 18, 1, 0, 0, 0, 18, 20, 1, 0, 0, 0, 19, 17, 1, 0, 0, 0, 20, 21, 5, 0, 0, 1, 21, 1, 1, 0, 0, 0, 22, 23, 3, 4, 2, 0, 23, 24, 5, 36, 0, 0, 24, 25, 5, 30, 0, 0, 25, 26, 3, 8, 4, 0, 26, 27, 5, 31, 0, 0, 27, 31, 5, 32, 0, 0, 28, 30, 3, 10, 5, 0, 29, 28, 1, 0, 0, 0, 30, 33, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 34, 1, 0, 0, 0, 33, 31, 1, 0, 0, 0, 34, 35, 5, 33, 0, 0, 35, 48, 1, 0, 0, 0, 36, 37, 3, 4, 2, 0, 37, 40, 3, 6, 3, 0, 38, 39, 5, 8, 0, 0, 39, 41, 3, 12, 6, 0, 40, 38, 1, 0, 0, 0, 40, 41, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 43, 5, 28, 0, 0, 43, 48, 1, 0, 0, 0, 44, 45, 3, 4, 2, 0, 45, 46, 5, 28, 0, 0, 46, 48, 1, 0, 0, 0, 47, 22, 1, 0, 0, 0, 47, 36, 1, 0, 0, 0, 47, 44, 1, 0, 0, 0, 48, 3, 1, 0, 0, 0, 49, 67, 5, 1, 0, 0, 50, 67, 5, 2, 0, 0, 51, 52, 5, 3, 0, 0, 52, 67, 5, 36, 0, 0, 53, 54, 5, 3, 0, 0, 54, 55, 5, 36, 0, 0, 55, 62, 5, 32, 0, 0, 56, 57, 3, 4, 2, 0, 57, 58, 3, 6, 3, 0, 58, 59, 5, 28, 0, 0, 59, 61, 1, 0, 0, 0, 60, 56, 1, 0, 0, 0, 61, 64, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 65, 1, 0, 0, 0, 64, 62, 1, 0, 0, 0, 65, 67, 5, 33, 0, 0, 66, 49, 1, 0, 0, 0, 66, 50, 1, 0, 0, 0, 66, 51, 1, 0, 0, 0, 66, 53, 1, 0, 0, 0, 67, 5, 1, 0, 0, 0, 68, 69, 6, 3, -1, 0, 69, 77, 5, 36, 0, 0, 70, 71, 5, 30, 0, 0, 71, 72, 3, 6, 3, 0, 72, 73, 5, 31, 0, 0, 73, 77, 1, 0, 0, 0, 74, 75, 5, 11, 0, 0, 75, 77, 3, 6, 3, 1, 76, 68, 1, 0, 0, 0, 76, 70, 1, 0, 0, 0, 76, 74, 1, 0, 0, 0, 77, 84, 1, 0, 0, 0, 78, 79, 10, 2, 0, 0, 79, 80, 5, 34, 0, 0, 80, 81, 5, 37, 0, 0, 81, 83, 5, 35, 0, 0, 82, 78, 1, 0, 0, 0, 83, 86, 1, 0, 0, 0, 84, 82, 1, 0, 0, 0, 84, 85, 1, 0, 0, 0, 85, 7, 1, 0, 0, 0, 86, 84, 1, 0, 0, 0, 87, 88, 3, 4, 2, 0, 88, 95, 3, 6, 3, 0, 89, 90, 5, 29, 0, 0, 90, 91, 3, 4, 2, 0, 91, 92, 3, 6, 3, 0, 92, 94, 1, 0, 0, 0, 93, 89, 1, 0, 0, 0, 94, 97, 1, 0, 0, 0, 95, 93, 1, 0, 0, 0, 95, 96, 1, 0, 0, 0, 96, 99, 1, 0, 0, 0, 97, 95, 1, 0, 0, 0, 98, 87, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 9, 1, 0, 0, 0, 100, 104, 5, 32, 0, 0, 101, 103, 3, 10, 5, 0, 102, 101, 1, 0, 0, 0, 103, 106, 1, 0, 0, 0, 104, 102, 1, 0, 0, 0, 104, 105, 1, 0, 0, 0, 105, 107, 1, 0, 0, 0, 106, 104, 1, 0, 0, 0, 107, 139, 5, 33, 0, 0, 108, 109, 3, 4, 2, 0, 109, 112, 3, 6, 3, 0, 110, 111, 5, 8, 0, 0, 111, 113, 3, 12, 6, 0, 112, 110, 1, 0, 0, 0, 112, 113, 1, 0, 0, 0, 113, 114, 1, 0, 0, 0, 114, 115, 5, 28, 0, 0, 115, 139, 1, 0, 0, 0, 116, 117, 5, 5, 0, 0, 117, 118, 5, 30, 0, 0, 118, 119, 3, 12, 6, 0, 119, 120, 5, 31, 0, 0, 120, 123, 3, 10, 5, 0, 121, 122, 5, 6, 0, 0, 122, 124, 3, 10, 5, 0, 123, 121, 1, 0, 0, 0, 123, 124, 1, 0, 0, 0, 124, 139, 1, 0, 0, 0, 125, 126, 5, 7, 0, 0, 126, 127, 5, 30, 0, 0, 127, 128, 3, 12, 6, 0, 128, 129, 5, 31, 0, 0, 129, 130, 3, 10, 5, 0, 130, 139, 1, 0, 0, 0, 131, 132, 5, 4, 0, 0, 132, 133, 3, 12, 6, 0, 133, 134, 5, 28, 0, 0, 134, 139, 1, 0, 0, 0, 135, 136, 3, 12, 6, 0, 136, 137, 5, 28, 0, 0, 137, 139, 1, 0, 0, 0, 138, 100, 1, 0, 0, 0, 138, 108, 1, 0, 0, 0, 138, 116, 1, 0, 0, 0, 138, 125, 1, 0, 0, 0, 138, 131, 1, 0, 0, 0, 138, 135, 1, 0, 0, 0, 139, 11, 1, 0, 0, 0, 140, 141, 6, 6, -1, 0, 141, 176, 5, 36, 0, 0, 142, 176, 5, 37, 0, 0, 143, 176, 5, 38, 0, 0, 144, 145, 5, 30, 0, 0, 145, 146, 3, 12, 6, 0, 146, 147, 5, 31, 0, 0, 147, 176, 1, 0, 0, 0, 148, 149, 5, 36, 0, 0, 149, 158, 5, 30, 0, 0, 150, 155, 3, 12, 6, 0, 151, 152, 5, 29, 0, 0, 152, 154, 3, 12, 6, 0, 153, 151, 1, 0, 0, 0, 154, 157, 1, 0, 0, 0, 155, 153, 1, 0, 0, 0, 155, 156, 1, 0, 0, 0, 156, 159, 1, 0, 0, 0, 157, 155, 1, 0, 0, 0, 158, 150, 1, 0, 0, 0, 158, 159, 1, 0, 0, 0, 159, 160, 1, 0, 0, 0, 160, 176, 5, 31, 0, 0, 161, 162, 5, 23, 0, 0, 162, 176, 3, 12, 6, 14, 163, 164, 5, 24, 0, 0, 164, 176, 3, 12, 6, 13, 165, 166, 5, 9, 0, 0, 166, 176, 3, 12, 6, 12, 167, 168, 5, 10, 0, 0, 168, 176, 3, 12, 6, 11, 169, 170, 5, 22, 0, 0, 170, 176, 3, 12, 6, 10, 171, 172, 5, 11, 0, 0, 172, 176, 3, 12, 6, 9, 173, 174, 5, 27, 0, 0, 174, 176, 3, 12, 6, 8, 175, 140, 1, 0, 0, 0, 175, 142, 1, 0, 0, 0, 175, 143, 1, 0, 0, 0, 175, 144, 1, 0, 0, 0, 175, 148, 1, 0, 0, 0, 175, 161, 1, 0, 0, 0, 175, 163, 1, 0, 0, 0, 175, 165, 1, 0, 0, 0, 175, 167, 1, 0, 0, 0, 175, 169, 1, 0, 0, 0, 175, 171, 1, 0, 0, 0, 175, 173, 1, 0, 0, 0, 176, 215, 1, 0, 0, 0, 177, 178, 10, 7, 0, 0, 178, 179, 7, 0, 0, 0, 179, 214, 3, 12, 6, 8, 180, 181, 10, 6, 0, 0, 181, 182, 7, 1, 0, 0, 182, 214, 3, 12, 6, 7, 183, 184, 10, 5, 0, 0, 184, 185, 7, 2, 0, 0, 185, 214, 3, 12, 6, 6, 186, 187, 10, 4, 0, 0, 187, 188, 7, 3, 0, 0, 188, 214, 3, 12, 6, 5, 189, 190, 10, 3, 0, 0, 190, 191, 5, 20, 0, 0, 191, 214, 3, 12, 6, 4, 192, 193, 10, 2, 0, 0, 193, 194, 5, 21, 0, 0, 194, 214, 3, 12, 6, 3, 195, 196, 10, 1, 0, 0, 196, 197, 5, 8, 0, 0, 197, 214, 3, 12, 6, 1, 198, 199, 10, 20, 0, 0, 199, 214, 5, 23, 0, 0, 200, 201, 10, 19, 0, 0, 201, 214, 5, 24, 0, 0, 202, 203, 10, 18, 0, 0, 203, 204, 5, 34, 0, 0, 204, 205, 3, 12, 6, 0, 205, 206, 5, 35, 0, 0, 206, 214, 1, 0, 0, 0, 207, 208, 10, 17, 0, 0, 208, 209, 5, 25, 0, 0, 209, 214, 5, 36, 0, 0, 210, 211, 10, 16, 0, 0, 211, 212, 5, 26, 0, 0, 212, 214, 5, 36, 0, 0, 213, 177, 1, 0, 0, 0, 213, 180, 1, 0, 0, 0, 213, 183, 1, 0, 0, 0, 213, 186, 1, 0, 0, 0, 213, 189, 1, 0, 0, 0, 213, 192, 1, 0, 0, 0, 213, 195, 1, 0, 0, 0, 213, 198, 1, 0, 0, 0, 213, 200, 1, 0, 0, 0, 213, 202, 1, 0, 0, 0, 213, 207, 1, 0, 0, 0, 213, 210, 1, 0, 0, 0, 214, 217, 1, 0, 0, 0, 215, 213, 1, 0, 0, 0, 215, 216, 1, 0, 0, 0, 216, 13, 1, 0, 0, 0, 217, 215, 1, 0, 0, 0, 19, 17, 31, 40, 47, 62, 66, 76, 84, 95, 98, 104, 112, 123, 138, 155, 158, 175, 213, 215] \ No newline at end of file diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.tokens b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.tokens deleted file mode 100644 index 66553e7..0000000 --- a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/Splc.tokens +++ /dev/null @@ -1,76 +0,0 @@ -INT=1 -CHAR=2 -STRUCT=3 -RETURN=4 -IF=5 -ELSE=6 -WHILE=7 -ASSIGN=8 -PLUS=9 -MINUS=10 -STAR=11 -DIV=12 -MOD=13 -LT=14 -LE=15 -GT=16 -GE=17 -EQ=18 -NEQ=19 -AND=20 -OR=21 -NOT=22 -INC=23 -DEC=24 -DOT=25 -ARROW=26 -AMP=27 -SEMI=28 -COMMA=29 -LPAREN=30 -RPAREN=31 -LBRACE=32 -RBRACE=33 -LBRACK=34 -RBRACK=35 -Identifier=36 -Number=37 -Char=38 -WS=39 -LINE_COMMENT=40 -BLOCK_COMMENT=41 -'int'=1 -'char'=2 -'struct'=3 -'return'=4 -'if'=5 -'else'=6 -'while'=7 -'='=8 -'+'=9 -'-'=10 -'*'=11 -'/'=12 -'%'=13 -'<'=14 -'<='=15 -'>'=16 -'>='=17 -'=='=18 -'!='=19 -'&&'=20 -'||'=21 -'!'=22 -'++'=23 -'--'=24 -'.'=25 -'->'=26 -'&'=27 -';'=28 -','=29 -'('=30 -')'=31 -'{'=32 -'}'=33 -'['=34 -']'=35 diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseListener.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseListener.class deleted file mode 100644 index b620eba..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseListener.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseVisitor.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseVisitor.class deleted file mode 100644 index f8216dc..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcBaseVisitor.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.class deleted file mode 100644 index 2acc155..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.interp b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.interp deleted file mode 100644 index 16e5e50..0000000 --- a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.interp +++ /dev/null @@ -1,140 +0,0 @@ -token literal names: -null -'int' -'char' -'struct' -'return' -'if' -'else' -'while' -'=' -'+' -'-' -'*' -'/' -'%' -'<' -'<=' -'>' -'>=' -'==' -'!=' -'&&' -'||' -'!' -'++' -'--' -'.' -'->' -'&' -';' -',' -'(' -')' -'{' -'}' -'[' -']' -null -null -null -null -null -null - -token symbolic names: -null -INT -CHAR -STRUCT -RETURN -IF -ELSE -WHILE -ASSIGN -PLUS -MINUS -STAR -DIV -MOD -LT -LE -GT -GE -EQ -NEQ -AND -OR -NOT -INC -DEC -DOT -ARROW -AMP -SEMI -COMMA -LPAREN -RPAREN -LBRACE -RBRACE -LBRACK -RBRACK -Identifier -Number -Char -WS -LINE_COMMENT -BLOCK_COMMENT - -rule names: -INT -CHAR -STRUCT -RETURN -IF -ELSE -WHILE -ASSIGN -PLUS -MINUS -STAR -DIV -MOD -LT -LE -GT -GE -EQ -NEQ -AND -OR -NOT -INC -DEC -DOT -ARROW -AMP -SEMI -COMMA -LPAREN -RPAREN -LBRACE -RBRACE -LBRACK -RBRACK -Identifier -Number -Char -WS -LINE_COMMENT -BLOCK_COMMENT - -channel names: -DEFAULT_TOKEN_CHANNEL -HIDDEN - -mode names: -DEFAULT_MODE - -atn: -[4, 0, 41, 242, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, 2, 26, 7, 26, 2, 27, 7, 27, 2, 28, 7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 2, 31, 7, 31, 2, 32, 7, 32, 2, 33, 7, 33, 2, 34, 7, 34, 2, 35, 7, 35, 2, 36, 7, 36, 2, 37, 7, 37, 2, 38, 7, 38, 2, 39, 7, 39, 2, 40, 7, 40, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 8, 1, 8, 1, 9, 1, 9, 1, 10, 1, 10, 1, 11, 1, 11, 1, 12, 1, 12, 1, 13, 1, 13, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, 17, 1, 18, 1, 18, 1, 18, 1, 19, 1, 19, 1, 19, 1, 20, 1, 20, 1, 20, 1, 21, 1, 21, 1, 22, 1, 22, 1, 22, 1, 23, 1, 23, 1, 23, 1, 24, 1, 24, 1, 25, 1, 25, 1, 25, 1, 26, 1, 26, 1, 27, 1, 27, 1, 28, 1, 28, 1, 29, 1, 29, 1, 30, 1, 30, 1, 31, 1, 31, 1, 32, 1, 32, 1, 33, 1, 33, 1, 34, 1, 34, 1, 35, 1, 35, 5, 35, 188, 8, 35, 10, 35, 12, 35, 191, 9, 35, 1, 36, 1, 36, 1, 36, 5, 36, 196, 8, 36, 10, 36, 12, 36, 199, 9, 36, 3, 36, 201, 8, 36, 1, 37, 1, 37, 1, 37, 1, 37, 3, 37, 207, 8, 37, 1, 37, 1, 37, 1, 38, 4, 38, 212, 8, 38, 11, 38, 12, 38, 213, 1, 38, 1, 38, 1, 39, 1, 39, 1, 39, 1, 39, 5, 39, 222, 8, 39, 10, 39, 12, 39, 225, 9, 39, 1, 39, 1, 39, 1, 40, 1, 40, 1, 40, 1, 40, 5, 40, 233, 8, 40, 10, 40, 12, 40, 236, 9, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 234, 0, 41, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, 41, 21, 43, 22, 45, 23, 47, 24, 49, 25, 51, 26, 53, 27, 55, 28, 57, 29, 59, 30, 61, 31, 63, 32, 65, 33, 67, 34, 69, 35, 71, 36, 73, 37, 75, 38, 77, 39, 79, 40, 81, 41, 1, 0, 8, 3, 0, 65, 90, 95, 95, 97, 122, 3, 0, 48, 57, 65, 90, 97, 122, 1, 0, 49, 57, 1, 0, 48, 57, 4, 0, 10, 10, 13, 13, 39, 39, 92, 92, 5, 0, 39, 39, 48, 48, 92, 92, 110, 110, 116, 116, 3, 0, 9, 10, 13, 13, 32, 32, 2, 0, 10, 10, 13, 13, 248, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0, 0, 0, 0, 47, 1, 0, 0, 0, 0, 49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1, 0, 0, 0, 0, 55, 1, 0, 0, 0, 0, 57, 1, 0, 0, 0, 0, 59, 1, 0, 0, 0, 0, 61, 1, 0, 0, 0, 0, 63, 1, 0, 0, 0, 0, 65, 1, 0, 0, 0, 0, 67, 1, 0, 0, 0, 0, 69, 1, 0, 0, 0, 0, 71, 1, 0, 0, 0, 0, 73, 1, 0, 0, 0, 0, 75, 1, 0, 0, 0, 0, 77, 1, 0, 0, 0, 0, 79, 1, 0, 0, 0, 0, 81, 1, 0, 0, 0, 1, 83, 1, 0, 0, 0, 3, 87, 1, 0, 0, 0, 5, 92, 1, 0, 0, 0, 7, 99, 1, 0, 0, 0, 9, 106, 1, 0, 0, 0, 11, 109, 1, 0, 0, 0, 13, 114, 1, 0, 0, 0, 15, 120, 1, 0, 0, 0, 17, 122, 1, 0, 0, 0, 19, 124, 1, 0, 0, 0, 21, 126, 1, 0, 0, 0, 23, 128, 1, 0, 0, 0, 25, 130, 1, 0, 0, 0, 27, 132, 1, 0, 0, 0, 29, 134, 1, 0, 0, 0, 31, 137, 1, 0, 0, 0, 33, 139, 1, 0, 0, 0, 35, 142, 1, 0, 0, 0, 37, 145, 1, 0, 0, 0, 39, 148, 1, 0, 0, 0, 41, 151, 1, 0, 0, 0, 43, 154, 1, 0, 0, 0, 45, 156, 1, 0, 0, 0, 47, 159, 1, 0, 0, 0, 49, 162, 1, 0, 0, 0, 51, 164, 1, 0, 0, 0, 53, 167, 1, 0, 0, 0, 55, 169, 1, 0, 0, 0, 57, 171, 1, 0, 0, 0, 59, 173, 1, 0, 0, 0, 61, 175, 1, 0, 0, 0, 63, 177, 1, 0, 0, 0, 65, 179, 1, 0, 0, 0, 67, 181, 1, 0, 0, 0, 69, 183, 1, 0, 0, 0, 71, 185, 1, 0, 0, 0, 73, 200, 1, 0, 0, 0, 75, 202, 1, 0, 0, 0, 77, 211, 1, 0, 0, 0, 79, 217, 1, 0, 0, 0, 81, 228, 1, 0, 0, 0, 83, 84, 5, 105, 0, 0, 84, 85, 5, 110, 0, 0, 85, 86, 5, 116, 0, 0, 86, 2, 1, 0, 0, 0, 87, 88, 5, 99, 0, 0, 88, 89, 5, 104, 0, 0, 89, 90, 5, 97, 0, 0, 90, 91, 5, 114, 0, 0, 91, 4, 1, 0, 0, 0, 92, 93, 5, 115, 0, 0, 93, 94, 5, 116, 0, 0, 94, 95, 5, 114, 0, 0, 95, 96, 5, 117, 0, 0, 96, 97, 5, 99, 0, 0, 97, 98, 5, 116, 0, 0, 98, 6, 1, 0, 0, 0, 99, 100, 5, 114, 0, 0, 100, 101, 5, 101, 0, 0, 101, 102, 5, 116, 0, 0, 102, 103, 5, 117, 0, 0, 103, 104, 5, 114, 0, 0, 104, 105, 5, 110, 0, 0, 105, 8, 1, 0, 0, 0, 106, 107, 5, 105, 0, 0, 107, 108, 5, 102, 0, 0, 108, 10, 1, 0, 0, 0, 109, 110, 5, 101, 0, 0, 110, 111, 5, 108, 0, 0, 111, 112, 5, 115, 0, 0, 112, 113, 5, 101, 0, 0, 113, 12, 1, 0, 0, 0, 114, 115, 5, 119, 0, 0, 115, 116, 5, 104, 0, 0, 116, 117, 5, 105, 0, 0, 117, 118, 5, 108, 0, 0, 118, 119, 5, 101, 0, 0, 119, 14, 1, 0, 0, 0, 120, 121, 5, 61, 0, 0, 121, 16, 1, 0, 0, 0, 122, 123, 5, 43, 0, 0, 123, 18, 1, 0, 0, 0, 124, 125, 5, 45, 0, 0, 125, 20, 1, 0, 0, 0, 126, 127, 5, 42, 0, 0, 127, 22, 1, 0, 0, 0, 128, 129, 5, 47, 0, 0, 129, 24, 1, 0, 0, 0, 130, 131, 5, 37, 0, 0, 131, 26, 1, 0, 0, 0, 132, 133, 5, 60, 0, 0, 133, 28, 1, 0, 0, 0, 134, 135, 5, 60, 0, 0, 135, 136, 5, 61, 0, 0, 136, 30, 1, 0, 0, 0, 137, 138, 5, 62, 0, 0, 138, 32, 1, 0, 0, 0, 139, 140, 5, 62, 0, 0, 140, 141, 5, 61, 0, 0, 141, 34, 1, 0, 0, 0, 142, 143, 5, 61, 0, 0, 143, 144, 5, 61, 0, 0, 144, 36, 1, 0, 0, 0, 145, 146, 5, 33, 0, 0, 146, 147, 5, 61, 0, 0, 147, 38, 1, 0, 0, 0, 148, 149, 5, 38, 0, 0, 149, 150, 5, 38, 0, 0, 150, 40, 1, 0, 0, 0, 151, 152, 5, 124, 0, 0, 152, 153, 5, 124, 0, 0, 153, 42, 1, 0, 0, 0, 154, 155, 5, 33, 0, 0, 155, 44, 1, 0, 0, 0, 156, 157, 5, 43, 0, 0, 157, 158, 5, 43, 0, 0, 158, 46, 1, 0, 0, 0, 159, 160, 5, 45, 0, 0, 160, 161, 5, 45, 0, 0, 161, 48, 1, 0, 0, 0, 162, 163, 5, 46, 0, 0, 163, 50, 1, 0, 0, 0, 164, 165, 5, 45, 0, 0, 165, 166, 5, 62, 0, 0, 166, 52, 1, 0, 0, 0, 167, 168, 5, 38, 0, 0, 168, 54, 1, 0, 0, 0, 169, 170, 5, 59, 0, 0, 170, 56, 1, 0, 0, 0, 171, 172, 5, 44, 0, 0, 172, 58, 1, 0, 0, 0, 173, 174, 5, 40, 0, 0, 174, 60, 1, 0, 0, 0, 175, 176, 5, 41, 0, 0, 176, 62, 1, 0, 0, 0, 177, 178, 5, 123, 0, 0, 178, 64, 1, 0, 0, 0, 179, 180, 5, 125, 0, 0, 180, 66, 1, 0, 0, 0, 181, 182, 5, 91, 0, 0, 182, 68, 1, 0, 0, 0, 183, 184, 5, 93, 0, 0, 184, 70, 1, 0, 0, 0, 185, 189, 7, 0, 0, 0, 186, 188, 7, 1, 0, 0, 187, 186, 1, 0, 0, 0, 188, 191, 1, 0, 0, 0, 189, 187, 1, 0, 0, 0, 189, 190, 1, 0, 0, 0, 190, 72, 1, 0, 0, 0, 191, 189, 1, 0, 0, 0, 192, 201, 5, 48, 0, 0, 193, 197, 7, 2, 0, 0, 194, 196, 7, 3, 0, 0, 195, 194, 1, 0, 0, 0, 196, 199, 1, 0, 0, 0, 197, 195, 1, 0, 0, 0, 197, 198, 1, 0, 0, 0, 198, 201, 1, 0, 0, 0, 199, 197, 1, 0, 0, 0, 200, 192, 1, 0, 0, 0, 200, 193, 1, 0, 0, 0, 201, 74, 1, 0, 0, 0, 202, 206, 5, 39, 0, 0, 203, 207, 8, 4, 0, 0, 204, 205, 5, 92, 0, 0, 205, 207, 7, 5, 0, 0, 206, 203, 1, 0, 0, 0, 206, 204, 1, 0, 0, 0, 207, 208, 1, 0, 0, 0, 208, 209, 5, 39, 0, 0, 209, 76, 1, 0, 0, 0, 210, 212, 7, 6, 0, 0, 211, 210, 1, 0, 0, 0, 212, 213, 1, 0, 0, 0, 213, 211, 1, 0, 0, 0, 213, 214, 1, 0, 0, 0, 214, 215, 1, 0, 0, 0, 215, 216, 6, 38, 0, 0, 216, 78, 1, 0, 0, 0, 217, 218, 5, 47, 0, 0, 218, 219, 5, 47, 0, 0, 219, 223, 1, 0, 0, 0, 220, 222, 8, 7, 0, 0, 221, 220, 1, 0, 0, 0, 222, 225, 1, 0, 0, 0, 223, 221, 1, 0, 0, 0, 223, 224, 1, 0, 0, 0, 224, 226, 1, 0, 0, 0, 225, 223, 1, 0, 0, 0, 226, 227, 6, 39, 0, 0, 227, 80, 1, 0, 0, 0, 228, 229, 5, 47, 0, 0, 229, 230, 5, 42, 0, 0, 230, 234, 1, 0, 0, 0, 231, 233, 9, 0, 0, 0, 232, 231, 1, 0, 0, 0, 233, 236, 1, 0, 0, 0, 234, 235, 1, 0, 0, 0, 234, 232, 1, 0, 0, 0, 235, 237, 1, 0, 0, 0, 236, 234, 1, 0, 0, 0, 237, 238, 5, 42, 0, 0, 238, 239, 5, 47, 0, 0, 239, 240, 1, 0, 0, 0, 240, 241, 6, 40, 0, 0, 241, 82, 1, 0, 0, 0, 8, 0, 189, 197, 200, 206, 213, 223, 234, 1, 6, 0, 0] \ No newline at end of file diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.tokens b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.tokens deleted file mode 100644 index 66553e7..0000000 --- a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcLexer.tokens +++ /dev/null @@ -1,76 +0,0 @@ -INT=1 -CHAR=2 -STRUCT=3 -RETURN=4 -IF=5 -ELSE=6 -WHILE=7 -ASSIGN=8 -PLUS=9 -MINUS=10 -STAR=11 -DIV=12 -MOD=13 -LT=14 -LE=15 -GT=16 -GE=17 -EQ=18 -NEQ=19 -AND=20 -OR=21 -NOT=22 -INC=23 -DEC=24 -DOT=25 -ARROW=26 -AMP=27 -SEMI=28 -COMMA=29 -LPAREN=30 -RPAREN=31 -LBRACE=32 -RBRACE=33 -LBRACK=34 -RBRACK=35 -Identifier=36 -Number=37 -Char=38 -WS=39 -LINE_COMMENT=40 -BLOCK_COMMENT=41 -'int'=1 -'char'=2 -'struct'=3 -'return'=4 -'if'=5 -'else'=6 -'while'=7 -'='=8 -'+'=9 -'-'=10 -'*'=11 -'/'=12 -'%'=13 -'<'=14 -'<='=15 -'>'=16 -'>='=17 -'=='=18 -'!='=19 -'&&'=20 -'||'=21 -'!'=22 -'++'=23 -'--'=24 -'.'=25 -'->'=26 -'&'=27 -';'=28 -','=29 -'('=30 -')'=31 -'{'=32 -'}'=33 -'['=34 -']'=35 diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcListener.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcListener.class deleted file mode 100644 index b148f03..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcListener.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$BracketContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$BracketContext.class deleted file mode 100644 index e45b5dd..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$BracketContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExprStmtContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExprStmtContext.class deleted file mode 100644 index 7d84c0d..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExprStmtContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExpressionContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExpressionContext.class deleted file mode 100644 index c69f877..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ExpressionContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$FuncArgsContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$FuncArgsContext.class deleted file mode 100644 index d5c74c4..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$FuncArgsContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$GlobalDefContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$GlobalDefContext.class deleted file mode 100644 index f830717..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$GlobalDefContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$IfStmtContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$IfStmtContext.class deleted file mode 100644 index 7488d6b..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$IfStmtContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ProgramContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ProgramContext.class deleted file mode 100644 index 898e569..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ProgramContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ReturnStmtContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ReturnStmtContext.class deleted file mode 100644 index 26c8bad..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$ReturnStmtContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$SpecifierContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$SpecifierContext.class deleted file mode 100644 index 267ba8d..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$SpecifierContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$StatementContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$StatementContext.class deleted file mode 100644 index b3b1653..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$StatementContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecContext.class deleted file mode 100644 index c8503bf..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecStmtContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecStmtContext.class deleted file mode 100644 index b6b4660..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$VarDecStmtContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$WhileStmtContext.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$WhileStmtContext.class deleted file mode 100644 index 2017d7f..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser$WhileStmtContext.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser.class deleted file mode 100644 index 749d7ab..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcParser.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcVisitor.class b/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcVisitor.class deleted file mode 100644 index 4242d4a..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/generated/Splc/SplcVisitor.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/impl/project2/ConstExprVisitor.class b/out/production/CS323-Compilers-2025F-Projects/impl/project2/ConstExprVisitor.class deleted file mode 100644 index b2ba29d..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/impl/project2/ConstExprVisitor.class and /dev/null differ diff --git a/out/production/CS323-Compilers-2025F-Projects/impl/project2/Project2ErrorListener.class b/out/production/CS323-Compilers-2025F-Projects/impl/project2/Project2ErrorListener.class deleted file mode 100644 index a742e3b..0000000 Binary files a/out/production/CS323-Compilers-2025F-Projects/impl/project2/Project2ErrorListener.class and /dev/null differ diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 3325c2e..c9c31aa 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,10 +7,11 @@ public class Main { public static void main(String[] args) throws IOException { - { - InputStream input = new FileInputStream("testcases/project3/ok_01.splc"); - AbstractGrader grader = new Grader(input, System.out); - grader.run(); + InputStream input = System.in; + if (args.length > 0) { + input = new FileInputStream(args[0]); } + AbstractGrader grader = new Grader(input, System.out); + grader.run(); } } \ No newline at end of file diff --git a/src/main/java/impl/Compiler.java b/src/main/java/impl/Compiler.java index daff59c..d983c77 100644 --- a/src/main/java/impl/Compiler.java +++ b/src/main/java/impl/Compiler.java @@ -2,6 +2,7 @@ import framework.AbstractCompiler; import framework.AbstractGrader; +import framework.lang.Type; import framework.project3.Project3SemanticError; import generated.Splc.SplcBaseVisitor; import generated.Splc.SplcLexer; @@ -9,8 +10,10 @@ import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.TerminalNode; import java.io.IOException; +import java.util.*; public class Compiler extends AbstractCompiler { public Compiler(AbstractGrader grader) { @@ -23,35 +26,762 @@ public void start() throws IOException { SplcLexer lexer = new SplcLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); SplcParser parser = new SplcParser(tokens); + + // Remove default error listeners to suppress parse error messages + // Per project requirements: all test cases won't have syntax errors + parser.removeErrorListeners(); + lexer.removeErrorListeners(); - // TODO: XXX SplcParser.ProgramContext program = parser.program(); - new SplcBaseVisitor() { - // These are merely examples to show how to create and report a Semantic Error. - // The Alternative name (ExprID, VarDecBase, etc.) used here may not be the same as yours. - // So it's fine that this code won't compile. You are free to delete all of these code. - @Override - public Void visitExprID(SplcParser.ExprIDContext ctx) { - var ident = ctx.Identifier(); - grader.reportSemanticError(Project3SemanticError.undeclaredUse(ident)); - return null; + // Phase 1: Semantic analysis and error checking + SemanticAnalyzer analyzer = new SemanticAnalyzer(grader); + analyzer.visit(program); + + // If we get here, no semantic errors were found + // Phase 2: Print global variables and functions + grader.print("Variables:\n"); + for (Symbol symbol : analyzer.getGlobalVariables()) { + grader.print(symbol.getName() + ": " + symbol.getType().fullPrint() + "\n"); + } + grader.print("\n"); + grader.print("Functions:\n"); + for (Symbol symbol : analyzer.getGlobalFunctions()) { + grader.print(symbol.getName() + ": " + symbol.getType().prettyPrint() + "\n"); + } + } + + // ===== Type System Inner Classes ===== + + private static class BasicType implements Type { + public enum Kind { + INT, CHAR + } + + private final Kind kind; + + public BasicType(Kind kind) { + this.kind = kind; + } + + public Kind getKind() { + return kind; + } + + @Override + public String prettyPrint() { + return kind == Kind.INT ? "int" : "char"; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof BasicType)) return false; + BasicType other = (BasicType) obj; + return kind == other.kind; + } + + @Override + public int hashCode() { + return kind.hashCode(); + } + } + + private static class ArrayType implements Type { + private final Type elementType; + private final int length; + + public ArrayType(Type elementType, int length) { + this.elementType = elementType; + this.length = length; + } + + public Type getElementType() { + return elementType; + } + + public int getLength() { + return length; + } + + @Override + public String prettyPrint() { + return elementType.prettyPrint() + "[" + length + "]"; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof ArrayType)) return false; + ArrayType other = (ArrayType) obj; + return length == other.length && elementType.equals(other.elementType); + } + + @Override + public int hashCode() { + return elementType.hashCode() * 31 + length; + } + } + + private static class PointerType implements Type { + private final Type referencedType; + + public PointerType(Type referencedType) { + this.referencedType = referencedType; + } + + public Type getReferencedType() { + return referencedType; + } + + @Override + public String prettyPrint() { + return referencedType.prettyPrint() + "*"; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof PointerType)) return false; + PointerType other = (PointerType) obj; + return referencedType.equals(other.referencedType); + } + + @Override + public int hashCode() { + return referencedType.hashCode() * 17; + } + } + + private static class FunctionType implements Type { + private final Type returnType; + private final List parameterTypes; + + public FunctionType(Type returnType, List parameterTypes) { + this.returnType = returnType; + this.parameterTypes = new ArrayList<>(parameterTypes); + } + + public Type getReturnType() { + return returnType; + } + + public List getParameterTypes() { + return parameterTypes; + } + + @Override + public String prettyPrint() { + StringBuilder sb = new StringBuilder(); + sb.append(returnType.prettyPrint()).append("("); + for (int i = 0; i < parameterTypes.size(); i++) { + if (i > 0) sb.append(","); + sb.append(parameterTypes.get(i).prettyPrint()); } + sb.append(")"); + return sb.toString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof FunctionType)) return false; + FunctionType other = (FunctionType) obj; + return returnType.equals(other.returnType) && + parameterTypes.equals(other.parameterTypes); + } + + @Override + public int hashCode() { + return returnType.hashCode() * 31 + parameterTypes.hashCode(); + } + } + + private static class StructType implements Type { + private final String tag; + private final List members; + private final boolean isComplete; - @Override - public Void visitVarDecBase(SplcParser.VarDecBaseContext ctx) { - var ident = ctx.Identifier(); - grader.reportSemanticError(Project3SemanticError.redefinition(ident)); - return null; + public static class Member { + public final Type type; + public final String name; + + public Member(Type type, String name) { + this.type = type; + this.name = name; } - }.visit(program); + } - System.out.println("wow"); + // Constructor for incomplete struct + public StructType(String tag) { + this.tag = tag; + this.members = null; + this.isComplete = false; + } - grader.print("Variables:\n"); + // Constructor for complete struct + public StructType(String tag, List members) { + this.tag = tag; + this.members = new ArrayList<>(members); + this.isComplete = true; + } - grader.print("\n"); + public String getTag() { + return tag; + } - grader.print("Functions:\n"); + public List getMembers() { + return members; + } + + public boolean isComplete() { + return isComplete; + } + + @Override + public String prettyPrint() { + return "struct " + tag; + } + + @Override + public String fullPrint() { + if (!isComplete || members == null) { + return prettyPrint(); + } + StringBuilder sb = new StringBuilder("struct "); + sb.append(tag).append("{"); + for (Member member : members) { + sb.append(member.type.prettyPrint()).append(" ").append(member.name).append(";"); + } + sb.append("}"); + return sb.toString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof StructType)) return false; + StructType other = (StructType) obj; + // Two struct types are the same if they have the same tag + // and same completeness (in same scope context, but we check that elsewhere) + return tag.equals(other.tag); + } + + @Override + public int hashCode() { + return tag.hashCode(); + } + } + + // ===== Symbol Table Inner Classes ===== + + private static class Symbol { + public enum Kind { + VARIABLE, // Variables and function names (in "other" namespace) + FUNCTION, // Actually same as variable, but we track separately + FUNCTION_DECL, // Function declaration (no body) + STRUCT_TAG, // Structure tags + STRUCT_MEMBER // Structure members (each struct has its own namespace) + } + + private final String name; + private final Type type; + private final Kind kind; + private final int scopeId; // For tracking which scope this belongs to + + public Symbol(String name, Type type, Kind kind, int scopeId) { + this.name = name; + this.type = type; + this.kind = kind; + this.scopeId = scopeId; + } + + public String getName() { + return name; + } + + public Type getType() { + return type; + } + + public Kind getKind() { + return kind; + } + + public int getScopeId() { + return scopeId; + } + } + + private static class SymbolTable { + // Scopes for different namespaces + private final Deque> otherScopes = new ArrayDeque<>(); // variables, functions + private final Deque> tagScopes = new ArrayDeque<>(); // struct tags + + private int scopeIdCounter = 0; + private int currentScopeId = 0; + + public SymbolTable() { + // Start with file scope + enterScope(); + } + + public void enterScope() { + otherScopes.push(new LinkedHashMap<>()); + tagScopes.push(new LinkedHashMap<>()); + currentScopeId = scopeIdCounter++; + } + + public void exitScope() { + if (otherScopes.size() > 1) { + otherScopes.pop(); + tagScopes.pop(); + } + } + + public int getCurrentScopeId() { + return currentScopeId; + } + + // Add symbol to "other" namespace (variables, functions) + public boolean addOther(String name, Type type, Symbol.Kind kind) { + Map currentScope = otherScopes.peek(); + if (currentScope.containsKey(name)) { + return false; // Already exists in current scope + } + currentScope.put(name, new Symbol(name, type, kind, currentScopeId)); + return true; + } + + // Update existing symbol in current scope (for converting function decl to definition) + public void updateOther(String name, Type type, Symbol.Kind kind) { + Map currentScope = otherScopes.peek(); + currentScope.put(name, new Symbol(name, type, kind, currentScopeId)); + } + + // Add symbol to "tag" namespace (struct tags) + public boolean addTag(String name, Type type) { + Map currentScope = tagScopes.peek(); + if (currentScope.containsKey(name)) { + return false; // Already exists in current scope + } + currentScope.put(name, new Symbol(name, type, Symbol.Kind.STRUCT_TAG, currentScopeId)); + return true; + } + + // Update existing tag in current scope (for completing incomplete structs) + public void updateTag(String name, Type type) { + Map currentScope = tagScopes.peek(); + currentScope.put(name, new Symbol(name, type, Symbol.Kind.STRUCT_TAG, currentScopeId)); + } + + // Lookup in "other" namespace + public Symbol lookupOther(String name) { + for (Map scope : otherScopes) { + if (scope.containsKey(name)) { + return scope.get(name); + } + } + return null; + } + + // Lookup in "tag" namespace + public Symbol lookupTag(String name) { + for (Map scope : tagScopes) { + if (scope.containsKey(name)) { + return scope.get(name); + } + } + return null; + } + + // Check if symbol exists in current scope only (for redefinition check) + public boolean existsInCurrentScopeOther(String name) { + return otherScopes.peek().containsKey(name); + } + + public boolean existsInCurrentScopeTag(String name) { + return tagScopes.peek().containsKey(name); + } + + // Get all symbols in file scope (for printing at the end) + public List getFileScopeOthers() { + if (otherScopes.isEmpty()) return Collections.emptyList(); + + // File scope is at the bottom of the stack + Map fileScope = null; + for (Map scope : otherScopes) { + fileScope = scope; + } + return fileScope != null ? new ArrayList<>(fileScope.values()) : Collections.emptyList(); + } + } + + // ===== Semantic Analyzer ===== + + private static class SemanticAnalyzer extends SplcBaseVisitor { + private final AbstractGrader grader; + private final SymbolTable symbolTable; + private final List globalVariables = new ArrayList<>(); + private final List globalFunctions = new ArrayList<>(); + + public SemanticAnalyzer(AbstractGrader grader) { + this.grader = grader; + this.symbolTable = new SymbolTable(); + } + + public List getGlobalVariables() { + return globalVariables; + } + + public List getGlobalFunctions() { + return globalFunctions; + } + + @Override + public Type visitProgram(SplcParser.ProgramContext ctx) { + for (SplcParser.GlobalDefContext globalDef : ctx.globalDef()) { + visitGlobalDef(globalDef); + } + return null; + } + + @Override + public Type visitGlobalDef(SplcParser.GlobalDefContext ctx) { + Type specType = visitSpecifier(ctx.specifier()); + + if (ctx.LBRACE() != null) { + // Function definition: specifier Identifier LPAREN funcArgs RPAREN LBRACE statement* RBRACE + TerminalNode funcName = ctx.Identifier(); + List paramTypes = new ArrayList<>(); + if (ctx.funcArgs().specifier() != null && !ctx.funcArgs().specifier().isEmpty()) { + for (int i = 0; i < ctx.funcArgs().specifier().size(); i++) { + Type paramSpecType = visitSpecifier(ctx.funcArgs().specifier(i)); + Type paramType = buildTypeFromVarDec(ctx.funcArgs().varDec(i), paramSpecType); + paramTypes.add(paramType); + } + } + + FunctionType funcType = new FunctionType(specType, paramTypes); + + // Check if function already exists in current scope + Symbol existing = symbolTable.lookupOther(funcName.getText()); + if (existing != null && existing.getScopeId() == symbolTable.getCurrentScopeId()) { + // If it's already a definition (FUNCTION), that's a redefinition error + if (existing.getKind() == Symbol.Kind.FUNCTION) { + grader.reportSemanticError(Project3SemanticError.redefinition(funcName)); + } + // If it's a declaration (FUNCTION_DECL), we can define it - update it + symbolTable.updateOther(funcName.getText(), funcType, Symbol.Kind.FUNCTION); + } else { + // No existing symbol, add it + symbolTable.addOther(funcName.getText(), funcType, Symbol.Kind.FUNCTION); + } + + // Track in global functions list (but check if already there from declaration) + boolean alreadyInList = false; + for (Symbol s : globalFunctions) { + if (s.getName().equals(funcName.getText())) { + alreadyInList = true; + break; + } + } + if (!alreadyInList) { + globalFunctions.add(new Symbol(funcName.getText(), funcType, Symbol.Kind.FUNCTION, 0)); + } + + // Enter function scope for parameters and body + symbolTable.enterScope(); + + // Add parameters to the function scope + if (ctx.funcArgs().specifier() != null && !ctx.funcArgs().specifier().isEmpty()) { + for (int i = 0; i < ctx.funcArgs().specifier().size(); i++) { + Type paramSpecType = visitSpecifier(ctx.funcArgs().specifier(i)); + String paramName = extractIdentifierFromVarDec(ctx.funcArgs().varDec(i)); + Type paramType = buildTypeFromVarDec(ctx.funcArgs().varDec(i), paramSpecType); + + // Check for incomplete type + if (!isCompleteType(paramType)) { + grader.reportSemanticError(Project3SemanticError.definitionIncomplete( + getIdentifierNode(ctx.funcArgs().varDec(i)))); + } + + symbolTable.addOther(paramName, paramType, Symbol.Kind.VARIABLE); + } + } + + // Visit function body + for (SplcParser.StatementContext stmt : ctx.statement()) { + visit(stmt); + } + + symbolTable.exitScope(); + + } else if (ctx.varDec() != null) { + // Global variable definition: specifier varDec (ASSIGN expression)? SEMI + String varName = extractIdentifierFromVarDec(ctx.varDec()); + Type varType = buildTypeFromVarDec(ctx.varDec(), specType); + + // Check for incomplete type + if (!isCompleteType(varType)) { + grader.reportSemanticError(Project3SemanticError.definitionIncomplete( + getIdentifierNode(ctx.varDec()))); + } + + // Check redefinition + if (symbolTable.existsInCurrentScopeOther(varName)) { + grader.reportSemanticError(Project3SemanticError.redefinition(getIdentifierNode(ctx.varDec()))); + } + + symbolTable.addOther(varName, varType, Symbol.Kind.VARIABLE); + globalVariables.add(new Symbol(varName, varType, Symbol.Kind.VARIABLE, 0)); + + } else if (ctx.Identifier() != null && ctx.funcArgs() != null) { + // Function declaration: specifier Identifier LPAREN funcArgs RPAREN SEMI + TerminalNode funcName = ctx.Identifier(); + List paramTypes = new ArrayList<>(); + if (ctx.funcArgs().specifier() != null && !ctx.funcArgs().specifier().isEmpty()) { + for (int i = 0; i < ctx.funcArgs().specifier().size(); i++) { + Type paramSpecType = visitSpecifier(ctx.funcArgs().specifier(i)); + Type paramType = buildTypeFromVarDec(ctx.funcArgs().varDec(i), paramSpecType); + paramTypes.add(paramType); + } + } + + FunctionType funcType = new FunctionType(specType, paramTypes); + + // Check redeclaration - can't declare a function if already declared/defined + Symbol existing = symbolTable.lookupOther(funcName.getText()); + if (existing != null && existing.getScopeId() == symbolTable.getCurrentScopeId()) { + grader.reportSemanticError(Project3SemanticError.redeclaration(funcName)); + } + + symbolTable.addOther(funcName.getText(), funcType, Symbol.Kind.FUNCTION_DECL); + globalFunctions.add(new Symbol(funcName.getText(), funcType, Symbol.Kind.FUNCTION_DECL, 0)); + } + // else: global struct declaration (specifier SEMI) - already handled in visitSpecifier + + return null; + } + + @Override + public Type visitSpecifier(SplcParser.SpecifierContext ctx) { + if (ctx.INT() != null) { + return new BasicType(BasicType.Kind.INT); + } else if (ctx.CHAR() != null) { + return new BasicType(BasicType.Kind.CHAR); + } else if (ctx.STRUCT() != null) { + String tagName = ctx.Identifier().getText(); + + if (ctx.LBRACE() != null) { + // Complete struct: struct Identifier { ... } + + // Parse members first + List members = new ArrayList<>(); + Set memberNames = new HashSet<>(); + + for (int i = 0; i < ctx.specifier().size(); i++) { + Type memberSpecType = visitSpecifier(ctx.specifier(i)); + String memberName = extractIdentifierFromVarDec(ctx.varDec(i)); + Type memberType = buildTypeFromVarDec(ctx.varDec(i), memberSpecType); + + // Check for incomplete member type + if (!isCompleteType(memberType)) { + grader.reportSemanticError(Project3SemanticError.memberIncomplete( + getIdentifierNode(ctx.varDec(i)))); + } + + // Check for duplicate member names + if (memberNames.contains(memberName)) { + grader.reportSemanticError(Project3SemanticError.memberDuplicate( + getIdentifierNode(ctx.varDec(i)))); + } + memberNames.add(memberName); + + members.add(new StructType.Member(memberType, memberName)); + } + + // Now check if this tag was already declared as complete in this scope + Symbol existingTag = symbolTable.lookupTag(tagName); + if (existingTag != null && existingTag.getScopeId() == symbolTable.getCurrentScopeId()) { + StructType existingStructType = (StructType) existingTag.getType(); + if (existingStructType.isComplete()) { + grader.reportSemanticError(Project3SemanticError.redeclaration(ctx.Identifier())); + } + } + + StructType structType = new StructType(tagName, members); + + // Update or add the tag + if (existingTag != null && existingTag.getScopeId() == symbolTable.getCurrentScopeId()) { + symbolTable.updateTag(tagName, structType); + } else { + symbolTable.addTag(tagName, structType); + } + return structType; + + } else { + // Incomplete struct reference: struct Identifier + Symbol existingTag = symbolTable.lookupTag(tagName); + if (existingTag != null) { + // Return the existing type (could be complete or incomplete) + return existingTag.getType(); + } else { + // Declare new incomplete struct type + StructType structType = new StructType(tagName); + symbolTable.addTag(tagName, structType); + return structType; + } + } + } + return null; + } + + @Override + public Type visitBracket(SplcParser.BracketContext ctx) { + // Block statement: LBRACE statement* RBRACE + symbolTable.enterScope(); + for (SplcParser.StatementContext stmt : ctx.statement()) { + visit(stmt); + } + symbolTable.exitScope(); + return null; + } + + @Override + public Type visitVarDecStmt(SplcParser.VarDecStmtContext ctx) { + // Local variable declaration: specifier varDec (ASSIGN expression)? SEMI + Type specType = visitSpecifier(ctx.specifier()); + String varName = extractIdentifierFromVarDec(ctx.varDec()); + Type varType = buildTypeFromVarDec(ctx.varDec(), specType); + + // Check for incomplete type + if (!isCompleteType(varType)) { + grader.reportSemanticError(Project3SemanticError.definitionIncomplete( + getIdentifierNode(ctx.varDec()))); + } + + // Check redefinition in current scope + if (symbolTable.existsInCurrentScopeOther(varName)) { + grader.reportSemanticError(Project3SemanticError.redefinition( + getIdentifierNode(ctx.varDec()))); + } + + symbolTable.addOther(varName, varType, Symbol.Kind.VARIABLE); + + // Visit initialization expression if present + if (ctx.expression() != null) { + visitExpression(ctx.expression()); + } + return null; + } + + @Override + public Type visitIfStmt(SplcParser.IfStmtContext ctx) { + visitExpression(ctx.expression()); + visit(ctx.statement(0)); + if (ctx.statement().size() > 1) { + visit(ctx.statement(1)); + } + return null; + } + + @Override + public Type visitWhileStmt(SplcParser.WhileStmtContext ctx) { + visitExpression(ctx.expression()); + visit(ctx.statement()); + return null; + } + + @Override + public Type visitReturnStmt(SplcParser.ReturnStmtContext ctx) { + visitExpression(ctx.expression()); + return null; + } + + @Override + public Type visitExprStmt(SplcParser.ExprStmtContext ctx) { + visitExpression(ctx.expression()); + return null; + } + + @Override + public Type visitExpression(SplcParser.ExpressionContext ctx) { + // First, check if this expression has an identifier that needs to be looked up + if (ctx.Identifier() != null) { + String name = ctx.Identifier().getText(); + Symbol symbol = symbolTable.lookupOther(name); + if (symbol == null) { + grader.reportSemanticError(Project3SemanticError.undeclaredUse(ctx.Identifier())); + } + } + + // Visit all sub-expressions + for (int i = 0; i < ctx.getChildCount(); i++) { + if (ctx.getChild(i) instanceof SplcParser.ExpressionContext) { + visitExpression((SplcParser.ExpressionContext) ctx.getChild(i)); + } + } + + return null; + } + + // Helper methods + + private String extractIdentifierFromVarDec(SplcParser.VarDecContext ctx) { + if (ctx.Identifier() != null) { + return ctx.Identifier().getText(); + } else if (ctx.varDec() != null) { + return extractIdentifierFromVarDec(ctx.varDec()); + } + return null; + } + + private TerminalNode getIdentifierNode(SplcParser.VarDecContext ctx) { + if (ctx.Identifier() != null) { + return ctx.Identifier(); + } else if (ctx.varDec() != null) { + return getIdentifierNode(ctx.varDec()); + } + return null; + } + + private Type buildTypeFromVarDec(SplcParser.VarDecContext ctx, Type baseType) { + if (ctx.Identifier() != null) { + // Base case: just the identifier + return baseType; + } else if (ctx.LPAREN() != null && ctx.varDec() != null) { + // Parenthesized: LPAREN varDec RPAREN + return buildTypeFromVarDec(ctx.varDec(), baseType); + } else if (ctx.LBRACK() != null) { + // Array: varDec LBRACK Number RBRACK + // Build array type from baseType, then pass it down + int arraySize = Integer.parseInt(ctx.Number().getText()); + Type arrayType = new ArrayType(baseType, arraySize); + return buildTypeFromVarDec(ctx.varDec(), arrayType); + } else if (ctx.STAR() != null) { + // Pointer: STAR varDec + // Build pointer type from baseType, then pass it down + Type pointerType = new PointerType(baseType); + return buildTypeFromVarDec(ctx.varDec(), pointerType); + } + return baseType; + } + + private boolean isCompleteType(Type type) { + if (type instanceof BasicType) { + return true; + } else if (type instanceof PointerType) { + return true; // Pointers are always complete + } else if (type instanceof ArrayType) { + // Array is complete if element type is complete + return isCompleteType(((ArrayType) type).getElementType()); + } else if (type instanceof StructType) { + return ((StructType) type).isComplete(); + } else if (type instanceof FunctionType) { + return true; + } + return false; + } } }