diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
| commit | 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch) | |
| tree | ed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300 | |
| download | misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.tar.gz misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.zip | |
Diffstat (limited to 'Homework/cs5300')
508 files changed, 27932 insertions, 0 deletions
diff --git a/Homework/cs5300/.DS_Store b/Homework/cs5300/.DS_Store Binary files differnew file mode 100644 index 0000000..262c49e --- /dev/null +++ b/Homework/cs5300/.DS_Store diff --git a/Homework/cs5300/compiler/instructions.pdf b/Homework/cs5300/compiler/instructions.pdf Binary files differnew file mode 100644 index 0000000..e5e0f9d --- /dev/null +++ b/Homework/cs5300/compiler/instructions.pdf diff --git a/Homework/cs5300/compiler/p4-formatter-soln.zip b/Homework/cs5300/compiler/p4-formatter-soln.zip Binary files differnew file mode 100644 index 0000000..25900f4 --- /dev/null +++ b/Homework/cs5300/compiler/p4-formatter-soln.zip diff --git a/Homework/cs5300/compiler/p5-compiler.zip b/Homework/cs5300/compiler/p5-compiler.zip Binary files differnew file mode 100644 index 0000000..5d87d94 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler.zip diff --git a/Homework/cs5300/compiler/p5-compiler/antlr-4.9.1-complete.jar b/Homework/cs5300/compiler/p5-compiler/antlr-4.9.1-complete.jar Binary files differnew file mode 100644 index 0000000..1bb7e8c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/antlr-4.9.1-complete.jar diff --git a/Homework/cs5300/compiler/p5-compiler/classes/main/Main.class b/Homework/cs5300/compiler/p5-compiler/classes/main/Main.class Binary files differnew file mode 100644 index 0000000..f461245 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/main/Main.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseListener.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseListener.class Binary files differnew file mode 100644 index 0000000..5cddf6a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseListener.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseVisitor.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseVisitor.class Binary files differnew file mode 100644 index 0000000..1b7feb2 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusBaseVisitor.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusLexer.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusLexer.class Binary files differnew file mode 100644 index 0000000..9ae1810 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusLexer.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusListener.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusListener.class Binary files differnew file mode 100644 index 0000000..30456a2 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusListener.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$AndExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$AndExpressionContext.class Binary files differnew file mode 100644 index 0000000..7ebed9f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$AndExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$BreakStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$BreakStmtContext.class Binary files differnew file mode 100644 index 0000000..ebfeb4c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$BreakStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CallContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CallContext.class Binary files differnew file mode 100644 index 0000000..9ba5dd4 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CallContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CompoundStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CompoundStmtContext.class Binary files differnew file mode 100644 index 0000000..0fd5cf4 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$CompoundStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ConstantContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ConstantContext.class Binary files differnew file mode 100644 index 0000000..7e79cf9 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ConstantContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$DeclarationContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$DeclarationContext.class Binary files differnew file mode 100644 index 0000000..f8c234f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$DeclarationContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionContext.class Binary files differnew file mode 100644 index 0000000..1db10d6 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionStmtContext.class Binary files differnew file mode 100644 index 0000000..4a2fc3b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ExpressionStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FactorContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FactorContext.class Binary files differnew file mode 100644 index 0000000..1d89bc5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FactorContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FunDeclarationContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FunDeclarationContext.class Binary files differnew file mode 100644 index 0000000..0d219a8 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$FunDeclarationContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$IfStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$IfStmtContext.class Binary files differnew file mode 100644 index 0000000..49acf8b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$IfStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ImmutableContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ImmutableContext.class Binary files differnew file mode 100644 index 0000000..7302f7d --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ImmutableContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MulopContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MulopContext.class Binary files differnew file mode 100644 index 0000000..e368562 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MulopContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MutableContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MutableContext.class Binary files differnew file mode 100644 index 0000000..5f01f57 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$MutableContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$OrExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$OrExpressionContext.class Binary files differnew file mode 100644 index 0000000..d05c700 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$OrExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamContext.class Binary files differnew file mode 100644 index 0000000..2f63524 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamIdContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamIdContext.class Binary files differnew file mode 100644 index 0000000..c27dd1c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ParamIdContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ProgramContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ProgramContext.class Binary files differnew file mode 100644 index 0000000..bf240d5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ProgramContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelExpressionContext.class Binary files differnew file mode 100644 index 0000000..61bd7e1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelopContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelopContext.class Binary files differnew file mode 100644 index 0000000..d6b329b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$RelopContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ReturnStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ReturnStmtContext.class Binary files differnew file mode 100644 index 0000000..57570f5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$ReturnStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SimpleExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SimpleExpressionContext.class Binary files differnew file mode 100644 index 0000000..badd4ca --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SimpleExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$StatementContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$StatementContext.class Binary files differnew file mode 100644 index 0000000..adf350f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$StatementContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumExpressionContext.class Binary files differnew file mode 100644 index 0000000..97c7849 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumopContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumopContext.class Binary files differnew file mode 100644 index 0000000..0608a30 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$SumopContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TermExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TermExpressionContext.class Binary files differnew file mode 100644 index 0000000..c1a224c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TermExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TypeSpecifierContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TypeSpecifierContext.class Binary files differnew file mode 100644 index 0000000..6f969b1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$TypeSpecifierContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryExpressionContext.class Binary files differnew file mode 100644 index 0000000..e62d666 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryRelExpressionContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryRelExpressionContext.class Binary files differnew file mode 100644 index 0000000..bfad12c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryRelExpressionContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryopContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryopContext.class Binary files differnew file mode 100644 index 0000000..3ed211f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$UnaryopContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclIdContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclIdContext.class Binary files differnew file mode 100644 index 0000000..9a9a353 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclIdContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclarationContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclarationContext.class Binary files differnew file mode 100644 index 0000000..941590c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$VarDeclarationContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$WhileStmtContext.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$WhileStmtContext.class Binary files differnew file mode 100644 index 0000000..5c3d80f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser$WhileStmtContext.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser.class Binary files differnew file mode 100644 index 0000000..f27fd6c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusParser.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusVisitor.class b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusVisitor.class Binary files differnew file mode 100644 index 0000000..cc93b20 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/parser/CminusVisitor.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ASTVisitor.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ASTVisitor.class Binary files differnew file mode 100644 index 0000000..8c7f27b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ASTVisitor.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/MIPSResult.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/MIPSResult.class Binary files differnew file mode 100644 index 0000000..b4e28d5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/MIPSResult.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/RegisterAllocator.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/RegisterAllocator.class Binary files differnew file mode 100644 index 0000000..0da867a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/RegisterAllocator.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolInfo.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolInfo.class Binary files differnew file mode 100644 index 0000000..a9471c6 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolInfo.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolTable.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolTable.class Binary files differnew file mode 100644 index 0000000..27b6f64 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/SymbolTable.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AbstractNode.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AbstractNode.class Binary files differnew file mode 100644 index 0000000..66257f6 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AbstractNode.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Assignment.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Assignment.class Binary files differnew file mode 100644 index 0000000..14204a0 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Assignment.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AssignmentType.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AssignmentType.class Binary files differnew file mode 100644 index 0000000..c7fdd8a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/AssignmentType.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator$1.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator$1.class Binary files differnew file mode 100644 index 0000000..f683d64 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator$1.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator.class Binary files differnew file mode 100644 index 0000000..d91bd0a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperator.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperatorType.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperatorType.class Binary files differnew file mode 100644 index 0000000..08cd659 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BinaryOperatorType.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BoolConstant.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BoolConstant.class Binary files differnew file mode 100644 index 0000000..a3cee08 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/BoolConstant.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Break.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Break.class Binary files differnew file mode 100644 index 0000000..376bdb9 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Break.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Call.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Call.class Binary files differnew file mode 100644 index 0000000..241ee2e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Call.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CharConstant.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CharConstant.class Binary files differnew file mode 100644 index 0000000..48abee9 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CharConstant.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CompoundStatement.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CompoundStatement.class Binary files differnew file mode 100644 index 0000000..3474071 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/CompoundStatement.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Declaration.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Declaration.class Binary files differnew file mode 100644 index 0000000..f7c69f1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Declaration.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Expression.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Expression.class Binary files differnew file mode 100644 index 0000000..f9a56bf --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Expression.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ExpressionStatement.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ExpressionStatement.class Binary files differnew file mode 100644 index 0000000..7d301d8 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ExpressionStatement.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/FunDeclaration.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/FunDeclaration.class Binary files differnew file mode 100644 index 0000000..ba002b5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/FunDeclaration.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/If.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/If.class Binary files differnew file mode 100644 index 0000000..bc919ab --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/If.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Mutable.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Mutable.class Binary files differnew file mode 100644 index 0000000..5f9636a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Mutable.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Node.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Node.class Binary files differnew file mode 100644 index 0000000..ad066f3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Node.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/NumConstant.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/NumConstant.class Binary files differnew file mode 100644 index 0000000..01150f4 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/NumConstant.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Param.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Param.class Binary files differnew file mode 100644 index 0000000..0d84a94 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Param.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ParenExpression.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ParenExpression.class Binary files differnew file mode 100644 index 0000000..16f0ed3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/ParenExpression.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Program.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Program.class Binary files differnew file mode 100644 index 0000000..fe21182 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Program.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Return.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Return.class Binary files differnew file mode 100644 index 0000000..68bb5d7 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Return.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Statement.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Statement.class Binary files differnew file mode 100644 index 0000000..a15d63e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/Statement.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/StringConstant.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/StringConstant.class Binary files differnew file mode 100644 index 0000000..702959a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/StringConstant.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperator.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperator.class Binary files differnew file mode 100644 index 0000000..4962398 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperator.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperatorType.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperatorType.class Binary files differnew file mode 100644 index 0000000..b60267c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/UnaryOperatorType.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarDeclaration.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarDeclaration.class Binary files differnew file mode 100644 index 0000000..64effab --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarDeclaration.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarType.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarType.class Binary files differnew file mode 100644 index 0000000..7ff5f06 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/VarType.class diff --git a/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/While.class b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/While.class Binary files differnew file mode 100644 index 0000000..ec60041 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/classes/submit/ast/While.class diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar b/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar Binary files differnew file mode 100644 index 0000000..0021281 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm new file mode 100644 index 0000000..b849530 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm @@ -0,0 +1,35 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "Hello world" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm new file mode 100644 index 0000000..4d0bb1f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm @@ -0,0 +1,434 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 0 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 1 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 4 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 5 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 6 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 8 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 9 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 10 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 11 numbers of the Fibonacci sequence" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm new file mode 100644 index 0000000..f72fb29 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm @@ -0,0 +1,92 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel1 +datalabel2: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 0 through 9." diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm new file mode 100644 index 0000000..dbb716b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm @@ -0,0 +1,213 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel5: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 12 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel6 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel6: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 12 numbers of the Fibonacci sequence." diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm new file mode 100644 index 0000000..a459701 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm @@ -0,0 +1,296 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 0 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 0 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 2 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 2 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -44 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel1 +datalabel2: +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 3 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +li $t1 6 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This should print 0, 2, 2, 3, 6 and 36" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm new file mode 100644 index 0000000..4c16021 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm @@ -0,0 +1,289 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for sum +sum: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# sum +# n +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -16 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 -20 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel0: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -16 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +# Get n's offset from $sp from the symbol table and initialize n's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of n. +lw $t1 0($t2) +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -20 +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of sum. +lw $t1 0($t2) +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 16 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Get x's base address from the stack. +lw $t3 0($t3) +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t5 4 +# Add the stack pointer address to the offset. +add $t5 $t5 $sp +# Load the value of i. +lw $t4 0($t5) +# Multiply the index by -4. +li $t5 4 +mul $t4 $t4 $t5 +# Add the index offset to the offset. +add $t3 $t3 $t4 +# Load the value of x. +lw $t2 0($t3) +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 20 +j datalabel0 +datalabel1: +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t1 -20 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of sum. +lw $t0 0($t1) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel2 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel3: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel3 +datalabel4: +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 4 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 8 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel2: .asciiz "This should print 6 and 28" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm new file mode 100644 index 0000000..7f3312a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm @@ -0,0 +1,89 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 3 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 14 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 1 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 2 +mult $t0 $t1 +mflo $t0 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm new file mode 100644 index 0000000..69f73f3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm @@ -0,0 +1,73 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints the number 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm new file mode 100644 index 0000000..db276ba --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm @@ -0,0 +1,162 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# Update the stack pointer. +addi $sp $sp -8 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Entering a new scope. +# Symbols in symbol table: +# println +# b +# Update the stack pointer. +addi $sp $sp -4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +sub $t1 $zero $t1 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 8 +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm new file mode 100644 index 0000000..76837bc --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm @@ -0,0 +1,155 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for foo +foo: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for fum +fum: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 12 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t1 -8 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of b. +lw $t0 0($t1) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sub $t0 $t0 $t1 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Calling function fum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fum +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm new file mode 100644 index 0000000..1668afa --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm @@ -0,0 +1,138 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -16($sp) +li $t1 4 +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sw $t1 -16($sp) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm new file mode 100644 index 0000000..8414044 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm @@ -0,0 +1,141 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for identity +identity: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function identity +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal identity +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm new file mode 100644 index 0000000..b9a3263 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm @@ -0,0 +1,164 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add2 +add2: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +# Evaluate parameters and save to stack +# Calling function add +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +sw $t1 -20($sp) +# Evaluate parameters and save to stack +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of x. +lw $t2 0($t3) +sw $t2 -24($sp) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t3 -8 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of y. +lw $t2 0($t3) +sw $t2 -28($sp) +# Update the stack pointer +add $sp $sp -20 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 20 +# Restore $t0-9 registers +lw $t0 -16($sp) +lw $t1 -20($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -32($sp) +sw $t1 -20($sp) +li $t1 1 +sw $t1 -24($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -16($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -28($sp) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add2 +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add2 +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm new file mode 100644 index 0000000..db843e0 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm @@ -0,0 +1,239 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel3 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel2 +datalabel1: +datalabel2: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel6 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel4: +datalabel5: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel7 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel9 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel8 +datalabel7: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel10 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel8: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +slt $t0 $t1 $t0 +bne $t0 $zero datalabel11 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel13 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel12 +datalabel11: +datalabel12: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel14 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel16 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel15 +datalabel14: +datalabel15: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +bne $t0 $zero datalabel17 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel19 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel18 +datalabel17: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel20 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel18: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints [1..5] correct." +datalabel3: .asciiz "1 correct" +datalabel6: .asciiz "2 not correct" +datalabel9: .asciiz "2 not correct" +datalabel10: .asciiz "2 correct" +datalabel13: .asciiz "3 correct" +datalabel16: .asciiz "4 correct" +datalabel19: .asciiz "5 not correct" +datalabel20: .asciiz "5 correct" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test b/Homework/cs5300/compiler/p5-compiler/data/test new file mode 100644 index 0000000..89d5b0c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test @@ -0,0 +1,23 @@ +#! /bin/bash + +java -jar ./Mars4_5.jar test1.asm +java -jar ./Mars4_5.jar test2.asm +java -jar ./Mars4_5.jar test3.asm +java -jar ./Mars4_5.jar test4.asm +java -jar ./Mars4_5.jar test5.asm +java -jar ./Mars4_5.jar test6.asm +java -jar ./Mars4_5.jar test7.asm +java -jar ./Mars4_5.jar test8.asm +java -jar ./Mars4_5.jar test9.asm +java -jar ./Mars4_5.jar test10.asm +java -jar ./Mars4_5.jar test11.asm +java -jar ./Mars4_5.jar test12.asm +java -jar ./Mars4_5.jar test13.asm +java -jar ./Mars4_5.jar test14.asm + +# This loop runs the Java code to create the .asm then runs the .asm. +#for i in {1..14} +#do +# java -cp ../../out/production/p5-compiler:../../antlr-4.9.1-complete.jar main.Main ../test$i.c +# java -jar ./Mars4_5.jar ../test$i.asm +#done diff --git a/Homework/cs5300/compiler/p5-compiler/data/test1.asm b/Homework/cs5300/compiler/p5-compiler/data/test1.asm new file mode 100644 index 0000000..336d375 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test1.asm @@ -0,0 +1,44 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 string_1 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "Hello world" +string_1: .asciiz "hi" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test1.c b/Homework/cs5300/compiler/p5-compiler/data/test1.c new file mode 100644 index 0000000..fccaead --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test1.c @@ -0,0 +1,5 @@ +void main() +{ + println("Hello world"); + println(7, "hi"); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test10.c b/Homework/cs5300/compiler/p5-compiler/data/test10.c new file mode 100644 index 0000000..a90a21f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test10.c @@ -0,0 +1,20 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + println("This program prints the first 11 numbers of the Fibonacci sequence"); + println(fib(0)); // 1 + println(fib(1)); // 1 + println(fib(2)); // 2 + println(fib(3)); // 3 + println(fib(4)); // 5 + println(fib(5)); // 8 + println(fib(6)); // 13 + println(fib(7)); // 21 + println(fib(8)); // 34 + println(fib(9)); // 55 + println(fib(10)); // 89 +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test11.c b/Homework/cs5300/compiler/p5-compiler/data/test11.c new file mode 100644 index 0000000..865bcf8 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test11.c @@ -0,0 +1,9 @@ +void main() { + int i; + i = 0; + println("This program prints 0 through 9."); + while (i < 10) { + println(i); + i = i + 1; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test12.c b/Homework/cs5300/compiler/p5-compiler/data/test12.c new file mode 100644 index 0000000..4bb7f58 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test12.c @@ -0,0 +1,15 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + int i; + i = 0; + println("This program prints the first 12 numbers of the Fibonacci sequence."); + while (i < 12) { + println(fib(i)); + i = i + 1; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test13.c b/Homework/cs5300/compiler/p5-compiler/data/test13.c new file mode 100644 index 0000000..c1a5c72 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test13.c @@ -0,0 +1,23 @@ +void main() { + int a[10]; + int i; + + println("This should print 0, 2, 2, 3, 6 and 36"); + a[0] = 0; + println(a[0]); + a[2] = 2; + println(a[2]); + + i = 2; + a[i] = i; + println(a[i]); + + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + println(a[3]); + println(a[6]); + println(a[6]*6); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test14.c b/Homework/cs5300/compiler/p5-compiler/data/test14.c new file mode 100644 index 0000000..45565fa --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test14.c @@ -0,0 +1,27 @@ +// Sum the first n elements of the array +int sum(int x[], int n) { + int i; + int sum; + i = 0; + sum = 0; + while (i < n) { + sum = sum + x[i]; + i = i + 1; + } + return sum; +} + +void main() { + int a[10]; + int i; + + println("This should print 6 and 28"); + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + + println(sum(a, 4)); + println(sum(a, 8)); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test2.asm b/Homework/cs5300/compiler/p5-compiler/data/test2.asm new file mode 100644 index 0000000..e7792ab --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test2.asm @@ -0,0 +1,86 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 3 +li $t1 4 +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 14 +li $t1 2 +div $t0 $t1 +mflo $t2 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +li $t1 1 +mult $t0 $t1 +mflo $t2 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +li $t1 2 +mult $t0 $t1 +mflo $t2 +li $t0 2 +div $t2 $t0 +mflo $t1 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test2.c b/Homework/cs5300/compiler/p5-compiler/data/test2.c new file mode 100644 index 0000000..ec9b22a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test2.c @@ -0,0 +1,9 @@ +void main() +{ + println("This program prints 7 7 7 7 7 (separated by newlines)"); + println(7); + println(3 + 4); + println(14 / 2); + println(7 * 1); + println((7 * 2) / 2); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test3.asm b/Homework/cs5300/compiler/p5-compiler/data/test3.asm new file mode 100644 index 0000000..6afcca1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test3.asm @@ -0,0 +1,56 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 -4 +add $t0 $t0 $sp +li $t1 3 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 4 +sw $t1 0($t0) +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints the number 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test3.c b/Homework/cs5300/compiler/p5-compiler/data/test3.c new file mode 100644 index 0000000..1083434 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test3.c @@ -0,0 +1,9 @@ +void main() { + int a; + int b; + + println("This program prints the number 7"); + a = 3; + b = 4; + println(a+b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test4.asm b/Homework/cs5300/compiler/p5-compiler/data/test4.asm new file mode 100644 index 0000000..978e71e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test4.asm @@ -0,0 +1,118 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 -4 +add $t0 $t0 $sp +li $t1 3 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 2 +sw $t1 0($t0) +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# Update the stack pointer. +addi $sp $sp -8 +li $t0 -4 +add $t0 $t0 $sp +li $t1 5 +sw $t1 0($t0) +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 0 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Entering a new scope. +# Symbols in symbol table: +# println +# b +# Update the stack pointer. +addi $sp $sp -4 +li $t0 -4 +add $t0 $t0 $sp +li $t1 9 +sw $t1 0($t0) +li $t0 0 +add $t0 $t0 $sp +li $t1 2 +# Negation +sub $t1, $zero, $t1 +sw $t1 0($t0) +li $t0 0 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 4 +li $t0 0 +add $t0 $t0 $sp +li $t1 4 +sw $t1 0($t0) +# Exit scope. +addi $sp $sp 8 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test4.c b/Homework/cs5300/compiler/p5-compiler/data/test4.c new file mode 100644 index 0000000..04c877a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test4.c @@ -0,0 +1,21 @@ +void main() { + int a; + int b; + + println("This program prints 7 7 7"); + a = 3; + b = 2; + { + int a; + a = 5; + println(a + b); + { + int b; + b = 9; + a = -2; + println(a + b); + } + b = 4; + } + println(a + b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test5.asm b/Homework/cs5300/compiler/p5-compiler/data/test5.asm new file mode 100644 index 0000000..511f09f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test5.asm @@ -0,0 +1,105 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +foo: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +jr $ra +fum: +# Entering a new scope. +# Symbols in symbol table: +# a : -4 +# println : 0 +# b : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +li $t1 9 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 12 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +sub $t2 $t0 $t1 +li $t0 4 +add $t1 $t2 $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -12($sp) +add $sp $sp -12 +jal foo +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +add $sp $sp -4 +jal foo +add $sp $sp 4 +lw $t0 -4($sp) +move $ra $t0 +move $t0 $ra +sw $t0 -4($sp) +add $sp $sp -4 +jal fum +add $sp $sp 4 +lw $t0 -4($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test5.c b/Homework/cs5300/compiler/p5-compiler/data/test5.c new file mode 100644 index 0000000..835db83 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test5.c @@ -0,0 +1,19 @@ +void foo() { + println(7); +} + +void fum() { + int a; + int b; + a = 9; + b = 12; + println(b-a+4); + + foo(); +} + +void main() { + println("This program prints 7 7 7"); + foo(); + fum(); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test6.asm b/Homework/cs5300/compiler/p5-compiler/data/test6.asm new file mode 100644 index 0000000..83a0a59 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test6.asm @@ -0,0 +1,94 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# i : -12 +# y : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# a : -4 +# println : 0 +# b : -8 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -12($sp) +li $t1 3 +sw $t1 -16($sp) +li $t1 4 +sw $t1 -20($sp) +add $sp $sp -12 +jal add +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +li $t0 -4 +add $t0 $t0 $sp +li $t1 5 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 2 +sw $t1 0($t0) +move $t0 $ra +sw $t0 -12($sp) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +sw $t1 -16($sp) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +sw $t1 -20($sp) +add $sp $sp -12 +jal add +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test6.c b/Homework/cs5300/compiler/p5-compiler/data/test6.c new file mode 100644 index 0000000..03de49c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test6.c @@ -0,0 +1,15 @@ +void add(int x, int y) { + int i; + println(x+y); +} + +void main() { + int a; + int b; + println("This program prints 7 7"); + add(3, 4); + + a = 5; + b = 2; + add(a, b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test7.asm b/Homework/cs5300/compiler/p5-compiler/data/test7.asm new file mode 100644 index 0000000..a84c1c9 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test7.asm @@ -0,0 +1,107 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +identity: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# return : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +# symbol table size - -8 +sw $t0 -8($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# symbol table size - -12 +sw $t2 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 7 +sw $t1 -8($sp) +add $sp $sp -4 +jal identity +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -8 +lw $t1 -12($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 3 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +add $sp $sp -4 +jal add +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -12 +lw $t1 -16($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test7.c b/Homework/cs5300/compiler/p5-compiler/data/test7.c new file mode 100644 index 0000000..e7f45a5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test7.c @@ -0,0 +1,14 @@ +int identity(int x) { + return x; +} + +int add(int x, int y) { + return x+y; +} + +void main() { + println("This program prints 7 7"); + println(identity(7)); + println(add(3, 4)); +} + diff --git a/Homework/cs5300/compiler/p5-compiler/data/test8.asm b/Homework/cs5300/compiler/p5-compiler/data/test8.asm new file mode 100644 index 0000000..09281f5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test8.asm @@ -0,0 +1,118 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# symbol table size - -12 +sw $t2 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +add2: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +move $t0 $ra +sw $t0 -16($sp) +move $t1 $ra +sw $t0 -16($sp) +sw $t1 -20($sp) +li $t2 -4 +add $t2 $t2 $sp +lw $t2 0($t2) +sw $t2 -24($sp) +li $t2 -8 +add $t2 $t2 $sp +lw $t2 0($t2) +sw $t2 -28($sp) +add $sp $sp -20 +jal add +add $sp $sp 20 +lw $t0 -16($sp) +lw $t1 -20($sp) +# symbol table size - -28 +lw $t2 -32($sp) +move $ra $t1 +sw $t2 -20($sp) +li $t1 1 +sw $t1 -24($sp) +add $sp $sp -16 +jal add +add $sp $sp 16 +lw $t0 -16($sp) +# symbol table size - -24 +lw $t1 -28($sp) +move $ra $t0 +# symbol table size - -12 +sw $t1 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 2 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +add $sp $sp -4 +jal add2 +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -12 +lw $t1 -16($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test8.c b/Homework/cs5300/compiler/p5-compiler/data/test8.c new file mode 100644 index 0000000..df0f2ec --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test8.c @@ -0,0 +1,14 @@ + +int add(int x, int y) { + return x+y; +} + +int add2(int x, int y) { + return add(add(x,y),1); +} + +void main() { + println("This program prints 7"); + println(add2(2, 4)); +} + diff --git a/Homework/cs5300/compiler/p5-compiler/data/test9.c b/Homework/cs5300/compiler/p5-compiler/data/test9.c new file mode 100644 index 0000000..1aacb59 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test9.c @@ -0,0 +1,27 @@ +void main() { + int a; + a = 3; + println("This program prints [1..5] correct."); + if (a < 4) { + println("1 correct"); + } + if (a > 4) { + println("2 not correct"); + } + if (a > 4) { + println("2 not correct"); + } else { + println("2 correct"); + } + if (a <= 3) { + println("3 correct"); + } + if (a == 3) { + println("4 correct"); + } + if (a >= 4) { + println("5 not correct"); + } else { + println("5 correct"); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/main/Main.java b/Homework/cs5300/compiler/p5-compiler/main/Main.java new file mode 100644 index 0000000..9df6a18 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/main/Main.java @@ -0,0 +1,109 @@ +package main; + +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import parser.CminusLexer; +import parser.CminusParser; +import submit.ASTVisitor; +import org.antlr.v4.runtime.Parser; +import submit.RegisterAllocator; +import submit.SymbolTable; +import submit.ast.Node; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Main { + private static Logger LOGGER; + + /** + * @param args the command line arguments + * @throws java.io.IOException + */ + public static void main(String[] args) throws IOException { + // Logging setup + Level level = Level.INFO; + + // TODO Enable trace-level code as needed. When true, LOGGER.fine() statements + // will be visible. + final boolean trace = true; + if (trace) { + level = Level.ALL; + } + + Properties props = System.getProperties(); + props.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%6$s%n"); + Logger.getLogger("").setLevel(level); + for (Handler handler : Logger.getLogger("").getHandlers()) { + handler.setLevel(level); + } + LOGGER = Logger.getLogger(Parser.class.getName()); + + if (args.length < 1) { + throw new RuntimeException("Be sure to add your test C- file as a command-line parameter."); + } + final String filename = args[0]; + + LOGGER.info(""); + LOGGER.info("Parsing " + filename + "\n"); + LOGGER.info(""); + final CharStream charStream = CharStreams.fromFileName(filename); + CminusLexer lexer = new CminusLexer(charStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + CminusParser parser = new CminusParser(tokens); + parser.setBuildParseTree(true); + CminusParser.ProgramContext programCtx = parser.program(); + + LOGGER.info(""); + LOGGER.info("Building abstract syntax tree"); + LOGGER.info(""); + ASTVisitor v = new ASTVisitor(LOGGER); + Node ast = v.visitProgram(programCtx); + SymbolTable symbolTable = v.getSymbolTable(); + + LOGGER.info(""); + LOGGER.info("MIPS code:"); + LOGGER.info(""); + StringBuilder code = new StringBuilder(); + StringBuilder data = new StringBuilder(); + RegisterAllocator regAllocator = new RegisterAllocator(); + try { + ast.toMIPS(code, data, symbolTable, regAllocator); + } finally { + StringBuilder mips = new StringBuilder(); + mips.append("# All program code is placed after the\n" + + "# .text assembler directive\n" + + ".text\n" + + "\n" + + "# Declare main as a global function\n" + + ".globl\tmain\n\nj main\n"); + mips.append(code.toString()); + mips.append("\n# All memory structures are placed after the\n" + + "# .data assembler directive\n" + + ".data\n" + + "\n"); + mips.append(data.toString()); + + // Write MIPS code to stdout + LOGGER.info(mips.toString()); + + // Write MIPS code to output file for easy loading into MARS + final String fn = filename.substring(0, filename.length() - 1) + "asm"; + try { + FileWriter myWriter = new FileWriter(fn); + myWriter.write(mips.toString()); + myWriter.close(); + } catch (IOException e) { + LOGGER.severe("Error writing to file " + fn + "\n"); + e.printStackTrace(); + } + } + } + +} diff --git a/Homework/cs5300/compiler/p5-compiler/parser/Cminus.interp b/Homework/cs5300/compiler/p5-compiler/parser/Cminus.interp new file mode 100644 index 0000000..af140be --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/Cminus.interp @@ -0,0 +1,140 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +program +declaration +varDeclaration +varDeclId +funDeclaration +typeSpecifier +param +paramId +statement +compoundStmt +expressionStmt +ifStmt +whileStmt +returnStmt +breakStmt +expression +simpleExpression +orExpression +andExpression +unaryRelExpression +relExpression +relop +sumExpression +sumop +termExpression +mulop +unaryExpression +unaryop +factor +mutable +immutable +call +constant + + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 50, 329, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 3, 2, 6, 2, 70, 10, 2, 13, 2, 14, 2, 71, 3, 3, 3, 3, 5, 3, 76, 10, 3, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 82, 10, 4, 12, 4, 14, 4, 85, 11, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 94, 10, 5, 3, 6, 3, 6, 5, 6, 98, 10, 6, 3, 6, 3, 6, 3, 6, 5, 6, 103, 10, 6, 3, 6, 3, 6, 7, 6, 107, 10, 6, 12, 6, 14, 6, 110, 11, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 5, 9, 123, 10, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 5, 10, 131, 10, 10, 3, 11, 3, 11, 7, 11, 135, 10, 11, 12, 11, 14, 11, 138, 11, 11, 3, 11, 7, 11, 141, 10, 11, 12, 11, 14, 11, 144, 11, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 5, 12, 152, 10, 12, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 5, 13, 168, 10, 13, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 5, 15, 182, 10, 15, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 5, 17, 214, 10, 17, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 7, 19, 221, 10, 19, 12, 19, 14, 19, 224, 11, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 20, 7, 20, 231, 10, 20, 12, 20, 14, 20, 234, 11, 20, 3, 20, 3, 20, 3, 21, 7, 21, 239, 10, 21, 12, 21, 14, 21, 242, 11, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 7, 22, 249, 10, 22, 12, 22, 14, 22, 252, 11, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 7, 24, 261, 10, 24, 12, 24, 14, 24, 264, 11, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 7, 26, 273, 10, 26, 12, 26, 14, 26, 276, 11, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 28, 7, 28, 283, 10, 28, 12, 28, 14, 28, 286, 11, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 30, 3, 30, 5, 30, 294, 10, 30, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 5, 31, 302, 10, 31, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 5, 32, 310, 10, 32, 3, 33, 3, 33, 3, 33, 3, 33, 3, 33, 7, 33, 317, 10, 33, 12, 33, 14, 33, 320, 11, 33, 3, 33, 5, 33, 323, 10, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 2, 2, 35, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 2, 8, 3, 2, 10, 12, 3, 2, 30, 35, 3, 2, 36, 37, 3, 2, 38, 40, 4, 2, 37, 38, 41, 41, 4, 2, 42, 43, 45, 47, 2, 333, 2, 69, 3, 2, 2, 2, 4, 75, 3, 2, 2, 2, 6, 77, 3, 2, 2, 2, 8, 93, 3, 2, 2, 2, 10, 97, 3, 2, 2, 2, 12, 114, 3, 2, 2, 2, 14, 116, 3, 2, 2, 2, 16, 122, 3, 2, 2, 2, 18, 130, 3, 2, 2, 2, 20, 132, 3, 2, 2, 2, 22, 151, 3, 2, 2, 2, 24, 167, 3, 2, 2, 2, 26, 169, 3, 2, 2, 2, 28, 181, 3, 2, 2, 2, 30, 183, 3, 2, 2, 2, 32, 213, 3, 2, 2, 2, 34, 215, 3, 2, 2, 2, 36, 222, 3, 2, 2, 2, 38, 232, 3, 2, 2, 2, 40, 240, 3, 2, 2, 2, 42, 250, 3, 2, 2, 2, 44, 255, 3, 2, 2, 2, 46, 262, 3, 2, 2, 2, 48, 267, 3, 2, 2, 2, 50, 274, 3, 2, 2, 2, 52, 279, 3, 2, 2, 2, 54, 284, 3, 2, 2, 2, 56, 289, 3, 2, 2, 2, 58, 293, 3, 2, 2, 2, 60, 301, 3, 2, 2, 2, 62, 309, 3, 2, 2, 2, 64, 311, 3, 2, 2, 2, 66, 326, 3, 2, 2, 2, 68, 70, 5, 4, 3, 2, 69, 68, 3, 2, 2, 2, 70, 71, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 72, 3, 2, 2, 2, 72, 3, 3, 2, 2, 2, 73, 76, 5, 6, 4, 2, 74, 76, 5, 10, 6, 2, 75, 73, 3, 2, 2, 2, 75, 74, 3, 2, 2, 2, 76, 5, 3, 2, 2, 2, 77, 78, 5, 12, 7, 2, 78, 83, 5, 8, 5, 2, 79, 80, 7, 3, 2, 2, 80, 82, 5, 8, 5, 2, 81, 79, 3, 2, 2, 2, 82, 85, 3, 2, 2, 2, 83, 81, 3, 2, 2, 2, 83, 84, 3, 2, 2, 2, 84, 86, 3, 2, 2, 2, 85, 83, 3, 2, 2, 2, 86, 87, 7, 4, 2, 2, 87, 7, 3, 2, 2, 2, 88, 94, 7, 44, 2, 2, 89, 90, 7, 44, 2, 2, 90, 91, 7, 5, 2, 2, 91, 92, 7, 45, 2, 2, 92, 94, 7, 6, 2, 2, 93, 88, 3, 2, 2, 2, 93, 89, 3, 2, 2, 2, 94, 9, 3, 2, 2, 2, 95, 98, 7, 7, 2, 2, 96, 98, 5, 12, 7, 2, 97, 95, 3, 2, 2, 2, 97, 96, 3, 2, 2, 2, 98, 99, 3, 2, 2, 2, 99, 100, 7, 44, 2, 2, 100, 102, 7, 8, 2, 2, 101, 103, 5, 14, 8, 2, 102, 101, 3, 2, 2, 2, 102, 103, 3, 2, 2, 2, 103, 108, 3, 2, 2, 2, 104, 105, 7, 3, 2, 2, 105, 107, 5, 14, 8, 2, 106, 104, 3, 2, 2, 2, 107, 110, 3, 2, 2, 2, 108, 106, 3, 2, 2, 2, 108, 109, 3, 2, 2, 2, 109, 111, 3, 2, 2, 2, 110, 108, 3, 2, 2, 2, 111, 112, 7, 9, 2, 2, 112, 113, 5, 18, 10, 2, 113, 11, 3, 2, 2, 2, 114, 115, 9, 2, 2, 2, 115, 13, 3, 2, 2, 2, 116, 117, 5, 12, 7, 2, 117, 118, 5, 16, 9, 2, 118, 15, 3, 2, 2, 2, 119, 123, 7, 44, 2, 2, 120, 121, 7, 44, 2, 2, 121, 123, 7, 13, 2, 2, 122, 119, 3, 2, 2, 2, 122, 120, 3, 2, 2, 2, 123, 17, 3, 2, 2, 2, 124, 131, 5, 22, 12, 2, 125, 131, 5, 20, 11, 2, 126, 131, 5, 24, 13, 2, 127, 131, 5, 26, 14, 2, 128, 131, 5, 28, 15, 2, 129, 131, 5, 30, 16, 2, 130, 124, 3, 2, 2, 2, 130, 125, 3, 2, 2, 2, 130, 126, 3, 2, 2, 2, 130, 127, 3, 2, 2, 2, 130, 128, 3, 2, 2, 2, 130, 129, 3, 2, 2, 2, 131, 19, 3, 2, 2, 2, 132, 136, 7, 14, 2, 2, 133, 135, 5, 6, 4, 2, 134, 133, 3, 2, 2, 2, 135, 138, 3, 2, 2, 2, 136, 134, 3, 2, 2, 2, 136, 137, 3, 2, 2, 2, 137, 142, 3, 2, 2, 2, 138, 136, 3, 2, 2, 2, 139, 141, 5, 18, 10, 2, 140, 139, 3, 2, 2, 2, 141, 144, 3, 2, 2, 2, 142, 140, 3, 2, 2, 2, 142, 143, 3, 2, 2, 2, 143, 145, 3, 2, 2, 2, 144, 142, 3, 2, 2, 2, 145, 146, 7, 15, 2, 2, 146, 21, 3, 2, 2, 2, 147, 148, 5, 32, 17, 2, 148, 149, 7, 4, 2, 2, 149, 152, 3, 2, 2, 2, 150, 152, 7, 4, 2, 2, 151, 147, 3, 2, 2, 2, 151, 150, 3, 2, 2, 2, 152, 23, 3, 2, 2, 2, 153, 154, 7, 16, 2, 2, 154, 155, 7, 8, 2, 2, 155, 156, 5, 34, 18, 2, 156, 157, 7, 9, 2, 2, 157, 158, 5, 18, 10, 2, 158, 168, 3, 2, 2, 2, 159, 160, 7, 16, 2, 2, 160, 161, 7, 8, 2, 2, 161, 162, 5, 34, 18, 2, 162, 163, 7, 9, 2, 2, 163, 164, 5, 18, 10, 2, 164, 165, 7, 17, 2, 2, 165, 166, 5, 18, 10, 2, 166, 168, 3, 2, 2, 2, 167, 153, 3, 2, 2, 2, 167, 159, 3, 2, 2, 2, 168, 25, 3, 2, 2, 2, 169, 170, 7, 18, 2, 2, 170, 171, 7, 8, 2, 2, 171, 172, 5, 34, 18, 2, 172, 173, 7, 9, 2, 2, 173, 174, 5, 18, 10, 2, 174, 27, 3, 2, 2, 2, 175, 176, 7, 19, 2, 2, 176, 182, 7, 4, 2, 2, 177, 178, 7, 19, 2, 2, 178, 179, 5, 32, 17, 2, 179, 180, 7, 4, 2, 2, 180, 182, 3, 2, 2, 2, 181, 175, 3, 2, 2, 2, 181, 177, 3, 2, 2, 2, 182, 29, 3, 2, 2, 2, 183, 184, 7, 20, 2, 2, 184, 185, 7, 4, 2, 2, 185, 31, 3, 2, 2, 2, 186, 187, 5, 60, 31, 2, 187, 188, 7, 21, 2, 2, 188, 189, 5, 32, 17, 2, 189, 214, 3, 2, 2, 2, 190, 191, 5, 60, 31, 2, 191, 192, 7, 22, 2, 2, 192, 193, 5, 32, 17, 2, 193, 214, 3, 2, 2, 2, 194, 195, 5, 60, 31, 2, 195, 196, 7, 23, 2, 2, 196, 197, 5, 32, 17, 2, 197, 214, 3, 2, 2, 2, 198, 199, 5, 60, 31, 2, 199, 200, 7, 24, 2, 2, 200, 201, 5, 32, 17, 2, 201, 214, 3, 2, 2, 2, 202, 203, 5, 60, 31, 2, 203, 204, 7, 25, 2, 2, 204, 205, 5, 32, 17, 2, 205, 214, 3, 2, 2, 2, 206, 207, 5, 60, 31, 2, 207, 208, 7, 26, 2, 2, 208, 214, 3, 2, 2, 2, 209, 210, 5, 60, 31, 2, 210, 211, 7, 27, 2, 2, 211, 214, 3, 2, 2, 2, 212, 214, 5, 34, 18, 2, 213, 186, 3, 2, 2, 2, 213, 190, 3, 2, 2, 2, 213, 194, 3, 2, 2, 2, 213, 198, 3, 2, 2, 2, 213, 202, 3, 2, 2, 2, 213, 206, 3, 2, 2, 2, 213, 209, 3, 2, 2, 2, 213, 212, 3, 2, 2, 2, 214, 33, 3, 2, 2, 2, 215, 216, 5, 36, 19, 2, 216, 35, 3, 2, 2, 2, 217, 218, 5, 38, 20, 2, 218, 219, 7, 28, 2, 2, 219, 221, 3, 2, 2, 2, 220, 217, 3, 2, 2, 2, 221, 224, 3, 2, 2, 2, 222, 220, 3, 2, 2, 2, 222, 223, 3, 2, 2, 2, 223, 225, 3, 2, 2, 2, 224, 222, 3, 2, 2, 2, 225, 226, 5, 38, 20, 2, 226, 37, 3, 2, 2, 2, 227, 228, 5, 40, 21, 2, 228, 229, 7, 29, 2, 2, 229, 231, 3, 2, 2, 2, 230, 227, 3, 2, 2, 2, 231, 234, 3, 2, 2, 2, 232, 230, 3, 2, 2, 2, 232, 233, 3, 2, 2, 2, 233, 235, 3, 2, 2, 2, 234, 232, 3, 2, 2, 2, 235, 236, 5, 40, 21, 2, 236, 39, 3, 2, 2, 2, 237, 239, 7, 48, 2, 2, 238, 237, 3, 2, 2, 2, 239, 242, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 240, 241, 3, 2, 2, 2, 241, 243, 3, 2, 2, 2, 242, 240, 3, 2, 2, 2, 243, 244, 5, 42, 22, 2, 244, 41, 3, 2, 2, 2, 245, 246, 5, 46, 24, 2, 246, 247, 5, 44, 23, 2, 247, 249, 3, 2, 2, 2, 248, 245, 3, 2, 2, 2, 249, 252, 3, 2, 2, 2, 250, 248, 3, 2, 2, 2, 250, 251, 3, 2, 2, 2, 251, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 254, 5, 46, 24, 2, 254, 43, 3, 2, 2, 2, 255, 256, 9, 3, 2, 2, 256, 45, 3, 2, 2, 2, 257, 258, 5, 50, 26, 2, 258, 259, 5, 48, 25, 2, 259, 261, 3, 2, 2, 2, 260, 257, 3, 2, 2, 2, 261, 264, 3, 2, 2, 2, 262, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 265, 3, 2, 2, 2, 264, 262, 3, 2, 2, 2, 265, 266, 5, 50, 26, 2, 266, 47, 3, 2, 2, 2, 267, 268, 9, 4, 2, 2, 268, 49, 3, 2, 2, 2, 269, 270, 5, 54, 28, 2, 270, 271, 5, 52, 27, 2, 271, 273, 3, 2, 2, 2, 272, 269, 3, 2, 2, 2, 273, 276, 3, 2, 2, 2, 274, 272, 3, 2, 2, 2, 274, 275, 3, 2, 2, 2, 275, 277, 3, 2, 2, 2, 276, 274, 3, 2, 2, 2, 277, 278, 5, 54, 28, 2, 278, 51, 3, 2, 2, 2, 279, 280, 9, 5, 2, 2, 280, 53, 3, 2, 2, 2, 281, 283, 5, 56, 29, 2, 282, 281, 3, 2, 2, 2, 283, 286, 3, 2, 2, 2, 284, 282, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 287, 3, 2, 2, 2, 286, 284, 3, 2, 2, 2, 287, 288, 5, 58, 30, 2, 288, 55, 3, 2, 2, 2, 289, 290, 9, 6, 2, 2, 290, 57, 3, 2, 2, 2, 291, 294, 5, 62, 32, 2, 292, 294, 5, 60, 31, 2, 293, 291, 3, 2, 2, 2, 293, 292, 3, 2, 2, 2, 294, 59, 3, 2, 2, 2, 295, 302, 7, 44, 2, 2, 296, 297, 7, 44, 2, 2, 297, 298, 7, 5, 2, 2, 298, 299, 5, 32, 17, 2, 299, 300, 7, 6, 2, 2, 300, 302, 3, 2, 2, 2, 301, 295, 3, 2, 2, 2, 301, 296, 3, 2, 2, 2, 302, 61, 3, 2, 2, 2, 303, 304, 7, 8, 2, 2, 304, 305, 5, 32, 17, 2, 305, 306, 7, 9, 2, 2, 306, 310, 3, 2, 2, 2, 307, 310, 5, 64, 33, 2, 308, 310, 5, 66, 34, 2, 309, 303, 3, 2, 2, 2, 309, 307, 3, 2, 2, 2, 309, 308, 3, 2, 2, 2, 310, 63, 3, 2, 2, 2, 311, 312, 7, 44, 2, 2, 312, 318, 7, 8, 2, 2, 313, 314, 5, 32, 17, 2, 314, 315, 7, 3, 2, 2, 315, 317, 3, 2, 2, 2, 316, 313, 3, 2, 2, 2, 317, 320, 3, 2, 2, 2, 318, 316, 3, 2, 2, 2, 318, 319, 3, 2, 2, 2, 319, 322, 3, 2, 2, 2, 320, 318, 3, 2, 2, 2, 321, 323, 5, 32, 17, 2, 322, 321, 3, 2, 2, 2, 322, 323, 3, 2, 2, 2, 323, 324, 3, 2, 2, 2, 324, 325, 7, 9, 2, 2, 325, 65, 3, 2, 2, 2, 326, 327, 9, 7, 2, 2, 327, 67, 3, 2, 2, 2, 29, 71, 75, 83, 93, 97, 102, 108, 122, 130, 136, 142, 151, 167, 181, 213, 222, 232, 240, 250, 262, 274, 284, 293, 301, 309, 318, 322]
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/Cminus.tokens b/Homework/cs5300/compiler/p5-compiler/parser/Cminus.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/Cminus.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseListener.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseListener.java new file mode 100644 index 0000000..65a3493 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseListener.java @@ -0,0 +1,435 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link CminusListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class CminusBaseListener implements CminusListener { + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterConstant(CminusParser.ConstantContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitConstant(CminusParser.ConstantContext ctx) { } + + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitErrorNode(ErrorNode node) { } +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseVisitor.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseVisitor.java new file mode 100644 index 0000000..150f316 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusBaseVisitor.java @@ -0,0 +1,245 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + +/** + * This class provides an empty implementation of {@link CminusVisitor}, + * which can be extended to create a visitor which only needs to handle a subset + * of the available methods. + * + * @param <T> The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public class CminusBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements CminusVisitor<T> { + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitProgram(CminusParser.ProgramContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitDeclaration(CminusParser.DeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitVarDeclId(CminusParser.VarDeclIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitParam(CminusParser.ParamContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitParamId(CminusParser.ParamIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitStatement(CminusParser.StatementContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitIfStmt(CminusParser.IfStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitWhileStmt(CminusParser.WhileStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitReturnStmt(CminusParser.ReturnStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitBreakStmt(CminusParser.BreakStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitExpression(CminusParser.ExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitOrExpression(CminusParser.OrExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitAndExpression(CminusParser.AndExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitRelExpression(CminusParser.RelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitRelop(CminusParser.RelopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSumExpression(CminusParser.SumExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSumop(CminusParser.SumopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitTermExpression(CminusParser.TermExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitMulop(CminusParser.MulopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryop(CminusParser.UnaryopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitFactor(CminusParser.FactorContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitMutable(CminusParser.MutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitImmutable(CminusParser.ImmutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitCall(CminusParser.CallContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitConstant(CminusParser.ConstantContext ctx) { return visitChildren(ctx); } +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.interp b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.interp new file mode 100644 index 0000000..a9dc30a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.interp @@ -0,0 +1,163 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +T__7 +T__8 +T__9 +T__10 +T__11 +T__12 +T__13 +T__14 +T__15 +T__16 +T__17 +T__18 +T__19 +T__20 +T__21 +T__22 +T__23 +T__24 +T__25 +T__26 +T__27 +T__28 +T__29 +T__30 +T__31 +T__32 +T__33 +T__34 +T__35 +T__36 +T__37 +T__38 +T__39 +T__40 +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 50, 306, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 4, 35, 9, 35, 4, 36, 9, 36, 4, 37, 9, 37, 4, 38, 9, 38, 4, 39, 9, 39, 4, 40, 9, 40, 4, 41, 9, 41, 4, 42, 9, 42, 4, 43, 9, 43, 4, 44, 9, 44, 4, 45, 9, 45, 4, 46, 9, 46, 4, 47, 9, 47, 4, 48, 9, 48, 4, 49, 9, 49, 4, 50, 9, 50, 4, 51, 9, 51, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 16, 3, 16, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 27, 3, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 29, 3, 30, 3, 30, 3, 31, 3, 31, 3, 32, 3, 32, 3, 32, 3, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 3, 35, 3, 35, 3, 36, 3, 36, 3, 37, 3, 37, 3, 38, 3, 38, 3, 39, 3, 39, 3, 40, 3, 40, 3, 41, 3, 41, 3, 41, 3, 41, 3, 41, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 43, 3, 43, 3, 43, 7, 43, 237, 10, 43, 12, 43, 14, 43, 240, 11, 43, 3, 44, 6, 44, 243, 10, 44, 13, 44, 14, 44, 244, 3, 45, 3, 45, 3, 45, 3, 45, 7, 45, 251, 10, 45, 12, 45, 14, 45, 254, 11, 45, 3, 45, 3, 45, 3, 46, 3, 46, 3, 46, 3, 46, 5, 46, 262, 10, 46, 3, 46, 3, 46, 3, 47, 3, 47, 3, 48, 6, 48, 269, 10, 48, 13, 48, 14, 48, 270, 3, 48, 3, 48, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 279, 10, 49, 12, 49, 14, 49, 282, 11, 49, 3, 49, 5, 49, 285, 10, 49, 3, 49, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 292, 10, 49, 12, 49, 14, 49, 295, 11, 49, 3, 49, 3, 49, 5, 49, 299, 10, 49, 3, 49, 3, 49, 3, 50, 3, 50, 3, 51, 3, 51, 4, 252, 293, 2, 52, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35, 19, 37, 20, 39, 21, 41, 22, 43, 23, 45, 24, 47, 25, 49, 26, 51, 27, 53, 28, 55, 29, 57, 30, 59, 31, 61, 32, 63, 33, 65, 34, 67, 35, 69, 36, 71, 37, 73, 38, 75, 39, 77, 40, 79, 41, 81, 42, 83, 43, 85, 44, 87, 45, 89, 46, 91, 47, 93, 48, 95, 49, 97, 50, 99, 2, 101, 2, 3, 2, 6, 3, 2, 36, 36, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 12, 12, 15, 15, 4, 2, 67, 92, 99, 124, 2, 314, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3, 2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 2, 43, 3, 2, 2, 2, 2, 45, 3, 2, 2, 2, 2, 47, 3, 2, 2, 2, 2, 49, 3, 2, 2, 2, 2, 51, 3, 2, 2, 2, 2, 53, 3, 2, 2, 2, 2, 55, 3, 2, 2, 2, 2, 57, 3, 2, 2, 2, 2, 59, 3, 2, 2, 2, 2, 61, 3, 2, 2, 2, 2, 63, 3, 2, 2, 2, 2, 65, 3, 2, 2, 2, 2, 67, 3, 2, 2, 2, 2, 69, 3, 2, 2, 2, 2, 71, 3, 2, 2, 2, 2, 73, 3, 2, 2, 2, 2, 75, 3, 2, 2, 2, 2, 77, 3, 2, 2, 2, 2, 79, 3, 2, 2, 2, 2, 81, 3, 2, 2, 2, 2, 83, 3, 2, 2, 2, 2, 85, 3, 2, 2, 2, 2, 87, 3, 2, 2, 2, 2, 89, 3, 2, 2, 2, 2, 91, 3, 2, 2, 2, 2, 93, 3, 2, 2, 2, 2, 95, 3, 2, 2, 2, 2, 97, 3, 2, 2, 2, 3, 103, 3, 2, 2, 2, 5, 105, 3, 2, 2, 2, 7, 107, 3, 2, 2, 2, 9, 109, 3, 2, 2, 2, 11, 111, 3, 2, 2, 2, 13, 116, 3, 2, 2, 2, 15, 118, 3, 2, 2, 2, 17, 120, 3, 2, 2, 2, 19, 124, 3, 2, 2, 2, 21, 129, 3, 2, 2, 2, 23, 134, 3, 2, 2, 2, 25, 137, 3, 2, 2, 2, 27, 139, 3, 2, 2, 2, 29, 141, 3, 2, 2, 2, 31, 144, 3, 2, 2, 2, 33, 149, 3, 2, 2, 2, 35, 155, 3, 2, 2, 2, 37, 162, 3, 2, 2, 2, 39, 168, 3, 2, 2, 2, 41, 170, 3, 2, 2, 2, 43, 173, 3, 2, 2, 2, 45, 176, 3, 2, 2, 2, 47, 179, 3, 2, 2, 2, 49, 182, 3, 2, 2, 2, 51, 185, 3, 2, 2, 2, 53, 188, 3, 2, 2, 2, 55, 191, 3, 2, 2, 2, 57, 194, 3, 2, 2, 2, 59, 197, 3, 2, 2, 2, 61, 199, 3, 2, 2, 2, 63, 201, 3, 2, 2, 2, 65, 204, 3, 2, 2, 2, 67, 207, 3, 2, 2, 2, 69, 210, 3, 2, 2, 2, 71, 212, 3, 2, 2, 2, 73, 214, 3, 2, 2, 2, 75, 216, 3, 2, 2, 2, 77, 218, 3, 2, 2, 2, 79, 220, 3, 2, 2, 2, 81, 222, 3, 2, 2, 2, 83, 227, 3, 2, 2, 2, 85, 233, 3, 2, 2, 2, 87, 242, 3, 2, 2, 2, 89, 246, 3, 2, 2, 2, 91, 257, 3, 2, 2, 2, 93, 265, 3, 2, 2, 2, 95, 268, 3, 2, 2, 2, 97, 298, 3, 2, 2, 2, 99, 302, 3, 2, 2, 2, 101, 304, 3, 2, 2, 2, 103, 104, 7, 46, 2, 2, 104, 4, 3, 2, 2, 2, 105, 106, 7, 61, 2, 2, 106, 6, 3, 2, 2, 2, 107, 108, 7, 93, 2, 2, 108, 8, 3, 2, 2, 2, 109, 110, 7, 95, 2, 2, 110, 10, 3, 2, 2, 2, 111, 112, 7, 120, 2, 2, 112, 113, 7, 113, 2, 2, 113, 114, 7, 107, 2, 2, 114, 115, 7, 102, 2, 2, 115, 12, 3, 2, 2, 2, 116, 117, 7, 42, 2, 2, 117, 14, 3, 2, 2, 2, 118, 119, 7, 43, 2, 2, 119, 16, 3, 2, 2, 2, 120, 121, 7, 107, 2, 2, 121, 122, 7, 112, 2, 2, 122, 123, 7, 118, 2, 2, 123, 18, 3, 2, 2, 2, 124, 125, 7, 100, 2, 2, 125, 126, 7, 113, 2, 2, 126, 127, 7, 113, 2, 2, 127, 128, 7, 110, 2, 2, 128, 20, 3, 2, 2, 2, 129, 130, 7, 101, 2, 2, 130, 131, 7, 106, 2, 2, 131, 132, 7, 99, 2, 2, 132, 133, 7, 116, 2, 2, 133, 22, 3, 2, 2, 2, 134, 135, 7, 93, 2, 2, 135, 136, 7, 95, 2, 2, 136, 24, 3, 2, 2, 2, 137, 138, 7, 125, 2, 2, 138, 26, 3, 2, 2, 2, 139, 140, 7, 127, 2, 2, 140, 28, 3, 2, 2, 2, 141, 142, 7, 107, 2, 2, 142, 143, 7, 104, 2, 2, 143, 30, 3, 2, 2, 2, 144, 145, 7, 103, 2, 2, 145, 146, 7, 110, 2, 2, 146, 147, 7, 117, 2, 2, 147, 148, 7, 103, 2, 2, 148, 32, 3, 2, 2, 2, 149, 150, 7, 121, 2, 2, 150, 151, 7, 106, 2, 2, 151, 152, 7, 107, 2, 2, 152, 153, 7, 110, 2, 2, 153, 154, 7, 103, 2, 2, 154, 34, 3, 2, 2, 2, 155, 156, 7, 116, 2, 2, 156, 157, 7, 103, 2, 2, 157, 158, 7, 118, 2, 2, 158, 159, 7, 119, 2, 2, 159, 160, 7, 116, 2, 2, 160, 161, 7, 112, 2, 2, 161, 36, 3, 2, 2, 2, 162, 163, 7, 100, 2, 2, 163, 164, 7, 116, 2, 2, 164, 165, 7, 103, 2, 2, 165, 166, 7, 99, 2, 2, 166, 167, 7, 109, 2, 2, 167, 38, 3, 2, 2, 2, 168, 169, 7, 63, 2, 2, 169, 40, 3, 2, 2, 2, 170, 171, 7, 45, 2, 2, 171, 172, 7, 63, 2, 2, 172, 42, 3, 2, 2, 2, 173, 174, 7, 47, 2, 2, 174, 175, 7, 63, 2, 2, 175, 44, 3, 2, 2, 2, 176, 177, 7, 44, 2, 2, 177, 178, 7, 63, 2, 2, 178, 46, 3, 2, 2, 2, 179, 180, 7, 49, 2, 2, 180, 181, 7, 63, 2, 2, 181, 48, 3, 2, 2, 2, 182, 183, 7, 45, 2, 2, 183, 184, 7, 45, 2, 2, 184, 50, 3, 2, 2, 2, 185, 186, 7, 47, 2, 2, 186, 187, 7, 47, 2, 2, 187, 52, 3, 2, 2, 2, 188, 189, 7, 126, 2, 2, 189, 190, 7, 126, 2, 2, 190, 54, 3, 2, 2, 2, 191, 192, 7, 40, 2, 2, 192, 193, 7, 40, 2, 2, 193, 56, 3, 2, 2, 2, 194, 195, 7, 62, 2, 2, 195, 196, 7, 63, 2, 2, 196, 58, 3, 2, 2, 2, 197, 198, 7, 62, 2, 2, 198, 60, 3, 2, 2, 2, 199, 200, 7, 64, 2, 2, 200, 62, 3, 2, 2, 2, 201, 202, 7, 64, 2, 2, 202, 203, 7, 63, 2, 2, 203, 64, 3, 2, 2, 2, 204, 205, 7, 63, 2, 2, 205, 206, 7, 63, 2, 2, 206, 66, 3, 2, 2, 2, 207, 208, 7, 35, 2, 2, 208, 209, 7, 63, 2, 2, 209, 68, 3, 2, 2, 2, 210, 211, 7, 45, 2, 2, 211, 70, 3, 2, 2, 2, 212, 213, 7, 47, 2, 2, 213, 72, 3, 2, 2, 2, 214, 215, 7, 44, 2, 2, 215, 74, 3, 2, 2, 2, 216, 217, 7, 49, 2, 2, 217, 76, 3, 2, 2, 2, 218, 219, 7, 39, 2, 2, 219, 78, 3, 2, 2, 2, 220, 221, 7, 65, 2, 2, 221, 80, 3, 2, 2, 2, 222, 223, 7, 118, 2, 2, 223, 224, 7, 116, 2, 2, 224, 225, 7, 119, 2, 2, 225, 226, 7, 103, 2, 2, 226, 82, 3, 2, 2, 2, 227, 228, 7, 104, 2, 2, 228, 229, 7, 99, 2, 2, 229, 230, 7, 110, 2, 2, 230, 231, 7, 117, 2, 2, 231, 232, 7, 103, 2, 2, 232, 84, 3, 2, 2, 2, 233, 238, 5, 99, 50, 2, 234, 237, 5, 99, 50, 2, 235, 237, 5, 101, 51, 2, 236, 234, 3, 2, 2, 2, 236, 235, 3, 2, 2, 2, 237, 240, 3, 2, 2, 2, 238, 236, 3, 2, 2, 2, 238, 239, 3, 2, 2, 2, 239, 86, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 241, 243, 5, 101, 51, 2, 242, 241, 3, 2, 2, 2, 243, 244, 3, 2, 2, 2, 244, 242, 3, 2, 2, 2, 244, 245, 3, 2, 2, 2, 245, 88, 3, 2, 2, 2, 246, 252, 7, 36, 2, 2, 247, 248, 7, 94, 2, 2, 248, 251, 7, 36, 2, 2, 249, 251, 10, 2, 2, 2, 250, 247, 3, 2, 2, 2, 250, 249, 3, 2, 2, 2, 251, 254, 3, 2, 2, 2, 252, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 255, 3, 2, 2, 2, 254, 252, 3, 2, 2, 2, 255, 256, 7, 36, 2, 2, 256, 90, 3, 2, 2, 2, 257, 261, 7, 36, 2, 2, 258, 259, 7, 94, 2, 2, 259, 262, 7, 36, 2, 2, 260, 262, 10, 2, 2, 2, 261, 258, 3, 2, 2, 2, 261, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 264, 7, 36, 2, 2, 264, 92, 3, 2, 2, 2, 265, 266, 7, 35, 2, 2, 266, 94, 3, 2, 2, 2, 267, 269, 9, 3, 2, 2, 268, 267, 3, 2, 2, 2, 269, 270, 3, 2, 2, 2, 270, 268, 3, 2, 2, 2, 270, 271, 3, 2, 2, 2, 271, 272, 3, 2, 2, 2, 272, 273, 8, 48, 2, 2, 273, 96, 3, 2, 2, 2, 274, 275, 7, 49, 2, 2, 275, 276, 7, 49, 2, 2, 276, 280, 3, 2, 2, 2, 277, 279, 10, 4, 2, 2, 278, 277, 3, 2, 2, 2, 279, 282, 3, 2, 2, 2, 280, 278, 3, 2, 2, 2, 280, 281, 3, 2, 2, 2, 281, 284, 3, 2, 2, 2, 282, 280, 3, 2, 2, 2, 283, 285, 7, 15, 2, 2, 284, 283, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 286, 3, 2, 2, 2, 286, 299, 7, 12, 2, 2, 287, 288, 7, 49, 2, 2, 288, 289, 7, 44, 2, 2, 289, 293, 3, 2, 2, 2, 290, 292, 11, 2, 2, 2, 291, 290, 3, 2, 2, 2, 292, 295, 3, 2, 2, 2, 293, 294, 3, 2, 2, 2, 293, 291, 3, 2, 2, 2, 294, 296, 3, 2, 2, 2, 295, 293, 3, 2, 2, 2, 296, 297, 7, 44, 2, 2, 297, 299, 7, 49, 2, 2, 298, 274, 3, 2, 2, 2, 298, 287, 3, 2, 2, 2, 299, 300, 3, 2, 2, 2, 300, 301, 8, 49, 2, 2, 301, 98, 3, 2, 2, 2, 302, 303, 9, 5, 2, 2, 303, 100, 3, 2, 2, 2, 304, 305, 4, 50, 59, 2, 305, 102, 3, 2, 2, 2, 14, 2, 236, 238, 244, 250, 252, 261, 270, 280, 284, 293, 298, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.java new file mode 100644 index 0000000..8224ed8 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.java @@ -0,0 +1,232 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8", + "T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16", + "T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "T__23", "T__24", + "T__25", "T__26", "T__27", "T__28", "T__29", "T__30", "T__31", "T__32", + "T__33", "T__34", "T__35", "T__36", "T__37", "T__38", "T__39", "T__40", + "ID", "NUMCONST", "STRINGCONST", "CHARCONST", "BANG", "WS", "COMMENT", + "LETTER", "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public CminusLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\62\u0132\b\1\4\2"+ + "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+ + "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\3\2"+ + "\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3"+ + "\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3"+ + "\r\3\r\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23"+ + "\3\23\3\23\3\24\3\24\3\25\3\25\3\25\3\26\3\26\3\26\3\27\3\27\3\27\3\30"+ + "\3\30\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\33\3\34\3\34\3\34"+ + "\3\35\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3!\3\"\3\"\3\"\3#\3"+ + "#\3$\3$\3%\3%\3&\3&\3\'\3\'\3(\3(\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3+"+ + "\3+\3+\7+\u00ed\n+\f+\16+\u00f0\13+\3,\6,\u00f3\n,\r,\16,\u00f4\3-\3-"+ + "\3-\3-\7-\u00fb\n-\f-\16-\u00fe\13-\3-\3-\3.\3.\3.\3.\5.\u0106\n.\3.\3"+ + ".\3/\3/\3\60\6\60\u010d\n\60\r\60\16\60\u010e\3\60\3\60\3\61\3\61\3\61"+ + "\3\61\7\61\u0117\n\61\f\61\16\61\u011a\13\61\3\61\5\61\u011d\n\61\3\61"+ + "\3\61\3\61\3\61\3\61\7\61\u0124\n\61\f\61\16\61\u0127\13\61\3\61\3\61"+ + "\5\61\u012b\n\61\3\61\3\61\3\62\3\62\3\63\3\63\4\u00fc\u0125\2\64\3\3"+ + "\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21"+ + "!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!"+ + "A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\2e\2\3\2\6\3\2$$\5\2\13\f"+ + "\16\17\"\"\4\2\f\f\17\17\4\2C\\c|\2\u013a\2\3\3\2\2\2\2\5\3\2\2\2\2\7"+ + "\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2"+ + "\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2"+ + "\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2"+ + "\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2"+ + "\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2"+ + "\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M"+ + "\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y\3\2"+ + "\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\3g\3\2\2\2\5i\3\2\2\2"+ + "\7k\3\2\2\2\tm\3\2\2\2\13o\3\2\2\2\rt\3\2\2\2\17v\3\2\2\2\21x\3\2\2\2"+ + "\23|\3\2\2\2\25\u0081\3\2\2\2\27\u0086\3\2\2\2\31\u0089\3\2\2\2\33\u008b"+ + "\3\2\2\2\35\u008d\3\2\2\2\37\u0090\3\2\2\2!\u0095\3\2\2\2#\u009b\3\2\2"+ + "\2%\u00a2\3\2\2\2\'\u00a8\3\2\2\2)\u00aa\3\2\2\2+\u00ad\3\2\2\2-\u00b0"+ + "\3\2\2\2/\u00b3\3\2\2\2\61\u00b6\3\2\2\2\63\u00b9\3\2\2\2\65\u00bc\3\2"+ + "\2\2\67\u00bf\3\2\2\29\u00c2\3\2\2\2;\u00c5\3\2\2\2=\u00c7\3\2\2\2?\u00c9"+ + "\3\2\2\2A\u00cc\3\2\2\2C\u00cf\3\2\2\2E\u00d2\3\2\2\2G\u00d4\3\2\2\2I"+ + "\u00d6\3\2\2\2K\u00d8\3\2\2\2M\u00da\3\2\2\2O\u00dc\3\2\2\2Q\u00de\3\2"+ + "\2\2S\u00e3\3\2\2\2U\u00e9\3\2\2\2W\u00f2\3\2\2\2Y\u00f6\3\2\2\2[\u0101"+ + "\3\2\2\2]\u0109\3\2\2\2_\u010c\3\2\2\2a\u012a\3\2\2\2c\u012e\3\2\2\2e"+ + "\u0130\3\2\2\2gh\7.\2\2h\4\3\2\2\2ij\7=\2\2j\6\3\2\2\2kl\7]\2\2l\b\3\2"+ + "\2\2mn\7_\2\2n\n\3\2\2\2op\7x\2\2pq\7q\2\2qr\7k\2\2rs\7f\2\2s\f\3\2\2"+ + "\2tu\7*\2\2u\16\3\2\2\2vw\7+\2\2w\20\3\2\2\2xy\7k\2\2yz\7p\2\2z{\7v\2"+ + "\2{\22\3\2\2\2|}\7d\2\2}~\7q\2\2~\177\7q\2\2\177\u0080\7n\2\2\u0080\24"+ + "\3\2\2\2\u0081\u0082\7e\2\2\u0082\u0083\7j\2\2\u0083\u0084\7c\2\2\u0084"+ + "\u0085\7t\2\2\u0085\26\3\2\2\2\u0086\u0087\7]\2\2\u0087\u0088\7_\2\2\u0088"+ + "\30\3\2\2\2\u0089\u008a\7}\2\2\u008a\32\3\2\2\2\u008b\u008c\7\177\2\2"+ + "\u008c\34\3\2\2\2\u008d\u008e\7k\2\2\u008e\u008f\7h\2\2\u008f\36\3\2\2"+ + "\2\u0090\u0091\7g\2\2\u0091\u0092\7n\2\2\u0092\u0093\7u\2\2\u0093\u0094"+ + "\7g\2\2\u0094 \3\2\2\2\u0095\u0096\7y\2\2\u0096\u0097\7j\2\2\u0097\u0098"+ + "\7k\2\2\u0098\u0099\7n\2\2\u0099\u009a\7g\2\2\u009a\"\3\2\2\2\u009b\u009c"+ + "\7t\2\2\u009c\u009d\7g\2\2\u009d\u009e\7v\2\2\u009e\u009f\7w\2\2\u009f"+ + "\u00a0\7t\2\2\u00a0\u00a1\7p\2\2\u00a1$\3\2\2\2\u00a2\u00a3\7d\2\2\u00a3"+ + "\u00a4\7t\2\2\u00a4\u00a5\7g\2\2\u00a5\u00a6\7c\2\2\u00a6\u00a7\7m\2\2"+ + "\u00a7&\3\2\2\2\u00a8\u00a9\7?\2\2\u00a9(\3\2\2\2\u00aa\u00ab\7-\2\2\u00ab"+ + "\u00ac\7?\2\2\u00ac*\3\2\2\2\u00ad\u00ae\7/\2\2\u00ae\u00af\7?\2\2\u00af"+ + ",\3\2\2\2\u00b0\u00b1\7,\2\2\u00b1\u00b2\7?\2\2\u00b2.\3\2\2\2\u00b3\u00b4"+ + "\7\61\2\2\u00b4\u00b5\7?\2\2\u00b5\60\3\2\2\2\u00b6\u00b7\7-\2\2\u00b7"+ + "\u00b8\7-\2\2\u00b8\62\3\2\2\2\u00b9\u00ba\7/\2\2\u00ba\u00bb\7/\2\2\u00bb"+ + "\64\3\2\2\2\u00bc\u00bd\7~\2\2\u00bd\u00be\7~\2\2\u00be\66\3\2\2\2\u00bf"+ + "\u00c0\7(\2\2\u00c0\u00c1\7(\2\2\u00c18\3\2\2\2\u00c2\u00c3\7>\2\2\u00c3"+ + "\u00c4\7?\2\2\u00c4:\3\2\2\2\u00c5\u00c6\7>\2\2\u00c6<\3\2\2\2\u00c7\u00c8"+ + "\7@\2\2\u00c8>\3\2\2\2\u00c9\u00ca\7@\2\2\u00ca\u00cb\7?\2\2\u00cb@\3"+ + "\2\2\2\u00cc\u00cd\7?\2\2\u00cd\u00ce\7?\2\2\u00ceB\3\2\2\2\u00cf\u00d0"+ + "\7#\2\2\u00d0\u00d1\7?\2\2\u00d1D\3\2\2\2\u00d2\u00d3\7-\2\2\u00d3F\3"+ + "\2\2\2\u00d4\u00d5\7/\2\2\u00d5H\3\2\2\2\u00d6\u00d7\7,\2\2\u00d7J\3\2"+ + "\2\2\u00d8\u00d9\7\61\2\2\u00d9L\3\2\2\2\u00da\u00db\7\'\2\2\u00dbN\3"+ + "\2\2\2\u00dc\u00dd\7A\2\2\u00ddP\3\2\2\2\u00de\u00df\7v\2\2\u00df\u00e0"+ + "\7t\2\2\u00e0\u00e1\7w\2\2\u00e1\u00e2\7g\2\2\u00e2R\3\2\2\2\u00e3\u00e4"+ + "\7h\2\2\u00e4\u00e5\7c\2\2\u00e5\u00e6\7n\2\2\u00e6\u00e7\7u\2\2\u00e7"+ + "\u00e8\7g\2\2\u00e8T\3\2\2\2\u00e9\u00ee\5c\62\2\u00ea\u00ed\5c\62\2\u00eb"+ + "\u00ed\5e\63\2\u00ec\u00ea\3\2\2\2\u00ec\u00eb\3\2\2\2\u00ed\u00f0\3\2"+ + "\2\2\u00ee\u00ec\3\2\2\2\u00ee\u00ef\3\2\2\2\u00efV\3\2\2\2\u00f0\u00ee"+ + "\3\2\2\2\u00f1\u00f3\5e\63\2\u00f2\u00f1\3\2\2\2\u00f3\u00f4\3\2\2\2\u00f4"+ + "\u00f2\3\2\2\2\u00f4\u00f5\3\2\2\2\u00f5X\3\2\2\2\u00f6\u00fc\7$\2\2\u00f7"+ + "\u00f8\7^\2\2\u00f8\u00fb\7$\2\2\u00f9\u00fb\n\2\2\2\u00fa\u00f7\3\2\2"+ + "\2\u00fa\u00f9\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc\u00fd\3\2\2\2\u00fc\u00fa"+ + "\3\2\2\2\u00fd\u00ff\3\2\2\2\u00fe\u00fc\3\2\2\2\u00ff\u0100\7$\2\2\u0100"+ + "Z\3\2\2\2\u0101\u0105\7$\2\2\u0102\u0103\7^\2\2\u0103\u0106\7$\2\2\u0104"+ + "\u0106\n\2\2\2\u0105\u0102\3\2\2\2\u0105\u0104\3\2\2\2\u0106\u0107\3\2"+ + "\2\2\u0107\u0108\7$\2\2\u0108\\\3\2\2\2\u0109\u010a\7#\2\2\u010a^\3\2"+ + "\2\2\u010b\u010d\t\3\2\2\u010c\u010b\3\2\2\2\u010d\u010e\3\2\2\2\u010e"+ + "\u010c\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\3\2\2\2\u0110\u0111\b\60"+ + "\2\2\u0111`\3\2\2\2\u0112\u0113\7\61\2\2\u0113\u0114\7\61\2\2\u0114\u0118"+ + "\3\2\2\2\u0115\u0117\n\4\2\2\u0116\u0115\3\2\2\2\u0117\u011a\3\2\2\2\u0118"+ + "\u0116\3\2\2\2\u0118\u0119\3\2\2\2\u0119\u011c\3\2\2\2\u011a\u0118\3\2"+ + "\2\2\u011b\u011d\7\17\2\2\u011c\u011b\3\2\2\2\u011c\u011d\3\2\2\2\u011d"+ + "\u011e\3\2\2\2\u011e\u012b\7\f\2\2\u011f\u0120\7\61\2\2\u0120\u0121\7"+ + ",\2\2\u0121\u0125\3\2\2\2\u0122\u0124\13\2\2\2\u0123\u0122\3\2\2\2\u0124"+ + "\u0127\3\2\2\2\u0125\u0126\3\2\2\2\u0125\u0123\3\2\2\2\u0126\u0128\3\2"+ + "\2\2\u0127\u0125\3\2\2\2\u0128\u0129\7,\2\2\u0129\u012b\7\61\2\2\u012a"+ + "\u0112\3\2\2\2\u012a\u011f\3\2\2\2\u012b\u012c\3\2\2\2\u012c\u012d\b\61"+ + "\2\2\u012db\3\2\2\2\u012e\u012f\t\5\2\2\u012fd\3\2\2\2\u0130\u0131\4\62"+ + ";\2\u0131f\3\2\2\2\16\2\u00ec\u00ee\u00f4\u00fa\u00fc\u0105\u010e\u0118"+ + "\u011c\u0125\u012a\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.tokens b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusLexer.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusListener.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusListener.java new file mode 100644 index 0000000..bd141e3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusListener.java @@ -0,0 +1,340 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link CminusParser}. + */ +public interface CminusListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void enterProgram(CminusParser.ProgramContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void exitProgram(CminusParser.ProgramContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void enterDeclaration(CminusParser.DeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void exitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void enterVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void exitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void enterVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void exitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void enterFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void exitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void enterParam(CminusParser.ParamContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void exitParam(CminusParser.ParamContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void enterParamId(CminusParser.ParamIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void exitParamId(CminusParser.ParamIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void enterStatement(CminusParser.StatementContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void exitStatement(CminusParser.StatementContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void enterCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void exitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void enterIfStmt(CminusParser.IfStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void exitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void enterWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void exitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void enterReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void exitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void enterBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void exitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(CminusParser.ExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(CminusParser.ExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void enterOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void exitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void enterAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void exitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void enterRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void exitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void enterRelop(CminusParser.RelopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void exitRelop(CminusParser.RelopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void enterSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void exitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void enterSumop(CminusParser.SumopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void exitSumop(CminusParser.SumopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void enterTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void exitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void enterMulop(CminusParser.MulopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void exitMulop(CminusParser.MulopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void enterUnaryop(CminusParser.UnaryopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void exitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void enterFactor(CminusParser.FactorContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void exitFactor(CminusParser.FactorContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void enterMutable(CminusParser.MutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void exitMutable(CminusParser.MutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void enterImmutable(CminusParser.ImmutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void exitImmutable(CminusParser.ImmutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void enterCall(CminusParser.CallContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void exitCall(CminusParser.CallContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void enterConstant(CminusParser.ConstantContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void exitConstant(CminusParser.ConstantContext ctx); +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusParser.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusParser.java new file mode 100644 index 0000000..6749b21 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusParser.java @@ -0,0 +1,2454 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusParser extends Parser { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static final int + RULE_program = 0, RULE_declaration = 1, RULE_varDeclaration = 2, RULE_varDeclId = 3, + RULE_funDeclaration = 4, RULE_typeSpecifier = 5, RULE_param = 6, RULE_paramId = 7, + RULE_statement = 8, RULE_compoundStmt = 9, RULE_expressionStmt = 10, RULE_ifStmt = 11, + RULE_whileStmt = 12, RULE_returnStmt = 13, RULE_breakStmt = 14, RULE_expression = 15, + RULE_simpleExpression = 16, RULE_orExpression = 17, RULE_andExpression = 18, + RULE_unaryRelExpression = 19, RULE_relExpression = 20, RULE_relop = 21, + RULE_sumExpression = 22, RULE_sumop = 23, RULE_termExpression = 24, RULE_mulop = 25, + RULE_unaryExpression = 26, RULE_unaryop = 27, RULE_factor = 28, RULE_mutable = 29, + RULE_immutable = 30, RULE_call = 31, RULE_constant = 32; + private static String[] makeRuleNames() { + return new String[] { + "program", "declaration", "varDeclaration", "varDeclId", "funDeclaration", + "typeSpecifier", "param", "paramId", "statement", "compoundStmt", "expressionStmt", + "ifStmt", "whileStmt", "returnStmt", "breakStmt", "expression", "simpleExpression", + "orExpression", "andExpression", "unaryRelExpression", "relExpression", + "relop", "sumExpression", "sumop", "termExpression", "mulop", "unaryExpression", + "unaryop", "factor", "mutable", "immutable", "call", "constant" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public CminusParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + public static class ProgramContext extends ParserRuleContext { + public List<DeclarationContext> declaration() { + return getRuleContexts(DeclarationContext.class); + } + public DeclarationContext declaration(int i) { + return getRuleContext(DeclarationContext.class,i); + } + public ProgramContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_program; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterProgram(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitProgram(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitProgram(this); + else return visitor.visitChildren(this); + } + } + + public final ProgramContext program() throws RecognitionException { + ProgramContext _localctx = new ProgramContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_program); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(67); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(66); + declaration(); + } + } + setState(69); + _errHandler.sync(this); + _la = _input.LA(1); + } while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__4) | (1L << T__7) | (1L << T__8) | (1L << T__9))) != 0) ); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class DeclarationContext extends ParserRuleContext { + public VarDeclarationContext varDeclaration() { + return getRuleContext(VarDeclarationContext.class,0); + } + public FunDeclarationContext funDeclaration() { + return getRuleContext(FunDeclarationContext.class,0); + } + public DeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_declaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final DeclarationContext declaration() throws RecognitionException { + DeclarationContext _localctx = new DeclarationContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_declaration); + try { + setState(73); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(71); + varDeclaration(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(72); + funDeclaration(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclarationContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List<VarDeclIdContext> varDeclId() { + return getRuleContexts(VarDeclIdContext.class); + } + public VarDeclIdContext varDeclId(int i) { + return getRuleContext(VarDeclIdContext.class,i); + } + public VarDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitVarDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclarationContext varDeclaration() throws RecognitionException { + VarDeclarationContext _localctx = new VarDeclarationContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_varDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(75); + typeSpecifier(); + setState(76); + varDeclId(); + setState(81); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(77); + match(T__0); + setState(78); + varDeclId(); + } + } + setState(83); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(84); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public VarDeclIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclId(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitVarDeclId(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclIdContext varDeclId() throws RecognitionException { + VarDeclIdContext _localctx = new VarDeclIdContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_varDeclId); + try { + setState(91); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(86); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(87); + match(ID); + setState(88); + match(T__2); + setState(89); + match(NUMCONST); + setState(90); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FunDeclarationContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List<ParamContext> param() { + return getRuleContexts(ParamContext.class); + } + public ParamContext param(int i) { + return getRuleContext(ParamContext.class,i); + } + public FunDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_funDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFunDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFunDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitFunDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final FunDeclarationContext funDeclaration() throws RecognitionException { + FunDeclarationContext _localctx = new FunDeclarationContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_funDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(95); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__4: + { + setState(93); + match(T__4); + } + break; + case T__7: + case T__8: + case T__9: + { + setState(94); + typeSpecifier(); + } + break; + default: + throw new NoViableAltException(this); + } + setState(97); + match(ID); + setState(98); + match(T__5); + setState(100); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + setState(99); + param(); + } + } + + setState(106); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(102); + match(T__0); + setState(103); + param(); + } + } + setState(108); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(109); + match(T__6); + setState(110); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeSpecifierContext extends ParserRuleContext { + public TypeSpecifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_typeSpecifier; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTypeSpecifier(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTypeSpecifier(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitTypeSpecifier(this); + else return visitor.visitChildren(this); + } + } + + public final TypeSpecifierContext typeSpecifier() throws RecognitionException { + TypeSpecifierContext _localctx = new TypeSpecifierContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_typeSpecifier); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(112); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public ParamIdContext paramId() { + return getRuleContext(ParamIdContext.class,0); + } + public ParamContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_param; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParam(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParam(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitParam(this); + else return visitor.visitChildren(this); + } + } + + public final ParamContext param() throws RecognitionException { + ParamContext _localctx = new ParamContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_param); + try { + enterOuterAlt(_localctx, 1); + { + setState(114); + typeSpecifier(); + setState(115); + paramId(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ParamIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_paramId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParamId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParamId(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitParamId(this); + else return visitor.visitChildren(this); + } + } + + public final ParamIdContext paramId() throws RecognitionException { + ParamIdContext _localctx = new ParamIdContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_paramId); + try { + setState(120); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,7,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(117); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(118); + match(ID); + setState(119); + match(T__10); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class StatementContext extends ParserRuleContext { + public ExpressionStmtContext expressionStmt() { + return getRuleContext(ExpressionStmtContext.class,0); + } + public CompoundStmtContext compoundStmt() { + return getRuleContext(CompoundStmtContext.class,0); + } + public IfStmtContext ifStmt() { + return getRuleContext(IfStmtContext.class,0); + } + public WhileStmtContext whileStmt() { + return getRuleContext(WhileStmtContext.class,0); + } + public ReturnStmtContext returnStmt() { + return getRuleContext(ReturnStmtContext.class,0); + } + public BreakStmtContext breakStmt() { + return getRuleContext(BreakStmtContext.class,0); + } + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_statement; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterStatement(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitStatement(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitStatement(this); + else return visitor.visitChildren(this); + } + } + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_statement); + try { + setState(128); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__1: + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(122); + expressionStmt(); + } + break; + case T__11: + enterOuterAlt(_localctx, 2); + { + setState(123); + compoundStmt(); + } + break; + case T__13: + enterOuterAlt(_localctx, 3); + { + setState(124); + ifStmt(); + } + break; + case T__15: + enterOuterAlt(_localctx, 4); + { + setState(125); + whileStmt(); + } + break; + case T__16: + enterOuterAlt(_localctx, 5); + { + setState(126); + returnStmt(); + } + break; + case T__17: + enterOuterAlt(_localctx, 6); + { + setState(127); + breakStmt(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CompoundStmtContext extends ParserRuleContext { + public List<VarDeclarationContext> varDeclaration() { + return getRuleContexts(VarDeclarationContext.class); + } + public VarDeclarationContext varDeclaration(int i) { + return getRuleContext(VarDeclarationContext.class,i); + } + public List<StatementContext> statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public CompoundStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_compoundStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCompoundStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCompoundStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitCompoundStmt(this); + else return visitor.visitChildren(this); + } + } + + public final CompoundStmtContext compoundStmt() throws RecognitionException { + CompoundStmtContext _localctx = new CompoundStmtContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_compoundStmt); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(130); + match(T__11); + setState(134); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + { + setState(131); + varDeclaration(); + } + } + setState(136); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(140); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__1) | (1L << T__5) | (1L << T__11) | (1L << T__13) | (1L << T__15) | (1L << T__16) | (1L << T__17) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + { + setState(137); + statement(); + } + } + setState(142); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(143); + match(T__12); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ExpressionStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expressionStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpressionStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpressionStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitExpressionStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionStmtContext expressionStmt() throws RecognitionException { + ExpressionStmtContext _localctx = new ExpressionStmtContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_expressionStmt); + try { + setState(149); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(145); + expression(); + setState(146); + match(T__1); + } + break; + case T__1: + enterOuterAlt(_localctx, 2); + { + setState(148); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class IfStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public List<StatementContext> statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public IfStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_ifStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterIfStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitIfStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitIfStmt(this); + else return visitor.visitChildren(this); + } + } + + public final IfStmtContext ifStmt() throws RecognitionException { + IfStmtContext _localctx = new IfStmtContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_ifStmt); + try { + setState(165); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,12,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(151); + match(T__13); + setState(152); + match(T__5); + setState(153); + simpleExpression(); + setState(154); + match(T__6); + setState(155); + statement(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(157); + match(T__13); + setState(158); + match(T__5); + setState(159); + simpleExpression(); + setState(160); + match(T__6); + setState(161); + statement(); + setState(162); + match(T__14); + setState(163); + statement(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class WhileStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public WhileStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_whileStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterWhileStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitWhileStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitWhileStmt(this); + else return visitor.visitChildren(this); + } + } + + public final WhileStmtContext whileStmt() throws RecognitionException { + WhileStmtContext _localctx = new WhileStmtContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_whileStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(167); + match(T__15); + setState(168); + match(T__5); + setState(169); + simpleExpression(); + setState(170); + match(T__6); + setState(171); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ReturnStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ReturnStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_returnStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterReturnStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitReturnStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitReturnStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ReturnStmtContext returnStmt() throws RecognitionException { + ReturnStmtContext _localctx = new ReturnStmtContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_returnStmt); + try { + setState(179); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,13,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(173); + match(T__16); + setState(174); + match(T__1); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(175); + match(T__16); + setState(176); + expression(); + setState(177); + match(T__1); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class BreakStmtContext extends ParserRuleContext { + public BreakStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_breakStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterBreakStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitBreakStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitBreakStmt(this); + else return visitor.visitChildren(this); + } + } + + public final BreakStmtContext breakStmt() throws RecognitionException { + BreakStmtContext _localctx = new BreakStmtContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_breakStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(181); + match(T__17); + setState(182); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionContext extends ParserRuleContext { + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitExpression(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 30, RULE_expression); + try { + setState(211); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,14,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(184); + mutable(); + setState(185); + match(T__18); + setState(186); + expression(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(188); + mutable(); + setState(189); + match(T__19); + setState(190); + expression(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); + { + setState(192); + mutable(); + setState(193); + match(T__20); + setState(194); + expression(); + } + break; + case 4: + enterOuterAlt(_localctx, 4); + { + setState(196); + mutable(); + setState(197); + match(T__21); + setState(198); + expression(); + } + break; + case 5: + enterOuterAlt(_localctx, 5); + { + setState(200); + mutable(); + setState(201); + match(T__22); + setState(202); + expression(); + } + break; + case 6: + enterOuterAlt(_localctx, 6); + { + setState(204); + mutable(); + setState(205); + match(T__23); + } + break; + case 7: + enterOuterAlt(_localctx, 7); + { + setState(207); + mutable(); + setState(208); + match(T__24); + } + break; + case 8: + enterOuterAlt(_localctx, 8); + { + setState(210); + simpleExpression(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SimpleExpressionContext extends ParserRuleContext { + public OrExpressionContext orExpression() { + return getRuleContext(OrExpressionContext.class,0); + } + public SimpleExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_simpleExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSimpleExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSimpleExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSimpleExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SimpleExpressionContext simpleExpression() throws RecognitionException { + SimpleExpressionContext _localctx = new SimpleExpressionContext(_ctx, getState()); + enterRule(_localctx, 32, RULE_simpleExpression); + try { + enterOuterAlt(_localctx, 1); + { + setState(213); + orExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class OrExpressionContext extends ParserRuleContext { + public List<AndExpressionContext> andExpression() { + return getRuleContexts(AndExpressionContext.class); + } + public AndExpressionContext andExpression(int i) { + return getRuleContext(AndExpressionContext.class,i); + } + public OrExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_orExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterOrExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitOrExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitOrExpression(this); + else return visitor.visitChildren(this); + } + } + + public final OrExpressionContext orExpression() throws RecognitionException { + OrExpressionContext _localctx = new OrExpressionContext(_ctx, getState()); + enterRule(_localctx, 34, RULE_orExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(220); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(215); + andExpression(); + setState(216); + match(T__25); + } + } + } + setState(222); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + } + setState(223); + andExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class AndExpressionContext extends ParserRuleContext { + public List<UnaryRelExpressionContext> unaryRelExpression() { + return getRuleContexts(UnaryRelExpressionContext.class); + } + public UnaryRelExpressionContext unaryRelExpression(int i) { + return getRuleContext(UnaryRelExpressionContext.class,i); + } + public AndExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_andExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterAndExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitAndExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitAndExpression(this); + else return visitor.visitChildren(this); + } + } + + public final AndExpressionContext andExpression() throws RecognitionException { + AndExpressionContext _localctx = new AndExpressionContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_andExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(230); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(225); + unaryRelExpression(); + setState(226); + match(T__26); + } + } + } + setState(232); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + } + setState(233); + unaryRelExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryRelExpressionContext extends ParserRuleContext { + public RelExpressionContext relExpression() { + return getRuleContext(RelExpressionContext.class,0); + } + public List<TerminalNode> BANG() { return getTokens(CminusParser.BANG); } + public TerminalNode BANG(int i) { + return getToken(CminusParser.BANG, i); + } + public UnaryRelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryRelExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryRelExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryRelExpressionContext unaryRelExpression() throws RecognitionException { + UnaryRelExpressionContext _localctx = new UnaryRelExpressionContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_unaryRelExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(238); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==BANG) { + { + { + setState(235); + match(BANG); + } + } + setState(240); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(241); + relExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelExpressionContext extends ParserRuleContext { + public List<SumExpressionContext> sumExpression() { + return getRuleContexts(SumExpressionContext.class); + } + public SumExpressionContext sumExpression(int i) { + return getRuleContext(SumExpressionContext.class,i); + } + public List<RelopContext> relop() { + return getRuleContexts(RelopContext.class); + } + public RelopContext relop(int i) { + return getRuleContext(RelopContext.class,i); + } + public RelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final RelExpressionContext relExpression() throws RecognitionException { + RelExpressionContext _localctx = new RelExpressionContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_relExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(248); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(243); + sumExpression(); + setState(244); + relop(); + } + } + } + setState(250); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + } + setState(251); + sumExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelopContext extends ParserRuleContext { + public RelopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitRelop(this); + else return visitor.visitChildren(this); + } + } + + public final RelopContext relop() throws RecognitionException { + RelopContext _localctx = new RelopContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_relop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(253); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__27) | (1L << T__28) | (1L << T__29) | (1L << T__30) | (1L << T__31) | (1L << T__32))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumExpressionContext extends ParserRuleContext { + public List<TermExpressionContext> termExpression() { + return getRuleContexts(TermExpressionContext.class); + } + public TermExpressionContext termExpression(int i) { + return getRuleContext(TermExpressionContext.class,i); + } + public List<SumopContext> sumop() { + return getRuleContexts(SumopContext.class); + } + public SumopContext sumop(int i) { + return getRuleContext(SumopContext.class,i); + } + public SumExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSumExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SumExpressionContext sumExpression() throws RecognitionException { + SumExpressionContext _localctx = new SumExpressionContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_sumExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(260); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(255); + termExpression(); + setState(256); + sumop(); + } + } + } + setState(262); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + } + setState(263); + termExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumopContext extends ParserRuleContext { + public SumopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSumop(this); + else return visitor.visitChildren(this); + } + } + + public final SumopContext sumop() throws RecognitionException { + SumopContext _localctx = new SumopContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_sumop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(265); + _la = _input.LA(1); + if ( !(_la==T__33 || _la==T__34) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TermExpressionContext extends ParserRuleContext { + public List<UnaryExpressionContext> unaryExpression() { + return getRuleContexts(UnaryExpressionContext.class); + } + public UnaryExpressionContext unaryExpression(int i) { + return getRuleContext(UnaryExpressionContext.class,i); + } + public List<MulopContext> mulop() { + return getRuleContexts(MulopContext.class); + } + public MulopContext mulop(int i) { + return getRuleContext(MulopContext.class,i); + } + public TermExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_termExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTermExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTermExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitTermExpression(this); + else return visitor.visitChildren(this); + } + } + + public final TermExpressionContext termExpression() throws RecognitionException { + TermExpressionContext _localctx = new TermExpressionContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_termExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(272); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(267); + unaryExpression(); + setState(268); + mulop(); + } + } + } + setState(274); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + } + setState(275); + unaryExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MulopContext extends ParserRuleContext { + public MulopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mulop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMulop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMulop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitMulop(this); + else return visitor.visitChildren(this); + } + } + + public final MulopContext mulop() throws RecognitionException { + MulopContext _localctx = new MulopContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_mulop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(277); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__35) | (1L << T__36) | (1L << T__37))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryExpressionContext extends ParserRuleContext { + public FactorContext factor() { + return getRuleContext(FactorContext.class,0); + } + public List<UnaryopContext> unaryop() { + return getRuleContexts(UnaryopContext.class); + } + public UnaryopContext unaryop(int i) { + return getRuleContext(UnaryopContext.class,i); + } + public UnaryExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryExpressionContext unaryExpression() throws RecognitionException { + UnaryExpressionContext _localctx = new UnaryExpressionContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_unaryExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(282); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) { + { + { + setState(279); + unaryop(); + } + } + setState(284); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(285); + factor(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryopContext extends ParserRuleContext { + public UnaryopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryop(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryopContext unaryop() throws RecognitionException { + UnaryopContext _localctx = new UnaryopContext(_ctx, getState()); + enterRule(_localctx, 54, RULE_unaryop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(287); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FactorContext extends ParserRuleContext { + public ImmutableContext immutable() { + return getRuleContext(ImmutableContext.class,0); + } + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public FactorContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_factor; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFactor(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFactor(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitFactor(this); + else return visitor.visitChildren(this); + } + } + + public final FactorContext factor() throws RecognitionException { + FactorContext _localctx = new FactorContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_factor); + try { + setState(291); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,22,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(289); + immutable(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(290); + mutable(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MutableContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public MutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMutable(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitMutable(this); + else return visitor.visitChildren(this); + } + } + + public final MutableContext mutable() throws RecognitionException { + MutableContext _localctx = new MutableContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_mutable); + try { + setState(299); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,23,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(293); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(294); + match(ID); + setState(295); + match(T__2); + setState(296); + expression(); + setState(297); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ImmutableContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public CallContext call() { + return getRuleContext(CallContext.class,0); + } + public ConstantContext constant() { + return getRuleContext(ConstantContext.class,0); + } + public ImmutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_immutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterImmutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitImmutable(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitImmutable(this); + else return visitor.visitChildren(this); + } + } + + public final ImmutableContext immutable() throws RecognitionException { + ImmutableContext _localctx = new ImmutableContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_immutable); + try { + setState(307); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + enterOuterAlt(_localctx, 1); + { + setState(301); + match(T__5); + setState(302); + expression(); + setState(303); + match(T__6); + } + break; + case ID: + enterOuterAlt(_localctx, 2); + { + setState(305); + call(); + } + break; + case T__39: + case T__40: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + enterOuterAlt(_localctx, 3); + { + setState(306); + constant(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CallContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public List<ExpressionContext> expression() { + return getRuleContexts(ExpressionContext.class); + } + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class,i); + } + public CallContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_call; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCall(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCall(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitCall(this); + else return visitor.visitChildren(this); + } + } + + public final CallContext call() throws RecognitionException { + CallContext _localctx = new CallContext(_ctx, getState()); + enterRule(_localctx, 62, RULE_call); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(309); + match(ID); + setState(310); + match(T__5); + setState(316); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(311); + expression(); + setState(312); + match(T__0); + } + } + } + setState(318); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + } + setState(320); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__5) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + setState(319); + expression(); + } + } + + setState(322); + match(T__6); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ConstantContext extends ParserRuleContext { + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public TerminalNode CHARCONST() { return getToken(CminusParser.CHARCONST, 0); } + public TerminalNode STRINGCONST() { return getToken(CminusParser.STRINGCONST, 0); } + public ConstantContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_constant; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterConstant(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitConstant(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitConstant(this); + else return visitor.visitChildren(this); + } + } + + public final ConstantContext constant() throws RecognitionException { + ConstantContext _localctx = new ConstantContext(_ctx, getState()); + enterRule(_localctx, 64, RULE_constant); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(324); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__39) | (1L << T__40) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\62\u0149\4\2\t\2"+ + "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ + "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!"+ + "\t!\4\"\t\"\3\2\6\2F\n\2\r\2\16\2G\3\3\3\3\5\3L\n\3\3\4\3\4\3\4\3\4\7"+ + "\4R\n\4\f\4\16\4U\13\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\5\5^\n\5\3\6\3\6\5"+ + "\6b\n\6\3\6\3\6\3\6\5\6g\n\6\3\6\3\6\7\6k\n\6\f\6\16\6n\13\6\3\6\3\6\3"+ + "\6\3\7\3\7\3\b\3\b\3\b\3\t\3\t\3\t\5\t{\n\t\3\n\3\n\3\n\3\n\3\n\3\n\5"+ + "\n\u0083\n\n\3\13\3\13\7\13\u0087\n\13\f\13\16\13\u008a\13\13\3\13\7\13"+ + "\u008d\n\13\f\13\16\13\u0090\13\13\3\13\3\13\3\f\3\f\3\f\3\f\5\f\u0098"+ + "\n\f\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\5\r\u00a8"+ + "\n\r\3\16\3\16\3\16\3\16\3\16\3\16\3\17\3\17\3\17\3\17\3\17\3\17\5\17"+ + "\u00b6\n\17\3\20\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\5\21\u00d6\n\21\3\22\3\22\3\23\3\23\3\23\7\23\u00dd"+ + "\n\23\f\23\16\23\u00e0\13\23\3\23\3\23\3\24\3\24\3\24\7\24\u00e7\n\24"+ + "\f\24\16\24\u00ea\13\24\3\24\3\24\3\25\7\25\u00ef\n\25\f\25\16\25\u00f2"+ + "\13\25\3\25\3\25\3\26\3\26\3\26\7\26\u00f9\n\26\f\26\16\26\u00fc\13\26"+ + "\3\26\3\26\3\27\3\27\3\30\3\30\3\30\7\30\u0105\n\30\f\30\16\30\u0108\13"+ + "\30\3\30\3\30\3\31\3\31\3\32\3\32\3\32\7\32\u0111\n\32\f\32\16\32\u0114"+ + "\13\32\3\32\3\32\3\33\3\33\3\34\7\34\u011b\n\34\f\34\16\34\u011e\13\34"+ + "\3\34\3\34\3\35\3\35\3\36\3\36\5\36\u0126\n\36\3\37\3\37\3\37\3\37\3\37"+ + "\3\37\5\37\u012e\n\37\3 \3 \3 \3 \3 \3 \5 \u0136\n \3!\3!\3!\3!\3!\7!"+ + "\u013d\n!\f!\16!\u0140\13!\3!\5!\u0143\n!\3!\3!\3\"\3\"\3\"\2\2#\2\4\6"+ + "\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62\64\668:<>@B\2\b\3\2\n"+ + "\f\3\2\36#\3\2$%\3\2&(\4\2%&))\4\2*+-/\2\u014d\2E\3\2\2\2\4K\3\2\2\2\6"+ + "M\3\2\2\2\b]\3\2\2\2\na\3\2\2\2\fr\3\2\2\2\16t\3\2\2\2\20z\3\2\2\2\22"+ + "\u0082\3\2\2\2\24\u0084\3\2\2\2\26\u0097\3\2\2\2\30\u00a7\3\2\2\2\32\u00a9"+ + "\3\2\2\2\34\u00b5\3\2\2\2\36\u00b7\3\2\2\2 \u00d5\3\2\2\2\"\u00d7\3\2"+ + "\2\2$\u00de\3\2\2\2&\u00e8\3\2\2\2(\u00f0\3\2\2\2*\u00fa\3\2\2\2,\u00ff"+ + "\3\2\2\2.\u0106\3\2\2\2\60\u010b\3\2\2\2\62\u0112\3\2\2\2\64\u0117\3\2"+ + "\2\2\66\u011c\3\2\2\28\u0121\3\2\2\2:\u0125\3\2\2\2<\u012d\3\2\2\2>\u0135"+ + "\3\2\2\2@\u0137\3\2\2\2B\u0146\3\2\2\2DF\5\4\3\2ED\3\2\2\2FG\3\2\2\2G"+ + "E\3\2\2\2GH\3\2\2\2H\3\3\2\2\2IL\5\6\4\2JL\5\n\6\2KI\3\2\2\2KJ\3\2\2\2"+ + "L\5\3\2\2\2MN\5\f\7\2NS\5\b\5\2OP\7\3\2\2PR\5\b\5\2QO\3\2\2\2RU\3\2\2"+ + "\2SQ\3\2\2\2ST\3\2\2\2TV\3\2\2\2US\3\2\2\2VW\7\4\2\2W\7\3\2\2\2X^\7,\2"+ + "\2YZ\7,\2\2Z[\7\5\2\2[\\\7-\2\2\\^\7\6\2\2]X\3\2\2\2]Y\3\2\2\2^\t\3\2"+ + "\2\2_b\7\7\2\2`b\5\f\7\2a_\3\2\2\2a`\3\2\2\2bc\3\2\2\2cd\7,\2\2df\7\b"+ + "\2\2eg\5\16\b\2fe\3\2\2\2fg\3\2\2\2gl\3\2\2\2hi\7\3\2\2ik\5\16\b\2jh\3"+ + "\2\2\2kn\3\2\2\2lj\3\2\2\2lm\3\2\2\2mo\3\2\2\2nl\3\2\2\2op\7\t\2\2pq\5"+ + "\22\n\2q\13\3\2\2\2rs\t\2\2\2s\r\3\2\2\2tu\5\f\7\2uv\5\20\t\2v\17\3\2"+ + "\2\2w{\7,\2\2xy\7,\2\2y{\7\r\2\2zw\3\2\2\2zx\3\2\2\2{\21\3\2\2\2|\u0083"+ + "\5\26\f\2}\u0083\5\24\13\2~\u0083\5\30\r\2\177\u0083\5\32\16\2\u0080\u0083"+ + "\5\34\17\2\u0081\u0083\5\36\20\2\u0082|\3\2\2\2\u0082}\3\2\2\2\u0082~"+ + "\3\2\2\2\u0082\177\3\2\2\2\u0082\u0080\3\2\2\2\u0082\u0081\3\2\2\2\u0083"+ + "\23\3\2\2\2\u0084\u0088\7\16\2\2\u0085\u0087\5\6\4\2\u0086\u0085\3\2\2"+ + "\2\u0087\u008a\3\2\2\2\u0088\u0086\3\2\2\2\u0088\u0089\3\2\2\2\u0089\u008e"+ + "\3\2\2\2\u008a\u0088\3\2\2\2\u008b\u008d\5\22\n\2\u008c\u008b\3\2\2\2"+ + "\u008d\u0090\3\2\2\2\u008e\u008c\3\2\2\2\u008e\u008f\3\2\2\2\u008f\u0091"+ + "\3\2\2\2\u0090\u008e\3\2\2\2\u0091\u0092\7\17\2\2\u0092\25\3\2\2\2\u0093"+ + "\u0094\5 \21\2\u0094\u0095\7\4\2\2\u0095\u0098\3\2\2\2\u0096\u0098\7\4"+ + "\2\2\u0097\u0093\3\2\2\2\u0097\u0096\3\2\2\2\u0098\27\3\2\2\2\u0099\u009a"+ + "\7\20\2\2\u009a\u009b\7\b\2\2\u009b\u009c\5\"\22\2\u009c\u009d\7\t\2\2"+ + "\u009d\u009e\5\22\n\2\u009e\u00a8\3\2\2\2\u009f\u00a0\7\20\2\2\u00a0\u00a1"+ + "\7\b\2\2\u00a1\u00a2\5\"\22\2\u00a2\u00a3\7\t\2\2\u00a3\u00a4\5\22\n\2"+ + "\u00a4\u00a5\7\21\2\2\u00a5\u00a6\5\22\n\2\u00a6\u00a8\3\2\2\2\u00a7\u0099"+ + "\3\2\2\2\u00a7\u009f\3\2\2\2\u00a8\31\3\2\2\2\u00a9\u00aa\7\22\2\2\u00aa"+ + "\u00ab\7\b\2\2\u00ab\u00ac\5\"\22\2\u00ac\u00ad\7\t\2\2\u00ad\u00ae\5"+ + "\22\n\2\u00ae\33\3\2\2\2\u00af\u00b0\7\23\2\2\u00b0\u00b6\7\4\2\2\u00b1"+ + "\u00b2\7\23\2\2\u00b2\u00b3\5 \21\2\u00b3\u00b4\7\4\2\2\u00b4\u00b6\3"+ + "\2\2\2\u00b5\u00af\3\2\2\2\u00b5\u00b1\3\2\2\2\u00b6\35\3\2\2\2\u00b7"+ + "\u00b8\7\24\2\2\u00b8\u00b9\7\4\2\2\u00b9\37\3\2\2\2\u00ba\u00bb\5<\37"+ + "\2\u00bb\u00bc\7\25\2\2\u00bc\u00bd\5 \21\2\u00bd\u00d6\3\2\2\2\u00be"+ + "\u00bf\5<\37\2\u00bf\u00c0\7\26\2\2\u00c0\u00c1\5 \21\2\u00c1\u00d6\3"+ + "\2\2\2\u00c2\u00c3\5<\37\2\u00c3\u00c4\7\27\2\2\u00c4\u00c5\5 \21\2\u00c5"+ + "\u00d6\3\2\2\2\u00c6\u00c7\5<\37\2\u00c7\u00c8\7\30\2\2\u00c8\u00c9\5"+ + " \21\2\u00c9\u00d6\3\2\2\2\u00ca\u00cb\5<\37\2\u00cb\u00cc\7\31\2\2\u00cc"+ + "\u00cd\5 \21\2\u00cd\u00d6\3\2\2\2\u00ce\u00cf\5<\37\2\u00cf\u00d0\7\32"+ + "\2\2\u00d0\u00d6\3\2\2\2\u00d1\u00d2\5<\37\2\u00d2\u00d3\7\33\2\2\u00d3"+ + "\u00d6\3\2\2\2\u00d4\u00d6\5\"\22\2\u00d5\u00ba\3\2\2\2\u00d5\u00be\3"+ + "\2\2\2\u00d5\u00c2\3\2\2\2\u00d5\u00c6\3\2\2\2\u00d5\u00ca\3\2\2\2\u00d5"+ + "\u00ce\3\2\2\2\u00d5\u00d1\3\2\2\2\u00d5\u00d4\3\2\2\2\u00d6!\3\2\2\2"+ + "\u00d7\u00d8\5$\23\2\u00d8#\3\2\2\2\u00d9\u00da\5&\24\2\u00da\u00db\7"+ + "\34\2\2\u00db\u00dd\3\2\2\2\u00dc\u00d9\3\2\2\2\u00dd\u00e0\3\2\2\2\u00de"+ + "\u00dc\3\2\2\2\u00de\u00df\3\2\2\2\u00df\u00e1\3\2\2\2\u00e0\u00de\3\2"+ + "\2\2\u00e1\u00e2\5&\24\2\u00e2%\3\2\2\2\u00e3\u00e4\5(\25\2\u00e4\u00e5"+ + "\7\35\2\2\u00e5\u00e7\3\2\2\2\u00e6\u00e3\3\2\2\2\u00e7\u00ea\3\2\2\2"+ + "\u00e8\u00e6\3\2\2\2\u00e8\u00e9\3\2\2\2\u00e9\u00eb\3\2\2\2\u00ea\u00e8"+ + "\3\2\2\2\u00eb\u00ec\5(\25\2\u00ec\'\3\2\2\2\u00ed\u00ef\7\60\2\2\u00ee"+ + "\u00ed\3\2\2\2\u00ef\u00f2\3\2\2\2\u00f0\u00ee\3\2\2\2\u00f0\u00f1\3\2"+ + "\2\2\u00f1\u00f3\3\2\2\2\u00f2\u00f0\3\2\2\2\u00f3\u00f4\5*\26\2\u00f4"+ + ")\3\2\2\2\u00f5\u00f6\5.\30\2\u00f6\u00f7\5,\27\2\u00f7\u00f9\3\2\2\2"+ + "\u00f8\u00f5\3\2\2\2\u00f9\u00fc\3\2\2\2\u00fa\u00f8\3\2\2\2\u00fa\u00fb"+ + "\3\2\2\2\u00fb\u00fd\3\2\2\2\u00fc\u00fa\3\2\2\2\u00fd\u00fe\5.\30\2\u00fe"+ + "+\3\2\2\2\u00ff\u0100\t\3\2\2\u0100-\3\2\2\2\u0101\u0102\5\62\32\2\u0102"+ + "\u0103\5\60\31\2\u0103\u0105\3\2\2\2\u0104\u0101\3\2\2\2\u0105\u0108\3"+ + "\2\2\2\u0106\u0104\3\2\2\2\u0106\u0107\3\2\2\2\u0107\u0109\3\2\2\2\u0108"+ + "\u0106\3\2\2\2\u0109\u010a\5\62\32\2\u010a/\3\2\2\2\u010b\u010c\t\4\2"+ + "\2\u010c\61\3\2\2\2\u010d\u010e\5\66\34\2\u010e\u010f\5\64\33\2\u010f"+ + "\u0111\3\2\2\2\u0110\u010d\3\2\2\2\u0111\u0114\3\2\2\2\u0112\u0110\3\2"+ + "\2\2\u0112\u0113\3\2\2\2\u0113\u0115\3\2\2\2\u0114\u0112\3\2\2\2\u0115"+ + "\u0116\5\66\34\2\u0116\63\3\2\2\2\u0117\u0118\t\5\2\2\u0118\65\3\2\2\2"+ + "\u0119\u011b\58\35\2\u011a\u0119\3\2\2\2\u011b\u011e\3\2\2\2\u011c\u011a"+ + "\3\2\2\2\u011c\u011d\3\2\2\2\u011d\u011f\3\2\2\2\u011e\u011c\3\2\2\2\u011f"+ + "\u0120\5:\36\2\u0120\67\3\2\2\2\u0121\u0122\t\6\2\2\u01229\3\2\2\2\u0123"+ + "\u0126\5> \2\u0124\u0126\5<\37\2\u0125\u0123\3\2\2\2\u0125\u0124\3\2\2"+ + "\2\u0126;\3\2\2\2\u0127\u012e\7,\2\2\u0128\u0129\7,\2\2\u0129\u012a\7"+ + "\5\2\2\u012a\u012b\5 \21\2\u012b\u012c\7\6\2\2\u012c\u012e\3\2\2\2\u012d"+ + "\u0127\3\2\2\2\u012d\u0128\3\2\2\2\u012e=\3\2\2\2\u012f\u0130\7\b\2\2"+ + "\u0130\u0131\5 \21\2\u0131\u0132\7\t\2\2\u0132\u0136\3\2\2\2\u0133\u0136"+ + "\5@!\2\u0134\u0136\5B\"\2\u0135\u012f\3\2\2\2\u0135\u0133\3\2\2\2\u0135"+ + "\u0134\3\2\2\2\u0136?\3\2\2\2\u0137\u0138\7,\2\2\u0138\u013e\7\b\2\2\u0139"+ + "\u013a\5 \21\2\u013a\u013b\7\3\2\2\u013b\u013d\3\2\2\2\u013c\u0139\3\2"+ + "\2\2\u013d\u0140\3\2\2\2\u013e\u013c\3\2\2\2\u013e\u013f\3\2\2\2\u013f"+ + "\u0142\3\2\2\2\u0140\u013e\3\2\2\2\u0141\u0143\5 \21\2\u0142\u0141\3\2"+ + "\2\2\u0142\u0143\3\2\2\2\u0143\u0144\3\2\2\2\u0144\u0145\7\t\2\2\u0145"+ + "A\3\2\2\2\u0146\u0147\t\7\2\2\u0147C\3\2\2\2\35GKS]aflz\u0082\u0088\u008e"+ + "\u0097\u00a7\u00b5\u00d5\u00de\u00e8\u00f0\u00fa\u0106\u0112\u011c\u0125"+ + "\u012d\u0135\u013e\u0142"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/parser/CminusVisitor.java b/Homework/cs5300/compiler/p5-compiler/parser/CminusVisitor.java new file mode 100644 index 0000000..9088695 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/parser/CminusVisitor.java @@ -0,0 +1,211 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeVisitor; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link CminusParser}. + * + * @param <T> The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public interface CminusVisitor<T> extends ParseTreeVisitor<T> { + /** + * Visit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitProgram(CminusParser.ProgramContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParam(CminusParser.ParamContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParamId(CminusParser.ParamIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatement(CminusParser.StatementContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpression(CminusParser.ExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelop(CminusParser.RelopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumop(CminusParser.SumopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMulop(CminusParser.MulopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFactor(CminusParser.FactorContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMutable(CminusParser.MutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitImmutable(CminusParser.ImmutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCall(CminusParser.CallContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstant(CminusParser.ConstantContext ctx); +}
\ No newline at end of file diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ASTVisitor.java b/Homework/cs5300/compiler/p5-compiler/submit/ASTVisitor.java new file mode 100644 index 0000000..5cafbb1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ASTVisitor.java @@ -0,0 +1,342 @@ +package submit; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.TerminalNode; +import parser.CminusBaseVisitor; +import parser.CminusParser; +import submit.ast.*; + +public class ASTVisitor extends CminusBaseVisitor<Node> { + private final Logger LOGGER; + private SymbolTable symbolTable; + + public ASTVisitor(Logger LOGGER) { this.LOGGER = LOGGER; } + + public SymbolTable getSymbolTable() { return symbolTable; } + + private VarType getVarType(CminusParser.TypeSpecifierContext ctx) { + final String t = ctx.getText(); + return (t.equals("int")) ? VarType.INT + : (t.equals("bool")) ? VarType.BOOL + : VarType.CHAR; + } + + @Override + public Node visitProgram(CminusParser.ProgramContext ctx) { + symbolTable = new SymbolTable(); + List<Declaration> decls = new ArrayList<>(); + for (CminusParser.DeclarationContext d : ctx.declaration()) { + decls.add((Declaration)visitDeclaration(d)); + } + return new Program(decls); + } + + @Override + public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + List<String> ids = new ArrayList<>(); + List<Integer> arraySizes = new ArrayList<>(); + for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) { + String id = v.ID().getText(); + ids.add(id); + // symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + if (v.NUMCONST() != null) { + arraySizes.add(Integer.parseInt(v.NUMCONST().getText())); + } else { + int offset = symbolTable.addOffset(1); + + SymbolInfo symbol = new SymbolInfo(id, type, false, offset); + symbolTable.addSymbol(id, symbol); + + arraySizes.add(-1); + } + } + final boolean isStatic = false; + return new VarDeclaration(type, ids, arraySizes, isStatic); + } + + @Override + public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { + VarType returnType = null; + if (ctx.typeSpecifier() != null) { + returnType = getVarType(ctx.typeSpecifier()); + } + String id = ctx.ID().getText(); + symbolTable.addSymbol(id, new SymbolInfo(id, returnType, true)); + + List<Param> params = new ArrayList<>(); + + SymbolTable newTable = symbolTable.createChild(); + symbolTable = newTable; + + if (returnType != null) { + symbolTable.addSymbol("return", + new SymbolInfo("return", returnType, false, + symbolTable.addOffset(1))); + } + + for (CminusParser.ParamContext p : ctx.param()) { + params.add((Param)visitParam(p)); + } + + CompoundStatement statement = + (CompoundStatement)visitStatement(ctx.statement()); + + statement.getSymbolTable().addOtherTableBefore(newTable); + symbolTable = newTable.getParent(); + + return new FunDeclaration(returnType, id, params, statement); + } + + @Override + public Node visitParam(CminusParser.ParamContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + String id = ctx.paramId().ID().getText(); + symbolTable.addSymbol( + id, new SymbolInfo(id, type, false, symbolTable.addOffset(1))); + + return new Param(type, id, ctx.paramId().children.size() > 1); + } + + @Override + public Node visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { + SymbolTable child = symbolTable.createChild(); + symbolTable = child; + + List<Statement> statements = new ArrayList<>(); + for (CminusParser.VarDeclarationContext d : ctx.varDeclaration()) { + statements.add((VarDeclaration)visitVarDeclaration(d)); + } + for (CminusParser.StatementContext d : ctx.statement()) { + statements.add((Statement)visitStatement(d)); + } + symbolTable = child.getParent(); + + return new CompoundStatement(statements, child); + } + + @Override + public Node visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { + if (ctx.expression() == null) { + return Statement.empty(); + } + return new ExpressionStatement( + (Expression)visitExpression(ctx.expression())); + } + + @Override + public Node visitIfStmt(CminusParser.IfStmtContext ctx) { + Expression expression = + (Expression)visitSimpleExpression(ctx.simpleExpression()); + Statement trueStatement = (Statement)visitStatement(ctx.statement(0)); + Statement falseStatement = null; + if (ctx.statement().size() > 1) { + falseStatement = (Statement)visitStatement(ctx.statement(1)); + } + return new If(expression, trueStatement, falseStatement); + } + + @Override + public Node visitWhileStmt(CminusParser.WhileStmtContext ctx) { + Expression expression = + (Expression)visitSimpleExpression(ctx.simpleExpression()); + Statement statement = (Statement)visitStatement(ctx.statement()); + return new While(expression, statement); + } + + @Override + public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) { + if (ctx.expression() != null) { + return new Return((Expression)visitExpression(ctx.expression())); + } + return new Return(null); + } + + @Override + public Node visitBreakStmt(CminusParser.BreakStmtContext ctx) { + return new Break(); + } + + @Override + public Node visitExpression(CminusParser.ExpressionContext ctx) { + final Node ret; + CminusParser.MutableContext mutable = ctx.mutable(); + CminusParser.ExpressionContext expression = ctx.expression(); + if (mutable != null) { + // Assignment + ParseTree operator = ctx.getChild(1); + Mutable lhs = (Mutable)visitMutable( + mutable); // new Mutable(mutable.ID().getText(), (Expression) + // visitExpression(mutable.expression())); + Expression rhs = null; + if (expression != null) { + rhs = (Expression)visitExpression(expression); + } + ret = new Assignment(lhs, operator.getText(), rhs); + } else { + ret = visitSimpleExpression(ctx.simpleExpression()); + } + return ret; + } + + @Override + public Node visitOrExpression(CminusParser.OrExpressionContext ctx) { + List<Node> ands = new ArrayList<>(); + for (CminusParser.AndExpressionContext and : ctx.andExpression()) { + ands.add(visitAndExpression(and)); + } + if (ands.size() == 1) { + return ands.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)ands.get(0), "||", + (Expression)ands.get(1)); + for (int i = 2; i < ands.size(); ++i) { + op = new BinaryOperator(op, "||", (Expression)ands.get(i)); + } + return op; + } + + @Override + public Node visitAndExpression(CminusParser.AndExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.UnaryRelExpressionContext un : ctx.unaryRelExpression()) { + uns.add(visitUnaryRelExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)uns.get(0), "&&", + (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, "&&", (Expression)uns.get(i)); + } + return op; + } + + @Override + public Node + visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { + Expression e = (Expression)visitRelExpression(ctx.relExpression()); + for (TerminalNode n : ctx.BANG()) { + e = new UnaryOperator("!", e); + } + return e; + } + + @Override + public Node visitRelExpression(CminusParser.RelExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.SumExpressionContext un : ctx.sumExpression()) { + uns.add(visitSumExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator( + (Expression)uns.get(0), ctx.relop(0).getText(), (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, ctx.relop(i - 1).getText(), + (Expression)uns.get(i)); + } + return op; + } + + @Override + public Node visitSumExpression(CminusParser.SumExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.TermExpressionContext e : ctx.termExpression()) { + es.add(visitTermExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator( + (Expression)es.get(0), ctx.sumop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.sumop(i - 1).getText(), + (Expression)es.get(i)); + } + return op; + } + + @Override + public Node visitTermExpression(CminusParser.TermExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.UnaryExpressionContext e : ctx.unaryExpression()) { + es.add(visitUnaryExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator( + (Expression)es.get(0), ctx.mulop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.mulop(i - 1).getText(), + (Expression)es.get(i)); + } + return op; + } + + @Override + public Node visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { + Node ret = visitFactor(ctx.factor()); + for (int i = ctx.unaryop().size() - 1; i >= 0; i--) { + ret = new UnaryOperator(ctx.unaryop(i).getText(), (Expression)ret); + } + return ret; + } + + @Override + public Node visitMutable(CminusParser.MutableContext ctx) { + Expression e = null; + if (ctx.expression() != null) { + e = (Expression)visitExpression(ctx.expression()); + } + String id = ctx.ID().getText(); + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + + ": " + id); + } + return new Mutable(id, e); + } + + @Override + public Node visitImmutable(CminusParser.ImmutableContext ctx) { + if (ctx.expression() != null) { + return new ParenExpression((Expression)visitExpression(ctx.expression())); + } + return visitChildren(ctx); + } + + @Override + public Node visitCall(CminusParser.CallContext ctx) { + final String id = ctx.ID().getText(); + final List<Expression> args = new ArrayList<>(); + for (CminusParser.ExpressionContext e : ctx.expression()) { + args.add((Expression)visitExpression(e)); + } + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + + ": " + id); + } + return new Call(id, args); + } + + @Override + public Node visitConstant(CminusParser.ConstantContext ctx) { + final Node node; + if (ctx.NUMCONST() != null) { + node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText())); + } else if (ctx.CHARCONST() != null) { + node = new CharConstant(ctx.CHARCONST().getText().charAt(0)); + } else if (ctx.STRINGCONST() != null) { + node = new StringConstant(ctx.STRINGCONST().getText()); + } else { + node = new BoolConstant(ctx.getText().equals("true")); + } + return node; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/MIPSResult.java b/Homework/cs5300/compiler/p5-compiler/submit/MIPSResult.java new file mode 100644 index 0000000..bccfe76 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/MIPSResult.java @@ -0,0 +1,60 @@ +/* + */ +package submit; + +import submit.ast.VarType; + +/** + * This class represents the result of generating MIPS code. There are various + * forms this result can take: 1) void, for cases where the calling node doesn't + * need any information returned, such as a return statement. 2) register, for + * cases where the called node needs to inform the calling node what register a + * result is placed in, such as BinaryOperator. 3) address, for cases where the + * returning result is in memory, such as StringConstant. + * + * To instantiate a MIPSResult object use the factory methods create...(). + * + */ +public class MIPSResult { + + private final String register; + private final String address; + private final VarType type; + + public static MIPSResult createVoidResult() { + return new MIPSResult(null, null, null); + } + + public static MIPSResult createRegisterResult(String register, VarType type) { + return new MIPSResult(register, null, type); + } + + public static MIPSResult createAddressResult(String address, VarType type) { + return new MIPSResult(null, address, type); + } + + private MIPSResult(String register, String address, VarType type) { + this.register = register; + this.address = address; + this.type = type; + } + + /** + * Anytime you get a register from a result you should seriously consider + * calling regAllocator.clear(reg) after using the register to minimize + * register usage. + * + * @return + */ + public String getRegister() { + return register; + } + + public String getAddress() { + return address; + } + + public VarType getType() { + return type; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/RegisterAllocator.java b/Homework/cs5300/compiler/p5-compiler/submit/RegisterAllocator.java new file mode 100644 index 0000000..b0c9d03 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/RegisterAllocator.java @@ -0,0 +1,151 @@ +/* + */ +package submit; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * + * + * + */ +public final class RegisterAllocator { + + // True if t is used + private final boolean[] t = new boolean[10]; + private final boolean[] s = new boolean[8]; + private final Set<String> used = new HashSet<>(); + + public RegisterAllocator() { clearAll(); } + + public String getRegisterOrLoadIntoRegister(MIPSResult result, + StringBuilder code) { + if (result.getRegister() != null) { + return result.getRegister(); + } + String reg = result.getAddress(); + return this.loadIntoRegister(result, code, reg); + } + + public String loadIntoRegister(MIPSResult result, StringBuilder code, + String register) { + return loadIntoRegisterWithOffset(result, code, register, 0); + } + + public String loadIntoRegisterWithOffset(MIPSResult result, + StringBuilder code, String register, + int offset) { + code.append( + String.format("lw %s %d(%s)\n", register, offset, result.getAddress())); + + return register; + } + + public String getT() { + for (int i = 0; i < t.length; ++i) { + if (!t[i]) { + t[i] = true; + String str = "$t" + i; + used.add(str); + return str; + } + } + return null; + } + + // public String getS() { + // for (int i = 0; i < s.length; ++i) { + // if (!s[i]) { + // s[i] = true; + // String str = "$s" + i; + // used.add(str); + // return str; + // } + // } + // return null; + // } + + // Returns the number of bytes used to save the registers + public int saveRestore(StringBuilder code, int baseOffset, boolean s_or_t, + boolean save) { + boolean[] r = s; + String prefix = "$s"; + if (!s_or_t) { + r = t; + prefix = "$t"; + } + String instruction = "sw"; + if (!save) { + instruction = "lw"; + } + int offset = 0; + for (int i = 0; i < r.length; ++i) { + if (r[i]) { + offset -= 4; + String str = prefix + i; + code.append(instruction) + .append(" ") + .append(str) + .append(" ") + .append(offset - baseOffset) + .append("($sp)\n"); + } + } + return -offset; + } + + // public int saveS(StringBuilder code, int baseOffset) { + // return saveRestore(code, baseOffset, true, true); + // } + + public int saveT(StringBuilder code, int baseOffset) { + return saveRestore(code, baseOffset, false, true); + } + + // public int restoreS(StringBuilder code, int baseOffset) { + // return saveRestore(code, baseOffset, true, false); + // } + + public int restoreT(StringBuilder code, int baseOffset) { + return saveRestore(code, baseOffset, false, false); + } + + public List<String> getUsed() { return new ArrayList<>(used); } + + /** + * Any time you call this method you should seriously consider adding a + * corresponding clear() call. + * + * @return + */ + public String getAny() { + // String availS = getS(); + // if (availS != null) { + // return availS; + // } + String t = getT(); + if (t == null) { + throw new RuntimeException("Out of registers"); + } + return t; + } + + public void clear(String reg) { + if (reg.charAt(1) == 's') { + s[Integer.parseInt(reg.substring(2))] = false; + } else if (reg.charAt(1) == 't') { + t[Integer.parseInt(reg.substring(2))] = false; + } else { + throw new RuntimeException("Unexpected register in clear"); + } + } + + public void clearAll() { + Arrays.fill(t, false); + Arrays.fill(s, false); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/SymbolInfo.java b/Homework/cs5300/compiler/p5-compiler/submit/SymbolInfo.java new file mode 100644 index 0000000..c640718 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/SymbolInfo.java @@ -0,0 +1,43 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit; + +import submit.ast.VarType; + +/** + * + * @author edwajohn + */ +public class SymbolInfo { + + private final String id; + // In the case of a function, type is the return type + private final VarType type; + private final boolean function; + private int offset; + + public SymbolInfo(String id, VarType type, boolean function) { + this.id = id; + this.type = type; + this.function = function; + } + + public SymbolInfo(String id, VarType type, boolean function, int offset) { + this(id, type, function); + this.offset = offset; + } + + @Override + public String toString() { + return "<" + id + ", " + type + '>'; + } + + public VarType getType() { return type; } + + public boolean isFunction() { return function; } + + public int getOffset() { return offset; } + public void setOffset(int offset) { this.offset = offset; } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/SymbolTable.java b/Homework/cs5300/compiler/p5-compiler/submit/SymbolTable.java new file mode 100644 index 0000000..09251bc --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/SymbolTable.java @@ -0,0 +1,118 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import submit.ast.VarType; + +/* + * Code formatter project + * CS 4481 + */ +/** + * + */ +public class SymbolTable { + + private final HashMap<String, SymbolInfo> table; + private SymbolTable parent; + private final List<SymbolTable> children; + + private int offset; + + public static int LABEL_IDENTIFIER = 0; + + public static String nextId() { + return Integer.toString(SymbolTable.LABEL_IDENTIFIER++); + } + + public SymbolTable() { + offset = 0; + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + + this.addGlobalSymbols(); + } + + public List<String> symbolNames() { return new ArrayList<>(table.keySet()); } + + public void addGlobalSymbols() { + SymbolInfo println = new SymbolInfo("println", null, true); + this.addSymbol("println", println); + } + + public void addSymbol(String id, SymbolInfo symbol) { table.put(id, symbol); } + + public int addOffset(int n) { + offset -= 4 * n; + return offset; + } + + public int getOffset() { return this.offset; } + + // Add symbols in before and reorder offsets such that symbols in before have + // a "higher" offset + public void addOtherTableBefore(SymbolTable before) { + offset = 0; + List<String> thisSymbols = symbolNames(); + + for (String id : before.symbolNames()) { + SymbolInfo symbol = before.find(id); + if (!symbol.isFunction()) { + addOffset(1); + symbol.setOffset(offset); + } + addSymbol(id, symbol); + } + + for (String id : thisSymbols) { + SymbolInfo symbol = table.get(id); + if (!symbol.isFunction()) { + addOffset(1); + table.get(id).setOffset(offset); + } + } + } + + public int offsetOf(String id) { + if (table.containsKey(id)) { + return table.get(id).getOffset(); + } + if (parent != null) { + return -parent.getOffset() + parent.offsetOf(id); + } + return 0; // This shouldn't happen :D + } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { return parent; } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/AbstractNode.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/AbstractNode.java new file mode 100644 index 0000000..0a53882 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/AbstractNode.java @@ -0,0 +1,15 @@ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +public class AbstractNode implements Node { + public void toCminus(StringBuilder sb, String prefix) { + } + + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, SymbolTable symbolTable, + RegisterAllocator regAllocator) { + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Assignment.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Assignment.java new file mode 100644 index 0000000..9c06cbe --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Assignment.java @@ -0,0 +1,58 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class Assignment extends AbstractNode implements Expression { + + private final Mutable mutable; + private final AssignmentType type; + private final Expression rhs; + + public Assignment(Mutable mutable, String assign, Expression rhs) { + this.mutable = mutable; + this.type = AssignmentType.fromString(assign); + this.rhs = rhs; + } + + public void toCminus(StringBuilder builder, final String prefix) { + mutable.toCminus(builder, prefix); + if (rhs != null) { + builder.append(" ").append(type.toString()).append(" "); + rhs.toCminus(builder, prefix); + } else { + builder.append(type.toString()); + } + } + + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator regAllocator) { + MIPSResult mut = mutable.toMIPS(code, data, symbolTable, regAllocator); + MIPSResult result = rhs.toMIPS(code, data, symbolTable, regAllocator); + + String registerWithAddressOfMutable = mut.getAddress(); + + if (result.getRegister() != null) { + code.append(String.format("sw %s 0(%s)\n", result.getRegister(), + registerWithAddressOfMutable)); + regAllocator.clear(result.getRegister()); + } else if (result.getAddress() != null) { + regAllocator.loadIntoRegister(result, code, result.getAddress()); + regAllocator.clear(result.getAddress()); + } + + regAllocator.clear(registerWithAddressOfMutable); + + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/AssignmentType.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/AssignmentType.java new file mode 100644 index 0000000..9dd2bdf --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/AssignmentType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum AssignmentType { + + EQUALS("="), PLUS("+="), MINUS("-="), TIMES("*="), DIVIDE("/="), INC("++"), DEC("--"); + + private final String value; + + private AssignmentType(String value) { + this.value = value; + } + + public static AssignmentType fromString(String s) { + for (AssignmentType at : AssignmentType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in AssignType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperator.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperator.java new file mode 100644 index 0000000..3afce78 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperator.java @@ -0,0 +1,89 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class BinaryOperator extends AbstractNode implements Expression { + + private final Expression lhs, rhs; + private final BinaryOperatorType type; + + public BinaryOperator(Expression lhs, BinaryOperatorType type, + Expression rhs) { + this.lhs = lhs; + this.type = type; + this.rhs = rhs; + } + + public BinaryOperator(Expression lhs, String type, Expression rhs) { + this.lhs = lhs; + this.type = BinaryOperatorType.fromString(type); + this.rhs = rhs; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + lhs.toCminus(builder, prefix); + builder.append(" ").append(type).append(" "); + rhs.toCminus(builder, prefix); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + + switch (type) { + case PLUS: + case MINUS: + case TIMES: + case DIVIDE: + MIPSResult left = lhs.toMIPS(code, data, symbolTable, registerAllocator); + String leftRegister = + registerAllocator.getRegisterOrLoadIntoRegister(left, code); + + MIPSResult right = rhs.toMIPS(code, data, symbolTable, registerAllocator); + String rightRegister = + registerAllocator.getRegisterOrLoadIntoRegister(right, code); + String resultRegister = registerAllocator.getAny(); + switch (type) { + case PLUS: + code.append(String.format("add %s %s %s\n", resultRegister, + leftRegister, rightRegister)); + break; + case MINUS: + code.append(String.format("sub %s %s %s\n", resultRegister, + leftRegister, rightRegister)); + break; + case TIMES: + code.append(String.format("mult %s %s\n", leftRegister, rightRegister)); + code.append(String.format("mflo %s\n", resultRegister)); + break; + case DIVIDE: + code.append(String.format("div %s %s\n", leftRegister, rightRegister)); + code.append(String.format("mflo %s\n", resultRegister)); + break; + default: + break; + } + + registerAllocator.clear(leftRegister); + registerAllocator.clear(rightRegister); + + return MIPSResult.createRegisterResult(resultRegister, VarType.INT); + default: + break; + } + + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperatorType.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperatorType.java new file mode 100644 index 0000000..fea3d6e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/BinaryOperatorType.java @@ -0,0 +1,45 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum BinaryOperatorType { + + OR("||"), + AND("&&"), + LE("<="), + LT("<"), + GT(">"), + GE(">="), + EQ("=="), + NE("!="), + PLUS("+"), + MINUS("-"), + TIMES("*"), + DIVIDE("/"), + MOD("%"); + + private final String value; + + private BinaryOperatorType(String value) { this.value = value; } + + public static BinaryOperatorType fromString(String s) { + for (BinaryOperatorType at : BinaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in OperatorType.fromString(): " + + s); + } + + @Override + public String toString() { + return value; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/BoolConstant.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/BoolConstant.java new file mode 100644 index 0000000..2a7a281 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/BoolConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BoolConstant extends AbstractNode implements Expression { + private final boolean value; + + public BoolConstant(boolean value) { this.value = value; } + + public void toCminus(StringBuilder builder, final String prefix) { + if (value) { + builder.append("true"); + } else { + builder.append("false"); + } + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Break.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Break.java new file mode 100644 index 0000000..0582c91 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Break.java @@ -0,0 +1,16 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Break extends AbstractNode implements Statement { + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("break;\n"); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Call.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Call.java new file mode 100644 index 0000000..7da86cd --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Call.java @@ -0,0 +1,139 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class Call implements Expression { + + private final String id; + private final List<Expression> args; + + public Call(String id, List<Expression> args) { + this.id = id; + this.args = new ArrayList<>(args); + } + + private void println(List<MIPSResult> mipsResults, StringBuilder code, + StringBuilder data, SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + code.append("# println\n"); + + for (MIPSResult result : mipsResults) { + if (result.getRegister() == null && result.getAddress() != null) { + if (result.getAddress().startsWith("$")) { + // Hack - might be a register + registerAllocator.loadIntoRegister(result, code, "$a0"); + } else { + code.append(String.format("la $a0 %s\n", result.getAddress())); + } + } else if (result.getRegister() != null) { + code.append(String.format("move $a0 %s\n", result.getRegister())); + } + + if (result.getType() == VarType.INT) { + code.append("li $v0 1\n"); + } else if (result.getType() == VarType.CHAR) { + code.append("li $v0 11\n"); + } else { + code.append("li $v0 4\n"); + } + code.append("syscall\n"); + } + + // End line with newline + code.append(String.format("la $a0 %s\n", "newline")); + code.append("li $v0 4\n"); + code.append("syscall\n"); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id).append("("); + for (Expression arg : args) { + arg.toCminus(builder, prefix); + builder.append(", "); + } + if (!args.isEmpty()) { + builder.setLength(builder.length() - 2); + } + builder.append(")"); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + List<MIPSResult> argMips; + String resultRegister = null; + + if (this.id.equals("println")) { + argMips = + args.stream() + .map(e -> e.toMIPS(code, data, symbolTable, registerAllocator)) + .collect(Collectors.toList()); + this.println(argMips, code, data, symbolTable, registerAllocator); + } else { + argMips = new ArrayList<MIPSResult>(); + String returnAddr = registerAllocator.getAny(); + code.append(String.format("move %s $ra\n", returnAddr)); + int savedRegistersOffset = registerAllocator.saveRestore( + code, -symbolTable.getOffset(), false, true); + + int argsOffset = symbolTable.getOffset() - savedRegistersOffset; + int i = argsOffset; + for (Expression expression : args) { + MIPSResult mipsResult = + expression.toMIPS(code, data, symbolTable, registerAllocator); + argMips.add(mipsResult); + i -= 4; + + String reg = + registerAllocator.getRegisterOrLoadIntoRegister(mipsResult, code); + code.append(String.format("sw %s %d($sp)\n", reg, i)); + + registerAllocator.clear(reg); + } + + code.append(String.format("add $sp $sp %d\n", argsOffset)); + + code.append(String.format("jal %s\n", id)); + + code.append(String.format("add $sp $sp %d\n", -argsOffset)); + + registerAllocator.saveRestore(code, -symbolTable.getOffset(), false, + false); + code.append(String.format("# symbol table size - %d\n", i)); + + resultRegister = registerAllocator.getAny(); + registerAllocator.loadIntoRegisterWithOffset( + MIPSResult.createAddressResult("$sp", VarType.INT), code, + resultRegister, i - 4); + + code.append(String.format("move $ra %s\n", returnAddr)); + registerAllocator.clear(returnAddr); + } + + for (MIPSResult arg : argMips) + if (arg.getRegister() != null) + registerAllocator.clear(arg.getRegister()); + + if (resultRegister != null) { + VarType returnType = symbolTable.find(id).getType(); + return MIPSResult.createRegisterResult(resultRegister, returnType); + } + + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/CharConstant.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/CharConstant.java new file mode 100644 index 0000000..5334be2 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/CharConstant.java @@ -0,0 +1,19 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class CharConstant extends AbstractNode implements Expression { + private final char value; + + public CharConstant(char value) { this.value = value; } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("'").append(value).append("'"); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/CompoundStatement.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/CompoundStatement.java new file mode 100644 index 0000000..4d1c666 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/CompoundStatement.java @@ -0,0 +1,59 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.List; +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class CompoundStatement extends AbstractNode implements Statement { + + private final List<Statement> statements; + private SymbolTable symbolTable; + + public CompoundStatement(List<Statement> statements, + SymbolTable symbolTable) { + this.statements = statements; + this.symbolTable = symbolTable; + } + + public SymbolTable getSymbolTable() { return symbolTable; } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("{\n"); + for (Statement s : statements) { + s.toCminus(builder, prefix + " "); + } + builder.append(prefix).append("}\n"); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + code.append("# Entering a new scope.\n"); + code.append("# Symbols in symbol table:\n"); + for (String name : this.symbolTable.symbolNames()) + code.append( + String.format("# %s : %d\n", name, this.symbolTable.offsetOf(name))); + + code.append("# Update the stack pointer.\n"); + code.append(String.format("addi $sp $sp %d\n", symbolTable.getOffset())); + + for (Statement statement : statements) + statement.toMIPS(code, data, this.symbolTable, registerAllocator); + + code.append("# Exit scope.\n"); + code.append(String.format("addi $sp $sp %s\n", -symbolTable.getOffset())); + + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Declaration.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Declaration.java new file mode 100644 index 0000000..99c1c1a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Declaration.java @@ -0,0 +1,11 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Declaration extends Statement {} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Expression.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Expression.java new file mode 100644 index 0000000..d31753a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Expression.java @@ -0,0 +1,11 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Expression extends Node {} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/ExpressionStatement.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/ExpressionStatement.java new file mode 100644 index 0000000..7870a2e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/ExpressionStatement.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class ExpressionStatement extends AbstractNode implements Statement { + + private final Expression expression; + + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + expression.toCminus(builder, prefix); + builder.append(";\n"); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + expression.toMIPS(code, data, symbolTable, registerAllocator); + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/FunDeclaration.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/FunDeclaration.java new file mode 100644 index 0000000..f60840b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/FunDeclaration.java @@ -0,0 +1,65 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class FunDeclaration extends AbstractNode implements Declaration { + + private final VarType returnType; + private final String id; + private final ArrayList<Param> params; + private final Statement statement; + + public FunDeclaration(VarType returnType, String id, List<Param> params, + Statement statement) { + this.returnType = returnType; + this.id = id; + this.params = new ArrayList<>(params); + this.statement = statement; + } + + public void toCminus(StringBuilder builder, final String prefix) { + String rt = (returnType != null) ? returnType.toString() : "void"; + builder.append("\n").append(rt).append(" "); + builder.append(id); + builder.append("("); + + for (Param param : params) { + param.toCminus(builder, prefix); + builder.append(", "); + } + if (!params.isEmpty()) { + builder.delete(builder.length() - 2, builder.length()); + } + builder.append(")\n"); + statement.toCminus(builder, prefix); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + code.append(id).append(":\n"); + + statement.toMIPS(code, data, symbolTable, registerAllocator); + + if (id.equals("main")) + code.append("li $v0 10\nsyscall\n"); + else + code.append("jr $ra\n"); + registerAllocator.clearAll(); + + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/If.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/If.java new file mode 100644 index 0000000..1a481ed --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/If.java @@ -0,0 +1,45 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class If extends AbstractNode implements Statement { + + private final Expression expression; + private final Statement trueStatement; + private final Statement falseStatement; + + public If(Expression expression, Statement trueStatement, + Statement falseStatement) { + this.expression = expression; + this.trueStatement = trueStatement; + this.falseStatement = falseStatement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("if ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (trueStatement instanceof CompoundStatement) { + trueStatement.toCminus(builder, prefix); + } else { + trueStatement.toCminus(builder, prefix + " "); + } + if (falseStatement != null) { + builder.append(prefix).append("else\n"); + // falseStatement.toCminus(builder, prefix); + if (falseStatement instanceof CompoundStatement) { + falseStatement.toCminus(builder, prefix); + } else { + falseStatement.toCminus(builder, prefix + " "); + } + } + // builder.append(prefix).append("}"); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Mutable.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Mutable.java new file mode 100644 index 0000000..ac9b7ff --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Mutable.java @@ -0,0 +1,45 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class Mutable implements Expression { + + private final String id; + private final Expression index; + + public Mutable(String id, Expression index) { + this.id = id; + this.index = index; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id); + if (index != null) { + builder.append("["); + index.toCminus(builder, prefix); + builder.append("]"); + } + } + + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator regAllocator) { + String stackReg = regAllocator.getAny(); + + code.append(String.format("li %s %d\n", stackReg, symbolTable.offsetOf(id))) + .append(String.format("add %s %s $sp\n", stackReg, stackReg)); + + return MIPSResult.createAddressResult(stackReg, VarType.INT); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Node.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Node.java new file mode 100644 index 0000000..cc6c2d5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Node.java @@ -0,0 +1,12 @@ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +public interface Node { + void toCminus(StringBuilder builder, final String prefix); + + MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, RegisterAllocator regAllocator); +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/NumConstant.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/NumConstant.java new file mode 100644 index 0000000..3389238 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/NumConstant.java @@ -0,0 +1,34 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class NumConstant extends AbstractNode implements Expression { + + private final int value; + + public NumConstant(int value) { this.value = value; } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(Integer.toString(value)); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + String register = registerAllocator.getAny(); + code.append(String.format("li %s %d\n", register, value)); + + return MIPSResult.createRegisterResult(register, VarType.INT); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Param.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Param.java new file mode 100644 index 0000000..921b179 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Param.java @@ -0,0 +1,36 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Param extends AbstractNode implements Node { + + private final VarType type; + private final String id; + private final boolean array; + + public Param(VarType type, String id, boolean array) { + this.type = type; + this.id = id; + this.array = array; + } + + public VarType getType() { return type; } + + public String getId() { return id; } + + public boolean isArray() { return array; } + + public void toCminus(StringBuilder builder, final String prefix) { + if (isArray()) { + builder.append(type).append(" ").append(id).append("[]"); + } else { + builder.append(type).append(" ").append(id); + } + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/ParenExpression.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/ParenExpression.java new file mode 100644 index 0000000..e61dfe3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/ParenExpression.java @@ -0,0 +1,36 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class ParenExpression extends AbstractNode implements Expression { + + private final Expression expression; + + public ParenExpression(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append("("); + expression.toCminus(builder, prefix); + builder.append(")"); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + return expression.toMIPS(code, data, symbolTable, registerAllocator); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Program.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Program.java new file mode 100644 index 0000000..e32a7d2 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Program.java @@ -0,0 +1,50 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class Program extends AbstractNode implements Node { + + private ArrayList<Declaration> declarations; + + public Program(List<Declaration> declarations) { + this.declarations = new ArrayList<>(declarations); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + toCminus(builder, ""); + return builder.toString(); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + for (Declaration declaration : declarations) { + declaration.toCminus(builder, ""); + } + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + // Store newline in .data + data.append("newline: .asciiz \"\\n\"\n"); + + for (Declaration declaration : declarations) + declaration.toMIPS(code, data, symbolTable, registerAllocator); + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Return.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Return.java new file mode 100644 index 0000000..b5dd761 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Return.java @@ -0,0 +1,46 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class Return extends AbstractNode implements Statement { + private final Expression expr; + + public Return(Expression expr) { this.expr = expr; } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expr == null) { + builder.append("return;\n"); + } else { + builder.append("return "); + expr.toCminus(builder, prefix); + builder.append(";\n"); + } + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator regAllocator) { + if (expr != null) { + String reg = regAllocator.getRegisterOrLoadIntoRegister( + expr.toMIPS(code, data, symbolTable, regAllocator), code); + code.append( + String.format("# symbol table size - %d\n", symbolTable.getOffset())); + code.append( + String.format("sw %s %d($sp)\n", reg, symbolTable.getOffset())); + } + return MIPSResult.createVoidResult(); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/Statement.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/Statement.java new file mode 100644 index 0000000..2daeb9d --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/Statement.java @@ -0,0 +1,18 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; + +/** + * + * @author edwajohn + */ +public interface Statement extends Node { + public static CompoundStatement empty() { + submit.SymbolTable newTable = new submit.SymbolTable(); + return new CompoundStatement(new ArrayList<>(), newTable); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/StringConstant.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/StringConstant.java new file mode 100644 index 0000000..bd4c37d --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/StringConstant.java @@ -0,0 +1,34 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class StringConstant extends AbstractNode implements Expression { + + private final String value; + + public StringConstant(String value) { this.value = value; } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("\"").append(value).append("\""); + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator registerAllocator) { + String label = String.format("string_%s", SymbolTable.nextId()); + data.append(String.format("%s: .asciiz %s\n", label, value)); + + return MIPSResult.createAddressResult(label, null); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperator.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperator.java new file mode 100644 index 0000000..799a0be --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperator.java @@ -0,0 +1,54 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +/** + * + * @author edwajohn + */ +public class UnaryOperator extends AbstractNode implements Expression { + + private final UnaryOperatorType type; + private final Expression expression; + + public UnaryOperator(String type, Expression expression) { + this.type = UnaryOperatorType.fromString(type); + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(type); + expression.toCminus(builder, prefix); + } + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator regAllocator) { + MIPSResult result = + expression.toMIPS(code, data, symbolTable, regAllocator); + + if (type == UnaryOperatorType.NEG) { + code.append("# Negation\n"); + if (result.getAddress() != null) { + String reg = regAllocator.getRegisterOrLoadIntoRegister(result, code); + + code.append(String.format("sub %s, $zero, %s\n", reg, reg)) + .append(String.format("sw %s 0(%s)\n", reg, result.getAddress())); + + regAllocator.clear(reg); + } else { + code.append(String.format("sub %s, $zero, %s\n", result.getRegister(), + result.getRegister())); + } + } + + return result; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperatorType.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperatorType.java new file mode 100644 index 0000000..828ee5a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/UnaryOperatorType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum UnaryOperatorType { + + NOT("!"), NEG("-"), DEREF("*"), QUESTION("?"); + + private final String value; + + private UnaryOperatorType(String value) { + this.value = value; + } + + public static UnaryOperatorType fromString(String s) { + for (UnaryOperatorType at : UnaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in UnaryOperatorType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..809518f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java @@ -0,0 +1,48 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class VarDeclaration extends AbstractNode implements Declaration { + + private final VarType type; + private final List<String> ids; + private final List<Integer> arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List<String> ids, + List<Integer> arraySizes, boolean isStatic) { + this.type = type; + this.ids = new ArrayList<>(ids); + this.arraySizes = arraySizes; + this.isStatic = isStatic; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + if (isStatic) { + builder.append("static "); + } + builder.append(type).append(" "); + for (int i = 0; i < ids.size(); ++i) { + final String id = ids.get(i); + final int arraySize = arraySizes.get(i); + if (arraySize >= 0) { + builder.append(id).append("[").append(arraySize).append("]").append( + ", "); + } else { + builder.append(id).append(", "); + } + } + builder.delete(builder.length() - 2, builder.length()); + builder.append(";\n"); + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/VarType.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarType.java new file mode 100644 index 0000000..e797d50 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarType.java @@ -0,0 +1,34 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum VarType { + + INT("int"), + BOOL("bool"), + CHAR("char"); + + private final String value; + + private VarType(String value) { this.value = value; } + + public static VarType fromString(String s) { + for (VarType vt : VarType.values()) { + if (vt.value.equals(s)) { + return vt; + } + } + throw new RuntimeException("Illegal string in VarType.fromString()"); + } + + @Override + public String toString() { + return value; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/While.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/While.java new file mode 100644 index 0000000..70b8e9f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/While.java @@ -0,0 +1,32 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class While extends AbstractNode implements Statement { + + private final Expression expression; + private final Statement statement; + + public While(Expression expression, Statement statement) { + this.expression = expression; + this.statement = statement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("while ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (statement instanceof CompoundStatement) { + statement.toCminus(builder, prefix); + } else { + statement.toCminus(builder, prefix + " "); + } + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/test.sh b/Homework/cs5300/compiler/p5-compiler/test.sh new file mode 100644 index 0000000..03fad0a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/test.sh @@ -0,0 +1,5 @@ +#!/usr/bin/zsh + +javac -d classes -cp classes -cp *.jar **/*.java && java -cp "classes:antlr-4.9.1-complete.jar" main.Main data/test$1.c + +java -jar data/examples/Mars4_5.jar data/test$1.asm diff --git a/Homework/cs5300/compiler/submit/ASTVisitor.java b/Homework/cs5300/compiler/submit/ASTVisitor.java new file mode 100644 index 0000000..cbd794f --- /dev/null +++ b/Homework/cs5300/compiler/submit/ASTVisitor.java @@ -0,0 +1,276 @@ +package submit; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.TerminalNode; +import parser.CminusBaseVisitor; +import parser.CminusParser; +import submit.ast.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +public class ASTVisitor extends CminusBaseVisitor<Node> { + private final Logger LOGGER; + private SymbolTable symbolTable; + + public ASTVisitor(Logger LOGGER) { + this.LOGGER = LOGGER; + } + + private VarType getVarType(CminusParser.TypeSpecifierContext ctx) { + final String t = ctx.getText(); + return (t.equals("int")) ? VarType.INT : (t.equals("bool")) ? VarType.BOOL : VarType.CHAR; + } + + @Override public Node visitProgram(CminusParser.ProgramContext ctx) { + symbolTable = new SymbolTable(); + List<Declaration> decls = new ArrayList<>(); + for (CminusParser.DeclarationContext d : ctx.declaration()) { + decls.add((Declaration) visitDeclaration(d)); + } + return new Program(decls); + } + + @Override public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + List<String> ids = new ArrayList<>(); + List<Integer> arraySizes = new ArrayList<>(); + for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) { + String id = v.ID().getText(); + ids.add(id); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + if (v.NUMCONST() != null) { + arraySizes.add(Integer.parseInt(v.NUMCONST().getText())); + } else { + arraySizes.add(-1); + } + } + final boolean isStatic = false; + return new VarDeclaration(type, ids, arraySizes, isStatic); + } + + @Override public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { + VarType returnType = null; + if (ctx.typeSpecifier() != null) { + returnType = getVarType(ctx.typeSpecifier()); + } + String id = ctx.ID().getText(); + List<Param> params = new ArrayList<>(); + for (CminusParser.ParamContext p : ctx.param()) { + params.add((Param) visitParam(p)); + } + Statement statement = (Statement) visitStatement(ctx.statement()); + symbolTable.addSymbol(id, new SymbolInfo(id, returnType, true)); + return new FunDeclaration(returnType, id, params, statement); + } + + @Override public Node visitParam(CminusParser.ParamContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + String id = ctx.paramId().ID().getText(); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + return new Param(type, id, ctx.paramId().children.size() > 1); + } + + @Override public Node visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { + symbolTable = symbolTable.createChild(); + List<Statement> statements = new ArrayList<>(); + for (CminusParser.VarDeclarationContext d : ctx.varDeclaration()) { + statements.add((VarDeclaration) visitVarDeclaration(d)); + } + for (CminusParser.StatementContext d : ctx.statement()) { + statements.add((Statement) visitStatement(d)); + } + symbolTable = symbolTable.getParent(); + return new CompoundStatement(statements); + } + + @Override public Node visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { + if (ctx.expression() == null) { + return Statement.empty(); + } + return new ExpressionStatement((Expression) visitExpression(ctx.expression())); + } + + @Override public Node visitIfStmt(CminusParser.IfStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement trueStatement = (Statement) visitStatement(ctx.statement(0)); + Statement falseStatement = null; + if (ctx.statement().size() > 1) { + falseStatement = (Statement) visitStatement(ctx.statement(1)); + } + return new If(expression, trueStatement, falseStatement); + } + + @Override public Node visitWhileStmt(CminusParser.WhileStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement statement = (Statement) visitStatement(ctx.statement()); + return new While(expression, statement); + } + + @Override public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) { + if (ctx.expression() != null) { + return new Return((Expression) visitExpression(ctx.expression())); + } + return new Return(null); + } + + @Override public Node visitBreakStmt(CminusParser.BreakStmtContext ctx) { + return new Break(); + } + + @Override public Node visitExpression(CminusParser.ExpressionContext ctx) { + final Node ret; + CminusParser.MutableContext mutable = ctx.mutable(); + CminusParser.ExpressionContext expression = ctx.expression(); + if (mutable != null) { + // Assignment + ParseTree operator = ctx.getChild(1); + Mutable lhs = (Mutable) visitMutable(mutable);//new Mutable(mutable.ID().getText(), (Expression) visitExpression(mutable.expression())); + Expression rhs = null; + if (expression != null) { + rhs = (Expression) visitExpression(expression); + } + ret = new Assignment(lhs, operator.getText(), rhs); + } else { + ret = visitSimpleExpression(ctx.simpleExpression()); + } + return ret; + } + + @Override public Node visitOrExpression(CminusParser.OrExpressionContext ctx) { + List<Node> ands = new ArrayList<>(); + for (CminusParser.AndExpressionContext and : ctx.andExpression()) { + ands.add(visitAndExpression(and)); + } + if (ands.size() == 1) { + return ands.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)ands.get(0), "||", (Expression)ands.get(1)); + for (int i = 2; i < ands.size(); ++i) { + op = new BinaryOperator(op, "||", (Expression) ands.get(i)); + } + return op; + } + + @Override public Node visitAndExpression(CminusParser.AndExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.UnaryRelExpressionContext un : ctx.unaryRelExpression()) { + uns.add(visitUnaryRelExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)uns.get(0), "&&", (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, "&&", (Expression) uns.get(i)); + } + return op; + } + + @Override public Node visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { + Expression e = (Expression)visitRelExpression(ctx.relExpression()); + for (TerminalNode n : ctx.BANG()) { + e = new UnaryOperator("!", e); + } + return e; + } + + @Override public Node visitRelExpression(CminusParser.RelExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.SumExpressionContext un : ctx.sumExpression()) { + uns.add(visitSumExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)uns.get(0), ctx.relop(0).getText(), (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, ctx.relop(i-1).getText(), (Expression) uns.get(i)); + } + return op; + } + + @Override public Node visitSumExpression(CminusParser.SumExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.TermExpressionContext e : ctx.termExpression()) { + es.add(visitTermExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)es.get(0), ctx.sumop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.sumop(i-1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override public Node visitTermExpression(CminusParser.TermExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.UnaryExpressionContext e : ctx.unaryExpression()) { + es.add(visitUnaryExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)es.get(0), ctx.mulop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.mulop(i-1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override public Node visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { + Node ret = visitFactor(ctx.factor()); + for (int i = ctx.unaryop().size()-1; i >= 0; i--) { + ret = new UnaryOperator(ctx.unaryop(i).getText(), (Expression)ret); + } + return ret; + } + + @Override public Node visitMutable(CminusParser.MutableContext ctx) { + Expression e = null; + if (ctx.expression() != null) { + e = (Expression) visitExpression(ctx.expression()); + } + String id = ctx.ID().getText(); + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Mutable(id, e); + } + + @Override public Node visitImmutable(CminusParser.ImmutableContext ctx) { + if (ctx.expression() != null) { + return new ParenExpression((Expression) visitExpression(ctx.expression())); + } + return visitChildren(ctx); + } + + @Override public Node visitCall(CminusParser.CallContext ctx) { + final String id = ctx.ID().getText(); + final List<Expression> args = new ArrayList<>(); + for (CminusParser.ExpressionContext e : ctx.expression()) { + args.add((Expression) visitExpression(e)); + } + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Call(id, args); + } + + @Override public Node visitConstant(CminusParser.ConstantContext ctx) { + final Node node; + if (ctx.NUMCONST() != null) { + node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText())); + } else if (ctx.CHARCONST() != null) { + node = new CharConstant(ctx.CHARCONST().getText().charAt(0)); + } else if (ctx.STRINGCONST() != null) { + node = new StringConstant(ctx.STRINGCONST().getText()); + } else { + node = new BoolConstant(ctx.getText().equals("true")); + } + return node; + } +} diff --git a/Homework/cs5300/compiler/submit/SymbolInfo.java b/Homework/cs5300/compiler/submit/SymbolInfo.java new file mode 100644 index 0000000..19c3f9b --- /dev/null +++ b/Homework/cs5300/compiler/submit/SymbolInfo.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit; + +import submit.ast.VarType; + +/** + * + * @author edwajohn + */ +public class SymbolInfo { + + private final String id; + // In the case of a function, type is the return type + private final VarType type; + private final boolean function; + + public SymbolInfo(String id, VarType type, boolean function) { + this.id = id; + this.type = type; + this.function = function; + } + + @Override + public String toString() { + return "<" + id + ", " + type + '>'; + } + +} diff --git a/Homework/cs5300/compiler/submit/SymbolTable.java b/Homework/cs5300/compiler/submit/SymbolTable.java new file mode 100644 index 0000000..7ca6b27 --- /dev/null +++ b/Homework/cs5300/compiler/submit/SymbolTable.java @@ -0,0 +1,63 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/* + * Code formatter project + * CS 4481 + */ +/** + * + */ +public class SymbolTable { + + private final HashMap<String, SymbolInfo> table; + private SymbolTable parent; + private final List<SymbolTable> children; + + public SymbolTable() { + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + } + + public void addSymbol(String id, SymbolInfo symbol) { + table.put(id, symbol); + } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { + return parent; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Assignment.java b/Homework/cs5300/compiler/submit/ast/Assignment.java new file mode 100644 index 0000000..9edc536 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Assignment.java @@ -0,0 +1,34 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Assignment implements Expression, Node { + + private final Mutable mutable; + private final AssignmentType type; + private final Expression rhs; + + public Assignment(Mutable mutable, String assign, Expression rhs) { + this.mutable = mutable; + this.type = AssignmentType.fromString(assign); + this.rhs = rhs; + } + + public void toCminus(StringBuilder builder, final String prefix) { + mutable.toCminus(builder, prefix); + if (rhs != null) { + builder.append(" ").append(type.toString()).append(" "); + rhs.toCminus(builder, prefix); + } else { + builder.append(type.toString()); + + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/AssignmentType.java b/Homework/cs5300/compiler/submit/ast/AssignmentType.java new file mode 100644 index 0000000..9dd2bdf --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/AssignmentType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum AssignmentType { + + EQUALS("="), PLUS("+="), MINUS("-="), TIMES("*="), DIVIDE("/="), INC("++"), DEC("--"); + + private final String value; + + private AssignmentType(String value) { + this.value = value; + } + + public static AssignmentType fromString(String s) { + for (AssignmentType at : AssignmentType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in AssignType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BinaryOperator.java b/Homework/cs5300/compiler/submit/ast/BinaryOperator.java new file mode 100644 index 0000000..747b3ba --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BinaryOperator.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BinaryOperator implements Expression { + + private final Expression lhs, rhs; + private final BinaryOperatorType type; + + public BinaryOperator(Expression lhs, BinaryOperatorType type, Expression rhs) { + this.lhs = lhs; + this.type = type; + this.rhs = rhs; + } + + public BinaryOperator(Expression lhs, String type, Expression rhs) { + this.lhs = lhs; + this.type = BinaryOperatorType.fromString(type); + this.rhs = rhs; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + lhs.toCminus(builder, prefix); + builder.append(" ").append(type).append(" "); + rhs.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java b/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java new file mode 100644 index 0000000..37235c8 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum BinaryOperatorType { + + OR("||"), AND("&&"), + LE("<="), LT("<"), GT(">"), GE(">="), EQ("=="), NE("!="), + PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"), MOD("%"); + + private final String value; + + private BinaryOperatorType(String value) { + this.value = value; + } + + public static BinaryOperatorType fromString(String s) { + for (BinaryOperatorType at : BinaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in OperatorType.fromString(): " + s); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BoolConstant.java b/Homework/cs5300/compiler/submit/ast/BoolConstant.java new file mode 100644 index 0000000..963bebe --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BoolConstant.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BoolConstant implements Expression { + + private final boolean value; + + public BoolConstant(boolean value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (value) { + builder.append("true"); + } else { + builder.append("false"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Break.java b/Homework/cs5300/compiler/submit/ast/Break.java new file mode 100644 index 0000000..006d4d3 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Break.java @@ -0,0 +1,18 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Break implements Statement { + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("break;\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Call.java b/Homework/cs5300/compiler/submit/ast/Call.java new file mode 100644 index 0000000..700799b --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Call.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Call implements Expression { + + private final String id; + private final List<Expression> args; + + public Call(String id, List<Expression> args) { + this.id = id; + this.args = new ArrayList<>(args); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id).append("("); + for (Expression arg : args) { + arg.toCminus(builder, prefix); + builder.append(", "); + } + if (!args.isEmpty()) { + builder.setLength(builder.length() - 2); + } + builder.append(")"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/CharConstant.java b/Homework/cs5300/compiler/submit/ast/CharConstant.java new file mode 100644 index 0000000..8f41fac --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/CharConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class CharConstant implements Expression { + + private final char value; + + public CharConstant(char value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("'").append(value).append("'"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/CompoundStatement.java b/Homework/cs5300/compiler/submit/ast/CompoundStatement.java new file mode 100644 index 0000000..f745a26 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/CompoundStatement.java @@ -0,0 +1,30 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.List; + +/** + * + * @author edwajohn + */ +public class CompoundStatement implements Statement { + + private final List<Statement> statements; + + public CompoundStatement(List<Statement> statements) { + this.statements = statements; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("{\n"); + for (Statement s : statements) { + s.toCminus(builder, prefix + " "); + } + builder.append(prefix).append("}\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Declaration.java b/Homework/cs5300/compiler/submit/ast/Declaration.java new file mode 100644 index 0000000..c70e856 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Declaration.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Declaration extends Statement, Node { + +} diff --git a/Homework/cs5300/compiler/submit/ast/Expression.java b/Homework/cs5300/compiler/submit/ast/Expression.java new file mode 100644 index 0000000..910f6f0 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Expression.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Expression extends Node { + +} diff --git a/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java b/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java new file mode 100644 index 0000000..c0a647a --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ExpressionStatement implements Statement { + + private final Expression expression; + + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + expression.toCminus(builder, prefix); + builder.append(";\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/FunDeclaration.java b/Homework/cs5300/compiler/submit/ast/FunDeclaration.java new file mode 100644 index 0000000..39ced42 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/FunDeclaration.java @@ -0,0 +1,46 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class FunDeclaration implements Declaration, Node { + + private final VarType returnType; + private final String id; + private final ArrayList<Param> params; + private final Statement statement; + + public FunDeclaration(VarType returnType, String id, List<Param> params, + Statement statement) { + this.returnType = returnType; + this.id = id; + this.params = new ArrayList<>(params); + this.statement = statement; + } + + public void toCminus(StringBuilder builder, final String prefix) { + String rt = (returnType != null) ? returnType.toString() : "void"; + builder.append("\n").append(rt).append(" "); + builder.append(id); + builder.append("("); + + for (Param param : params) { + param.toCminus(builder, prefix); + builder.append(", "); + } + if (!params.isEmpty()) { + builder.delete(builder.length() - 2, builder.length()); + } + builder.append(")\n"); + statement.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/If.java b/Homework/cs5300/compiler/submit/ast/If.java new file mode 100644 index 0000000..ee62e30 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/If.java @@ -0,0 +1,44 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class If implements Statement { + + private final Expression expression; + private final Statement trueStatement; + private final Statement falseStatement; + + public If(Expression expression, Statement trueStatement, Statement falseStatement) { + this.expression = expression; + this.trueStatement = trueStatement; + this.falseStatement = falseStatement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("if ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (trueStatement instanceof CompoundStatement) { + trueStatement.toCminus(builder, prefix); + } else { + trueStatement.toCminus(builder, prefix + " "); + } + if (falseStatement != null) { + builder.append(prefix).append("else\n"); +// falseStatement.toCminus(builder, prefix); + if (falseStatement instanceof CompoundStatement) { + falseStatement.toCminus(builder, prefix); + } else { + falseStatement.toCminus(builder, prefix + " "); + } + } +// builder.append(prefix).append("}"); + } +} diff --git a/Homework/cs5300/compiler/submit/ast/Mutable.java b/Homework/cs5300/compiler/submit/ast/Mutable.java new file mode 100644 index 0000000..c7ef089 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Mutable.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Mutable implements Expression, Node { + + private final String id; + private final Expression index; + + public Mutable(String id, Expression index) { + this.id = id; + this.index = index; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id); + if (index != null) { + builder.append("["); + index.toCminus(builder, prefix); + builder.append("]"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Node.java b/Homework/cs5300/compiler/submit/ast/Node.java new file mode 100644 index 0000000..455bb3f --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Node.java @@ -0,0 +1,5 @@ +package submit.ast; + +public interface Node { + void toCminus(StringBuilder builder, final String prefix); +} diff --git a/Homework/cs5300/compiler/submit/ast/NumConstant.java b/Homework/cs5300/compiler/submit/ast/NumConstant.java new file mode 100644 index 0000000..991d7d9 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/NumConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class NumConstant implements Expression, Node { + + private final int value; + + public NumConstant(int value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(Integer.toString(value)); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Param.java b/Homework/cs5300/compiler/submit/ast/Param.java new file mode 100644 index 0000000..215bdb6 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Param.java @@ -0,0 +1,43 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Param implements Node { + + private final VarType type; + private final String id; + private final boolean array; + + public Param(VarType type, String id, boolean array) { + this.type = type; + this.id = id; + this.array = array; + } + + public VarType getType() { + return type; + } + + public String getId() { + return id; + } + + public boolean isArray() { + return array; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (isArray()) { + builder.append(type).append(" ").append(id).append("[]"); + } else { + builder.append(type).append(" ").append(id); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/ParenExpression.java b/Homework/cs5300/compiler/submit/ast/ParenExpression.java new file mode 100644 index 0000000..63dce8d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/ParenExpression.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ParenExpression implements Expression { + + private final Expression expression; + + public ParenExpression(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append("("); + expression.toCminus(builder, prefix); + builder.append(")"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Program.java b/Homework/cs5300/compiler/submit/ast/Program.java new file mode 100644 index 0000000..0a8b44d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Program.java @@ -0,0 +1,36 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Program implements Node { + + private ArrayList<Declaration> declarations; + + public Program(List<Declaration> declarations) { + this.declarations = new ArrayList<>(declarations); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + toCminus(builder, ""); + return builder.toString(); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + for (Declaration declaration : declarations) { + declaration.toCminus(builder, ""); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Return.java b/Homework/cs5300/compiler/submit/ast/Return.java new file mode 100644 index 0000000..c1f9d4d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Return.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Return implements Statement { + + private final Expression expr; + + public Return(Expression expr) { + this.expr = expr; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expr == null) { + builder.append("return;\n"); + } else { + builder.append("return "); + expr.toCminus(builder, prefix); + builder.append(";\n"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Statement.java b/Homework/cs5300/compiler/submit/ast/Statement.java new file mode 100644 index 0000000..2e0581e --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Statement.java @@ -0,0 +1,16 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; + +/** + * + * @author edwajohn + */ +public interface Statement extends Node { + public static CompoundStatement empty() { return new CompoundStatement(new ArrayList<>()); } + +} diff --git a/Homework/cs5300/compiler/submit/ast/StringConstant.java b/Homework/cs5300/compiler/submit/ast/StringConstant.java new file mode 100644 index 0000000..cac6bee --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/StringConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class StringConstant implements Expression { + + private final String value; + + public StringConstant(String value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("\"").append(value).append("\""); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/UnaryOperator.java b/Homework/cs5300/compiler/submit/ast/UnaryOperator.java new file mode 100644 index 0000000..47d69af --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/UnaryOperator.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class UnaryOperator implements Expression { + + private final UnaryOperatorType type; + private final Expression expression; + + public UnaryOperator(String type, Expression expression) { + this.type = UnaryOperatorType.fromString(type); + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(type); + expression.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java b/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java new file mode 100644 index 0000000..828ee5a --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum UnaryOperatorType { + + NOT("!"), NEG("-"), DEREF("*"), QUESTION("?"); + + private final String value; + + private UnaryOperatorType(String value) { + this.value = value; + } + + public static UnaryOperatorType fromString(String s) { + for (UnaryOperatorType at : UnaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in UnaryOperatorType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/VarDeclaration.java b/Homework/cs5300/compiler/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..9006619 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/VarDeclaration.java @@ -0,0 +1,47 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class VarDeclaration implements Declaration, Node { + + private final VarType type; + private final List<String> ids; + private final List<Integer> arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List<String> ids, List<Integer> arraySizes, boolean isStatic) { + this.type = type; + this.ids = new ArrayList<>(ids); + this.arraySizes = arraySizes; + this.isStatic = isStatic; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + if (isStatic) { + builder.append("static "); + } + builder.append(type).append(" "); + for (int i = 0; i < ids.size(); ++i) { + final String id = ids.get(i); + final int arraySize = arraySizes.get(i); + if (arraySize >= 0) { + builder.append(id).append("[").append(arraySize).append("]").append(", "); + } else { + builder.append(id).append(", "); + } + } + builder.delete(builder.length() - 2, builder.length()); + builder.append(";\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/VarType.java b/Homework/cs5300/compiler/submit/ast/VarType.java new file mode 100644 index 0000000..ba28946 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/VarType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum VarType { + + INT("int"), BOOL("bool"), CHAR("char"); + + private final String value; + + private VarType(String value) { + this.value = value; + } + + public static VarType fromString(String s) { + for (VarType vt : VarType.values()) { + if (vt.value.equals(s)) { + return vt; + } + } + throw new RuntimeException("Illegal string in VarType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/While.java b/Homework/cs5300/compiler/submit/ast/While.java new file mode 100644 index 0000000..9dcbb5b --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/While.java @@ -0,0 +1,33 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class While implements Statement { + + private final Expression expression; + private final Statement statement; + + public While(Expression expression, Statement statement) { + this.expression = expression; + this.statement = statement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("while ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (statement instanceof CompoundStatement) { + statement.toCminus(builder, prefix); + } else { + statement.toCminus(builder, prefix + " "); + } + + } +} diff --git a/Homework/cs5300/homework-eight/1.png b/Homework/cs5300/homework-eight/1.png Binary files differnew file mode 100644 index 0000000..dd79142 --- /dev/null +++ b/Homework/cs5300/homework-eight/1.png diff --git a/Homework/cs5300/homework-eight/2a.png b/Homework/cs5300/homework-eight/2a.png Binary files differnew file mode 100644 index 0000000..244afcc --- /dev/null +++ b/Homework/cs5300/homework-eight/2a.png diff --git a/Homework/cs5300/homework-eight/2b.png b/Homework/cs5300/homework-eight/2b.png Binary files differnew file mode 100644 index 0000000..f65006e --- /dev/null +++ b/Homework/cs5300/homework-eight/2b.png diff --git a/Homework/cs5300/homework-eight/3.png b/Homework/cs5300/homework-eight/3.png Binary files differnew file mode 100644 index 0000000..4ad1fc7 --- /dev/null +++ b/Homework/cs5300/homework-eight/3.png diff --git a/Homework/cs5300/homework-eight/4a.png b/Homework/cs5300/homework-eight/4a.png Binary files differnew file mode 100644 index 0000000..80bc11c --- /dev/null +++ b/Homework/cs5300/homework-eight/4a.png diff --git a/Homework/cs5300/homework-eight/4b.png b/Homework/cs5300/homework-eight/4b.png Binary files differnew file mode 100644 index 0000000..ed0a4c7 --- /dev/null +++ b/Homework/cs5300/homework-eight/4b.png diff --git a/Homework/cs5300/homework-eight/compilers_assn_8.org b/Homework/cs5300/homework-eight/compilers_assn_8.org new file mode 100644 index 0000000..8cf9e69 --- /dev/null +++ b/Homework/cs5300/homework-eight/compilers_assn_8.org @@ -0,0 +1,28 @@ +#+TITLE: Assignment Eight +#+AUTHOR: Lizzy Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +#+LATEX: \setlength\parindent{0pt} +#+LATEX_COMPILER: lualatex +#+OPTIONS: toc:nil + +* Question One +#+attr_latex: :width 240px +[[./1.png]] +* Question Two +** a +#+attr_latex: :width 340px +[[./2a.png]] +** b +#+attr_latex: :width 400px +[[./2b.png]] +* Question Three +#+attr_latex: :width 400px +[[./3.png]] +* Question Four +** a +#+attr_latex: :width 400px +[[./4a.png]] +** b +#+attr_latex: :width 400px +[[./4b.png]] diff --git a/Homework/cs5300/homework-eight/compilers_assn_8.pdf b/Homework/cs5300/homework-eight/compilers_assn_8.pdf Binary files differnew file mode 100644 index 0000000..6d36dd2 --- /dev/null +++ b/Homework/cs5300/homework-eight/compilers_assn_8.pdf diff --git a/Homework/cs5300/homework-eight/compilers_assn_8.tex b/Homework/cs5300/homework-eight/compilers_assn_8.tex new file mode 100644 index 0000000..dd907f9 --- /dev/null +++ b/Homework/cs5300/homework-eight/compilers_assn_8.tex @@ -0,0 +1,63 @@ +% Created 2023-03-15 Wed 21:40 +% Intended LaTeX compiler: lualatex +\documentclass[11pt]{article} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +\author{Lizzy Hunt} +\date{\today} +\title{Assignment Eight} +\hypersetup{ + pdfauthor={Lizzy Hunt}, + pdftitle={Assignment Eight}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:org3d54412} +\begin{center} +\includegraphics[width=240px]{./1.png} +\end{center} +\section{Question Two} +\label{sec:org5250acb} +\subsection{a} +\label{sec:orga277c15} +\begin{center} +\includegraphics[width=340px]{./2a.png} +\end{center} +\subsection{b} +\label{sec:orgc7a2b2a} +\begin{center} +\includegraphics[width=400px]{./2b.png} +\end{center} +\section{Question Three} +\label{sec:orgfc178d7} +\begin{center} +\includegraphics[width=400px]{./3.png} +\end{center} +\section{Question Four} +\label{sec:orgd44e931} +\subsection{a} +\label{sec:org5b58749} +\begin{center} +\includegraphics[width=400px]{./4a.png} +\end{center} +\subsection{b} +\label{sec:orgfe564d0} +\begin{center} +\includegraphics[width=400px]{./4b.png} +\end{center} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-five/1.excalidraw.png b/Homework/cs5300/homework-five/1.excalidraw.png Binary files differnew file mode 100644 index 0000000..272aafe --- /dev/null +++ b/Homework/cs5300/homework-five/1.excalidraw.png diff --git a/Homework/cs5300/homework-five/6.excalidraw.png b/Homework/cs5300/homework-five/6.excalidraw.png Binary files differnew file mode 100644 index 0000000..1a1caa4 --- /dev/null +++ b/Homework/cs5300/homework-five/6.excalidraw.png diff --git a/Homework/cs5300/homework-five/compilers_assn_5.html b/Homework/cs5300/homework-five/compilers_assn_5.html new file mode 100644 index 0000000..6d1a166 --- /dev/null +++ b/Homework/cs5300/homework-five/compilers_assn_5.html @@ -0,0 +1,456 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" +"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> +<head> +<!-- 2023-02-16 Thu 21:18 --> +<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> +<meta name="viewport" content="width=device-width, initial-scale=1" /> +<title>Assignment Five</title> +<meta name="author" content="Lizzy Hunt" /> +<meta name="generator" content="Org Mode" /> +<style> + #content { max-width: 60em; margin: auto; } + .title { text-align: center; + margin-bottom: .2em; } + .subtitle { text-align: center; + font-size: medium; + font-weight: bold; + margin-top:0; } + .todo { font-family: monospace; color: red; } + .done { font-family: monospace; color: green; } + .priority { font-family: monospace; color: orange; } + .tag { background-color: #eee; font-family: monospace; + padding: 2px; font-size: 80%; font-weight: normal; } + .timestamp { color: #bebebe; } + .timestamp-kwd { color: #5f9ea0; } + .org-right { margin-left: auto; margin-right: 0px; text-align: right; } + .org-left { margin-left: 0px; margin-right: auto; text-align: left; } + .org-center { margin-left: auto; margin-right: auto; text-align: center; } + .underline { text-decoration: underline; } + #postamble p, #preamble p { font-size: 90%; margin: .2em; } + p.verse { margin-left: 3%; } + pre { + border: 1px solid #e6e6e6; + border-radius: 3px; + background-color: #f2f2f2; + padding: 8pt; + font-family: monospace; + overflow: auto; + margin: 1.2em; + } + pre.src { + position: relative; + overflow: auto; + } + pre.src:before { + display: none; + position: absolute; + top: -8px; + right: 12px; + padding: 3px; + color: #555; + background-color: #f2f2f299; + } + pre.src:hover:before { display: inline; margin-top: 14px;} + /* Languages per Org manual */ + pre.src-asymptote:before { content: 'Asymptote'; } + pre.src-awk:before { content: 'Awk'; } + pre.src-authinfo::before { content: 'Authinfo'; } + pre.src-C:before { content: 'C'; } + /* pre.src-C++ doesn't work in CSS */ + pre.src-clojure:before { content: 'Clojure'; } + pre.src-css:before { content: 'CSS'; } + pre.src-D:before { content: 'D'; } + pre.src-ditaa:before { content: 'ditaa'; } + pre.src-dot:before { content: 'Graphviz'; } + pre.src-calc:before { content: 'Emacs Calc'; } + pre.src-emacs-lisp:before { content: 'Emacs Lisp'; } + pre.src-fortran:before { content: 'Fortran'; } + pre.src-gnuplot:before { content: 'gnuplot'; } + pre.src-haskell:before { content: 'Haskell'; } + pre.src-hledger:before { content: 'hledger'; } + pre.src-java:before { content: 'Java'; } + pre.src-js:before { content: 'Javascript'; } + pre.src-latex:before { content: 'LaTeX'; } + pre.src-ledger:before { content: 'Ledger'; } + pre.src-lisp:before { content: 'Lisp'; } + pre.src-lilypond:before { content: 'Lilypond'; } + pre.src-lua:before { content: 'Lua'; } + pre.src-matlab:before { content: 'MATLAB'; } + pre.src-mscgen:before { content: 'Mscgen'; } + pre.src-ocaml:before { content: 'Objective Caml'; } + pre.src-octave:before { content: 'Octave'; } + pre.src-org:before { content: 'Org mode'; } + pre.src-oz:before { content: 'OZ'; } + pre.src-plantuml:before { content: 'Plantuml'; } + pre.src-processing:before { content: 'Processing.js'; } + pre.src-python:before { content: 'Python'; } + pre.src-R:before { content: 'R'; } + pre.src-ruby:before { content: 'Ruby'; } + pre.src-sass:before { content: 'Sass'; } + pre.src-scheme:before { content: 'Scheme'; } + pre.src-screen:before { content: 'Gnu Screen'; } + pre.src-sed:before { content: 'Sed'; } + pre.src-sh:before { content: 'shell'; } + pre.src-sql:before { content: 'SQL'; } + pre.src-sqlite:before { content: 'SQLite'; } + /* additional languages in org.el's org-babel-load-languages alist */ + pre.src-forth:before { content: 'Forth'; } + pre.src-io:before { content: 'IO'; } + pre.src-J:before { content: 'J'; } + pre.src-makefile:before { content: 'Makefile'; } + pre.src-maxima:before { content: 'Maxima'; } + pre.src-perl:before { content: 'Perl'; } + pre.src-picolisp:before { content: 'Pico Lisp'; } + pre.src-scala:before { content: 'Scala'; } + pre.src-shell:before { content: 'Shell Script'; } + pre.src-ebnf2ps:before { content: 'ebfn2ps'; } + /* additional language identifiers per "defun org-babel-execute" + in ob-*.el */ + pre.src-cpp:before { content: 'C++'; } + pre.src-abc:before { content: 'ABC'; } + pre.src-coq:before { content: 'Coq'; } + pre.src-groovy:before { content: 'Groovy'; } + /* additional language identifiers from org-babel-shell-names in + ob-shell.el: ob-shell is the only babel language using a lambda to put + the execution function name together. */ + pre.src-bash:before { content: 'bash'; } + pre.src-csh:before { content: 'csh'; } + pre.src-ash:before { content: 'ash'; } + pre.src-dash:before { content: 'dash'; } + pre.src-ksh:before { content: 'ksh'; } + pre.src-mksh:before { content: 'mksh'; } + pre.src-posh:before { content: 'posh'; } + /* Additional Emacs modes also supported by the LaTeX listings package */ + pre.src-ada:before { content: 'Ada'; } + pre.src-asm:before { content: 'Assembler'; } + pre.src-caml:before { content: 'Caml'; } + pre.src-delphi:before { content: 'Delphi'; } + pre.src-html:before { content: 'HTML'; } + pre.src-idl:before { content: 'IDL'; } + pre.src-mercury:before { content: 'Mercury'; } + pre.src-metapost:before { content: 'MetaPost'; } + pre.src-modula-2:before { content: 'Modula-2'; } + pre.src-pascal:before { content: 'Pascal'; } + pre.src-ps:before { content: 'PostScript'; } + pre.src-prolog:before { content: 'Prolog'; } + pre.src-simula:before { content: 'Simula'; } + pre.src-tcl:before { content: 'tcl'; } + pre.src-tex:before { content: 'TeX'; } + pre.src-plain-tex:before { content: 'Plain TeX'; } + pre.src-verilog:before { content: 'Verilog'; } + pre.src-vhdl:before { content: 'VHDL'; } + pre.src-xml:before { content: 'XML'; } + pre.src-nxml:before { content: 'XML'; } + /* add a generic configuration mode; LaTeX export needs an additional + (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */ + pre.src-conf:before { content: 'Configuration File'; } + + table { border-collapse:collapse; } + caption.t-above { caption-side: top; } + caption.t-bottom { caption-side: bottom; } + td, th { vertical-align:top; } + th.org-right { text-align: center; } + th.org-left { text-align: center; } + th.org-center { text-align: center; } + td.org-right { text-align: right; } + td.org-left { text-align: left; } + td.org-center { text-align: center; } + dt { font-weight: bold; } + .footpara { display: inline; } + .footdef { margin-bottom: 1em; } + .figure { padding: 1em; } + .figure p { text-align: center; } + .equation-container { + display: table; + text-align: center; + width: 100%; + } + .equation { + vertical-align: middle; + } + .equation-label { + display: table-cell; + text-align: right; + vertical-align: middle; + } + .inlinetask { + padding: 10px; + border: 2px solid gray; + margin: 10px; + background: #ffffcc; + } + #org-div-home-and-up + { text-align: right; font-size: 70%; white-space: nowrap; } + textarea { overflow-x: auto; } + .linenr { font-size: smaller } + .code-highlighted { background-color: #ffff00; } + .org-info-js_info-navigation { border-style: none; } + #org-info-js_console-label + { font-size: 10px; font-weight: bold; white-space: nowrap; } + .org-info-js_search-highlight + { background-color: #ffff00; color: #000000; font-weight: bold; } + .org-svg { } +</style> +<script> + window.MathJax = { + tex: { + ams: { + multlineWidth: '85%' + }, + tags: 'ams', + tagSide: 'right', + tagIndent: '.8em' + }, + chtml: { + scale: 1.0, + displayAlign: 'center', + displayIndent: '0em' + }, + svg: { + scale: 1.0, + displayAlign: 'center', + displayIndent: '0em' + }, + output: { + font: 'mathjax-modern', + displayOverflow: 'overflow' + } + }; +</script> + +<script + id="MathJax-script" + async + src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"> +</script> +</head> +<body> +<div id="content" class="content"> +<h1 class="title">Assignment Five</h1> + +<div id="outline-container-orgd749271" class="outline-2"> +<h2 id="orgd749271"><span class="section-number-2">1.</span> Question One</h2> +<div class="outline-text-2" id="text-1"> +\begin{verbatim} +N -> N a N b N + -> ε a N b N (using N -> ε) + -> ε a N a N b N b N (using N -> N a N b N) + -> ε a N a N b N a N b N b N (using N -> N a N b N) + -> ε a ε a N b N a N b N b N (using N -> ε) + -> ε a ε a ε b N a N b N b N (using N -> ε) + -> ε a ε a ε b ε a N b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b ε (using N -> ε) + -> aababb (remove epsilons for clarity) +\end{verbatim} + + +<div id="orgedc3ab8" class="figure"> +<p><img src="./1.excalidraw.png" alt="1.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org35f1897" class="outline-2"> +<h2 id="org35f1897"><span class="section-number-2">2.</span> Question Two</h2> +<div class="outline-text-2" id="text-2"> +\begin{verbatim} +N -> N N + -> N * N (using N -> N *) + -> (N) * N (using N -> (N)) + -> (N + N) * N (using N -> N + N) + -> (a + N) * N (using N -> a) + -> (a + a) * N (using N -> a) + -> (a + a) * a (using N -> a) +\end{verbatim} +</div> +</div> + +<div id="outline-container-org2f075dc" class="outline-2"> +<h2 id="org2f075dc"><span class="section-number-2">3.</span> Question Three</h2> +<div class="outline-text-2" id="text-3"> +\begin{verbatim} +N -> b N a N + -> b ε a N (using N -> ε) + -> b ε a a N b N (using N -> a N b N) + -> b ε a a a N b N b N (using N -> a N b N) + -> b ε a a a ε b N b N (using N -> ε) + -> b ε a a a ε b ε b N (using N -> ε) + -> b ε a a a ε b ε b ε (using N -> ε) +\end{verbatim} +</div> +</div> + +<div id="outline-container-org733adfa" class="outline-2"> +<h2 id="org733adfa"><span class="section-number-2">4.</span> Question Four</h2> +<div class="outline-text-2" id="text-4"> +\begin{verbatim} +N -> aNbN +N -> bNaN +N -> ε +\end{verbatim} +</div> +</div> + +<div id="outline-container-orgd670219" class="outline-2"> +<h2 id="orgd670219"><span class="section-number-2">5.</span> Question Five</h2> +<div class="outline-text-2" id="text-5"> +<p> +\(count(a) \neq count(b) \Rightarrow count(a) > count(b) \vee count(a) < count(b)\) +</p> + +<ul class="org-ul"> +<li>More A's than B's = G</li> +<li>Less A's than B's = L</li> +</ul> + +\begin{verbatim} +S -> G +S -> L +G -> GG +G -> EAE +L -> LL +L -> EBE +E -> aEaE +E -> bEaE +E -> ε +A -> aA +A -> a +B -> bB +B -> b +\end{verbatim} +</div> +</div> + +<div id="outline-container-orgf54c9c6" class="outline-2"> +<h2 id="orgf54c9c6"><span class="section-number-2">6.</span> Question Six</h2> +<div class="outline-text-2" id="text-6"> +<p> +This one we can actually create from a DFA! +</p> + + +<div id="org1a151d7" class="figure"> +<p><img src="./6.excalidraw.png" alt="6.excalidraw.png" /> +</p> +</div> + +\begin{verbatim} +S -> aN +N -> aM +N -> bS +M -> MM +M -> a +\end{verbatim} +</div> +</div> + +<div id="outline-container-org0ed867b" class="outline-2"> +<h2 id="org0ed867b"><span class="section-number-2">7.</span> Question Seven</h2> +<div class="outline-text-2" id="text-7"> +<p> +Consider a grammar S with regular expressions in its production's bodies. After replacing any extensions on regular expressions with their corresponding algebraic equivalents as given in 3.3.5, hen, we can transform S into an equivalent grammar by the following rules for nonterminals or terminals: +</p> +</div> + +<div id="outline-container-orgb149809" class="outline-3"> +<h3 id="orgb149809"><span class="section-number-3">7.1.</span> Kleene Closure</h3> +<div class="outline-text-3" id="text-7-1"> +<p> +If we have a match in a production body corresponding to Kleene Closure, then we can transform it into two new productions: +</p> + +\begin{verbatim} +B* +\end{verbatim} + +<p> +goes to +</p> + +\begin{verbatim} +A -> ε +A -> BA +\end{verbatim} +</div> +</div> + +<div id="outline-container-org84f1fcf" class="outline-3"> +<h3 id="org84f1fcf"><span class="section-number-3">7.2.</span> Union</h3> +<div class="outline-text-3" id="text-7-2"> +<p> +If we have a match in a production body corresponding to the union operator, then we can rewrite it as two new productions: +</p> + +\begin{verbatim} +B | C +\end{verbatim} + +<p> +goes to +</p> + +\begin{verbatim} +A -> B +A -> C +\end{verbatim} +</div> +</div> + +<div id="outline-container-org1072c90" class="outline-3"> +<h3 id="org1072c90"><span class="section-number-3">7.3.</span> Example(s)</h3> +<div class="outline-text-3" id="text-7-3"> +<p> +For example, consider the grammar: +</p> + +\begin{verbatim} +N -> (A | B)* +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +<p> +then we can form our first transformation by replacing N (no need to rename since it's already the start symbol): +</p> + +\begin{verbatim} +N -> ε +N -> (A | B)N +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +<p> +which is then further reduced when we transform (A | B) +</p> + +\begin{verbatim} +N -> ε +N -> DN +D -> A +D -> B +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} +</div> +</div> +</div> +</div> +<div id="postamble" class="status"> +<p class="author">Author: Lizzy Hunt</p> +<p class="date">Created: 2023-02-16 Thu 21:18</p> +<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p> +</div> +</body> +</html>
\ No newline at end of file diff --git a/Homework/cs5300/homework-five/compilers_assn_5.org b/Homework/cs5300/homework-five/compilers_assn_5.org new file mode 100644 index 0000000..f89a774 --- /dev/null +++ b/Homework/cs5300/homework-five/compilers_assn_5.org @@ -0,0 +1,157 @@ +#+TITLE: Assignment Five +#+AUTHOR: Lizzy Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +#+LATEX: \setlength\parindent{0pt} +#+LATEX_COMPILER: lualatex +#+OPTIONS: toc:nil + +* Question One +\begin{verbatim} +N -> N a N b N + -> ε a N b N (using N -> ε) + -> ε a N a N b N b N (using N -> N a N b N) + -> ε a N a N b N a N b N b N (using N -> N a N b N) + -> ε a ε a N b N a N b N b N (using N -> ε) + -> ε a ε a ε b N a N b N b N (using N -> ε) + -> ε a ε a ε b ε a N b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b ε (using N -> ε) + -> aababb (remove epsilons for clarity) +\end{verbatim} + + +#+attr_latex: :width 225px +[[./1.excalidraw.png]] + +* Question Two +\begin{verbatim} +N -> N N + -> N * N (using N -> N *) + -> (N) * N (using N -> (N)) + -> (N + N) * N (using N -> N + N) + -> (a + N) * N (using N -> a) + -> (a + a) * N (using N -> a) + -> (a + a) * a (using N -> a) +\end{verbatim} + +* Question Three +\begin{verbatim} +N -> b N a N + -> b ε a N (using N -> ε) + -> b ε a a N b N (using N -> a N b N) + -> b ε a a a N b N b N (using N -> a N b N) + -> b ε a a a ε b N b N (using N -> ε) + -> b ε a a a ε b ε b N (using N -> ε) + -> b ε a a a ε b ε b ε (using N -> ε) +\end{verbatim} + +* Question Four +\begin{verbatim} +N -> aNbN +N -> bNaN +N -> ε +\end{verbatim} + +* Question Five +$count(a) \neq count(b) \Rightarrow count(a) > count(b) \vee count(a) < count(b)$ + ++ More A's than B's = G ++ Less A's than B's = L + +\begin{verbatim} +S -> G +S -> L +G -> GG +G -> EAE +L -> LL +L -> EBE +E -> aEaE +E -> bEaE +E -> ε +A -> aA +A -> a +B -> bB +B -> b +\end{verbatim} + +* Question Six +This one we can actually create from a DFA! + +#+attr_latex: :width 150px +[[./6.excalidraw.png]] + +\begin{verbatim} +S -> aN +N -> aM +N -> bS +M -> MM +M -> a +\end{verbatim} + +* Question Seven +A grammar, S, with regular expressions in its production's bodies, after replacing any extensions on regular expressions with their corresponding algebraic equivalents as given in 3.3.5, then, we can transform S into an equivalent grammar by the following rules for nonterminals or terminals: + +** Kleene Closure +If we have a match in a production body corresponding to Kleene Closure, then we can transform it into two new productions: + +\begin{verbatim} +B* +\end{verbatim} + +is mapped to + +\begin{verbatim} +A -> ε +A -> BA +\end{verbatim} + +** Union +If we have a match in a production body corresponding to the union operator, then we can rewrite it as two new productions: + +\begin{verbatim} +B | C +\end{verbatim} + +is mapped to + +\begin{verbatim} +A -> B +A -> C +\end{verbatim} + +** Example +Consider the grammar: + +\begin{verbatim} +N -> (A | B)* +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +then we can form our first transformation by replacing N (with no need to rename since it's already the start symbol): + +\begin{verbatim} +N -> ε +N -> (A | B)N +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +which is then further reduced when we transform (A | B) + +\begin{verbatim} +N -> ε +N -> DN +D -> A +D -> B +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} diff --git a/Homework/cs5300/homework-five/compilers_assn_5.pdf b/Homework/cs5300/homework-five/compilers_assn_5.pdf Binary files differnew file mode 100644 index 0000000..399c7ea --- /dev/null +++ b/Homework/cs5300/homework-five/compilers_assn_5.pdf diff --git a/Homework/cs5300/homework-five/compilers_assn_5.tex b/Homework/cs5300/homework-five/compilers_assn_5.tex new file mode 100644 index 0000000..160bb9a --- /dev/null +++ b/Homework/cs5300/homework-five/compilers_assn_5.tex @@ -0,0 +1,192 @@ +% Created 2023-02-16 Thu 21:32 +% Intended LaTeX compiler: lualatex +\documentclass[11pt]{article} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +\author{Lizzy Hunt} +\date{\today} +\title{Assignment Five} +\hypersetup{ + pdfauthor={Lizzy Hunt}, + pdftitle={Assignment Five}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:org921f457} +\begin{verbatim} +N -> N a N b N + -> ε a N b N (using N -> ε) + -> ε a N a N b N b N (using N -> N a N b N) + -> ε a N a N b N a N b N b N (using N -> N a N b N) + -> ε a ε a N b N a N b N b N (using N -> ε) + -> ε a ε a ε b N a N b N b N (using N -> ε) + -> ε a ε a ε b ε a N b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b N b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b N (using N -> ε) + -> ε a ε a ε b ε a ε b ε b ε (using N -> ε) + -> aababb (remove epsilons for clarity) +\end{verbatim} + + +\begin{center} +\includegraphics[width=225px]{./1.excalidraw.png} +\end{center} + +\section{Question Two} +\label{sec:org8175f4b} +\begin{verbatim} +N -> N N + -> N * N (using N -> N *) + -> (N) * N (using N -> (N)) + -> (N + N) * N (using N -> N + N) + -> (a + N) * N (using N -> a) + -> (a + a) * N (using N -> a) + -> (a + a) * a (using N -> a) +\end{verbatim} + +\section{Question Three} +\label{sec:org30f8f3d} +\begin{verbatim} +N -> b N a N + -> b ε a N (using N -> ε) + -> b ε a a N b N (using N -> a N b N) + -> b ε a a a N b N b N (using N -> a N b N) + -> b ε a a a ε b N b N (using N -> ε) + -> b ε a a a ε b ε b N (using N -> ε) + -> b ε a a a ε b ε b ε (using N -> ε) +\end{verbatim} + +\section{Question Four} +\label{sec:orgc6e6679} +\begin{verbatim} +N -> aNbN +N -> bNaN +N -> ε +\end{verbatim} + +\section{Question Five} +\label{sec:orgef2e893} +\(count(a) \neq count(b) \Rightarrow count(a) > count(b) \vee count(a) < count(b)\) + +\begin{itemize} +\item More A's than B's = G +\item Less A's than B's = L +\end{itemize} + +\begin{verbatim} +S -> G +S -> L +G -> GG +G -> EAE +L -> LL +L -> EBE +E -> aEaE +E -> bEaE +E -> ε +A -> aA +A -> a +B -> bB +B -> b +\end{verbatim} + +\section{Question Six} +\label{sec:orgfe19196} +This one we can actually create from a DFA! + +\begin{center} +\includegraphics[width=150px]{./6.excalidraw.png} +\end{center} + +\begin{verbatim} +S -> aN +N -> aM +N -> bS +M -> MM +M -> a +\end{verbatim} + +\section{Question Seven} +\label{sec:org2dfc6f9} +A grammar, S, with regular expressions in its production's bodies, after replacing any extensions on regular expressions with their corresponding algebraic equivalents as given in 3.3.5, then, we can transform S into an equivalent grammar by the following rules for nonterminals or terminals: + +\subsection{Kleene Closure} +\label{sec:orgdac51cc} +If we have a match in a production body corresponding to Kleene Closure, then we can transform it into two new productions: + +\begin{verbatim} +B* +\end{verbatim} + +is mapped to + +\begin{verbatim} +A -> ε +A -> BA +\end{verbatim} + +\subsection{Union} +\label{sec:org1941049} +If we have a match in a production body corresponding to the union operator, then we can rewrite it as two new productions: + +\begin{verbatim} +B | C +\end{verbatim} + +is mapped to + +\begin{verbatim} +A -> B +A -> C +\end{verbatim} + +\subsection{Example} +\label{sec:org0ac391e} +Consider the grammar: + +\begin{verbatim} +N -> (A | B)* +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +then we can form our first transformation by replacing N (with no need to rename since it's already the start symbol): + +\begin{verbatim} +N -> ε +N -> (A | B)N +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} + +which is then further reduced when we transform (A | B) + +\begin{verbatim} +N -> ε +N -> DN +D -> A +D -> B +A -> aA +A -> ε +B -> bB +B -> ε +\end{verbatim} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-five/missfont.log b/Homework/cs5300/homework-five/missfont.log new file mode 100644 index 0000000..669c341 --- /dev/null +++ b/Homework/cs5300/homework-five/missfont.log @@ -0,0 +1,22 @@ +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas/BI +mktextfm Consolas +mktextfm Consolas/B +mktextfm Consolas +mktextfm Consolas/I +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas/BI +mktextfm Consolas +mktextfm Consolas/B +mktextfm Consolas +mktextfm Consolas/I +mktextfm Consolas +mktextfm Consolas +mktextfm Consolas diff --git a/Homework/cs5300/homework-four/1a.excalidraw.png b/Homework/cs5300/homework-four/1a.excalidraw.png Binary files differnew file mode 100644 index 0000000..8d32414 --- /dev/null +++ b/Homework/cs5300/homework-four/1a.excalidraw.png diff --git a/Homework/cs5300/homework-four/1b.excalidraw.png b/Homework/cs5300/homework-four/1b.excalidraw.png Binary files differnew file mode 100644 index 0000000..48052c9 --- /dev/null +++ b/Homework/cs5300/homework-four/1b.excalidraw.png diff --git a/Homework/cs5300/homework-four/1c.excalidraw.png b/Homework/cs5300/homework-four/1c.excalidraw.png Binary files differnew file mode 100644 index 0000000..ee91905 --- /dev/null +++ b/Homework/cs5300/homework-four/1c.excalidraw.png diff --git a/Homework/cs5300/homework-four/1d.excalidraw.png b/Homework/cs5300/homework-four/1d.excalidraw.png Binary files differnew file mode 100644 index 0000000..63c1047 --- /dev/null +++ b/Homework/cs5300/homework-four/1d.excalidraw.png diff --git a/Homework/cs5300/homework-four/1e.excalidraw.png b/Homework/cs5300/homework-four/1e.excalidraw.png Binary files differnew file mode 100644 index 0000000..e2fd108 --- /dev/null +++ b/Homework/cs5300/homework-four/1e.excalidraw.png diff --git a/Homework/cs5300/homework-four/1f.excalidraw.png b/Homework/cs5300/homework-four/1f.excalidraw.png Binary files differnew file mode 100644 index 0000000..9499748 --- /dev/null +++ b/Homework/cs5300/homework-four/1f.excalidraw.png diff --git a/Homework/cs5300/homework-four/4a.excalidraw.png b/Homework/cs5300/homework-four/4a.excalidraw.png Binary files differnew file mode 100644 index 0000000..3738bea --- /dev/null +++ b/Homework/cs5300/homework-four/4a.excalidraw.png diff --git a/Homework/cs5300/homework-four/4b.excalidraw.png b/Homework/cs5300/homework-four/4b.excalidraw.png Binary files differnew file mode 100644 index 0000000..0c0ac27 --- /dev/null +++ b/Homework/cs5300/homework-four/4b.excalidraw.png diff --git a/Homework/cs5300/homework-four/4c.excalidraw.png b/Homework/cs5300/homework-four/4c.excalidraw.png Binary files differnew file mode 100644 index 0000000..3387c62 --- /dev/null +++ b/Homework/cs5300/homework-four/4c.excalidraw.png diff --git a/Homework/cs5300/homework-four/5.1.excalidraw.png b/Homework/cs5300/homework-four/5.1.excalidraw.png Binary files differnew file mode 100644 index 0000000..c8b213e --- /dev/null +++ b/Homework/cs5300/homework-four/5.1.excalidraw.png diff --git a/Homework/cs5300/homework-four/5.2.excalidraw.png b/Homework/cs5300/homework-four/5.2.excalidraw.png Binary files differnew file mode 100644 index 0000000..348d8da --- /dev/null +++ b/Homework/cs5300/homework-four/5.2.excalidraw.png diff --git a/Homework/cs5300/homework-four/51a.excalidraw.png b/Homework/cs5300/homework-four/51a.excalidraw.png Binary files differnew file mode 100644 index 0000000..c3c4376 --- /dev/null +++ b/Homework/cs5300/homework-four/51a.excalidraw.png diff --git a/Homework/cs5300/homework-four/51b.excalidraw.png b/Homework/cs5300/homework-four/51b.excalidraw.png Binary files differnew file mode 100644 index 0000000..58df697 --- /dev/null +++ b/Homework/cs5300/homework-four/51b.excalidraw.png diff --git a/Homework/cs5300/homework-four/51c.excalidraw.png b/Homework/cs5300/homework-four/51c.excalidraw.png Binary files differnew file mode 100644 index 0000000..702f1a1 --- /dev/null +++ b/Homework/cs5300/homework-four/51c.excalidraw.png diff --git a/Homework/cs5300/homework-four/52a.excalidraw.png b/Homework/cs5300/homework-four/52a.excalidraw.png Binary files differnew file mode 100644 index 0000000..d325abf --- /dev/null +++ b/Homework/cs5300/homework-four/52a.excalidraw.png diff --git a/Homework/cs5300/homework-four/52b.excalidraw.png b/Homework/cs5300/homework-four/52b.excalidraw.png Binary files differnew file mode 100644 index 0000000..cd1a0b2 --- /dev/null +++ b/Homework/cs5300/homework-four/52b.excalidraw.png diff --git a/Homework/cs5300/homework-four/52c.excalidraw.png b/Homework/cs5300/homework-four/52c.excalidraw.png Binary files differnew file mode 100644 index 0000000..7bf7ae6 --- /dev/null +++ b/Homework/cs5300/homework-four/52c.excalidraw.png diff --git a/Homework/cs5300/homework-four/6a.excalidraw.png b/Homework/cs5300/homework-four/6a.excalidraw.png Binary files differnew file mode 100644 index 0000000..b733155 --- /dev/null +++ b/Homework/cs5300/homework-four/6a.excalidraw.png diff --git a/Homework/cs5300/homework-four/6b.excalidraw.png b/Homework/cs5300/homework-four/6b.excalidraw.png Binary files differnew file mode 100644 index 0000000..d27ef0b --- /dev/null +++ b/Homework/cs5300/homework-four/6b.excalidraw.png diff --git a/Homework/cs5300/homework-four/6c.excalidraw.png b/Homework/cs5300/homework-four/6c.excalidraw.png Binary files differnew file mode 100644 index 0000000..d73f558 --- /dev/null +++ b/Homework/cs5300/homework-four/6c.excalidraw.png diff --git a/Homework/cs5300/homework-four/compilers_assn_4.html b/Homework/cs5300/homework-four/compilers_assn_4.html new file mode 100644 index 0000000..9c2110c --- /dev/null +++ b/Homework/cs5300/homework-four/compilers_assn_4.html @@ -0,0 +1,942 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" +"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> +<head> +<!-- 2023-02-12 Sun 17:33 --> +<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> +<meta name="viewport" content="width=device-width, initial-scale=1" /> +<title>Assignment Three</title> +<meta name="author" content="Lizzy Hunt" /> +<meta name="generator" content="Org Mode" /> +<style> + #content { max-width: 60em; margin: auto; } + .title { text-align: center; + margin-bottom: .2em; } + .subtitle { text-align: center; + font-size: medium; + font-weight: bold; + margin-top:0; } + .todo { font-family: monospace; color: red; } + .done { font-family: monospace; color: green; } + .priority { font-family: monospace; color: orange; } + .tag { background-color: #eee; font-family: monospace; + padding: 2px; font-size: 80%; font-weight: normal; } + .timestamp { color: #bebebe; } + .timestamp-kwd { color: #5f9ea0; } + .org-right { margin-left: auto; margin-right: 0px; text-align: right; } + .org-left { margin-left: 0px; margin-right: auto; text-align: left; } + .org-center { margin-left: auto; margin-right: auto; text-align: center; } + .underline { text-decoration: underline; } + #postamble p, #preamble p { font-size: 90%; margin: .2em; } + p.verse { margin-left: 3%; } + pre { + border: 1px solid #e6e6e6; + border-radius: 3px; + background-color: #f2f2f2; + padding: 8pt; + font-family: monospace; + overflow: auto; + margin: 1.2em; + } + pre.src { + position: relative; + overflow: auto; + } + pre.src:before { + display: none; + position: absolute; + top: -8px; + right: 12px; + padding: 3px; + color: #555; + background-color: #f2f2f299; + } + pre.src:hover:before { display: inline; margin-top: 14px;} + /* Languages per Org manual */ + pre.src-asymptote:before { content: 'Asymptote'; } + pre.src-awk:before { content: 'Awk'; } + pre.src-authinfo::before { content: 'Authinfo'; } + pre.src-C:before { content: 'C'; } + /* pre.src-C++ doesn't work in CSS */ + pre.src-clojure:before { content: 'Clojure'; } + pre.src-css:before { content: 'CSS'; } + pre.src-D:before { content: 'D'; } + pre.src-ditaa:before { content: 'ditaa'; } + pre.src-dot:before { content: 'Graphviz'; } + pre.src-calc:before { content: 'Emacs Calc'; } + pre.src-emacs-lisp:before { content: 'Emacs Lisp'; } + pre.src-fortran:before { content: 'Fortran'; } + pre.src-gnuplot:before { content: 'gnuplot'; } + pre.src-haskell:before { content: 'Haskell'; } + pre.src-hledger:before { content: 'hledger'; } + pre.src-java:before { content: 'Java'; } + pre.src-js:before { content: 'Javascript'; } + pre.src-latex:before { content: 'LaTeX'; } + pre.src-ledger:before { content: 'Ledger'; } + pre.src-lisp:before { content: 'Lisp'; } + pre.src-lilypond:before { content: 'Lilypond'; } + pre.src-lua:before { content: 'Lua'; } + pre.src-matlab:before { content: 'MATLAB'; } + pre.src-mscgen:before { content: 'Mscgen'; } + pre.src-ocaml:before { content: 'Objective Caml'; } + pre.src-octave:before { content: 'Octave'; } + pre.src-org:before { content: 'Org mode'; } + pre.src-oz:before { content: 'OZ'; } + pre.src-plantuml:before { content: 'Plantuml'; } + pre.src-processing:before { content: 'Processing.js'; } + pre.src-python:before { content: 'Python'; } + pre.src-R:before { content: 'R'; } + pre.src-ruby:before { content: 'Ruby'; } + pre.src-sass:before { content: 'Sass'; } + pre.src-scheme:before { content: 'Scheme'; } + pre.src-screen:before { content: 'Gnu Screen'; } + pre.src-sed:before { content: 'Sed'; } + pre.src-sh:before { content: 'shell'; } + pre.src-sql:before { content: 'SQL'; } + pre.src-sqlite:before { content: 'SQLite'; } + /* additional languages in org.el's org-babel-load-languages alist */ + pre.src-forth:before { content: 'Forth'; } + pre.src-io:before { content: 'IO'; } + pre.src-J:before { content: 'J'; } + pre.src-makefile:before { content: 'Makefile'; } + pre.src-maxima:before { content: 'Maxima'; } + pre.src-perl:before { content: 'Perl'; } + pre.src-picolisp:before { content: 'Pico Lisp'; } + pre.src-scala:before { content: 'Scala'; } + pre.src-shell:before { content: 'Shell Script'; } + pre.src-ebnf2ps:before { content: 'ebfn2ps'; } + /* additional language identifiers per "defun org-babel-execute" + in ob-*.el */ + pre.src-cpp:before { content: 'C++'; } + pre.src-abc:before { content: 'ABC'; } + pre.src-coq:before { content: 'Coq'; } + pre.src-groovy:before { content: 'Groovy'; } + /* additional language identifiers from org-babel-shell-names in + ob-shell.el: ob-shell is the only babel language using a lambda to put + the execution function name together. */ + pre.src-bash:before { content: 'bash'; } + pre.src-csh:before { content: 'csh'; } + pre.src-ash:before { content: 'ash'; } + pre.src-dash:before { content: 'dash'; } + pre.src-ksh:before { content: 'ksh'; } + pre.src-mksh:before { content: 'mksh'; } + pre.src-posh:before { content: 'posh'; } + /* Additional Emacs modes also supported by the LaTeX listings package */ + pre.src-ada:before { content: 'Ada'; } + pre.src-asm:before { content: 'Assembler'; } + pre.src-caml:before { content: 'Caml'; } + pre.src-delphi:before { content: 'Delphi'; } + pre.src-html:before { content: 'HTML'; } + pre.src-idl:before { content: 'IDL'; } + pre.src-mercury:before { content: 'Mercury'; } + pre.src-metapost:before { content: 'MetaPost'; } + pre.src-modula-2:before { content: 'Modula-2'; } + pre.src-pascal:before { content: 'Pascal'; } + pre.src-ps:before { content: 'PostScript'; } + pre.src-prolog:before { content: 'Prolog'; } + pre.src-simula:before { content: 'Simula'; } + pre.src-tcl:before { content: 'tcl'; } + pre.src-tex:before { content: 'TeX'; } + pre.src-plain-tex:before { content: 'Plain TeX'; } + pre.src-verilog:before { content: 'Verilog'; } + pre.src-vhdl:before { content: 'VHDL'; } + pre.src-xml:before { content: 'XML'; } + pre.src-nxml:before { content: 'XML'; } + /* add a generic configuration mode; LaTeX export needs an additional + (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */ + pre.src-conf:before { content: 'Configuration File'; } + + table { border-collapse:collapse; } + caption.t-above { caption-side: top; } + caption.t-bottom { caption-side: bottom; } + td, th { vertical-align:top; } + th.org-right { text-align: center; } + th.org-left { text-align: center; } + th.org-center { text-align: center; } + td.org-right { text-align: right; } + td.org-left { text-align: left; } + td.org-center { text-align: center; } + dt { font-weight: bold; } + .footpara { display: inline; } + .footdef { margin-bottom: 1em; } + .figure { padding: 1em; } + .figure p { text-align: center; } + .equation-container { + display: table; + text-align: center; + width: 100%; + } + .equation { + vertical-align: middle; + } + .equation-label { + display: table-cell; + text-align: right; + vertical-align: middle; + } + .inlinetask { + padding: 10px; + border: 2px solid gray; + margin: 10px; + background: #ffffcc; + } + #org-div-home-and-up + { text-align: right; font-size: 70%; white-space: nowrap; } + textarea { overflow-x: auto; } + .linenr { font-size: smaller } + .code-highlighted { background-color: #ffff00; } + .org-info-js_info-navigation { border-style: none; } + #org-info-js_console-label + { font-size: 10px; font-weight: bold; white-space: nowrap; } + .org-info-js_search-highlight + { background-color: #ffff00; color: #000000; font-weight: bold; } + .org-svg { } +</style> +</head> +<body> +<div id="content" class="content"> +<h1 class="title">Assignment Three</h1> + +<div id="outline-container-org7e0749d" class="outline-2"> +<h2 id="org7e0749d"><span class="section-number-2">1.</span> Question One</h2> +<div class="outline-text-2" id="text-1"> +</div> +<div id="outline-container-org107c001" class="outline-3"> +<h3 id="org107c001"><span class="section-number-3">1.1.</span> a</h3> +<div class="outline-text-3" id="text-1-1"> + +<div id="orgbf0d7d2" class="figure"> +<p><img src="./1a.excalidraw.png" alt="1a.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-orgf901103" class="outline-3"> +<h3 id="orgf901103"><span class="section-number-3">1.2.</span> b</h3> +<div class="outline-text-3" id="text-1-2"> + +<div id="org62ecf1f" class="figure"> +<p><img src="./1b.excalidraw.png" alt="1b.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-org1fe7828" class="outline-3"> +<h3 id="org1fe7828"><span class="section-number-3">1.3.</span> c</h3> +<div class="outline-text-3" id="text-1-3"> + +<div id="org26be2ab" class="figure"> +<p><img src="./1c.excalidraw.png" alt="1c.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-orge5de671" class="outline-3"> +<h3 id="orge5de671"><span class="section-number-3">1.4.</span> d</h3> +<div class="outline-text-3" id="text-1-4"> + +<div id="orgc536c3f" class="figure"> +<p><img src="./1d.excalidraw.png" alt="1d.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-orgecd6420" class="outline-3"> +<h3 id="orgecd6420"><span class="section-number-3">1.5.</span> e</h3> +<div class="outline-text-3" id="text-1-5"> + +<div id="org1dc2d79" class="figure"> +<p><img src="./1e.excalidraw.png" alt="1e.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-orgca3f84d" class="outline-3"> +<h3 id="orgca3f84d"><span class="section-number-3">1.6.</span> f</h3> +<div class="outline-text-3" id="text-1-6"> + +<div id="org9785c54" class="figure"> +<p><img src="./1f.excalidraw.png" alt="1f.excalidraw.png" /> +</p> +</div> +</div> +</div> +</div> + +<div id="outline-container-org7c2f734" class="outline-2"> +<h2 id="org7c2f734"><span class="section-number-2">2.</span> Question Two</h2> +<div class="outline-text-2" id="text-2"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-right" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> +</colgroup> +<tbody> +<tr> +<td class="org-right">State</td> +<td class="org-left">a</td> +<td class="org-left">b</td> +<td class="org-left">e</td> +</tr> + +<tr> +<td class="org-right">0</td> +<td class="org-left">φ</td> +<td class="org-left">φ</td> +<td class="org-left">{1, 3}</td> +</tr> + +<tr> +<td class="org-right">1</td> +<td class="org-left">{2, 4}</td> +<td class="org-left">{4}</td> +<td class="org-left">φ</td> +</tr> + +<tr> +<td class="org-right">2</td> +<td class="org-left">{2}</td> +<td class="org-left">φ</td> +<td class="org-left">φ</td> +</tr> + +<tr> +<td class="org-right">3</td> +<td class="org-left">φ</td> +<td class="org-left">{4}</td> +<td class="org-left">φ</td> +</tr> + +<tr> +<td class="org-right">4</td> +<td class="org-left">φ</td> +<td class="org-left">{4}</td> +<td class="org-left">φ</td> +</tr> +</tbody> +</table> +</div> +</div> + +<div id="outline-container-orgbcbd2f9" class="outline-2"> +<h2 id="orgbcbd2f9"><span class="section-number-2">3.</span> Question Three</h2> +<div class="outline-text-2" id="text-3"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-right" /> + +<col class="org-right" /> + +<col class="org-right" /> +</colgroup> +<tbody> +<tr> +<td class="org-right">State</td> +<td class="org-right">a</td> +<td class="org-right">b</td> +</tr> + +<tr> +<td class="org-right">0</td> +<td class="org-right">1</td> +<td class="org-right">0</td> +</tr> + +<tr> +<td class="org-right">1</td> +<td class="org-right">1</td> +<td class="org-right">2</td> +</tr> + +<tr> +<td class="org-right">2</td> +<td class="org-right">1</td> +<td class="org-right">3</td> +</tr> + +<tr> +<td class="org-right">3</td> +<td class="org-right">1</td> +<td class="org-right">0</td> +</tr> +</tbody> +</table> +</div> +</div> + +<div id="outline-container-orgaf65b7e" class="outline-2"> +<h2 id="orgaf65b7e"><span class="section-number-2">4.</span> Question Four</h2> +<div class="outline-text-2" id="text-4"> +</div> +<div id="outline-container-org4be19f8" class="outline-3"> +<h3 id="org4be19f8"><span class="section-number-3">4.1.</span> a</h3> +<div class="outline-text-3" id="text-4-1"> + +<div id="orgcc9f99e" class="figure"> +<p><img src="./4a.excalidraw.png" alt="4a.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-orgd115a75" class="outline-3"> +<h3 id="orgd115a75"><span class="section-number-3">4.2.</span> b</h3> +<div class="outline-text-3" id="text-4-2"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">DFA State</td> +<td class="org-left">NFA States</td> +<td class="org-left">a</td> +<td class="org-left">b</td> +<td class="org-left">c</td> +</tr> + +<tr> +<td class="org-left">d<sub>0</sub></td> +<td class="org-left">{s<sub>0</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>5</sub>, s<sub>9</sub>}</td> +<td class="org-left">{s<sub>3</sub>, s<sub>6</sub>} (d<sub>1</sub>)</td> +<td class="org-left">s<sub>e</sub></td> +<td class="org-left">s<sub>e</sub></td> +</tr> + +<tr> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">{s<sub>3</sub>, s<sub>6</sub>}</td> +<td class="org-left">s<sub>e</sub></td> +<td class="org-left">{s<sub>4</sub>, s<sub>8</sub>, s<sub>9</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>5</sub>} (d<sub>2</sub>)</td> +<td class="org-left">{s<sub>7</sub>, s<sub>8</sub>, s<sub>9</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>5</sub>} (d<sub>3</sub>)</td> +</tr> + +<tr> +<td class="org-left">d<sub>2</sub></td> +<td class="org-left">{s<sub>4</sub>, s<sub>8</sub>, s<sub>9</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>5</sub>}</td> +<td class="org-left">{s<sub>3</sub>, s<sub>6</sub>} (d<sub>1</sub>)</td> +<td class="org-left">s<sub>e</sub></td> +<td class="org-left">s<sub>e</sub></td> +</tr> + +<tr> +<td class="org-left">d<sub>3</sub></td> +<td class="org-left">{s<sub>7</sub>, s<sub>8</sub>, s<sub>9</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>5</sub>}</td> +<td class="org-left">{s<sub>3</sub>, s<sub>6</sub>} (d<sub>1</sub>)</td> +<td class="org-left">s<sub>e</sub></td> +<td class="org-left">s<sub>e</sub></td> +</tr> +</tbody> +</table> + + +<div id="org8f60a9e" class="figure"> +<p><img src="./4b.excalidraw.png" alt="4b.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-org666b996" class="outline-3"> +<h3 id="org666b996"><span class="section-number-3">4.3.</span> c</h3> +<div class="outline-text-3" id="text-4-3"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">partition</td> +<td class="org-left">a</td> +<td class="org-left">b</td> +<td class="org-left">c</td> +</tr> + +<tr> +<td class="org-left">{d<sub>1</sub>}, {d<sub>0</sub>, d<sub>2</sub>, d<sub>3</sub>}</td> +<td class="org-left">{d<sub>0</sub>, d<sub>2</sub>, d<sub>3</sub>} ⇒ {d<sub>1</sub>, d<sub>0</sub>, d<sub>0</sub>}</td> +<td class="org-left">{d<sub>0</sub>, d<sub>2</sub>, d<sub>3</sub>} ⇒ {-, -, -}</td> +<td class="org-left">{d<sub>0</sub>, d<sub>2</sub>, d<sub>3</sub>} ⇒ {-, -, -}</td> +</tr> + +<tr> +<td class="org-left">{d<sub>1</sub>}, {d<sub>0</sub>}, {d<sub>2</sub>, d<sub>3</sub>}</td> +<td class="org-left">{d<sub>2</sub>, d<sub>3</sub>} ⇒ {d<sub>0</sub>, d<sub>0</sub>}</td> +<td class="org-left">{d<sub>2</sub>, d<sub>3</sub>} ⇒ {-, -}</td> +<td class="org-left">{d<sub>2</sub>, d<sub>3</sub>} ⇒ {-, -}</td> +</tr> +</tbody> +</table> + + +<div id="org16f148c" class="figure"> +<p><img src="./4c.excalidraw.png" alt="4c.excalidraw.png" /> +</p> +</div> +</div> +</div> +</div> + +<div id="outline-container-orgcacfc17" class="outline-2"> +<h2 id="orgcacfc17"><span class="section-number-2">5.</span> Question Five</h2> +<div class="outline-text-2" id="text-5"> +</div> +<div id="outline-container-org3f7b0bc" class="outline-3"> +<h3 id="org3f7b0bc"><span class="section-number-3">5.1.</span> regex 1</h3> +<div class="outline-text-3" id="text-5-1"> +</div> +<div id="outline-container-org52b54a4" class="outline-4"> +<h4 id="org52b54a4"><span class="section-number-4">5.1.1.</span> NFA</h4> +<div class="outline-text-4" id="text-5-1-1"> + +<div id="org956aa58" class="figure"> +<p><img src="./51a.excalidraw.png" alt="51a.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org090cd4b" class="outline-4"> +<h4 id="org090cd4b"><span class="section-number-4">5.1.2.</span> DFA</h4> +<div class="outline-text-4" id="text-5-1-2"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">DFA State</td> +<td class="org-left">NFA States</td> +<td class="org-left">1</td> +<td class="org-left">0</td> +</tr> + +<tr> +<td class="org-left">d<sub>0</sub></td> +<td class="org-left">{s<sub>0</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>4</sub>, s<sub>7</sub>}</td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>6</sub>, s<sub>7</sub>} (d<sub>1</sub>)</td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>6</sub>, s<sub>7</sub>} (d<sub>2</sub>)</td> +</tr> + +<tr> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>6</sub>, s<sub>7</sub>}</td> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">d<sub>2</sub></td> +</tr> + +<tr> +<td class="org-left">d<sub>2</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>6</sub>, s<sub>7</sub>}</td> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">d<sub>2</sub></td> +</tr> +</tbody> +</table> + + +<div id="org7bed478" class="figure"> +<p><img src="./51b.excalidraw.png" alt="51b.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org62cf966" class="outline-4"> +<h4 id="org62cf966"><span class="section-number-4">5.1.3.</span> Minimization</h4> +<div class="outline-text-4" id="text-5-1-3"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-right" /> + +<col class="org-right" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">partition</td> +<td class="org-right">1</td> +<td class="org-right">0</td> +</tr> + +<tr> +<td class="org-left">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>}</td> +<td class="org-right">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>} ⇒ {d<sub>1</sub>, d<sub>1</sub>, d<sub>1</sub>}</td> +<td class="org-right">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>} ⇒ {d<sub>2</sub>, d<sub>2</sub>, d<sub>2</sub>}</td> +</tr> +</tbody> +</table> + + +<div id="org614c57e" class="figure"> +<p><img src="./51c.excalidraw.png" alt="51c.excalidraw.png" /> +</p> +</div> +</div> +</div> +</div> + +<div id="outline-container-orgd3af6b5" class="outline-3"> +<h3 id="orgd3af6b5"><span class="section-number-3">5.2.</span> regex 2</h3> +<div class="outline-text-3" id="text-5-2"> +</div> +<div id="outline-container-org849c5a4" class="outline-4"> +<h4 id="org849c5a4"><span class="section-number-4">5.2.1.</span> NFA</h4> +<div class="outline-text-4" id="text-5-2-1"> + +<div id="orgbfe6a52" class="figure"> +<p><img src="./52a.excalidraw.png" alt="52a.excalidraw.png" /> +</p> +</div> +</div> +</div> +<div id="outline-container-org3131975" class="outline-4"> +<h4 id="org3131975"><span class="section-number-4">5.2.2.</span> DFA</h4> +<div class="outline-text-4" id="text-5-2-2"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> + +<col class="org-left" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">DFA State</td> +<td class="org-left">NFA States</td> +<td class="org-left">1</td> +<td class="org-left">0</td> +</tr> + +<tr> +<td class="org-left">d<sub>0</sub></td> +<td class="org-left">{s<sub>0</sub>, s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>5</sub>, s<sub>11</sub>, s<sub>6</sub>, s<sub>12</sub> }</td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>5</sub>, s<sub>6</sub>, s<sub>7</sub>, s<sub>8</sub>, s<sub>10</sub>, s<sub>11</sub>, s<sub>12</sub>} (d<sub>1</sub>)</td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>11</sub>, s<sub>6</sub>, s<sub>12</sub>} (d<sub>2</sub>)</td> +</tr> + +<tr> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>5</sub>, s<sub>6</sub>, s<sub>7</sub>, s<sub>8</sub>, s<sub>10</sub>, s<sub>11</sub>, s<sub>12</sub>}</td> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>9</sub>, s<sub>11</sub>, s<sub>6</sub>, s<sub>12</sub>} (d<sub>3</sub>)</td> +</tr> + +<tr> +<td class="org-left">d<sub>2</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>11</sub>, s<sub>6</sub>}</td> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">d<sub>2</sub></td> +</tr> + +<tr> +<td class="org-left">d<sub>3</sub></td> +<td class="org-left">{s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, s<sub>4</sub>, s<sub>5</sub>, s<sub>9</sub>, s<sub>11</sub>, s<sub>6</sub>, s<sub>12</sub>}</td> +<td class="org-left">d<sub>1</sub></td> +<td class="org-left">d<sub>2</sub></td> +</tr> +</tbody> +</table> + + +<div id="orgf90b87b" class="figure"> +<p><img src="./52b.excalidraw.png" alt="52b.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org83ddae3" class="outline-4"> +<h4 id="org83ddae3"><span class="section-number-4">5.2.3.</span> Minimization</h4> +<div class="outline-text-4" id="text-5-2-3"> +<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"> + + +<colgroup> +<col class="org-left" /> + +<col class="org-right" /> + +<col class="org-right" /> +</colgroup> +<tbody> +<tr> +<td class="org-left">partition</td> +<td class="org-right">1</td> +<td class="org-right">0</td> +</tr> + +<tr> +<td class="org-left">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>, d<sub>3</sub>}</td> +<td class="org-right">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>, d<sub>3</sub>} ⇒ {d<sub>1</sub>, d<sub>1</sub>, d<sub>1</sub>, d<sub>1</sub>}</td> +<td class="org-right">{d<sub>0</sub>, d<sub>1</sub>, d<sub>2</sub>, d<sub>3</sub>} ⇒ {d<sub>2</sub>, d<sub>3</sub>, d<sub>2</sub>, d<sub>2</sub>}</td> +</tr> +</tbody> +</table> + + +<div id="org5d55532" class="figure"> +<p><img src="./52c.excalidraw.png" alt="52c.excalidraw.png" /> +</p> +</div> +</div> +</div> +</div> +</div> + +<div id="outline-container-orge1550cc" class="outline-2"> +<h2 id="orge1550cc"><span class="section-number-2">6.</span> Question Six</h2> +<div class="outline-text-2" id="text-6"> +</div> +<div id="outline-container-org66f8b31" class="outline-3"> +<h3 id="org66f8b31"><span class="section-number-3">6.1.</span> a</h3> +<div class="outline-text-3" id="text-6-1"> +</div> +<div id="outline-container-org4cf062f" class="outline-4"> +<h4 id="org4cf062f"><span class="section-number-4">6.1.1.</span> DFA</h4> +<div class="outline-text-4" id="text-6-1-1"> + +<div id="org13c1329" class="figure"> +<p><img src="./6a.excalidraw.png" alt="6a.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org1d52e47" class="outline-4"> +<h4 id="org1d52e47"><span class="section-number-4">6.1.2.</span> catMap</h4> +<div class="outline-text-4" id="text-6-1-2"> +<div class="org-src-container"> +<pre class="src src-json">{ + '-': 'sign', + '+': 'sign', + [0-9]: 'digit', + 'e': 'scntfc' +} +</pre> +</div> +</div> +</div> + +<div id="outline-container-org2b46fc1" class="outline-4"> +<h4 id="org2b46fc1"><span class="section-number-4">6.1.3.</span> transMap</h4> +<div class="outline-text-4" id="text-6-1-3"> +<div class="org-src-container"> +<pre class="src src-json">{ + 's0': { + 'sign': 's1', + 'digit': 's2', + }, + 's1': { + 'digit': 's2', + }, + 's2': { + 'digit': 's2', + 'scntfc': 's3', + }, + 's3': { + 'sign': 's4', + 'digit': 's5', + }, + 's4': { + 'digit': 's6', + }, + 's5': { + 'digit': 's6', + }, + 's6': { + 'digit': 's6', + }, +} +</pre> +</div> +</div> +</div> + +<div id="outline-container-orgac3f2f9" class="outline-4"> +<h4 id="orgac3f2f9"><span class="section-number-4">6.1.4.</span> tokenTypes</h4> +<div class="outline-text-4" id="text-6-1-4"> +<div class="org-src-container"> +<pre class="src src-json">{ + 's2': 'integer', + 's6': 'integer, +} +</pre> +</div> +</div> +</div> +</div> +<div id="outline-container-org93935d7" class="outline-3"> +<h3 id="org93935d7"><span class="section-number-3">6.2.</span> b</h3> +<div class="outline-text-3" id="text-6-2"> +</div> +<div id="outline-container-org6e45588" class="outline-4"> +<h4 id="org6e45588"><span class="section-number-4">6.2.1.</span> DFA</h4> +<div class="outline-text-4" id="text-6-2-1"> + +<div id="org27f1518" class="figure"> +<p><img src="./6b.excalidraw.png" alt="6b.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-orgf2c4830" class="outline-4"> +<h4 id="orgf2c4830"><span class="section-number-4">6.2.2.</span> catMap</h4> +<div class="outline-text-4" id="text-6-2-2"> +<div class="org-src-container"> +<pre class="src src-json">{ + [a-zA-Z]: 'letter', + '_': 'letter', + [0-9]: 'digit', +} +</pre> +</div> +</div> +</div> + +<div id="outline-container-orgde4a77d" class="outline-4"> +<h4 id="orgde4a77d"><span class="section-number-4">6.2.3.</span> transMap</h4> +<div class="outline-text-4" id="text-6-2-3"> +<div class="org-src-container"> +<pre class="src src-json">{ + 's1': { + 'letter': 's2' + }, + 's2': { + 'letter': 's2', + 'digit': 's2', + }, +} +</pre> +</div> +</div> +</div> +</div> + +<div id="outline-container-org097d578" class="outline-3"> +<h3 id="org097d578"><span class="section-number-3">6.3.</span> c</h3> +<div class="outline-text-3" id="text-6-3"> +</div> +<div id="outline-container-orge1b389e" class="outline-4"> +<h4 id="orge1b389e"><span class="section-number-4">6.3.1.</span> DFA</h4> +<div class="outline-text-4" id="text-6-3-1"> + +<div id="org8cc9249" class="figure"> +<p><img src="./6c.excalidraw.png" alt="6c.excalidraw.png" /> +</p> +</div> +</div> +</div> + +<div id="outline-container-org33dd191" class="outline-4"> +<h4 id="org33dd191"><span class="section-number-4">6.3.2.</span> catMap</h4> +<div class="outline-text-4" id="text-6-3-2"> +<div class="org-src-container"> +<pre class="src src-json">{ + [^(\n)*/]: 'nonc', + '\n': 'newline', + 'eof': 'eof', + '/': 'slash', + '*': 'star' +} +</pre> +</div> +</div> +</div> +<div id="outline-container-orgd453e37" class="outline-4"> +<h4 id="orgd453e37"><span class="section-number-4">6.3.3.</span> transMap</h4> +<div class="outline-text-4" id="text-6-3-3"> +<div class="org-src-container"> +<pre class="src src-json">{ + 's0': { + 'slash': 's1', + }, + 's1': { + 'slash': 's2', + 'star': 's4', + }, + 's2': { + 'nonc': 's2', + 'newline': 's3', + 'eof': 's3' + }, + 's4': { + 'nonc': 's4', + 'star': 's5', + }, + 's5': { + 'nonc': 's4', + 'newline': 's4', + 'slash': 's6' + } +} +</pre> +</div> +</div> +</div> + +<div id="outline-container-org3cf8c57" class="outline-4"> +<h4 id="org3cf8c57"><span class="section-number-4">6.3.4.</span> tokenType</h4> +<div class="outline-text-4" id="text-6-3-4"> +<div class="org-src-container"> +<pre class="src src-json">{ + 's3': 'singleLineComment', + 's6': 'multiLineComment', +} +</pre> +</div> +</div> +</div> +</div> +</div> +</div> +<div id="postamble" class="status"> +<p class="author">Author: Lizzy Hunt</p> +<p class="date">Created: 2023-02-12 Sun 17:33</p> +<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p> +</div> +</body> +</html>
\ No newline at end of file diff --git a/Homework/cs5300/homework-four/compilers_assn_4.org b/Homework/cs5300/homework-four/compilers_assn_4.org new file mode 100644 index 0000000..da16615 --- /dev/null +++ b/Homework/cs5300/homework-four/compilers_assn_4.org @@ -0,0 +1,236 @@ +#+TITLE: Assignment Three +#+AUTHOR: Lizzy Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage{amsmath} \usepackage[a4paper,margin=1in,landscape]{geometry} +#+LATEX: \setlength\parindent{0pt} +#+OPTIONS: toc:nil + +* Question One +** a +#+attr_latex: :width 250px +[[./1a.excalidraw.png]] +** b +#+attr_latex: :width 250px +[[./1b.excalidraw.png]] +** c +#+attr_latex: :width 250px +[[./1c.excalidraw.png]] +** d +#+attr_latex: :width 250px +[[./1d.excalidraw.png]] +** e +#+attr_latex: :width 250px +[[./1e.excalidraw.png]] +** f +#+attr_latex: :width 200px +[[./1f.excalidraw.png]] + +* Question Two +| State | a | b | e | +| 0 | \phi | \phi | {1, 3} | +| 1 | {2, 4} | {4} | \phi | +| 2 | {2} | \phi | \phi | +| 3 | \phi | {4} | \phi | +| 4 | \phi | {4} | \phi | + +* Question Three +| State | a | b | +| 0 | 1 | 0 | +| 1 | 1 | 2 | +| 2 | 1 | 3 | +| 3 | 1 | 0 | + +* Question Four +** a +#+attr_latex: :width 350px +[[./4a.excalidraw.png]] +** b +| DFA State | NFA States | a | b | c | +| d_0 | {s_0, s_1, s_2, s_5, s_9} | {s_3, s_6} (d_1) | s_e | s_e | +| d_1 | {s_3, s_6} | s_e | {s_4, s_8, s_9, s_1, s_2, s_5} (d_2) | {s_7, s_8, s_9, s_1, s_2, s_5} (d_3) | +| d_2 | {s_4, s_8, s_9, s_1, s_2, s_5} | {s_3, s_6} (d_1) | s_e | s_e | +| d_3 | {s_7, s_8, s_9, s_1, s_2, s_5} | {s_3, s_6} (d_1) | s_e | s_e | + +#+attr_latex: :width 200px +[[./4b.excalidraw.png]] +** c +| partition | a | b | c | +| {d_1}, {d_0, d_2, d_3} | {d_0, d_2, d_3} \Rightarrow {d_1, d_0, d_0} | {d_0, d_2, d_3} \Rightarrow {-, -, -} | {d_0, d_2, d_3} \Rightarrow {-, -, -} | +| {d_1}, {d_0}, {d_2, d_3} | {d_2, d_3} \Rightarrow {d_0, d_0} | {d_2, d_3} \Rightarrow {-, -} | {d_2, d_3} \Rightarrow {-, -} | + +#+attr_latex: :width 200px +[[./4c.excalidraw.png]] + +* Question Five +** regex 1 +*** NFA +#+attr_latex: :width 350px +[[./51a.excalidraw.png]] + +*** DFA +| DFA State | NFA States | 1 | 0 | +| d_0 | {s_0, s_1, s_2, s_4, s_7} | {s_1, s_2, s_3, s_4, s_6, s_7} (d_1) | {s_1, s_2, s_4, s_5, s_6, s_7} (d_2) | +| d_1 | {s_1, s_2, s_3, s_4, s_6, s_7} | d_1 | d_2 | +| d_2 | {s_1, s_2, s_4, s_5, s_6, s_7} | d_1 | d_2 | + +#+attr_latex: :width 200px +[[./51b.excalidraw.png]] + +*** Minimization +| partition | 1 | 0 | +| {d_0, d_1, d_2} | {d_0, d_1, d_2} \Rightarrow {d_1, d_1, d_1} | {d_0, d_1, d_2} \Rightarrow {d_2, d_2, d_2} | + +#+attr_latex: :width 75px +[[./51c.excalidraw.png]] + +** regex 2 +*** NFA +#+attr_latex: :width 350px +[[./52a.excalidraw.png]] +*** DFA +| DFA State | NFA States | 1 | 0 | +| d_0 | {s_0, s_1, s_2, s_3, s_5, s_11, s_6, s_12 } | {s_1, s_2, s_3, s_5, s_6, s_7, s_8, s_10, s_11, s_12} (d_1) | {s_1, s_2, s_3, s_4, s_5, s_11, s_6, s_12} (d_2) | +| d_1 | {s_1, s_2, s_3, s_5, s_6, s_7, s_8, s_10, s_11, s_12} | d_1 | {s_1, s_2, s_3, s_4, s_5, s_9, s_11, s_6, s_12} (d_3) | +| d_2 | {s_1, s_2, s_3, s_4, s_5, s_11, s_6} | d_1 | d_2 | +| d_3 | {s_1, s_2, s_3, s_4, s_5, s_9, s_11, s_6, s_12} | d_1 | d_2 | + +#+attr_latex: :width 150px +[[./52b.excalidraw.png]] + +*** Minimization +| partition | 1 | 0 | +| {d_0, d_1, d_2, d_3} | {d_0, d_1, d_2, d_3} \Rightarrow {d_1, d_1, d_1, d_1} | {d_0, d_1, d_2, d_3} \Rightarrow {d_2, d_3, d_2, d_2} | + +#+attr_latex: :width 100px +[[./52c.excalidraw.png]] + +* Question Six +** a +*** DFA +#+attr_latex: :width 350px +[[./6a.excalidraw.png]] + +*** catMap +#+BEGIN_SRC json +{ + '-': 'sign', + '+': 'sign', + [0-9]: 'digit', + 'e': 'scntfc' +} +#+END_SRC + +*** transMap +#+BEGIN_SRC json +{ + 's0': { + 'sign': 's1', + 'digit': 's2', + }, + 's1': { + 'digit': 's2', + }, + 's2': { + 'digit': 's2', + 'scntfc': 's3', + }, + 's3': { + 'sign': 's4', + 'digit': 's5', + }, + 's4': { + 'digit': 's6', + }, + 's5': { + 'digit': 's6', + }, + 's6': { + 'digit': 's6', + }, +} +#+END_SRC + +*** tokenTypes +#+BEGIN_SRC json +{ + 's2': 'integer', + 's6': 'integer, +} +#+END_SRC +** b +*** DFA +#+attr_latex: :width 200px +[[./6b.excalidraw.png]] + +*** catMap +#+BEGIN_SRC json +{ + [a-zA-Z]: 'letter', + '_': 'letter', + [0-9]: 'digit', +} +#+END_SRC + +*** transMap +#+BEGIN_SRC json +{ + 's1': { + 'letter': 's2' + }, + 's2': { + 'letter': 's2', + 'digit': 's2', + }, +} +#+END_SRC + +** c +*** DFA +#+attr_latex: :width 400px +[[./6c.excalidraw.png]] + +*** catMap +#+BEGIN_SRC json +{ + [^(\n)*/]: 'nonc', + '\n': 'newline', + 'eof': 'eof', + '/': 'slash', + '*': 'star' +} +#+END_SRC +*** transMap +#+BEGIN_SRC json +{ + 's0': { + 'slash': 's1', + }, + 's1': { + 'slash': 's2', + 'star': 's4', + }, + 's2': { + 'nonc': 's2', + 'newline': 's3', + 'eof': 's3' + }, + 's4': { + 'nonc': 's4', + 'star': 's5', + }, + 's5': { + 'nonc': 's4', + 'newline': 's4', + 'slash': 's6' + } +} +#+END_SRC + +*** tokenType +#+BEGIN_SRC json +{ + 's3': 'singleLineComment', + 's6': 'multiLineComment', +} +#+END_SRC + diff --git a/Homework/cs5300/homework-four/compilers_assn_4.pdf b/Homework/cs5300/homework-four/compilers_assn_4.pdf Binary files differnew file mode 100644 index 0000000..44fc347 --- /dev/null +++ b/Homework/cs5300/homework-four/compilers_assn_4.pdf diff --git a/Homework/cs5300/homework-four/compilers_assn_4.tex b/Homework/cs5300/homework-four/compilers_assn_4.tex new file mode 100644 index 0000000..b1a2942 --- /dev/null +++ b/Homework/cs5300/homework-four/compilers_assn_4.tex @@ -0,0 +1,346 @@ +% Created 2023-02-13 Mon 21:13 +% Intended LaTeX compiler: pdflatex +\documentclass[11pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage{amsmath} \usepackage[a4paper,margin=1in,landscape]{geometry} +\author{Lizzy Hunt} +\date{\today} +\title{Assignment Three} +\hypersetup{ + pdfauthor={Lizzy Hunt}, + pdftitle={Assignment Three}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:org48bca57} +\subsection{a} +\label{sec:orgede0650} +\begin{center} +\includegraphics[width=250px]{./1a.excalidraw.png} +\end{center} +\subsection{b} +\label{sec:org59deb20} +\begin{center} +\includegraphics[width=250px]{./1b.excalidraw.png} +\end{center} +\subsection{c} +\label{sec:orgfdb1a94} +\begin{center} +\includegraphics[width=250px]{./1c.excalidraw.png} +\end{center} +\subsection{d} +\label{sec:org48223e7} +\begin{center} +\includegraphics[width=250px]{./1d.excalidraw.png} +\end{center} +\subsection{e} +\label{sec:orga20ed32} +\begin{center} +\includegraphics[width=250px]{./1e.excalidraw.png} +\end{center} +\subsection{f} +\label{sec:org14bd212} +\begin{center} +\includegraphics[width=200px]{./1f.excalidraw.png} +\end{center} + +\section{Question Two} +\label{sec:org576a7f2} +\begin{center} +\begin{tabular}{rlll} +State & a & b & e\\[0pt] +0 & \(\phi\) & \(\phi\) & \{1, 3\}\\[0pt] +1 & \{2, 4\} & \{4\} & \(\phi\)\\[0pt] +2 & \{2\} & \(\phi\) & \(\phi\)\\[0pt] +3 & \(\phi\) & \{4\} & \(\phi\)\\[0pt] +4 & \(\phi\) & \{4\} & \(\phi\)\\[0pt] +\end{tabular} +\end{center} + +\section{Question Three} +\label{sec:org998bd71} +\begin{center} +\begin{tabular}{rrr} +State & a & b\\[0pt] +0 & 1 & 0\\[0pt] +1 & 1 & 2\\[0pt] +2 & 1 & 3\\[0pt] +3 & 1 & 0\\[0pt] +\end{tabular} +\end{center} + +\section{Question Four} +\label{sec:org7c30c40} +\subsection{a} +\label{sec:org487ab26} +\begin{center} +\includegraphics[width=350px]{./4a.excalidraw.png} +\end{center} +\subsection{b} +\label{sec:org41a1b03} +\begin{center} +\begin{tabular}{lllll} +DFA State & NFA States & a & b & c\\[0pt] +d\textsubscript{0} & \{s\textsubscript{0}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{5}, s\textsubscript{9}\} & \{s\textsubscript{3}, s\textsubscript{6}\} (d\textsubscript{1}) & s\textsubscript{e} & s\textsubscript{e}\\[0pt] +d\textsubscript{1} & \{s\textsubscript{3}, s\textsubscript{6}\} & s\textsubscript{e} & \{s\textsubscript{4}, s\textsubscript{8}, s\textsubscript{9}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{5}\} (d\textsubscript{2}) & \{s\textsubscript{7}, s\textsubscript{8}, s\textsubscript{9}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{5}\} (d\textsubscript{3})\\[0pt] +d\textsubscript{2} & \{s\textsubscript{4}, s\textsubscript{8}, s\textsubscript{9}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{5}\} & \{s\textsubscript{3}, s\textsubscript{6}\} (d\textsubscript{1}) & s\textsubscript{e} & s\textsubscript{e}\\[0pt] +d\textsubscript{3} & \{s\textsubscript{7}, s\textsubscript{8}, s\textsubscript{9}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{5}\} & \{s\textsubscript{3}, s\textsubscript{6}\} (d\textsubscript{1}) & s\textsubscript{e} & s\textsubscript{e}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=200px]{./4b.excalidraw.png} +\end{center} +\subsection{c} +\label{sec:orgf8b74f7} +\begin{center} +\begin{tabular}{llll} +partition & a & b & c\\[0pt] +\{d\textsubscript{1}\}, \{d\textsubscript{0}, d\textsubscript{2}, d\textsubscript{3}\} & \{d\textsubscript{0}, d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{d\textsubscript{1}, d\textsubscript{0}, d\textsubscript{0}\} & \{d\textsubscript{0}, d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{-, -, -\} & \{d\textsubscript{0}, d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{-, -, -\}\\[0pt] +\{d\textsubscript{1}\}, \{d\textsubscript{0}\}, \{d\textsubscript{2}, d\textsubscript{3}\} & \{d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{d\textsubscript{0}, d\textsubscript{0}\} & \{d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{-, -\} & \{d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{-, -\}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=200px]{./4c.excalidraw.png} +\end{center} + +\section{Question Five} +\label{sec:org8639475} +\subsection{regex 1} +\label{sec:org6ec41c1} +\subsubsection{NFA} +\label{sec:org4702761} +\begin{center} +\includegraphics[width=350px]{./51a.excalidraw.png} +\end{center} + +\subsubsection{DFA} +\label{sec:org1f48afb} +\begin{center} +\begin{tabular}{llll} +DFA State & NFA States & 1 & 0\\[0pt] +d\textsubscript{0} & \{s\textsubscript{0}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{4}, s\textsubscript{7}\} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{6}, s\textsubscript{7}\} (d\textsubscript{1}) & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{6}, s\textsubscript{7}\} (d\textsubscript{2})\\[0pt] +d\textsubscript{1} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{6}, s\textsubscript{7}\} & d\textsubscript{1} & d\textsubscript{2}\\[0pt] +d\textsubscript{2} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{6}, s\textsubscript{7}\} & d\textsubscript{1} & d\textsubscript{2}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=200px]{./51b.excalidraw.png} +\end{center} + +\subsubsection{Minimization} +\label{sec:orgbd13a9a} +\begin{center} +\begin{tabular}{lrr} +partition & 1 & 0\\[0pt] +\{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}\} & \{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}\} \(\Rightarrow\) \{d\textsubscript{1}, d\textsubscript{1}, d\textsubscript{1}\} & \{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}\} \(\Rightarrow\) \{d\textsubscript{2}, d\textsubscript{2}, d\textsubscript{2}\}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=75px]{./51c.excalidraw.png} +\end{center} + +\subsection{regex 2} +\label{sec:org65aa1cc} +\subsubsection{NFA} +\label{sec:org0a8dd3a} +\begin{center} +\includegraphics[width=350px]{./52a.excalidraw.png} +\end{center} +\subsubsection{DFA} +\label{sec:orgbf87fa0} +\begin{center} +\begin{tabular}{llll} +DFA State & NFA States & 1 & 0\\[0pt] +d\textsubscript{0} & \{s\textsubscript{0}, s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{5}, s\textsubscript{11}, s\textsubscript{6}, s\textsubscript{12} \} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{5}, s\textsubscript{6}, s\textsubscript{7}, s\textsubscript{8}, s\textsubscript{10}, s\textsubscript{11}, s\textsubscript{12}\} (d\textsubscript{1}) & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{11}, s\textsubscript{6}, s\textsubscript{12}\} (d\textsubscript{2})\\[0pt] +d\textsubscript{1} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{5}, s\textsubscript{6}, s\textsubscript{7}, s\textsubscript{8}, s\textsubscript{10}, s\textsubscript{11}, s\textsubscript{12}\} & d\textsubscript{1} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{9}, s\textsubscript{11}, s\textsubscript{6}, s\textsubscript{12}\} (d\textsubscript{3})\\[0pt] +d\textsubscript{2} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{11}, s\textsubscript{6}\} & d\textsubscript{1} & d\textsubscript{2}\\[0pt] +d\textsubscript{3} & \{s\textsubscript{1}, s\textsubscript{2}, s\textsubscript{3}, s\textsubscript{4}, s\textsubscript{5}, s\textsubscript{9}, s\textsubscript{11}, s\textsubscript{6}, s\textsubscript{12}\} & d\textsubscript{1} & d\textsubscript{2}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=150px]{./52b.excalidraw.png} +\end{center} + +\subsubsection{Minimization} +\label{sec:orgba6f5c6} +\begin{center} +\begin{tabular}{lrr} +partition & 1 & 0\\[0pt] +\{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}, d\textsubscript{3}\} & \{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{d\textsubscript{1}, d\textsubscript{1}, d\textsubscript{1}, d\textsubscript{1}\} & \{d\textsubscript{0}, d\textsubscript{1}, d\textsubscript{2}, d\textsubscript{3}\} \(\Rightarrow\) \{d\textsubscript{2}, d\textsubscript{3}, d\textsubscript{2}, d\textsubscript{2}\}\\[0pt] +\end{tabular} +\end{center} + +\begin{center} +\includegraphics[width=100px]{./52c.excalidraw.png} +\end{center} + +\section{Question Six} +\label{sec:org7b91bcb} +\subsection{a} +\label{sec:orgb412f3c} +\subsubsection{DFA} +\label{sec:org4ae1ae3} +\begin{center} +\includegraphics[width=350px]{./6a.excalidraw.png} +\end{center} + +\subsubsection{catMap} +\label{sec:org443dec8} +\begin{verbatim} +{ + '-': 'sign', + '+': 'sign', + [0-9]: 'digit', + 'e': 'scntfc' +} +\end{verbatim} + +\subsubsection{transMap} +\label{sec:org77c35dc} +\begin{verbatim} +{ + 's0': { + 'sign': 's1', + 'digit': 's2', + }, + 's1': { + 'digit': 's2', + }, + 's2': { + 'digit': 's2', + 'scntfc': 's3', + }, + 's3': { + 'sign': 's4', + 'digit': 's5', + }, + 's4': { + 'digit': 's6', + }, + 's5': { + 'digit': 's6', + }, + 's6': { + 'digit': 's6', + }, +} +\end{verbatim} + +\subsubsection{tokenTypes} +\label{sec:orgefb9ab0} +\begin{verbatim} +{ + 's2': 'integer', + 's6': 'integer, +} +\end{verbatim} +\subsection{b} +\label{sec:org6479f00} +\subsubsection{DFA} +\label{sec:org3c2c109} +\begin{center} +\includegraphics[width=200px]{./6b.excalidraw.png} +\end{center} + +\subsubsection{catMap} +\label{sec:org5fb6951} +\begin{verbatim} +{ + [a-zA-Z]: 'letter', + '_': 'letter', + [0-9]: 'digit', +} +\end{verbatim} + +\subsubsection{transMap} +\label{sec:org51968cd} +\begin{verbatim} +{ + 's1': { + 'letter': 's2' + }, + 's2': { + 'letter': 's2', + 'digit': 's2', + }, +} +\end{verbatim} + +\subsection{c} +\label{sec:orgaadecbc} +\subsubsection{DFA} +\label{sec:orgbc936b3} +\begin{center} +\includegraphics[width=400px]{./6c.excalidraw.png} +\end{center} + +\subsubsection{catMap} +\label{sec:org0f61017} +\begin{verbatim} +{ + [^(\n)*/]: 'nonc', + '\n': 'newline', + 'eof': 'eof', + '/': 'slash', + '*': 'star' +} +\end{verbatim} +\subsubsection{transMap} +\label{sec:orge8e62ef} +\begin{verbatim} +{ + 's0': { + 'slash': 's1', + }, + 's1': { + 'slash': 's2', + 'star': 's4', + }, + 's2': { + 'nonc': 's2', + 'newline': 's3', + 'eof': 's3' + }, + 's4': { + 'nonc': 's4', + 'star': 's5', + }, + 's5': { + 'nonc': 's4', + 'newline': 's4', + 'slash': 's6' + } +} +\end{verbatim} + +\subsubsection{tokenType} +\label{sec:orgb999994} +\begin{verbatim} +{ + 's3': 'singleLineComment', + 's6': 'multiLineComment', +} +\end{verbatim} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-one/compilers-one.org b/Homework/cs5300/homework-one/compilers-one.org new file mode 100644 index 0000000..a723cd8 --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.org @@ -0,0 +1,137 @@ +#+TITLE: Homework - Chapter One +#+AUTHOR: Logan Hunt +#+OPTIONS: toc:nil +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \noindent \notag \usepackage{ dsfont } + +* Question One +A compiler is a program that reads source in one language, and translates it to an equivalent program to another language, +which can immediately be run in that language. An interpreter, on the other hand, directly executes the source as the +program continues. + +* Question Two +** a +Compiled machine code is typically much faster than interpreted instructions. +** b +An interpreter can give better error diagnostics. + +* Question Three +In terms of portability, compiled programs are less so than interpreters. Compiled programs (to machine code) target +a single architecture, and thus require seperate compilation, or cross-compilation over all target architectures. +Interpreted programs on the other hand will run anywhere, as long as there is an interpreter implementation for the +architecture. + +* Question Four +Java is a shady devil that likes to play both sides. Java source is compiled to intermediate Java bytecodes, which +are then interpreted by the Java Virtual Machine. The compilation aspect of Java here, is in the translation to JVM +bytecode instructions. + +* Question Five +One might want to view generated assembly code to debug an issue in their code and step through the compiler's +output, understand any optimizations the compiler may make, or to just explore its output. + +* Question Six +\begin{verbatim} +R1 = 20 +R2 = 12 +\end{verbatim} + +* Question Seven +id2 is not a register + +* Question Eight +\begin{verbatim} +1. R1 = 9 +2. R2 = 2 +3. R1 = 11 +4. R1 = 6.0 +5. id3 = R1 = 6.0 +\end{verbatim} + +* Question Nine +** JavaScript ++ imperative ++ declarative ++ third-generation ++ object-oriented (through prototypes) ++ functional ++ scripting +** Python ++ imperative ++ declarative ++ third-generation ++ object-oriented ++ functional ++ scripting + +* Question Ten +According to the book, "A distinguishing feature of object-oriented programming is the ability of each object to invoke the appropriate +method in response to a message." In C, this is not possible as structs do not have support +for methods. + +* Question Eleven +1. Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably + be a nightmare to fix. +2. Anything in the Linux kernel that runs in user space. +3. ~malloc~ + +* Question Twelve +This contrived program: +#+BEGIN_SRC C + int main() { + int i; + for (i = 0; i < 10000000; i++); + return 0; + } +#+END_SRC + +It wouldn't be so bad for the compiler to just set i = 10000000. + +* Question Thirteen +| Declaration | Scope | +| ~int b = 1~ | B_1 - B_2 | +| ~int a = 2~ | B_2 - B_4 | +| ~int b = 2~ | B_2 - B_3 - B_4 | +| ~int b = 3~ | B_3 | +| ~int a = 4~ | B_4 - B_5 | +| ~int b = 4~ | B_4 | +| ~int a = 5~ | B_5 | + +* Question Fourteen +\begin{verbatim} +i = 5 +j = 8 + i = 4[ +j = 9 +w = j - i = 9 - 4 = 5 +x = j - i = 9 - 5 = 4 + j = 10 +y = j - i = 10 - 5 = 5 +i = 3 +z = j - i = 9 - 3 = 6 +\end{verbatim} + +Thus, ~w = 5, x = 4, y = 5, z = 6~. + +* Question Fifteen +\begin{verbatim} +i = 2 +j = 5 + i = 3 +w = i + j = 3 + 2 = 5 +x = i + j = 2 + 5 = 7 + j = 9 +i = 7 +y = i + j = 7 + 9 = 16 +i = 6 +z = i + j = 6 + 5 = 11 +\end{verbatim} + +Thus, ~w = 5, x = 7, y = 16, z = 11~ + +* Question Sixteen +b : x = 4 \Rightarrow x = (x+3)-1 \Rightarrow x = 6 + +c : x = 1 \Rightarrow (x + 3) \Rightarrow 4 + +"6,4" diff --git a/Homework/cs5300/homework-one/compilers-one.pdf b/Homework/cs5300/homework-one/compilers-one.pdf Binary files differnew file mode 100644 index 0000000..f650f17 --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.pdf diff --git a/Homework/cs5300/homework-one/compilers-one.tex b/Homework/cs5300/homework-one/compilers-one.tex new file mode 100644 index 0000000..223758a --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.tex @@ -0,0 +1,191 @@ +% Created 2023-01-16 Mon 13:28 +% Intended LaTeX compiler: pdflatex +\documentclass[11pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\noindent \notag \usepackage{ dsfont } +\author{Logan Hunt} +\date{\today} +\title{Homework - Chapter One} +\hypersetup{ + pdfauthor={Logan Hunt}, + pdftitle={Homework - Chapter One}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.5.5)}, + pdflang={English}} +\begin{document} + +\maketitle + +\section{Question One} +\label{sec:orge85a534} +A compiler is a program that reads source in one language, and translates it to an equivalent program to another language, +which can immediately be run in that language. An interpreter, on the other hand, directly executes the source as the +program continues. + +\section{Question Two} +\label{sec:orgb550d55} +\subsection{a} +\label{sec:org1f4ed7b} +Compiled machine code is typically much faster than interpreted instructions. +\subsection{b} +\label{sec:orga3b0952} +An interpreter can give better error diagnostics. + +\section{Question Three} +\label{sec:orgdddbe9a} +In terms of portability, compiled programs are less so than interpreters. Compiled programs (to machine code) target +a single architecture, and thus require seperate compilation, or cross-compilation over all target architectures. +Interpreted programs on the other hand will run anywhere, as long as there is an interpreter implementation for the +architecture. + +\section{Question Four} +\label{sec:org348f3da} +Java is a shady devil that likes to play both sides. Java source is compiled to intermediate Java bytecodes, which +are then interpreted by the Java Virtual Machine. The compilation aspect of Java here, is in the translation to JVM +bytecode instructions. + +\section{Question Five} +\label{sec:org2f098fc} +One might want to view generated assembly code to debug an issue in their code and step through the compiler's +output, understand any optimizations the compiler may make, or to just explore its output. + +\section{Question Six} +\label{sec:org50a4a97} +\begin{verbatim} +R1 = 20 +R2 = 12 +\end{verbatim} + +\section{Question Seven} +\label{sec:org199da87} +id2 is not a register + +\section{Question Eight} +\label{sec:org3216fe2} +\begin{verbatim} +1. R1 = 9 +2. R2 = 2 +3. R1 = 11 +4. R1 = 6.0 +5. id3 = R1 = 6.0 +\end{verbatim} + +\section{Question Nine} +\label{sec:org2cf7709} +\subsection{JavaScript} +\label{sec:org769857f} +\begin{itemize} +\item imperative +\item declarative +\item third-generation +\item object-oriented (through prototypes) +\item functional +\item scripting +\end{itemize} +\subsection{Python} +\label{sec:orgb53d3f0} +\begin{itemize} +\item imperative +\item declarative +\item third-generation +\item object-oriented +\item functional +\item scripting +\end{itemize} + +\section{Question Ten} +\label{sec:org6722ba8} +According to the book, "A distinguishing feature of object-oriented programming is the ability of each object to invoke the appropriate +method in response to a message." In C, this is not possible as structs do not have support +for methods. + +\section{Question Eleven} +\label{sec:orgc3a76ec} +\begin{enumerate} +\item Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably +be a nightmare to fix. +\item Anything in the Linux kernel that runs in user space. +\item \texttt{malloc} +\end{enumerate} + +\section{Question Twelve} +\label{sec:org5f61a58} +This contrived program: +\begin{verbatim} +int main() { + int i; + for (i = 0; i < 10000000; i++); + return 0; +} +\end{verbatim} + +It wouldn't be so bad for the compiler to just set i = 10000000. + +\section{Question Thirteen} +\label{sec:orgebfa92b} +\begin{center} +\begin{tabular}{ll} +Declaration & Scope\\[0pt] +\texttt{int b = 1} & B\textsubscript{1} - B\textsubscript{2}\\[0pt] +\texttt{int a = 2} & B\textsubscript{2} - B\textsubscript{4}\\[0pt] +\texttt{int b = 2} & B\textsubscript{2} - B\textsubscript{3} - B\textsubscript{4}\\[0pt] +\texttt{int b = 3} & B\textsubscript{3}\\[0pt] +\texttt{int a = 4} & B\textsubscript{4} - B\textsubscript{5}\\[0pt] +\texttt{int b = 4} & B\textsubscript{4}\\[0pt] +\texttt{int a = 5} & B\textsubscript{5}\\[0pt] +\end{tabular} +\end{center} + +\section{Question Fourteen} +\label{sec:org5304edd} +\begin{verbatim} +i = 5 +j = 8 + i = 4[ +j = 9 +w = j - i = 9 - 4 = 5 +x = j - i = 9 - 5 = 4 + j = 10 +y = j - i = 10 - 5 = 5 +i = 3 +z = j - i = 9 - 3 = 6 +\end{verbatim} + +Thus, \texttt{w = 5, x = 4, y = 5, z = 6}. + +\section{Question Fifteen} +\label{sec:org9f74779} +\begin{verbatim} +i = 2 +j = 5 + i = 3 +w = i + j = 3 + 2 = 5 +x = i + j = 2 + 5 = 7 + j = 9 +i = 7 +y = i + j = 7 + 9 = 16 +i = 6 +z = i + j = 6 + 5 = 11 +\end{verbatim} + +Thus, \texttt{w = 5, x = 7, y = 16, z = 11} + +\section{Question Sixteen} +\label{sec:org31a90e4} +b : x = 4 \(\Rightarrow\) x = (x+3)-1 \(\Rightarrow\) x = 6 + +c : x = 1 \(\Rightarrow\) (x + 3) \(\Rightarrow\) 4 + +"6,4" +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-one/hw-ch1-src-1.zip b/Homework/cs5300/homework-one/hw-ch1-src-1.zip Binary files differnew file mode 100644 index 0000000..c8bbce3 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src-1.zip diff --git a/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf b/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf Binary files differnew file mode 100644 index 0000000..6f70e51 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf diff --git a/Homework/cs5300/homework-one/hw-ch1-src/common.tex b/Homework/cs5300/homework-one/hw-ch1-src/common.tex new file mode 100644 index 0000000..81550ab --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/common.tex @@ -0,0 +1,52 @@ +\usepackage{version} + +\usepackage{makeidx} % allows index generation +\usepackage{graphicx} % standard LaTeX graphics tool + % when including figure files +\usepackage{subfigure} +\usepackage{multicol} % used for the two-column index +\usepackage[bottom]{footmisc}% places footnotes at page bottom +\usepackage{amstext,amssymb,amsmath} % I included these - AKG +\usepackage{url} +\usepackage{color} +\usepackage{multirow} +\usepackage{booktabs} + +% example use of listings (font is fixed-width): +% \begin{lstlisting}[mathescape,basicstyle=\fontfamily{lmvtt}\selectfont] +\usepackage{listings} +\lstset{basicstyle=\ttfamily} + +%------------------------------------------------------------------------------- +% Decrease margins +%------------------------------------------------------------------------------- +\oddsidemargin0cm +\topmargin-2cm +\textwidth16.5cm +\textheight23.5cm + +%------------------------------------------------------------------------------- +% this makes list spacing much better. +%------------------------------------------------------------------------------- +\newenvironment{tightenumerate}{ +\begin{enumerate} + \setlength{\itemsep}{1pt} + \setlength{\parskip}{0pt} + \setlength{\parsep}{0pt} +}{\end{enumerate} +} + +%------------------------------------------------------------------------------- +% this makes list spacing much better. +%------------------------------------------------------------------------------- +\newenvironment{tightitemize}{ +\begin{itemize} + \setlength{\itemsep}{1pt} + \setlength{\parskip}{0pt} + \setlength{\parsep}{0pt} +}{\end{itemize} +} + +\newcommand{\myspace}{\vspace{4 mm}} +\newcommand{\soln}{\paragraph{Solution:}} + diff --git a/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex b/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex new file mode 100644 index 0000000..7a1eed3 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex @@ -0,0 +1,196 @@ +\documentclass{article} + +\input{common} + +\renewcommand{\tt}[1]{\texttt{#1}} +\newcommand{\kc}{$^*$} +\renewcommand{\not}{$^\wedge$} +\newcommand{\pc}{$^+$} +\newcommand{\e}{$\epsilon$} + +\includeversion{version:soln} + +\begin{document} + +\begin{flushleft} +Homework - Chapter 1 \\ +CS 5300 %\\ + +\end{flushleft} + +\begin{enumerate} +\item (6 points) What is the difference between a compiler and an interpreter? + \begin{version:soln} + \soln Put your solution here. + \end{version:soln} + +\myspace +\item (6 points) According to the book, what are the advantages of + \begin{tightenumerate} + \item a compiler over an interpreter? + \item an interpreter over a compiler? + \end{tightenumerate} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Discuss the portability across computers of compiled vs. interpreted programs. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Strictly speaking, Java is an interpreted language. Discuss how it has elements of being a compiled language. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) The C compiler \textit{gcc} has an \tt{-S} option that will output assembly code before the assembler is run. Why might someone want to do this? Give at least two reasons. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) If id2 has a value of 8 and id3 has a value of 12, after running the following code, what will be in registers R1 and R2? +\begin{lstlisting} +LDF R1, id2 +LDF R2, id3 +ADDF R1, R1, R2 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) The assembler will fail on this assembly code. Why? +\begin{lstlisting} +LDF R1, id2 +LDF R2, id3 +ADDF R1, id2, R2 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) If id5 is 2 and id7 is 9, what will be in id3 after running this code? +\begin{lstlisting} +LDF R1, id7 +LDF R2, id5 +ADDF R1, R1, R2 +MULF R1, R2, #3.0 +STF id3, R1 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Which of the following terms apply to Javascript? Which apply to Python? \\ +a)~imperative b)~declarative c)~von Neumann d)~object-oriented e)~third-generation f)~fourth-generation g)~scripting + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (5 points) C is not considered an object-oriented language, yet it has structs, which are kind of like objects. Why is it not object-oriented? + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Give three examples of programs for which optimization that changes the behavior of the program would be very, very bad. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (5 points) Give an example of a program for which optimization that changes the behavior of the program might not be so bad. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (8 points) Given the following blocks in a C++ program, create a table like figure 1.11 in the textbook. + +\includegraphics[width=.4\textwidth]{blocks.pdf} + +\begin{tabular}{c|c} +\toprule +Declaration & Scope \\ +\midrule +\tt{int b = 1;} & \\ +\tt{int a = 2;} & \\ +\tt{int b = 2;} & \\ +\tt{int b = 3;} & \\ +\tt{int a = 4;} & \\ +\tt{int b = 4;} & \\ +\tt{int a = 5;} & \\ +\bottomrule +\end{tabular} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +%\newpage +\item (8 points) For the following block-structured C code, indicate the values assigned to $w, x, y,$ and $z$. +\begin{lstlisting}[language=C] +int w, x, y, z; +int i = 5; +int j = 8; +{ int i = 4; + j = 9; + w = j - i; +} +x = j - i; +{ int j = 10; + y = j - i; + i = 3; +} +z = j - i; +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\newpage +\item (8 points) For the following block-structured C code, indicate the values assigned to $w, x, y,$ and $z$. +\begin{lstlisting}[language=C] +int w, x, y, z; +int i = 2; +int j = 5; +{ int i = 3; + w = i + j; +} +x = i + j; +{ int j = 9; + i = 7; + y = i + j; + { i = 6; + } +} +z = i + j; +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) What is printed by the following C code? +\begin{lstlisting}[language=C] +#define a (x+3) +int x = 4; +void b() { x = a-1; printf("%d,", x); } +void c() { int x = 1; printf("%d\n", a); } +void main() { b(); c(); } +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\end{enumerate} +\end{document} diff --git a/Homework/cs5300/homework-seven/compilers_assn_7.org b/Homework/cs5300/homework-seven/compilers_assn_7.org new file mode 100644 index 0000000..a6e5f35 --- /dev/null +++ b/Homework/cs5300/homework-seven/compilers_assn_7.org @@ -0,0 +1,285 @@ +#+TITLE: Assignment Seven +#+AUTHOR: Lizzy Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +#+LATEX: \setlength\parindent{0pt} +#+LATEX_COMPILER: lualatex +#+OPTIONS: toc:nil + +* Question One +** Removal of Left Recursion +\begin{verbatim} +A -> BbA' +A' -> aA' | ε +B -> aB | b +\end{verbatim} +** Left Factoring +No need to left factor this grammar +** Parse Table +*** Calculation of first and follow +\begin{verbatim} +first(A) = first(B) = {a, b} +first(A') = first(a) U first(ε) = {a, ε} +first(B) = first(a) U first(b) = {a, b} + +follow(A) = {$} +follow(A') = follow(A) = {$} +follow(B) = {b} +\end{verbatim} + +*** Predictive Parse Table +| Nonterminal | a | b | $ | +| A | A \rightarrow BbA' | A \rightarrow BbA' | | +| A' | A' \rightarrow aA' | | A' \rightarrow \epsilon | +| B | B \rightarrow aB | B \rightarrow b | | + +** Parse of "abba" +| Stack | Input | Action | Matched | +| A$ | abba$ | A \rightarrow BbA' | | +| BbA'$ | abba$ | B \rightarrow aB | | +| aBbA'$ | bba$ | Match a | a | +| BbA'$ | bba$ | B \rightarrow b | a | +| bbA'$ | bba$ | Match b | ab | +| bA'$ | ba$ | Match b | abb | +| A'$ | a$ | A \rightarrow aA' | abb | +| aA'$ | a$ | Match a | abba | +| A'$ | $ | A' \rightarrow ε | abba | +| ε$ | $ | Match ε | abba | +| $ | $ | Match $ | | +| | | Accept | | + +* Question Two +| Stack | Input | Action | +| $ | (id)*id$ | shift | +| $( | id)*id$ | shift | +| $(id | )*id$ | reduce F \rightarrow id | +| $(F | )*id$ | reduce F \rightarrow E | +| $(E) | *id$ | shift | +| $F | *id$ | reduce F \rightarrow (E) | +| $T | *id$ | reduce T \rightarrow F | +| $T* | id$ | shift | +| $T*id | $ | shift | +| $T*F | $ | reduce F \rightarrow id | +| $T | $ | reduce T \rightarrow T * F | +| $E | $ | reduce E \rightarrow T | +| $E | $ | accept | + +* Question Three +| Stack | Input | Action | +| $ | ()(())$ | Shift | +| $( | )(())$ | Shift | +| $() | (())$ | Shift | +| $Pair | (())$ | Reduce R5 | +| $List | (())$ | Reduce R3 | +| $List( | ())$ | Shift | +| $List(( | ))$ | Shift | +| $List(() | )$ | Shift | +| $List(Pair | )$ | Reduce R5 | +| $List(Pair) | $ | Shift | +| $List Pair | $ | Reduce R4 | +| $Goal | $ | Reduce R1 | +| $Goal | $ | Accept | + +* Question Four +** a +{[N \rightarrow \cdot N + N], [N \rightarrow \cdot NN], [N \rightarrow \cdot (N)], [N \rightarrow \cdot N *], [N \rightarrow \cdot a]} + +** b +{[N \rightarrow (\cdot N)], [N \rightarrow \cdot NN], [N \rightarrow \cdot (N)], [N \rightarrow \cdot N *], [N \rightarrow \cdot a]} + +** c +{[N \rightarrow (N \cdot)]} +* Question Five +** a +{[A \rightarrow \cdot C B], [C \rightarrow \cdot E D], [E \rightarrow \cdot x A y], [E \rightarrow \cdot x]} + +** b +{[B \rightarrow a \cdot C B], [C \rightarrow \cdot E D], [E \rightarrow \cdot x A y], [E \rightarrow \cdot x]} + +** c +{[B \rightarrow a C \cdot B], [B \rightarrow \cdot a C B]} + +** d +{[C \rightarrow E D \cdot]} + +** e +{[D \rightarrow b \cdot E D], [E \rightarrow \cdot x A y], [E \rightarrow \cdot z]} + +** f +{} + +* Question Six +| GOTO | state | items | +| | 0 | closure(A' \rightarrow \cdot A) | +| | | A' \rightarrow \cdot A | +| | | A \rightarrow \cdot a A | +| | | A \rightarrow \cdot B | +| | | B \rightarrow \cdot b | +| (0, A) | 1 | closure(A' \rightarrow A \cdot) | +| | | A' \rightarrow A \cdot | +| (0, B) | 2 | closure(A \rightarrow B \cdot) | +| | | A \rightarrow B \cdot | +| (0, a) | 3 | closure(A \rightarrow a \cdot A b B) | +| | | A \rightarrow a \cdot A | +| | | A \rightarrow \cdot a A | +| | | A \rightarrow \cdot B | +| | | B \rightarrow \cdot b | +| (0, b) | 4 | closure(B \rightarrow b \cdot ) | +| | | B \rightarrow b \cdot | +| (3, A) | 5 | closure(A \rightarrow a A \cdot b B) | +| | | A \rightarrow a A \cdot | +| (3, B) | 2 | A \rightarrow B \cdot | +| (3, a) | 3 | A \rightarrow a \cdot A | +| | | A \rightarrow \cdot a A | +| | | A \rightarrow \cdot B | +| | | B \rightarrow \cdot b | +| (3, b) | 4 | B \rightarrow b \cdot | +| (5, b) | 6 | closure(A \rightarrow a A b \cdot B) | +| | | A \rightarrow a A b \cdot B | +| | | B \rightarrow \cdot b | +| (6, B) | 7 | closure(A \rightarrow a A b B \cdot) | +| | | A \rightarrow a A b B \cdot | +| (6, b) | 4 | B \rightarrow b \cdot | + +* Question Seven +** First, Follow +\begin{verbatim} +first(A') = first(A) = {a} +first(A) = first(B) = {a} +first(B) = first(a) = {a} + +follow(A') = {$} +follow(A) = first(b) U follow(A') = {b, $} +follow(B) = first(a) U follow(A) = {a, b, $} +\end{verbatim} + +** Items +| GOTO | state | items | +| | 0 | closure(A' \rightarrow \cdot A) | +| | | A' \rightarrow \cdot A | +| | | A \rightarrow \cdot A b B | +| | | A \rightarrow \cdot B a | +| | | B \rightarrow \cdot a | +| (0, A) | 1 | closure({A \rightarrow A \cdot b B, A' \rightarrow A \cdot}) | +| | | A \rightarrow A \cdot b B | +| | | A' \rightarrow A \cdot | +| (0, B) | 2 | closure(A \rightarrow B \cdot a) | +| | | A \rightarrow B \cdot a | +| (0, a) | 3 | closure(B \rightarrow a \cdot) | +| | | B \rightarrow a \cdot | +| (1, b) | 4 | closure(A \rightarrow A b \cdot B) | +| | | A \rightarrow A b \cdot B | +| | | B \rightarrow \cdot a | +| (2, a) | 5 | closure(A \rightarrow B a \cdot) | +| | | A \rightarrow B a \cdot | +| (4, B) | 6 | closure(A \rightarrow A b B \cdot) | +| | | A \rightarrow A b B \cdot | +| (4, a) | 3 | closure(B \rightarrow a \cdot) | +| | | B \rightarrow a \cdot | + +** SLR Parse Table +| state | a | b | $ | A | B | +| 0 | s3 | | | 1 | 2 | +| 1 | | s4 | acc | | | +| 2 | s5 | | | | | +| 3 | r4 | r4 | r4 | | | +| 4 | s3 | | | | 6 | +| 5 | | r3 | r3 | | | +| 6 | | r2 | r2 | | | + +** Parse of "aaba" +| stack | symbols | input | action | +| 0 | | aaba$ | shift 3 | +| 0 3 | a | aba$ | reduce 4 | +| 0 2 | B | aba$ | shift 5 | +| 0 2 5 | Ba | ba$ | reduce 3 | +| 0 1 | A | ba$ | shift 4 | +| 0 1 4 | Ab | a$ | shift 3 | +| 0 1 4 3 | Aba | $ | reduce 4 | +| 0 1 4 6 | AbB | $ | reduce 2 | +| 0 1 | A | $ | accept | + +* Question Eight +** First, Follow +\begin{verbatim} +first(A') = first(A) = {a, b, f} +first(A) = first(a) U first(B) = {a, b, f} +first(B) = first(b) U first(f) = {b, f} + +follow(A') = {$} +follow(A) = follow(A') = {$} +follow(B) = first(a) = {a} +\end{verbatim} +** Items +| GOTO | state | items | +| | 0 | closure(A' \rightarrow \cdot A, \$) | +| | | A' \rightarrow \cdot A, $ | +| | | A \rightarrow \cdot B a, $ | +| | | A \rightarrow \cdot a A, $ | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (0, A) | 1 | closure(A' \rightarrow A \cdot, $) | +| | | A' \rightarrow A \cdot, $ | +| (0, B) | 2 | closure(A \rightarrow B \cdot a, $) | +| | | A \rightarrow B \cdot a, $ | +| (0, a) | 3 | closure(A \rightarrow a \cdot A, $) | +| | | A \rightarrow a \cdot A, $ | +| | | A \rightarrow \cdot B a, $ | +| | | A \rightarrow \cdot a A, $ | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (0, b) | 4 | closure(B \rightarrow b \cdot B, a) | +| | | B \rightarrow b \cdot B, a | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (0, f) | 5 | closure(B \rightarrow f \cdot, a) | +| | | B \rightarrow f \cdot, a | +| (2, a) | 6 | closure(A \rightarrow B a \cdot, $) | +| | | A \rightarrow B a \cdot, $ | +| (3, A) | 7 | closure(A \rightarrow a A \cdot, $) | +| | | A \rightarrow a A \cdot, \$ | +| (3, B) | 2 | closure(A \rightarrow B \cdot a, $) | +| | | A \rightarrow B \cdot a, $ | +| (3, a) | 3 | closure(A \rightarrow a \cdot A, $) | +| | | A \rightarrow a \cdot A, $ | +| | | A \rightarrow \cdot B a, $ | +| | | A \rightarrow \cdot a A, $ | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (3, b) | 4 | closure(B \rightarrow b \cdot B, a) | +| | | B \rightarrow b \cdot B, a | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (3, f) | 5 | closure(B \rightarrow f \cdot, a) | +| | | B \rightarrow f \cdot, a | +| (4, B) | 8 | closure(B \rightarrow b B \cdot, a) | +| | | B \rightarrow b B \cdot, a | +| (4, b) | 4 | closure(B \rightarrow b \cdot B, a) | +| | | B \rightarrow b \cdot B, a | +| | | B \rightarrow \cdot b B, a | +| | | B \rightarrow \cdot f, a | +| (4, f) | 5 | closure(B \rightarrow f \cdot, a) | +| | | B \rightarrow f \cdot, a | +** LR Parse Table +| state | a | b | f | $ | A | B | +| 0 | s3 | s4 | s5 | | 1 | 2 | +| 1 | | | | acc | | | +| 2 | s6 | | | | | | +| 3 | s3 | s4 | s5 | | 7 | 2 | +| 4 | | s4 | s5 | | | 8 | +| 5 | r5 | | | | | | +| 6 | | | | r2 | | | +| 7 | | | | r3 | | | +| 8 | r4 | | | | | | + +** Parse of "abfa" +| stack | symbols | input | action | +| 0 | | abfa$ | shift 3 | +| 0 3 | a | bfa$ | shift 4 | +| 0 3 4 | ab | fa$ | shift 5 | +| 0 3 4 5 | abf | a$ | reduce 5 | +| 0 3 4 8 | abB | a$ | reduce 4 | +| 0 3 2 | aB | a$ | shift 6 | +| 0 3 2 6 | aBa | $ | reduce 2 | +| 0 3 7 | aBa | $ | reduce 3 | +| 0 1 | aA | $ | accept | diff --git a/Homework/cs5300/homework-seven/compilers_assn_7.pdf b/Homework/cs5300/homework-seven/compilers_assn_7.pdf Binary files differnew file mode 100644 index 0000000..9c80b5e --- /dev/null +++ b/Homework/cs5300/homework-seven/compilers_assn_7.pdf diff --git a/Homework/cs5300/homework-seven/compilers_assn_7.tex b/Homework/cs5300/homework-seven/compilers_assn_7.tex new file mode 100644 index 0000000..fc4c607 --- /dev/null +++ b/Homework/cs5300/homework-seven/compilers_assn_7.tex @@ -0,0 +1,381 @@ +% Created 2023-02-27 Mon 22:14 +% Intended LaTeX compiler: lualatex +\documentclass[11pt]{article} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +\author{Lizzy Hunt} +\date{\today} +\title{Assignment Seven} +\hypersetup{ + pdfauthor={Lizzy Hunt}, + pdftitle={Assignment Seven}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:org41ddc16} +\subsection{Removal of Left Recursion} +\label{sec:org2e2ade2} +\begin{verbatim} +A -> BbA' +A' -> aA' | ε +B -> aB | b +\end{verbatim} +\subsection{Left Factoring} +\label{sec:orge4a4f75} +No need to left factor this grammar +\subsection{Parse Table} +\label{sec:org43628e4} +\subsubsection{Calculation of first and follow} +\label{sec:org8e7d2cc} +\begin{verbatim} +first(A) = first(B) = {a, b} +first(A') = first(a) U first(ε) = {a, ε} +first(B) = first(a) U first(b) = {a, b} + +follow(A) = {$} +follow(A') = follow(A) = {$} +follow(B) = {b} +\end{verbatim} + +\subsubsection{Predictive Parse Table} +\label{sec:orgc2ae298} +\begin{center} +\begin{tabular}{llll} +Nonterminal & a & b & \$\\[0pt] +A & A \(\rightarrow\) BbA' & A \(\rightarrow\) BbA' & \\[0pt] +A' & A' \(\rightarrow\) aA' & & A' \(\rightarrow\) \(\epsilon\)\\[0pt] +B & B \(\rightarrow\) aB & B \(\rightarrow\) b & \\[0pt] +\end{tabular} +\end{center} + +\subsection{Parse of "abba"} +\label{sec:orge26f0a1} +\begin{center} +\begin{tabular}{llll} +Stack & Input & Action & Matched\\[0pt] +A\$ & abba\$ & A \(\rightarrow\) BbA' & \\[0pt] +BbA'\$ & abba\$ & B \(\rightarrow\) aB & \\[0pt] +aBbA'\$ & bba\$ & Match a & a\\[0pt] +BbA'\$ & bba\$ & B \(\rightarrow\) b & a\\[0pt] +bbA'\$ & bba\$ & Match b & ab\\[0pt] +bA'\$ & ba\$ & Match b & abb\\[0pt] +A'\$ & a\$ & A \(\rightarrow\) aA' & abb\\[0pt] +aA'\$ & a\$ & Match a & abba\\[0pt] +A'\$ & \$ & A' \(\rightarrow\) ε & abba\\[0pt] +ε\$ & \$ & Match ε & abba\\[0pt] +\$ & \$ & Match \$ & \\[0pt] + & & Accept & \\[0pt] +\end{tabular} +\end{center} + +\section{Question Two} +\label{sec:orgc3fb7f0} +\begin{center} +\begin{tabular}{lll} +Stack & Input & Action\\[0pt] +\$ & (id)*id\$ & shift\\[0pt] +\$( & id)*id\$ & shift\\[0pt] +\$(id & )*id\$ & reduce F \(\rightarrow\) id\\[0pt] +\$(F & )*id\$ & reduce F \(\rightarrow\) E\\[0pt] +\$(E) & *id\$ & shift\\[0pt] +\$F & *id\$ & reduce F \(\rightarrow\) (E)\\[0pt] +\$T & *id\$ & reduce T \(\rightarrow\) F\\[0pt] +\$T* & id\$ & shift\\[0pt] +\$T*id & \$ & shift\\[0pt] +\$T*F & \$ & reduce F \(\rightarrow\) id\\[0pt] +\$T & \$ & reduce T \(\rightarrow\) T * F\\[0pt] +\$E & \$ & reduce E \(\rightarrow\) T\\[0pt] +\$E & \$ & accept\\[0pt] +\end{tabular} +\end{center} + +\section{Question Three} +\label{sec:org180e5b7} +\begin{center} +\begin{tabular}{lll} +Stack & Input & Action\\[0pt] +\$ & ()(())\$ & Shift\\[0pt] +\$( & )(())\$ & Shift\\[0pt] +\$() & (())\$ & Shift\\[0pt] +\$Pair & (())\$ & Reduce R5\\[0pt] +\$List & (())\$ & Reduce R3\\[0pt] +\$List( & ())\$ & Shift\\[0pt] +\$List(( & ))\$ & Shift\\[0pt] +\$List(() & )\$ & Shift\\[0pt] +\$List(Pair & )\$ & Reduce R5\\[0pt] +\$List(Pair) & \$ & Shift\\[0pt] +\$List Pair & \$ & Reduce R4\\[0pt] +\$Goal & \$ & Reduce R1\\[0pt] +\$Goal & \$ & Accept\\[0pt] +\end{tabular} +\end{center} + +\section{Question Four} +\label{sec:orgd14094c} +\subsection{a} +\label{sec:org6873356} +\{[N \(\rightarrow\) \(\cdot\) N + N], [N \(\rightarrow\) \(\cdot\) NN], [N \(\rightarrow\) \(\cdot\) (N)], [N \(\rightarrow\) \(\cdot\) N *], [N \(\rightarrow\) \(\cdot\) a]\} + +\subsection{b} +\label{sec:orgdf45b11} +\{[N \(\rightarrow\) (\(\cdot\) N)], [N \(\rightarrow\) \(\cdot\) NN], [N \(\rightarrow\) \(\cdot\) (N)], [N \(\rightarrow\) \(\cdot\) N *], [N \(\rightarrow\) \(\cdot\) a]\} + +\subsection{c} +\label{sec:orga001b72} +\{[N \(\rightarrow\) (N \(\cdot\))]\} +\section{Question Five} +\label{sec:org4370427} +\subsection{a} +\label{sec:orgb30ece5} +\{[A \(\rightarrow\) \(\cdot\) C B], [C \(\rightarrow\) \(\cdot\) E D], [E \(\rightarrow\) \(\cdot\) x A y], [E \(\rightarrow\) \(\cdot\) x]\} + +\subsection{b} +\label{sec:orgd13c8f9} +\{[B \(\rightarrow\) a \(\cdot\) C B], [C \(\rightarrow\) \(\cdot\) E D], [E \(\rightarrow\) \(\cdot\) x A y], [E \(\rightarrow\) \(\cdot\) x]\} + +\subsection{c} +\label{sec:org2b3dacb} +\{[B \(\rightarrow\) a C \(\cdot\) B], [B \(\rightarrow\) \(\cdot\) a C B]\} + +\subsection{d} +\label{sec:org5acc456} +\{[C \(\rightarrow\) E D \(\cdot\)]\} + +\subsection{e} +\label{sec:org467fb21} +\{[D \(\rightarrow\) b \(\cdot\) E D], [E \(\rightarrow\) \(\cdot\) x A y], [E \(\rightarrow\) \(\cdot\) z]\} + +\subsection{f} +\label{sec:orge62af27} +\{\} + +\section{Question Six} +\label{sec:orgce910f1} +\begin{center} +\begin{tabular}{lrl} +GOTO & state & items\\[0pt] + & 0 & closure(A' \(\rightarrow\) \(\cdot\) A)\\[0pt] + & & A' \(\rightarrow\) \(\cdot\) A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b\\[0pt] +(0, A) & 1 & closure(A' \(\rightarrow\) A \(\cdot\))\\[0pt] + & & A' \(\rightarrow\) A \(\cdot\)\\[0pt] +(0, B) & 2 & closure(A \(\rightarrow\) B \(\cdot\))\\[0pt] + & & A \(\rightarrow\) B \(\cdot\)\\[0pt] +(0, a) & 3 & closure(A \(\rightarrow\) a \(\cdot\) A b B)\\[0pt] + & & A \(\rightarrow\) a \(\cdot\) A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b\\[0pt] +(0, b) & 4 & closure(B \(\rightarrow\) b \(\cdot\) )\\[0pt] + & & B \(\rightarrow\) b \(\cdot\)\\[0pt] +(3, A) & 5 & closure(A \(\rightarrow\) a A \(\cdot\) b B)\\[0pt] + & & A \(\rightarrow\) a A \(\cdot\)\\[0pt] +(3, B) & 2 & A \(\rightarrow\) B \(\cdot\)\\[0pt] +(3, a) & 3 & A \(\rightarrow\) a \(\cdot\) A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b\\[0pt] +(3, b) & 4 & B \(\rightarrow\) b \(\cdot\)\\[0pt] +(5, b) & 6 & closure(A \(\rightarrow\) a A b \(\cdot\) B)\\[0pt] + & & A \(\rightarrow\) a A b \(\cdot\) B\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b\\[0pt] +(6, B) & 7 & closure(A \(\rightarrow\) a A b B \(\cdot\))\\[0pt] + & & A \(\rightarrow\) a A b B \(\cdot\)\\[0pt] +(6, b) & 4 & B \(\rightarrow\) b \(\cdot\)\\[0pt] +\end{tabular} +\end{center} + +\section{Question Seven} +\label{sec:org5e0e964} +\subsection{First, Follow} +\label{sec:org631e4e0} +\begin{verbatim} +first(A') = first(A) = {a} +first(A) = first(B) = {a} +first(B) = first(a) = {a} + +follow(A') = {$} +follow(A) = first(b) U follow(A') = {b, $} +follow(B) = first(a) U follow(A) = {a, b, $} +\end{verbatim} + +\subsection{Items} +\label{sec:orga0edb23} +\begin{center} +\begin{tabular}{lrl} +GOTO & state & items\\[0pt] + & 0 & closure(A' \(\rightarrow\) \(\cdot\) A)\\[0pt] + & & A' \(\rightarrow\) \(\cdot\) A\\[0pt] + & & A \(\rightarrow\) \(\cdot\) A b B\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) a\\[0pt] +(0, A) & 1 & closure(\{A \(\rightarrow\) A \(\cdot\) b B, A' \(\rightarrow\) A \(\cdot\)\})\\[0pt] + & & A \(\rightarrow\) A \(\cdot\) b B\\[0pt] + & & A' \(\rightarrow\) A \(\cdot\)\\[0pt] +(0, B) & 2 & closure(A \(\rightarrow\) B \(\cdot\) a)\\[0pt] + & & A \(\rightarrow\) B \(\cdot\) a\\[0pt] +(0, a) & 3 & closure(B \(\rightarrow\) a \(\cdot\))\\[0pt] + & & B \(\rightarrow\) a \(\cdot\)\\[0pt] +(1, b) & 4 & closure(A \(\rightarrow\) A b \(\cdot\) B)\\[0pt] + & & A \(\rightarrow\) A b \(\cdot\) B\\[0pt] + & & B \(\rightarrow\) \(\cdot\) a\\[0pt] +(2, a) & 5 & closure(A \(\rightarrow\) B a \(\cdot\))\\[0pt] + & & A \(\rightarrow\) B a \(\cdot\)\\[0pt] +(4, B) & 6 & closure(A \(\rightarrow\) A b B \(\cdot\))\\[0pt] + & & A \(\rightarrow\) A b B \(\cdot\)\\[0pt] +(4, a) & 3 & closure(B \(\rightarrow\) a \(\cdot\))\\[0pt] + & & B \(\rightarrow\) a \(\cdot\)\\[0pt] +\end{tabular} +\end{center} + +\subsection{SLR Parse Table} +\label{sec:org65e916a} +\begin{center} +\begin{tabular}{rlllrr} +state & a & b & \$ & A & B\\[0pt] +0 & s3 & & & 1 & 2\\[0pt] +1 & & s4 & acc & & \\[0pt] +2 & s5 & & & & \\[0pt] +3 & r4 & r4 & r4 & & \\[0pt] +4 & s3 & & & & 6\\[0pt] +5 & & r3 & r3 & & \\[0pt] +6 & & r2 & r2 & & \\[0pt] +\end{tabular} +\end{center} + +\subsection{Parse of "aaba"} +\label{sec:orgc668927} +\begin{center} +\begin{tabular}{llll} +stack & symbols & input & action\\[0pt] +0 & & aaba\$ & shift 3\\[0pt] +0 3 & a & aba\$ & reduce 4\\[0pt] +0 2 & B & aba\$ & shift 5\\[0pt] +0 2 5 & Ba & ba\$ & reduce 3\\[0pt] +0 1 & A & ba\$ & shift 4\\[0pt] +0 1 4 & Ab & a\$ & shift 3\\[0pt] +0 1 4 3 & Aba & \$ & reduce 4\\[0pt] +0 1 4 6 & AbB & \$ & reduce 2\\[0pt] +0 1 & A & \$ & accept\\[0pt] +\end{tabular} +\end{center} + +\section{Question Eight} +\label{sec:org9b3771e} +\subsection{First, Follow} +\label{sec:org8892bb8} +\begin{verbatim} +first(A') = first(A) = {a, b, f} +first(A) = first(a) U first(B) = {a, b, f} +first(B) = first(b) U first(f) = {b, f} + +follow(A') = {$} +follow(A) = follow(A') = {$} +follow(B) = first(a) = {a} +\end{verbatim} +\subsection{Items} +\label{sec:orgc50d475} +\begin{center} +\begin{tabular}{lrl} +GOTO & state & items\\[0pt] + & 0 & closure(A' \(\rightarrow\) \(\cdot\) A, $\backslash$$)\\[0pt] + & & A' \(\rightarrow\) \(\cdot\) A, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B a, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A, \$\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(0, A) & 1 & closure(A' \(\rightarrow\) A \(\cdot\), \$)\\[0pt] + & & A' \(\rightarrow\) A \(\cdot\), \$\\[0pt] +(0, B) & 2 & closure(A \(\rightarrow\) B \(\cdot\) a, \$)\\[0pt] + & & A \(\rightarrow\) B \(\cdot\) a, \$\\[0pt] +(0, a) & 3 & closure(A \(\rightarrow\) a \(\cdot\) A, \$)\\[0pt] + & & A \(\rightarrow\) a \(\cdot\) A, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B a, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A, \$\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(0, b) & 4 & closure(B \(\rightarrow\) b \(\cdot\) B, a)\\[0pt] + & & B \(\rightarrow\) b \(\cdot\) B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(0, f) & 5 & closure(B \(\rightarrow\) f \(\cdot\), a)\\[0pt] + & & B \(\rightarrow\) f \(\cdot\), a\\[0pt] +(2, a) & 6 & closure(A \(\rightarrow\) B a \(\cdot\), \$)\\[0pt] + & & A \(\rightarrow\) B a \(\cdot\), \$\\[0pt] +(3, A) & 7 & closure(A \(\rightarrow\) a A \(\cdot\), \$)\\[0pt] + & & A \(\rightarrow\) a A \(\cdot\), $\backslash$$\\[0pt] +(3, B) & 2 & closure(A \(\rightarrow\) B \(\cdot\) a, \$)\\[0pt] + & & A \(\rightarrow\) B \(\cdot\) a, \$\\[0pt] +(3, a) & 3 & closure(A \(\rightarrow\) a \(\cdot\) A, \$)\\[0pt] + & & A \(\rightarrow\) a \(\cdot\) A, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) B a, \$\\[0pt] + & & A \(\rightarrow\) \(\cdot\) a A, \$\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(3, b) & 4 & closure(B \(\rightarrow\) b \(\cdot\) B, a)\\[0pt] + & & B \(\rightarrow\) b \(\cdot\) B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(3, f) & 5 & closure(B \(\rightarrow\) f \(\cdot\), a)\\[0pt] + & & B \(\rightarrow\) f \(\cdot\), a\\[0pt] +(4, B) & 8 & closure(B \(\rightarrow\) b B \(\cdot\), a)\\[0pt] + & & B \(\rightarrow\) b B \(\cdot\), a\\[0pt] +(4, b) & 4 & closure(B \(\rightarrow\) b \(\cdot\) B, a)\\[0pt] + & & B \(\rightarrow\) b \(\cdot\) B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) b B, a\\[0pt] + & & B \(\rightarrow\) \(\cdot\) f, a\\[0pt] +(4, f) & 5 & closure(B \(\rightarrow\) f \(\cdot\), a)\\[0pt] + & & B \(\rightarrow\) f \(\cdot\), a\\[0pt] +\end{tabular} +\end{center} +\subsection{LR Parse Table} +\label{sec:org69f6dbf} +\begin{center} +\begin{tabular}{rllllrr} +state & a & b & f & \$ & A & B\\[0pt] +0 & s3 & s4 & s5 & & 1 & 2\\[0pt] +1 & & & & acc & & \\[0pt] +2 & s6 & & & & & \\[0pt] +3 & s3 & s4 & s5 & & 7 & 2\\[0pt] +4 & & s4 & s5 & & & 8\\[0pt] +5 & r5 & & & & & \\[0pt] +6 & & & & r2 & & \\[0pt] +7 & & & & r3 & & \\[0pt] +8 & r4 & & & & & \\[0pt] +\end{tabular} +\end{center} + +\subsection{Parse of "abfa"} +\label{sec:org1d0a90c} +\begin{center} +\begin{tabular}{llll} +stack & symbols & input & action\\[0pt] +0 & & abfa\$ & shift 3\\[0pt] +0 3 & a & bfa\$ & shift 4\\[0pt] +0 3 4 & ab & fa\$ & shift 5\\[0pt] +0 3 4 5 & abf & a\$ & reduce 5\\[0pt] +0 3 4 8 & abB & a\$ & reduce 4\\[0pt] +0 3 2 & aB & a\$ & shift 6\\[0pt] +0 3 2 6 & aBa & \$ & reduce 2\\[0pt] +0 3 7 & aBa & \$ & reduce 3\\[0pt] +0 1 & aA & \$ & accept\\[0pt] +\end{tabular} +\end{center} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-six/328e45ffe10876ae6e22f4d5a990f625-emacs-elixir-format.ex b/Homework/cs5300/homework-six/328e45ffe10876ae6e22f4d5a990f625-emacs-elixir-format.ex new file mode 100644 index 0000000..159ea1f --- /dev/null +++ b/Homework/cs5300/homework-six/328e45ffe10876ae6e22f4d5a990f625-emacs-elixir-format.ex @@ -0,0 +1 @@ +0 Swiper:
\ No newline at end of file diff --git a/Homework/cs5300/homework-six/compilers_assn_6.org b/Homework/cs5300/homework-six/compilers_assn_6.org new file mode 100644 index 0000000..0652712 --- /dev/null +++ b/Homework/cs5300/homework-six/compilers_assn_6.org @@ -0,0 +1,113 @@ +#+TITLE: Assignment Six +#+AUTHOR: Lizzy Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +#+LATEX: \setlength\parindent{0pt} +#+LATEX_COMPILER: lualatex +#+OPTIONS: toc:nil + +* Question One +\begin{verbatim} +A -> BcDA' +A' -> eD | ε +B -> xBB' +B' -> yD | zD | ε +D -> Z +\end{verbatim} + +* Question Two +\begin{verbatim} +A -> wA' +A' -> xA' | yA' | ε +\end{verbatim} + +* Question Three +\begin{verbatim} +A -> wA' | yzA' +A' -> xA' | ε +\end{verbatim} + +* Question Four +\begin{verbatim} +A -> Bx +B -> Cy | Dz +C -> Awp | z +D -> t + +For i = 1, there is no immediate left recursion + +When i = 2 there is also no immediate left recursion + +When i = 3 we substituted A in C: + +A -> Bx +B -> Cy | Dz +C -> Bxwp | z +D -> t + +Then B: +A -> Bx +B -> Cy | Dz +C -> Cyxwp | Dzxwp | z +D -> t + +And remove the immediate left recursion in C: +A -> Bx +B -> Cy | Dz +C -> Dzxwp | zC' +C' -> yxwpC' | ε +D -> t + +When i = 4, there's no left recursion + +\end{verbatim} + +* Question Five +\begin{verbatim} +A -> Bx | Cy +B -> Cz +C -> Bw | v + +When i = 1, no immediate left recursion + +When i = 2, no immediate left recursion + +When i = 3, we substitute B in C + +A -> Bx | Cy +B -> Cz +C -> Czw | v + +, which we remove the immediate left recursion from + +A -> Bx | Cy +B -> Cz +C -> vC' +C' -> zwC' | ε +\end{verbatim} + +* Question Six +\begin{verbatim} +first(A) = first(B) \ ε U first(x) U first(y) = {t, w, u, x, y} +first(B) = first(C) U first(u) U {ε} = {t, w, u, ε} +first(C) = first(t) U first(w) = {t, w} + +follow(A) = {$} U follow(B) = {$, x} +follow(B) = {x} +follow(C) = follow(A) U {z} = {$, x, z} +\end{verbatim} + +* Question Seven +\begin{verbatim} +first(A) = first(C) = first(E) = {x, z} +first(B) = first(a) U {ε} = {a, ε} +first(C) = first(E) = {x, z} +first(D) = first(b) U {ε} = {b, ε} +first(E) = first(x) U first(z) = {x, z} + +follow(A) = {$} U first(y) = {$, y} +follow(B) = follow(A) = {$, y} +follow(C) = first(B) \ ε U follow(B)= {a, $, y} +follow(D) = follow(C) = {a, $, y} +follow(E) = first(D) \ ε U follow(D) = {b, a, $, y} +\end{verbatim} diff --git a/Homework/cs5300/homework-six/compilers_assn_6.pdf b/Homework/cs5300/homework-six/compilers_assn_6.pdf Binary files differnew file mode 100644 index 0000000..fa3aebc --- /dev/null +++ b/Homework/cs5300/homework-six/compilers_assn_6.pdf diff --git a/Homework/cs5300/homework-six/compilers_assn_6.tex b/Homework/cs5300/homework-six/compilers_assn_6.tex new file mode 100644 index 0000000..bec27f3 --- /dev/null +++ b/Homework/cs5300/homework-six/compilers_assn_6.tex @@ -0,0 +1,141 @@ +% Created 2023-02-21 Tue 21:49 +% Intended LaTeX compiler: lualatex +\documentclass[11pt]{article} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} +\author{Lizzy Hunt} +\date{\today} +\title{Assignment Six} +\hypersetup{ + pdfauthor={Lizzy Hunt}, + pdftitle={Assignment Six}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:orgd2b70de} +\begin{verbatim} +A -> BcDA' +A' -> eD | ε +B -> xBB' +B' -> yD | zD | ε +D -> Z +\end{verbatim} + +\section{Question Two} +\label{sec:org3d41f44} +\begin{verbatim} +A -> wA' +A' -> xA' | yA' | ε +\end{verbatim} + +\section{Question Three} +\label{sec:org454e290} +\begin{verbatim} +A -> wA' | yzA' +A' -> xA' | ε +\end{verbatim} + +\section{Question Four} +\label{sec:orgafb492b} +\begin{verbatim} +A -> Bx +B -> Cy | Dz +C -> Awp | z +D -> t + +For i = 1, there is no immediate left recursion + +When i = 2 there is also no immediate left recursion + +When i = 3 we substituted A in C: + +A -> Bx +B -> Cy | Dz +C -> Bxwp | z +D -> t + +Then B: +A -> Bx +B -> Cy | Dz +C -> Cyxwp | Dzxwp | z +D -> t + +And remove the immediate left recursion in C: +A -> Bx +B -> Cy | Dz +C -> Dzxwp | zC' +C' -> yxwpC' | ε +D -> t + +When i = 4, there's no left recursion + +\end{verbatim} + +\section{Question Five} +\label{sec:orged171dd} +\begin{verbatim} +A -> Bx | Cy +B -> Cz +C -> Bw | v + +When i = 1, no immediate left recursion + +When i = 2, no immediate left recursion + +When i = 3, we substitute B in C + +A -> Bx | Cy +B -> Cz +C -> Czw | v + +, which we remove the immediate left recursion from + +A -> Bx | Cy +B -> Cz +C -> vC' +C' -> zwC' | ε +\end{verbatim} + +\section{Question Six} +\label{sec:org0f2cc1b} +\begin{verbatim} +first(A) = first(B) \ ε U first(x) U first(y) = {t, w, u, x, y} +first(B) = first(C) U first(u) U {ε} = {t, w, u, ε} +first(C) = first(t) U first(w) = {t, w} + +follow(A) = {$} U follow(B) = {$, x} +follow(B) = {x} +follow(C) = follow(A) U {z} = {$, x, z} +\end{verbatim} + +\section{Question Seven} +\label{sec:orgd013fa5} +\begin{verbatim} +first(A) = first(C) = first(E) = {x, z} +first(B) = first(a) U {ε} = {a, ε} +first(C) = first(E) = {x, z} +first(D) = first(b) U {ε} = {b, ε} +first(E) = first(x) U first(z) = {x, z} + +follow(A) = {$} U first(y) = {$, y} +follow(B) = follow(A) = {$, y} +follow(C) = first(B) \ ε U follow(B)= {a, $, y} +follow(D) = follow(C) = {a, $, y} +follow(E) = first(D) \ ε U follow(D) = {b, a, $, y} +\end{verbatim} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-three/10b.png b/Homework/cs5300/homework-three/10b.png Binary files differnew file mode 100644 index 0000000..704e444 --- /dev/null +++ b/Homework/cs5300/homework-three/10b.png diff --git a/Homework/cs5300/homework-three/2.png b/Homework/cs5300/homework-three/2.png Binary files differnew file mode 100644 index 0000000..391e2ab --- /dev/null +++ b/Homework/cs5300/homework-three/2.png diff --git a/Homework/cs5300/homework-three/4ee134d048f8b6933168eb850fdc47da-emacs-elixir-format.ex b/Homework/cs5300/homework-three/4ee134d048f8b6933168eb850fdc47da-emacs-elixir-format.ex new file mode 100644 index 0000000..7a71d35 --- /dev/null +++ b/Homework/cs5300/homework-three/4ee134d048f8b6933168eb850fdc47da-emacs-elixir-format.ex @@ -0,0 +1 @@ +File compilers_assn_3.pdf changed on disk. Reread from disk? (yes or no)
\ No newline at end of file diff --git a/Homework/cs5300/homework-three/compilers_assn_3.org b/Homework/cs5300/homework-three/compilers_assn_3.org new file mode 100644 index 0000000..0d4028e --- /dev/null +++ b/Homework/cs5300/homework-three/compilers_assn_3.org @@ -0,0 +1,165 @@ +#+TITLE: Assignment Three +#+AUTHOR: Logan Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage{amsmath} +#+LATEX: \setlength\parindent{0pt} +#+OPTIONS: toc:nil + +* Question One +\begin{verbatim} +<void> <id, cube> <(> <float> <id, y> <)> <{> + <if> <(> <id, y> <comparison, >> <number, 3.14159> <)> \\ + <return> <id, y> <mult_op> <id, y> <mult_op> <id, y> + <return> <number, -1> +<}> +\end{verbatim} + +* Question Two +#+attr_latex: :width 250px +[[./2.png]] + +The first buffer is filled twice. The second is filled once, and the last contains three elements. +* Question Three +Strings starting and ending with "x" with any number of x or y's in between. + +* Question Four +** a +\begin{verbatim} +col(o|ou)r +\end{verbatim} +** b +\begin{verbatim} +m(o|u)m +\end{verbatim} +** c +\begin{verbatim} +analy(z|s)e +\end{verbatim} + +* Question Five +** a +1. bbbaa +2. bbbcc +3. bbbac +4. bbbca +** b +1. bbb +2. aa +3. cc +4. ac +5. ca +** c +1. bbb +2. acac +** d +1. bbbacac + +* Question Six +** a +\begin{verbatim} +In [1]: ["aggie"[0:n] for n in range(0, len("aggie")+1)] +Out[1]: ['', 'a', 'ag', 'agg', 'aggi', 'aggie'] +\end{verbatim} + +** b +\begin{verbatim} +In [3]: ["aggie"[n:len("aggie")] for n in range(0, len("aggie")+1)] +Out[3]: ['aggie', 'ggie', 'gie', 'ie', 'e', ''] +\end{verbatim} + +** c +\begin{verbatim} +In [4]: ["aggie"[0:n] for n in range(1, len("aggie"))] +Out[4]: ['a', 'ag', 'agg', 'aggi'] +\end{verbatim} + +** d +\begin{verbatim} +In [8]: def subs(s): + ...: proper_substrings = set() + ...: for i in range(len(s)+1): + ...: for j in range(len(s)+1): + ...: if (i <= j): + ...: sub = s[i:j] + ...: if (sub != "" and sub != s): + ...: proper_substrings.add(sub) + ...: return proper_substrings + ...: + +In [9]: subs("aggie") +Out[9]: +{'a', + 'ag', + 'agg', + 'aggi', + 'e', + 'g', + 'gg', + 'ggi', + 'ggie', + 'gi', + 'gie', + 'i', + 'ie'} +\end{verbatim} + +* Question Seven +\begin{verbatim} +(u|U)(n|N)(i|I)(o|O)(n|N) +\end{verbatim} + +* Question Eight +$\textdollar([0-9]|[0-9]^2|[0-9]^3)(,[0-9]^3)^*.[0-9][0-9]$ + +* Question Nine +nonzeroDigit \Rightarrow 1|2|3|4|5|6|7|8|9|A|B|C|D|E|F + +hex \Rightarrow nonzeroDigit^{+}(0|nonzeroDigit)^{*} + +* Question Ten +** a +\begin{verbatim} +(0011)*|(1100)* +\end{verbatim} +** b +[[./10b.png]] + +\begin{verbatim} +((00)|(0(11)*0?))|((11)|(1(00)*1?)) +\end{verbatim} +** c +\begin{verbatim} +a?b?c?d? +\end{verbatim} + +* Question Eleven +** a +\begin{verbatim} +(\t| )+ +\end{verbatim} +** b +\begin{verbatim} +(/\*)[^(*/)]*(\*/) +\end{verbatim} +** c +\begin{verbatim} +"[^"]*" +\end{verbatim} + +* Question Twelve + +nonVowelPart \Rightarrow [\textasciicircum aeiou]^{*} + +aPart \Rightarrow a^{+}[\textasciicircum eiou]^{*} + +ePart \Rightarrow e^{+}[\textasciicircum aiou]^{*} + +iPart \Rightarrow i^{+}[\textasciicircum aeou]^{*} + +oPart \Rightarrow o^{+}[\textasciicircum aeiu]^{*} + +uPart \Rightarrow u^{+} [\textasciicircum aeio]^{*} + +str \Rightarrow (nonVowelPart)(aPart)(ePart)(iPart)(oPart)(uPart) + + diff --git a/Homework/cs5300/homework-three/compilers_assn_3.pdf b/Homework/cs5300/homework-three/compilers_assn_3.pdf Binary files differnew file mode 100644 index 0000000..91306db --- /dev/null +++ b/Homework/cs5300/homework-three/compilers_assn_3.pdf diff --git a/Homework/cs5300/homework-three/compilers_assn_3.tex b/Homework/cs5300/homework-three/compilers_assn_3.tex new file mode 100644 index 0000000..bf3bdd8 --- /dev/null +++ b/Homework/cs5300/homework-three/compilers_assn_3.tex @@ -0,0 +1,227 @@ +% Created 2023-02-01 Wed 00:48 +% Intended LaTeX compiler: pdflatex +\documentclass[11pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage{amsmath} +\author{Logan Hunt} +\date{\today} +\title{Assignment Three} +\hypersetup{ + pdfauthor={Logan Hunt}, + pdftitle={Assignment Three}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:orgdf8aa7e} +\begin{verbatim} +<void> <id, cube> <(> <float> <id, y> <)> <{> + <if> <(> <id, y> <comparison, >> <number, 3.14159> <)> \\ + <return> <id, y> <mult_op> <id, y> <mult_op> <id, y> + <return> <number, -1> +<}> +\end{verbatim} + +\section{Question Two} +\label{sec:orgdde4dcf} +\begin{center} +\includegraphics[width=250px]{./2.png} +\end{center} + +The first buffer is filled twice. The second is filled once, and the last contains three elements. +\section{Question Three} +\label{sec:org265d5c8} +Strings starting and ending with "x" with any number of x or y's in between. + +\section{Question Four} +\label{sec:org2c5df0e} +\subsection{a} +\label{sec:orgbea3125} +\begin{verbatim} +col(o|ou)r +\end{verbatim} +\subsection{b} +\label{sec:org3be70df} +\begin{verbatim} +m(o|u)m +\end{verbatim} +\subsection{c} +\label{sec:org79ea881} +\begin{verbatim} +analy(z|s)e +\end{verbatim} + +\section{Question Five} +\label{sec:org29ab12a} +\subsection{a} +\label{sec:org9be87fc} +\begin{enumerate} +\item bbbaa +\item bbbcc +\item bbbac +\item bbbca +\end{enumerate} +\subsection{b} +\label{sec:org7c1600e} +\begin{enumerate} +\item bbb +\item aa +\item cc +\item ac +\item ca +\end{enumerate} +\subsection{c} +\label{sec:orgc0a6132} +\begin{enumerate} +\item bbb +\item acac +\end{enumerate} +\subsection{d} +\label{sec:org7e7a221} +\begin{enumerate} +\item bbbacac +\end{enumerate} + +\section{Question Six} +\label{sec:org4ae00e1} +\subsection{a} +\label{sec:org95b78bd} +\begin{verbatim} +In [1]: ["aggie"[0:n] for n in range(0, len("aggie")+1)] +Out[1]: ['', 'a', 'ag', 'agg', 'aggi', 'aggie'] +\end{verbatim} + +\subsection{b} +\label{sec:orge1463c6} +\begin{verbatim} +In [3]: ["aggie"[n:len("aggie")] for n in range(0, len("aggie")+1)] +Out[3]: ['aggie', 'ggie', 'gie', 'ie', 'e', ''] +\end{verbatim} + +\subsection{c} +\label{sec:org8c9e2bb} +\begin{verbatim} +In [4]: ["aggie"[0:n] for n in range(1, len("aggie"))] +Out[4]: ['a', 'ag', 'agg', 'aggi'] +\end{verbatim} + +\subsection{d} +\label{sec:org1e2b7fd} +\begin{verbatim} +In [8]: def subs(s): + ...: proper_substrings = set() + ...: for i in range(len(s)+1): + ...: for j in range(len(s)+1): + ...: if (i <= j): + ...: sub = s[i:j] + ...: if (sub != "" and sub != s): + ...: proper_substrings.add(sub) + ...: return proper_substrings + ...: + +In [9]: subs("aggie") +Out[9]: +{'a', + 'ag', + 'agg', + 'aggi', + 'e', + 'g', + 'gg', + 'ggi', + 'ggie', + 'gi', + 'gie', + 'i', + 'ie'} +\end{verbatim} + +\section{Question Seven} +\label{sec:org7f2ee0a} +\begin{verbatim} +(u|U)(n|N)(i|I)(o|O)(n|N) +\end{verbatim} + +\section{Question Eight} +\label{sec:orgc132ce5} +\(\textdollar([0-9]|[0-9]^2|[0-9]^3)(,[0-9]^3)^*.[0-9][0-9]\) + +\section{Question Nine} +\label{sec:orgaafffb5} +nonzeroDigit \(\Rightarrow\) 1|2|3|4|5|6|7|8|9|A|B|C|D|E|F + +hex \(\Rightarrow\) nonzeroDigit\textsuperscript{+}(0|nonzeroDigit)\textsuperscript{*} + +\section{Question Ten} +\label{sec:org00e8901} +\subsection{a} +\label{sec:org06fd6db} +\begin{verbatim} +(0011)*|(1100)* +\end{verbatim} +\subsection{b} +\label{sec:orgdb7ace2} +\begin{center} +\includegraphics[width=.9\linewidth]{./10b.png} +\end{center} + +\begin{verbatim} +((00)|(0(11)*0?))|((11)|(1(00)*1?)) +\end{verbatim} +\subsection{c} +\label{sec:org754d92f} +\begin{verbatim} +a?b?c?d? +\end{verbatim} + +\section{Question Eleven} +\label{sec:org1470498} +\subsection{a} +\label{sec:org7adfc46} +\begin{verbatim} +(\t| )+ +\end{verbatim} +\subsection{b} +\label{sec:orgda2b5ea} +\begin{verbatim} +(/\*)[^(*/)]*(\*/) +\end{verbatim} +\subsection{c} +\label{sec:org21471a7} +\begin{verbatim} +"[^"]*" +\end{verbatim} + +\section{Question Twelve} +\label{sec:org4ac98dc} + +nonVowelPart \(\Rightarrow\) [\textasciicircum aeiou]\textsuperscript{*} + +aPart \(\Rightarrow\) a\textsuperscript{+}[\textasciicircum eiou]\textsuperscript{*} + +ePart \(\Rightarrow\) e\textsuperscript{+}[\textasciicircum aiou]\textsuperscript{*} + +iPart \(\Rightarrow\) i\textsuperscript{+}[\textasciicircum aeou]\textsuperscript{*} + +oPart \(\Rightarrow\) o\textsuperscript{+}[\textasciicircum aeiu]\textsuperscript{*} + +uPart \(\Rightarrow\) u\textsuperscript{+} [\textasciicircum aeio]\textsuperscript{*} + +str \(\Rightarrow\) (nonVowelPart)(aPart)(ePart)(iPart)(oPart)(uPart) +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/homework-two/11-1.png b/Homework/cs5300/homework-two/11-1.png Binary files differnew file mode 100644 index 0000000..a473d2b --- /dev/null +++ b/Homework/cs5300/homework-two/11-1.png diff --git a/Homework/cs5300/homework-two/11-2.png b/Homework/cs5300/homework-two/11-2.png Binary files differnew file mode 100644 index 0000000..39e507a --- /dev/null +++ b/Homework/cs5300/homework-two/11-2.png diff --git a/Homework/cs5300/homework-two/17.png b/Homework/cs5300/homework-two/17.png Binary files differnew file mode 100644 index 0000000..2ee9912 --- /dev/null +++ b/Homework/cs5300/homework-two/17.png diff --git a/Homework/cs5300/homework-two/18.png b/Homework/cs5300/homework-two/18.png Binary files differnew file mode 100644 index 0000000..b32de02 --- /dev/null +++ b/Homework/cs5300/homework-two/18.png diff --git a/Homework/cs5300/homework-two/4c.png b/Homework/cs5300/homework-two/4c.png Binary files differnew file mode 100644 index 0000000..a83f0e4 --- /dev/null +++ b/Homework/cs5300/homework-two/4c.png diff --git a/Homework/cs5300/homework-two/4d.png b/Homework/cs5300/homework-two/4d.png Binary files differnew file mode 100644 index 0000000..c45ec6d --- /dev/null +++ b/Homework/cs5300/homework-two/4d.png diff --git a/Homework/cs5300/homework-two/5a.png b/Homework/cs5300/homework-two/5a.png Binary files differnew file mode 100644 index 0000000..251b097 --- /dev/null +++ b/Homework/cs5300/homework-two/5a.png diff --git a/Homework/cs5300/homework-two/5b.png b/Homework/cs5300/homework-two/5b.png Binary files differnew file mode 100644 index 0000000..af0594c --- /dev/null +++ b/Homework/cs5300/homework-two/5b.png diff --git a/Homework/cs5300/homework-two/9.png b/Homework/cs5300/homework-two/9.png Binary files differnew file mode 100644 index 0000000..5dd948a --- /dev/null +++ b/Homework/cs5300/homework-two/9.png diff --git a/Homework/cs5300/homework-two/Infix.class b/Homework/cs5300/homework-two/Infix.class Binary files differnew file mode 100644 index 0000000..6def347 --- /dev/null +++ b/Homework/cs5300/homework-two/Infix.class diff --git a/Homework/cs5300/homework-two/Infix.java b/Homework/cs5300/homework-two/Infix.java new file mode 100644 index 0000000..33c8dcf --- /dev/null +++ b/Homework/cs5300/homework-two/Infix.java @@ -0,0 +1,72 @@ +import java.util.ArrayList; +import java.io.IOException; + +public class Infix { + private ArrayList<Character> chars; + private int index; + + public static void main(String[] args) { + String str = "+++12-835"; + if (args.length > 0) { + str = args[0]; + } + try { + Infix parser = new Infix(str); + } catch (Exception e) { + System.out.println("error"); + } + } + + public Infix(String terminals) throws IOException { + this.index = 0; + this.chars = new ArrayList<Character>(); + for (char c: terminals.toCharArray()) + this.chars.add(c); + + this.list(); + + if (this.index < this.chars.size()) + throw new IOException("Could not parse entire string"); + + System.out.println(); + } + + private void list() throws IOException { + char lookahead = this.lookahead(); + if (lookahead == '+' || lookahead == '-') { + match(lookahead); + System.out.write('('); + list(); + System.out.write(lookahead); + list(); + System.out.write(')'); + } + else if (lookahead >= '0' && lookahead <= '9') + digit(); + else + throw new IOException("Expected lookahead to be a digit or plus/minus"); + } + + private void digit() throws IOException { + char lookahead = this.lookahead(); + if (lookahead >= '0' && lookahead <= '9') { + match(lookahead); + System.out.write(lookahead); + return; + } + throw new IOException("Expected lookahead to be a digit"); + } + + private void match(char t) throws IOException { + boolean matches = this.lookahead() == t; + + if (matches) + this.index += 1; + else if (!matches) + throw new IOException("Expected " + (char)this.lookahead() + " to match " + (char)t); + } + + private char lookahead() { + return this.chars.get(this.index); + } +} diff --git a/Homework/cs5300/homework-two/InfixTest.py b/Homework/cs5300/homework-two/InfixTest.py new file mode 100644 index 0000000..826085d --- /dev/null +++ b/Homework/cs5300/homework-two/InfixTest.py @@ -0,0 +1,30 @@ +#!/usr/bin/python + +import os + +i=1 +def test(cmd, expected): + global i + stream = os.popen(cmd) + output = stream.read() + output = output.strip() + if output != expected: + print('Failed test {} ({}). Should have been "{}". Was "{}".' + .format(i, cmd, expected, output)) + else: + print('Passed test {}.'.format(i)) + i=i+1 +# Compile the code +os.system('javac Infix.java') +# Run the tests. The first argument is the command to run +# and the second argument is the expected output of the +# program. +test('java Infix +12', '(1+2)') +test('java Infix -+121', '((1+2)-1)') +test('java Infix -+9-8+127', '((9+(8-(1+2)))-7)') +test('java Infix -+9-81+27', '((9+(8-1))-(2+7))') +test('java Infix +7', '(7+error') +test('java Infix -9+7', '(9-(7+error') +test('java Infix -+9-8+12', '((9+(8-(1+2)))-error') +test('java Infix -+9-a+127', '((9+(error') +test('java Infix -9-8+12+7', '(9-(8-(1+2)))error') diff --git a/Homework/cs5300/homework-two/compilers_assn_2.org b/Homework/cs5300/homework-two/compilers_assn_2.org new file mode 100644 index 0000000..186303d --- /dev/null +++ b/Homework/cs5300/homework-two/compilers_assn_2.org @@ -0,0 +1,192 @@ +#+TITLE: Assignment Two +#+AUTHOR: Logan Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage{amsmath} +#+LATEX: \setlength\parindent{0pt} +#+OPTIONS: toc:nil + +* Question One +** a +There are four productions. +** b +The terminals of this grammar are: + +\begin{verbatim} +a b c d +\end{verbatim} +** c +N_1, N_2, N_3, N_4 are all nonterminals. +** d +The start symbol is N_1. +** e +The symbols N_1, N_2, N_3, N_4 are present in the production heads. +** f +N_2, N_3, N_4, "a", "b", "c", "d" are present in the production bodies. +* Question Two +** a +There are fifteen productions. +** b +The terminals of this grammar are: +\begin{verbatim} +0 1 2 3 4 5 6 7 8 9 + - +\end{verbatim} +** c +The nonterminals are "list" and "digit". +** d +The start symbol is "list". +** e +"list" and "digit" are present in the production heads. +** f +"list", "digit", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, $+$, $-$ are present in the production bodies. +* Question Three +\begin{verbatim} +x -> A (3) +x -> A (3) +xx* -> AA* (2) +(xx*)x+ -> AA+ (1) +((xx*)x+)x+ -> AA+ (1) +\end{verbatim} +* Question Four +** a +Examples: +1. "xy" +2. "xxyy" +3. "xxxyyy" +4. "xxxxyyyy" + +This grammar generates a string of x's prepended to a string of y's with the same amount of characters. + +The grammar is unambiguous. + +** b +Example: +1. "x" +2. "-xx" +3. "+-xx-xx" +4. "-x-xx" + +This grammar generates addition and subtraction prefix expressions on "x" or chains of "x". + +The grammar is unambiguous. + +** c +Example: +1. "" +2. "()" +3. "()()" +4. "(())" + +This grammar generates balanced sets of potentially nested parentheses. + +The grammar is ambiguous. For example, the string "()()" can be parsed multiple ways: + +#+attr_latex: :width 250px +[[./4c.png]] + +** d +1. "" +2. "xyyx" +3. "xyxy" +4. "xy" + +This grammar generates jumbled combinations of the same number of x's and y's. + +The grammar is ambiguous. + +For example, the string "xyxy" has two parse trees: + +#+attr_latex: :width 250px +[[./4d.png]] + +** e +1. "(x)**" +2. "x+x" +3. "x*" +4. "xx" + +This grammar generates combinations of + / * expressions on x in any nested amount of balanced parentheses. + +This grammar is unambiguous. + +* Question Five +** a +#+attr_latex: :width 250px +[[./5a.png]] + +** b +#+attr_latex: :width 250px +[[./5b.png]] + +* Question Six +\begin{verbatim} +expr -> expr expr op +expr -> factor +op -> + | - | / | * +factor -> 0 +factor -> 1 +factor ... 9 +\end{verbatim} +* Question Seven +\begin{verbatim} +sentence -> character +sentence -> character,sentence +character -> a +character -> b +character -> ... +\end{verbatim} +* Question Eight +\begin{verbatim} +expr -> expr - term +expr -> expr + term +expr -> expr * term +expr -> expr / term +expr -> term +term -> +num +term -> -num +term -> num +num -> Integer +num -> Ident +\end{verbatim} +* Question Nine +#+attr_latex: :width 250px +[[./9.png]] +* Question Ten +\begin{verbatim} +expr -> {print '+'} expr + term +expr -> {print '-'} expr - term +expr -> term +term -> 0 {print '0'} +term -> 1 {print '1'} +term -> ... {print ...} +\end{verbatim} +* Question Eleven +** 3-1+2 +#+attr_latex: :width 250px +[[./11-1.png]] +** 2+3-1 +#+attr_latex: :width 250px +[[./11-2.png]] + +* Question Twelve +Predictive parsing is a special kind of recursive descent parsing where the "lookahead unambiguously determines the flow of control". +* Question Thirteen +None of the productions have the same start symbol (disjoint ~FIRST~ sets), and it is not left-recursive. +* Question Fourteen +See ~Infix.java~. +* Question Fifteen +\begin{verbatim} +A -> w R +R -> x y z R | \epsilon +\end{verbatim} +* Question Sixteen +\begin{verbatim} +S -> w R +R -> x y z R +R -> g h R +R -> \epsilon +\end{verbatim} +* Question Seventeen +[[./17.png]] +* Question Eighteen +#+attr_latex: :width 250px +[[./18.png]] diff --git a/Homework/cs5300/homework-two/compilers_assn_2.pdf b/Homework/cs5300/homework-two/compilers_assn_2.pdf Binary files differnew file mode 100644 index 0000000..f5ea2f2 --- /dev/null +++ b/Homework/cs5300/homework-two/compilers_assn_2.pdf diff --git a/Homework/cs5300/homework-two/compilers_assn_2.tex b/Homework/cs5300/homework-two/compilers_assn_2.tex new file mode 100644 index 0000000..408558b --- /dev/null +++ b/Homework/cs5300/homework-two/compilers_assn_2.tex @@ -0,0 +1,275 @@ +% Created 2023-01-27 Fri 20:04 +% Intended LaTeX compiler: pdflatex +\documentclass[11pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\notindent \notga \usepackage{ dsfont } \usepackage{amsmath} +\author{Logan Hunt} +\date{\today} +\title{Assignment Two} +\hypersetup{ + pdfauthor={Logan Hunt}, + pdftitle={Assignment Two}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, + pdflang={English}} +\begin{document} + +\maketitle +\setlength\parindent{0pt} + +\section{Question One} +\label{sec:org87beba7} +\subsection{a} +\label{sec:orgf2add9e} +There are four productions. +\subsection{b} +\label{sec:orgc7248f7} +The terminals of this grammar are: + +\begin{verbatim} +a b c d +\end{verbatim} +\subsection{c} +\label{sec:orgf876127} +N\textsubscript{1}, N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4} are all nonterminals. +\subsection{d} +\label{sec:orgf042bc3} +The start symbol is N\textsubscript{1}. +\subsection{e} +\label{sec:orgd668266} +The symbols N\textsubscript{1}, N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4} are present in the production heads. +\subsection{f} +\label{sec:org34be0af} +N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4}, "a", "b", "c", "d" are present in the production bodies. +\section{Question Two} +\label{sec:org4b362f4} +\subsection{a} +\label{sec:orga65ff15} +There are fifteen productions. +\subsection{b} +\label{sec:org3c23122} +The terminals of this grammar are: +\begin{verbatim} +0 1 2 3 4 5 6 7 8 9 + - +\end{verbatim} +\subsection{c} +\label{sec:org899f753} +The nonterminals are "list" and "digit". +\subsection{d} +\label{sec:org3be2a40} +The start symbol is "list". +\subsection{e} +\label{sec:org19adb89} +"list" and "digit" are present in the production heads. +\subsection{f} +\label{sec:org6764041} +"list", "digit", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \(+\), \(-\) are present in the production bodies. +\section{Question Three} +\label{sec:orgcef93d4} +\begin{verbatim} +x -> A (3) +x -> A (3) +xx* -> AA* (2) +(xx*)x+ -> AA+ (1) +((xx*)x+)x+ -> AA+ (1) +\end{verbatim} +\section{Question Four} +\label{sec:orge97ecd2} +\subsection{a} +\label{sec:org8eb6283} +Examples: +\begin{enumerate} +\item "xy" +\item "xxyy" +\item "xxxyyy" +\item "xxxxyyyy" +\end{enumerate} + +This grammar generates a string of x's prepended to a string of y's with the same amount of characters. + +The grammar is unambiguous. + +\subsection{b} +\label{sec:org3b9ad4f} +Example: +\begin{enumerate} +\item "x" +\item "-xx" +\item "+-xx-xx" +\item "-x-xx" +\end{enumerate} + +This grammar generates addition and subtraction prefix expressions on "x" or chains of "x". + +The grammar is unambiguous. + +\subsection{c} +\label{sec:orgb960951} +Example: +\begin{enumerate} +\item "" +\item "()" +\item "()()" +\item "(())" +\end{enumerate} + +This grammar generates balanced sets of potentially nested parentheses. + +The grammar is ambiguous. For example, the string "()()" can be parsed multiple ways: + +\begin{center} +\includegraphics[width=250px]{./4c.png} +\end{center} + +\subsection{d} +\label{sec:org13f5216} +\begin{enumerate} +\item "" +\item "xyyx" +\item "xyxy" +\item "xy" +\end{enumerate} + +This grammar generates jumbled combinations of the same number of x's and y's. + +The grammar is ambiguous. + +For example, the string "xyxy" has two parse trees: + +\begin{center} +\includegraphics[width=250px]{./4d.png} +\end{center} + +\subsection{e} +\label{sec:org88fd9c7} +\begin{enumerate} +\item "(x)**" +\item "x+x" +\item "x*" +\item "xx" +\end{enumerate} + +This grammar generates combinations of + / * expressions on x in any nested amount of balanced parentheses. + +This grammar is unambiguous. + +\section{Question Five} +\label{sec:org85d516c} +\subsection{a} +\label{sec:orga24c671} +\begin{center} +\includegraphics[width=250px]{./5a.png} +\end{center} + +\subsection{b} +\label{sec:org2837306} +\begin{center} +\includegraphics[width=250px]{./5b.png} +\end{center} + +\section{Question Six} +\label{sec:orgfa68e73} +\begin{verbatim} +expr -> expr expr op +expr -> factor +op -> + | - | / | * +factor -> 0 +factor -> 1 +factor ... 9 +\end{verbatim} +\section{Question Seven} +\label{sec:org6a6c81c} +\begin{verbatim} +sentence -> character +sentence -> character,sentence +character -> a +character -> b +character -> ... +\end{verbatim} +\section{Question Eight} +\label{sec:orgc9de4dd} +\begin{verbatim} +expr -> expr - term +expr -> expr + term +expr -> expr * term +expr -> expr / term +expr -> term +term -> +num +term -> -num +term -> num +num -> Integer +num -> Ident +\end{verbatim} +\section{Question Nine} +\label{sec:org0c5cd84} +\begin{center} +\includegraphics[width=250px]{./9.png} +\end{center} +\section{Question Ten} +\label{sec:org51dec25} +\begin{verbatim} +expr -> {print '+'} expr + term +expr -> {print '-'} expr - term +expr -> term +term -> 0 {print '0'} +term -> 1 {print '1'} +term -> ... {print ...} +\end{verbatim} +\section{Question Eleven} +\label{sec:orgfe9e241} +\subsection{3-1+2} +\label{sec:org327f033} +\begin{center} +\includegraphics[width=250px]{./11-1.png} +\end{center} +\subsection{2+3-1} +\label{sec:org9170442} +\begin{center} +\includegraphics[width=250px]{./11-2.png} +\end{center} + +\section{Question Twelve} +\label{sec:org24eacf8} +Predictive parsing is a special kind of recursive descent parsing where the "lookahead unambiguously determines the flow of control". +\section{Question Thirteen} +\label{sec:org23d57f5} +None of the productions have the same start symbol (disjoint \texttt{FIRST} sets), and it is not left-recursive. +\section{Question Fourteen} +\label{sec:org71b8b64} +See \texttt{Infix.java}. +\section{Question Fifteen} +\label{sec:org1569332} +\begin{verbatim} +A -> w R +R -> x y z R | \epsilon +\end{verbatim} +\section{Question Sixteen} +\label{sec:org626e3a3} +\begin{verbatim} +S -> w R +R -> x y z R +R -> g h R +R -> \epsilon +\end{verbatim} +\section{Question Seventeen} +\label{sec:org8167863} +\begin{center} +\includegraphics[width=.9\linewidth]{./17.png} +\end{center} +\section{Question Eighteen} +\label{sec:org2d4c8ce} +\begin{center} +\includegraphics[width=250px]{./18.png} +\end{center} +\end{document}
\ No newline at end of file diff --git a/Homework/cs5300/hw-MIPS/Mars4_5.jar b/Homework/cs5300/hw-MIPS/Mars4_5.jar Binary files differnew file mode 100644 index 0000000..0021281 --- /dev/null +++ b/Homework/cs5300/hw-MIPS/Mars4_5.jar diff --git a/Homework/cs5300/hw-MIPS/code/add.asm b/Homework/cs5300/hw-MIPS/code/add.asm new file mode 100644 index 0000000..7f9ff9d --- /dev/null +++ b/Homework/cs5300/hw-MIPS/code/add.asm @@ -0,0 +1,12 @@ +.data + +.text +main: + li $s0, 3 + li $s1, 4 + add $a0, $s0, $s1 + li $v0, 1 + syscall + + li $v0,10 + syscall diff --git a/Homework/cs5300/hw-MIPS/code/array.asm b/Homework/cs5300/hw-MIPS/code/array.asm new file mode 100644 index 0000000..f802ec6 --- /dev/null +++ b/Homework/cs5300/hw-MIPS/code/array.asm @@ -0,0 +1,64 @@ +.text + +.globl main + +main: + subi $sp, $sp, 4 + sw $ra, 0($sp) # Put return address on the bottom of the stack + + subi $sp, $sp, 12 # 3 elements on the stack + la $t0, 0($sp) # Part of the dope vector - address of the array + li $t1, 3 # Might as well put the array length here too + li $s0, 1 # Put in registers... + li $s1, 3 + li $s2, 5 + sw $s0, 0($sp) # Push on stack... + sw $s1, 4($sp) + sw $s2, 8($sp) + + subi $sp, $sp, 8 # Dope vector two words: <address> <length> + sw $t0, 0($sp) + sw $t1, 4($sp) + + jal fee + + lw $a0, 0($sp) + addi $sp, $sp, 4 + + li $v0, 1 + syscall + + li $v0, 10 + syscall + +fee: + li $t0, 0 # i + li $t1, 0 # sum + lw $t2, 4($sp) # n + lw $t3, 0($sp) # address + addi $sp, $sp, 16 + + b loop + +loop: + li $t4, 4 # sizeof(int) + + mult $t0, $t4 + mflo $t6 + add $t5, $t3, $t6 + lw $t7, ($t5) # element = address + i * 4 + + add $t1, $t1, $t7 # sum = sum + element + addi $t0, $t0, 1 # i = i + 1 + + bne $t0, $t2, loop + + # return sum + subi $sp, $sp, 4 + sw $t1, 0($sp) + + jr $ra + +# Start .data segment (data!) +.data + diff --git a/Homework/cs5300/hw-MIPS/code/fee.asm b/Homework/cs5300/hw-MIPS/code/fee.asm new file mode 100644 index 0000000..c38b5d6 --- /dev/null +++ b/Homework/cs5300/hw-MIPS/code/fee.asm @@ -0,0 +1,44 @@ +.text + +.globl main +main: + # put 3 and 4 on the stack using the register $sp + subi $sp, $sp, 8 + li $s0, 3 + li $s1, 4 + + sw $s0, 0($sp) + sw $s1, 4($sp) + + # call fee. You'll probably want to use the jal instruction to + # store the current address in $ra + + jal fee + + # load results from stack to registers + lw $a0, 0($sp) + addi $sp, $sp, 4 + # print result + li $v0, 1 + syscall + + # exit + li $v0, 10 + syscall +fee: + # copy a and b from stack to local registers + lw $s1, 4($sp) + lw $s0, 0($sp) + + # add a and b + add $t0, $s1, $s0 + + # place result on stack + addi $sp, $sp, 4 # No need to keep $sp allocated for two words when we're just returning a result + sw $t0, 0($sp) + + # return using jr instruction + jr $ra + +# Start .data segment (data!) +.data diff --git a/Homework/cs5300/hw-MIPS/hw-MIPS.pdf b/Homework/cs5300/hw-MIPS/hw-MIPS.pdf Binary files differnew file mode 100644 index 0000000..c8bf507 --- /dev/null +++ b/Homework/cs5300/hw-MIPS/hw-MIPS.pdf diff --git a/Homework/cs5300/p4-formatter/antlr-4.9.1-complete.jar b/Homework/cs5300/p4-formatter/antlr-4.9.1-complete.jar Binary files differnew file mode 100644 index 0000000..1bb7e8c --- /dev/null +++ b/Homework/cs5300/p4-formatter/antlr-4.9.1-complete.jar diff --git a/Homework/cs5300/p4-formatter/data/test0.c b/Homework/cs5300/p4-formatter/data/test0.c new file mode 100644 index 0000000..1198b0e --- /dev/null +++ b/Homework/cs5300/p4-formatter/data/test0.c @@ -0,0 +1,29 @@ +int + + + a + , b; +int c[ 3 + +]; + +int fie () { + +print(a); + } + +int foo(int a) { + print(a); +} + +int add(int a[], int b) { + int c, z; + c -= 3; + if (c || b<3 || !!!(false) || b >12 +z < 14+3-2*1) { + c -= 3 + 8; + b--; + {} + z = (a[0] + c[2] - (b+a*3)); + } +} + diff --git a/Homework/cs5300/p4-formatter/data/test0.out b/Homework/cs5300/p4-formatter/data/test0.out new file mode 100644 index 0000000..12b25f6 --- /dev/null +++ b/Homework/cs5300/p4-formatter/data/test0.out @@ -0,0 +1,40 @@ +Parsing data/test0.c + + + +Building abstract syntax tree + +Undefined symbol on line 12: print +Undefined symbol on line 16: print + +Formatted code: + +int a, b; +int c[3]; + +int fie() +{ + print(a); +} + +int foo(int a) +{ + print(a); +} + +int add(int a[], int b) +{ + int c, z; + c -= 3; + if (c || b < 3 || !!!(false) || b > 12 + z < 14 + 3 - 2 * 1) + { + c -= 3 + 8; + b--; + { + } + z = (a[0] + c[2] - (b + a * 3)); + } +} + + +Process finished with exit code 0 diff --git a/Homework/cs5300/p4-formatter/data/test1.c b/Homework/cs5300/p4-formatter/data/test1.c new file mode 100644 index 0000000..bad6240 --- /dev/null +++ b/Homework/cs5300/p4-formatter/data/test1.c @@ -0,0 +1,65 @@ +int a, b; +int c[3]; + +int add(int a[], int b) { + int c, z; + c -= 3; + if (c || b<3) { + c -= 3; + } else + c *= 3; + while (b > 0) { + int d; + b = a; + } + b = a; + z = a; + return b; +} + +void nothing(char c) { + // do nothing + add(1, 3); +} + +int ant(int bat, int cat[], bool dog, bool elk, int fox) +{ + int gnu, hog[100]; + gnu = hog[2] = 3**cat; + if (dog && elk || bat > cat[3]) dog = !dog; + else fox++; + if (bat <= fox) { + while (dog) { + int hog; // hog in new scope + hog = fox; + // undefined symbol error + dog = fred(fox++, cat)>666; + if (hog>bat) break; + else if (fox!=0) fox += 7; + } + } + return (fox+bat*cat[bat])/-fox; +} + +int main() { + int a; + a = 0; + while (a + < 3 +) a++; + + + b = 3; + c *= (18 / (3-1)-3) ; + c ++; + // Should give an undefined symbol error + z++; + break ; + + // Should give an underfined symbol error + fie(); + return + + 0; + +} diff --git a/Homework/cs5300/p4-formatter/data/test1.out b/Homework/cs5300/p4-formatter/data/test1.out new file mode 100644 index 0000000..1daaaea --- /dev/null +++ b/Homework/cs5300/p4-formatter/data/test1.out @@ -0,0 +1,82 @@ + +Parsing data/test1.c + + + +Building abstract syntax tree + +Undefined symbol on line 36: fred +Undefined symbol on line 56: z +Undefined symbol on line 60: fie + +Formatted code: + +int a, b; +int c[3]; + +int add(int a[], int b) +{ + int c, z; + c -= 3; + if (c || b < 3) + { + c -= 3; + } + else + c *= 3; + while (b > 0) + { + int d; + b = a; + } + b = a; + z = a; + return b; +} + +void nothing(char c) +{ + add(1, 3); +} + +int ant(int bat, int cat[], bool dog, bool elk, int fox) +{ + int gnu, hog[100]; + gnu = hog[2] = 3 * *cat; + if (dog && elk || bat > cat[3]) + dog = !dog; + else + fox++; + if (bat <= fox) + { + while (dog) + { + int hog; + hog = fox; + dog = fred(fox++, cat) > 666; + if (hog > bat) + break; + else + if (fox != 0) + fox += 7; + } + } + return (fox + bat * cat[bat]) / -fox; +} + +int main() +{ + int a; + a = 0; + while (a < 3) + a++; + b = 3; + c *= (18 / (3 - 1) - 3); + c++; + z++; + break; + fie(); + return 0; +} + + diff --git a/Homework/cs5300/p4-formatter/grammar/Cminus.g4 b/Homework/cs5300/p4-formatter/grammar/Cminus.g4 new file mode 100644 index 0000000..9538eea --- /dev/null +++ b/Homework/cs5300/p4-formatter/grammar/Cminus.g4 @@ -0,0 +1,91 @@ +grammar Cminus; + +program : declaration+ ; + +declaration : varDeclaration | funDeclaration ; + +varDeclaration : typeSpecifier varDeclId (',' varDeclId)* ';' ; + +varDeclId : ID | ID '[' NUMCONST ']' ; + +funDeclaration : ('void' | typeSpecifier) ID '(' param? (',' param)* ')' statement ; +//funDeclaration : ('void' | typeSpecifier) ID '(' param? (',' param)* ')' compoundStatement ; + +typeSpecifier : 'int' | 'bool' | 'char' ; + +param : typeSpecifier paramId ; + +paramId : ID | ID '[]' ; + +statement : expressionStmt | compoundStmt | ifStmt + | whileStmt | returnStmt | breakStmt ; + +compoundStmt : '{' varDeclaration* statement* '}' ; + +expressionStmt : expression ';' | ';' ; + +ifStmt : 'if' '(' simpleExpression ')' statement + | 'if' '(' simpleExpression ')' statement 'else' statement + ; + +whileStmt : 'while' '(' simpleExpression ')' statement ; + +returnStmt : 'return' ';' | 'return' expression ';' ; + +breakStmt : 'break' ';' ; + +expression : mutable '=' expression + | mutable '+=' expression | mutable '-=' expression + | mutable '*=' expression | mutable '/=' expression + | mutable '++' | mutable '--' | simpleExpression + ; + +simpleExpression : orExpression ; + +orExpression : (andExpression '||')* andExpression ; + +andExpression : (unaryRelExpression '&&')* unaryRelExpression ; + +unaryRelExpression : BANG* relExpression ; + +relExpression : (sumExpression relop)* sumExpression ; + +relop : '<=' | '<' | '>' | '>=' | '==' | '!=' ; + +sumExpression : (termExpression sumop)* termExpression ; + +sumop : '+' | '-' ; + +termExpression : (unaryExpression mulop)* unaryExpression ; + +mulop : '*' | '/' | '%' ; + +unaryExpression : unaryop* factor ; + +unaryop : '-' | '*' | '?' ; + +factor : immutable | mutable ; + +mutable : ID | ID '[' expression ']' ; + +immutable : '(' expression ')' | call | constant ; + +call : ID '(' (expression ',')* expression? ')' ; + +constant : NUMCONST | CHARCONST | STRINGCONST | 'true' | 'false' ; + +ID : LETTER (LETTER | DIGIT)* ; +NUMCONST : DIGIT+ ; +STRINGCONST : '"' ('\\"'|~'"')*? '"' ; +CHARCONST : '"' ('\\"'|~'"') '"' ; +BANG : '!' ; + +WS : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip ; +COMMENT + : ( '//' ~[\r\n]* '\r'? '\n' + | '/*' .*? '*/' + ) -> skip + ; + +fragment LETTER : ('a'..'z' | 'A'..'Z'); +fragment DIGIT : ('0'..'9'); diff --git a/Homework/cs5300/p4-formatter/main/Main.java b/Homework/cs5300/p4-formatter/main/Main.java new file mode 100644 index 0000000..7eb7d72 --- /dev/null +++ b/Homework/cs5300/p4-formatter/main/Main.java @@ -0,0 +1,76 @@ +package main; + +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import parser.CminusLexer; +import parser.CminusParser; +import submit.ASTVisitor; +import org.antlr.v4.runtime.Parser; +import submit.ast.Node; + +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Main { + private static Logger LOGGER; + + /** + * @param args the command line arguments + * @throws java.io.IOException + */ + public static void main(String[] args) throws IOException { + // Logging setup + Level level = Level.INFO; + + // TODO Enable trace-level code as needed. When true, LOGGER.fine() statements + // will be visible. + final boolean trace = true; + if (trace) { + level = Level.ALL; + } + + Properties props = System.getProperties(); + props.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%6$s%n"); + Logger.getLogger("").setLevel(level); + for (Handler handler : Logger.getLogger("").getHandlers()) { + handler.setLevel(level); + } + LOGGER = Logger.getLogger(Parser.class.getName()); + + // TODO Update the filename as needed + final String filename = "data/test1.c"; + + LOGGER.info(""); + LOGGER.info("Parsing " + filename + "\n"); + LOGGER.info(""); + final CharStream charStream = CharStreams.fromFileName(filename); + CminusLexer lexer = new CminusLexer(charStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + CminusParser parser = new CminusParser(tokens); + parser.setBuildParseTree(true); + CminusParser.ProgramContext programCtx = parser.program(); + + // TODO Implement building of the parse tree + LOGGER.info(""); + LOGGER.info("Building abstract syntax tree"); + LOGGER.info(""); + ASTVisitor v = new ASTVisitor(LOGGER); + Node ast = v.visitProgram(programCtx); + + // TODO Output formatted code + LOGGER.info(""); + LOGGER.info("Formatted code:"); + LOGGER.info(""); + StringBuilder builder = new StringBuilder(); + try { + ast.toCminus(builder, ""); + } finally { + LOGGER.info(builder.toString()); + } + } + +} diff --git a/Homework/cs5300/p4-formatter/parser/Cminus.interp b/Homework/cs5300/p4-formatter/parser/Cminus.interp new file mode 100644 index 0000000..af140be --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/Cminus.interp @@ -0,0 +1,140 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +program +declaration +varDeclaration +varDeclId +funDeclaration +typeSpecifier +param +paramId +statement +compoundStmt +expressionStmt +ifStmt +whileStmt +returnStmt +breakStmt +expression +simpleExpression +orExpression +andExpression +unaryRelExpression +relExpression +relop +sumExpression +sumop +termExpression +mulop +unaryExpression +unaryop +factor +mutable +immutable +call +constant + + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 50, 329, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 3, 2, 6, 2, 70, 10, 2, 13, 2, 14, 2, 71, 3, 3, 3, 3, 5, 3, 76, 10, 3, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 82, 10, 4, 12, 4, 14, 4, 85, 11, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 94, 10, 5, 3, 6, 3, 6, 5, 6, 98, 10, 6, 3, 6, 3, 6, 3, 6, 5, 6, 103, 10, 6, 3, 6, 3, 6, 7, 6, 107, 10, 6, 12, 6, 14, 6, 110, 11, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 5, 9, 123, 10, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 5, 10, 131, 10, 10, 3, 11, 3, 11, 7, 11, 135, 10, 11, 12, 11, 14, 11, 138, 11, 11, 3, 11, 7, 11, 141, 10, 11, 12, 11, 14, 11, 144, 11, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 5, 12, 152, 10, 12, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 5, 13, 168, 10, 13, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 5, 15, 182, 10, 15, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 5, 17, 214, 10, 17, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 7, 19, 221, 10, 19, 12, 19, 14, 19, 224, 11, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 20, 7, 20, 231, 10, 20, 12, 20, 14, 20, 234, 11, 20, 3, 20, 3, 20, 3, 21, 7, 21, 239, 10, 21, 12, 21, 14, 21, 242, 11, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 7, 22, 249, 10, 22, 12, 22, 14, 22, 252, 11, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 7, 24, 261, 10, 24, 12, 24, 14, 24, 264, 11, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 7, 26, 273, 10, 26, 12, 26, 14, 26, 276, 11, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 28, 7, 28, 283, 10, 28, 12, 28, 14, 28, 286, 11, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 30, 3, 30, 5, 30, 294, 10, 30, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 5, 31, 302, 10, 31, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 5, 32, 310, 10, 32, 3, 33, 3, 33, 3, 33, 3, 33, 3, 33, 7, 33, 317, 10, 33, 12, 33, 14, 33, 320, 11, 33, 3, 33, 5, 33, 323, 10, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 2, 2, 35, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 2, 8, 3, 2, 10, 12, 3, 2, 30, 35, 3, 2, 36, 37, 3, 2, 38, 40, 4, 2, 37, 38, 41, 41, 4, 2, 42, 43, 45, 47, 2, 333, 2, 69, 3, 2, 2, 2, 4, 75, 3, 2, 2, 2, 6, 77, 3, 2, 2, 2, 8, 93, 3, 2, 2, 2, 10, 97, 3, 2, 2, 2, 12, 114, 3, 2, 2, 2, 14, 116, 3, 2, 2, 2, 16, 122, 3, 2, 2, 2, 18, 130, 3, 2, 2, 2, 20, 132, 3, 2, 2, 2, 22, 151, 3, 2, 2, 2, 24, 167, 3, 2, 2, 2, 26, 169, 3, 2, 2, 2, 28, 181, 3, 2, 2, 2, 30, 183, 3, 2, 2, 2, 32, 213, 3, 2, 2, 2, 34, 215, 3, 2, 2, 2, 36, 222, 3, 2, 2, 2, 38, 232, 3, 2, 2, 2, 40, 240, 3, 2, 2, 2, 42, 250, 3, 2, 2, 2, 44, 255, 3, 2, 2, 2, 46, 262, 3, 2, 2, 2, 48, 267, 3, 2, 2, 2, 50, 274, 3, 2, 2, 2, 52, 279, 3, 2, 2, 2, 54, 284, 3, 2, 2, 2, 56, 289, 3, 2, 2, 2, 58, 293, 3, 2, 2, 2, 60, 301, 3, 2, 2, 2, 62, 309, 3, 2, 2, 2, 64, 311, 3, 2, 2, 2, 66, 326, 3, 2, 2, 2, 68, 70, 5, 4, 3, 2, 69, 68, 3, 2, 2, 2, 70, 71, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 72, 3, 2, 2, 2, 72, 3, 3, 2, 2, 2, 73, 76, 5, 6, 4, 2, 74, 76, 5, 10, 6, 2, 75, 73, 3, 2, 2, 2, 75, 74, 3, 2, 2, 2, 76, 5, 3, 2, 2, 2, 77, 78, 5, 12, 7, 2, 78, 83, 5, 8, 5, 2, 79, 80, 7, 3, 2, 2, 80, 82, 5, 8, 5, 2, 81, 79, 3, 2, 2, 2, 82, 85, 3, 2, 2, 2, 83, 81, 3, 2, 2, 2, 83, 84, 3, 2, 2, 2, 84, 86, 3, 2, 2, 2, 85, 83, 3, 2, 2, 2, 86, 87, 7, 4, 2, 2, 87, 7, 3, 2, 2, 2, 88, 94, 7, 44, 2, 2, 89, 90, 7, 44, 2, 2, 90, 91, 7, 5, 2, 2, 91, 92, 7, 45, 2, 2, 92, 94, 7, 6, 2, 2, 93, 88, 3, 2, 2, 2, 93, 89, 3, 2, 2, 2, 94, 9, 3, 2, 2, 2, 95, 98, 7, 7, 2, 2, 96, 98, 5, 12, 7, 2, 97, 95, 3, 2, 2, 2, 97, 96, 3, 2, 2, 2, 98, 99, 3, 2, 2, 2, 99, 100, 7, 44, 2, 2, 100, 102, 7, 8, 2, 2, 101, 103, 5, 14, 8, 2, 102, 101, 3, 2, 2, 2, 102, 103, 3, 2, 2, 2, 103, 108, 3, 2, 2, 2, 104, 105, 7, 3, 2, 2, 105, 107, 5, 14, 8, 2, 106, 104, 3, 2, 2, 2, 107, 110, 3, 2, 2, 2, 108, 106, 3, 2, 2, 2, 108, 109, 3, 2, 2, 2, 109, 111, 3, 2, 2, 2, 110, 108, 3, 2, 2, 2, 111, 112, 7, 9, 2, 2, 112, 113, 5, 18, 10, 2, 113, 11, 3, 2, 2, 2, 114, 115, 9, 2, 2, 2, 115, 13, 3, 2, 2, 2, 116, 117, 5, 12, 7, 2, 117, 118, 5, 16, 9, 2, 118, 15, 3, 2, 2, 2, 119, 123, 7, 44, 2, 2, 120, 121, 7, 44, 2, 2, 121, 123, 7, 13, 2, 2, 122, 119, 3, 2, 2, 2, 122, 120, 3, 2, 2, 2, 123, 17, 3, 2, 2, 2, 124, 131, 5, 22, 12, 2, 125, 131, 5, 20, 11, 2, 126, 131, 5, 24, 13, 2, 127, 131, 5, 26, 14, 2, 128, 131, 5, 28, 15, 2, 129, 131, 5, 30, 16, 2, 130, 124, 3, 2, 2, 2, 130, 125, 3, 2, 2, 2, 130, 126, 3, 2, 2, 2, 130, 127, 3, 2, 2, 2, 130, 128, 3, 2, 2, 2, 130, 129, 3, 2, 2, 2, 131, 19, 3, 2, 2, 2, 132, 136, 7, 14, 2, 2, 133, 135, 5, 6, 4, 2, 134, 133, 3, 2, 2, 2, 135, 138, 3, 2, 2, 2, 136, 134, 3, 2, 2, 2, 136, 137, 3, 2, 2, 2, 137, 142, 3, 2, 2, 2, 138, 136, 3, 2, 2, 2, 139, 141, 5, 18, 10, 2, 140, 139, 3, 2, 2, 2, 141, 144, 3, 2, 2, 2, 142, 140, 3, 2, 2, 2, 142, 143, 3, 2, 2, 2, 143, 145, 3, 2, 2, 2, 144, 142, 3, 2, 2, 2, 145, 146, 7, 15, 2, 2, 146, 21, 3, 2, 2, 2, 147, 148, 5, 32, 17, 2, 148, 149, 7, 4, 2, 2, 149, 152, 3, 2, 2, 2, 150, 152, 7, 4, 2, 2, 151, 147, 3, 2, 2, 2, 151, 150, 3, 2, 2, 2, 152, 23, 3, 2, 2, 2, 153, 154, 7, 16, 2, 2, 154, 155, 7, 8, 2, 2, 155, 156, 5, 34, 18, 2, 156, 157, 7, 9, 2, 2, 157, 158, 5, 18, 10, 2, 158, 168, 3, 2, 2, 2, 159, 160, 7, 16, 2, 2, 160, 161, 7, 8, 2, 2, 161, 162, 5, 34, 18, 2, 162, 163, 7, 9, 2, 2, 163, 164, 5, 18, 10, 2, 164, 165, 7, 17, 2, 2, 165, 166, 5, 18, 10, 2, 166, 168, 3, 2, 2, 2, 167, 153, 3, 2, 2, 2, 167, 159, 3, 2, 2, 2, 168, 25, 3, 2, 2, 2, 169, 170, 7, 18, 2, 2, 170, 171, 7, 8, 2, 2, 171, 172, 5, 34, 18, 2, 172, 173, 7, 9, 2, 2, 173, 174, 5, 18, 10, 2, 174, 27, 3, 2, 2, 2, 175, 176, 7, 19, 2, 2, 176, 182, 7, 4, 2, 2, 177, 178, 7, 19, 2, 2, 178, 179, 5, 32, 17, 2, 179, 180, 7, 4, 2, 2, 180, 182, 3, 2, 2, 2, 181, 175, 3, 2, 2, 2, 181, 177, 3, 2, 2, 2, 182, 29, 3, 2, 2, 2, 183, 184, 7, 20, 2, 2, 184, 185, 7, 4, 2, 2, 185, 31, 3, 2, 2, 2, 186, 187, 5, 60, 31, 2, 187, 188, 7, 21, 2, 2, 188, 189, 5, 32, 17, 2, 189, 214, 3, 2, 2, 2, 190, 191, 5, 60, 31, 2, 191, 192, 7, 22, 2, 2, 192, 193, 5, 32, 17, 2, 193, 214, 3, 2, 2, 2, 194, 195, 5, 60, 31, 2, 195, 196, 7, 23, 2, 2, 196, 197, 5, 32, 17, 2, 197, 214, 3, 2, 2, 2, 198, 199, 5, 60, 31, 2, 199, 200, 7, 24, 2, 2, 200, 201, 5, 32, 17, 2, 201, 214, 3, 2, 2, 2, 202, 203, 5, 60, 31, 2, 203, 204, 7, 25, 2, 2, 204, 205, 5, 32, 17, 2, 205, 214, 3, 2, 2, 2, 206, 207, 5, 60, 31, 2, 207, 208, 7, 26, 2, 2, 208, 214, 3, 2, 2, 2, 209, 210, 5, 60, 31, 2, 210, 211, 7, 27, 2, 2, 211, 214, 3, 2, 2, 2, 212, 214, 5, 34, 18, 2, 213, 186, 3, 2, 2, 2, 213, 190, 3, 2, 2, 2, 213, 194, 3, 2, 2, 2, 213, 198, 3, 2, 2, 2, 213, 202, 3, 2, 2, 2, 213, 206, 3, 2, 2, 2, 213, 209, 3, 2, 2, 2, 213, 212, 3, 2, 2, 2, 214, 33, 3, 2, 2, 2, 215, 216, 5, 36, 19, 2, 216, 35, 3, 2, 2, 2, 217, 218, 5, 38, 20, 2, 218, 219, 7, 28, 2, 2, 219, 221, 3, 2, 2, 2, 220, 217, 3, 2, 2, 2, 221, 224, 3, 2, 2, 2, 222, 220, 3, 2, 2, 2, 222, 223, 3, 2, 2, 2, 223, 225, 3, 2, 2, 2, 224, 222, 3, 2, 2, 2, 225, 226, 5, 38, 20, 2, 226, 37, 3, 2, 2, 2, 227, 228, 5, 40, 21, 2, 228, 229, 7, 29, 2, 2, 229, 231, 3, 2, 2, 2, 230, 227, 3, 2, 2, 2, 231, 234, 3, 2, 2, 2, 232, 230, 3, 2, 2, 2, 232, 233, 3, 2, 2, 2, 233, 235, 3, 2, 2, 2, 234, 232, 3, 2, 2, 2, 235, 236, 5, 40, 21, 2, 236, 39, 3, 2, 2, 2, 237, 239, 7, 48, 2, 2, 238, 237, 3, 2, 2, 2, 239, 242, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 240, 241, 3, 2, 2, 2, 241, 243, 3, 2, 2, 2, 242, 240, 3, 2, 2, 2, 243, 244, 5, 42, 22, 2, 244, 41, 3, 2, 2, 2, 245, 246, 5, 46, 24, 2, 246, 247, 5, 44, 23, 2, 247, 249, 3, 2, 2, 2, 248, 245, 3, 2, 2, 2, 249, 252, 3, 2, 2, 2, 250, 248, 3, 2, 2, 2, 250, 251, 3, 2, 2, 2, 251, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 254, 5, 46, 24, 2, 254, 43, 3, 2, 2, 2, 255, 256, 9, 3, 2, 2, 256, 45, 3, 2, 2, 2, 257, 258, 5, 50, 26, 2, 258, 259, 5, 48, 25, 2, 259, 261, 3, 2, 2, 2, 260, 257, 3, 2, 2, 2, 261, 264, 3, 2, 2, 2, 262, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 265, 3, 2, 2, 2, 264, 262, 3, 2, 2, 2, 265, 266, 5, 50, 26, 2, 266, 47, 3, 2, 2, 2, 267, 268, 9, 4, 2, 2, 268, 49, 3, 2, 2, 2, 269, 270, 5, 54, 28, 2, 270, 271, 5, 52, 27, 2, 271, 273, 3, 2, 2, 2, 272, 269, 3, 2, 2, 2, 273, 276, 3, 2, 2, 2, 274, 272, 3, 2, 2, 2, 274, 275, 3, 2, 2, 2, 275, 277, 3, 2, 2, 2, 276, 274, 3, 2, 2, 2, 277, 278, 5, 54, 28, 2, 278, 51, 3, 2, 2, 2, 279, 280, 9, 5, 2, 2, 280, 53, 3, 2, 2, 2, 281, 283, 5, 56, 29, 2, 282, 281, 3, 2, 2, 2, 283, 286, 3, 2, 2, 2, 284, 282, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 287, 3, 2, 2, 2, 286, 284, 3, 2, 2, 2, 287, 288, 5, 58, 30, 2, 288, 55, 3, 2, 2, 2, 289, 290, 9, 6, 2, 2, 290, 57, 3, 2, 2, 2, 291, 294, 5, 62, 32, 2, 292, 294, 5, 60, 31, 2, 293, 291, 3, 2, 2, 2, 293, 292, 3, 2, 2, 2, 294, 59, 3, 2, 2, 2, 295, 302, 7, 44, 2, 2, 296, 297, 7, 44, 2, 2, 297, 298, 7, 5, 2, 2, 298, 299, 5, 32, 17, 2, 299, 300, 7, 6, 2, 2, 300, 302, 3, 2, 2, 2, 301, 295, 3, 2, 2, 2, 301, 296, 3, 2, 2, 2, 302, 61, 3, 2, 2, 2, 303, 304, 7, 8, 2, 2, 304, 305, 5, 32, 17, 2, 305, 306, 7, 9, 2, 2, 306, 310, 3, 2, 2, 2, 307, 310, 5, 64, 33, 2, 308, 310, 5, 66, 34, 2, 309, 303, 3, 2, 2, 2, 309, 307, 3, 2, 2, 2, 309, 308, 3, 2, 2, 2, 310, 63, 3, 2, 2, 2, 311, 312, 7, 44, 2, 2, 312, 318, 7, 8, 2, 2, 313, 314, 5, 32, 17, 2, 314, 315, 7, 3, 2, 2, 315, 317, 3, 2, 2, 2, 316, 313, 3, 2, 2, 2, 317, 320, 3, 2, 2, 2, 318, 316, 3, 2, 2, 2, 318, 319, 3, 2, 2, 2, 319, 322, 3, 2, 2, 2, 320, 318, 3, 2, 2, 2, 321, 323, 5, 32, 17, 2, 322, 321, 3, 2, 2, 2, 322, 323, 3, 2, 2, 2, 323, 324, 3, 2, 2, 2, 324, 325, 7, 9, 2, 2, 325, 65, 3, 2, 2, 2, 326, 327, 9, 7, 2, 2, 327, 67, 3, 2, 2, 2, 29, 71, 75, 83, 93, 97, 102, 108, 122, 130, 136, 142, 151, 167, 181, 213, 222, 232, 240, 250, 262, 274, 284, 293, 301, 309, 318, 322]
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/Cminus.tokens b/Homework/cs5300/p4-formatter/parser/Cminus.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/Cminus.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/Homework/cs5300/p4-formatter/parser/CminusBaseListener.java b/Homework/cs5300/p4-formatter/parser/CminusBaseListener.java new file mode 100644 index 0000000..65a3493 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusBaseListener.java @@ -0,0 +1,435 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link CminusListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class CminusBaseListener implements CminusListener { + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterConstant(CminusParser.ConstantContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitConstant(CminusParser.ConstantContext ctx) { } + + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitErrorNode(ErrorNode node) { } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusBaseVisitor.java b/Homework/cs5300/p4-formatter/parser/CminusBaseVisitor.java new file mode 100644 index 0000000..150f316 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusBaseVisitor.java @@ -0,0 +1,245 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + +/** + * This class provides an empty implementation of {@link CminusVisitor}, + * which can be extended to create a visitor which only needs to handle a subset + * of the available methods. + * + * @param <T> The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public class CminusBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements CminusVisitor<T> { + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitProgram(CminusParser.ProgramContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitDeclaration(CminusParser.DeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitVarDeclId(CminusParser.VarDeclIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitParam(CminusParser.ParamContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitParamId(CminusParser.ParamIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitStatement(CminusParser.StatementContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitIfStmt(CminusParser.IfStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitWhileStmt(CminusParser.WhileStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitReturnStmt(CminusParser.ReturnStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitBreakStmt(CminusParser.BreakStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitExpression(CminusParser.ExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitOrExpression(CminusParser.OrExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitAndExpression(CminusParser.AndExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitRelExpression(CminusParser.RelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitRelop(CminusParser.RelopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSumExpression(CminusParser.SumExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitSumop(CminusParser.SumopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitTermExpression(CminusParser.TermExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitMulop(CminusParser.MulopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitUnaryop(CminusParser.UnaryopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitFactor(CminusParser.FactorContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitMutable(CminusParser.MutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitImmutable(CminusParser.ImmutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitCall(CminusParser.CallContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + * <p>The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.</p> + */ + @Override public T visitConstant(CminusParser.ConstantContext ctx) { return visitChildren(ctx); } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusLexer.interp b/Homework/cs5300/p4-formatter/parser/CminusLexer.interp new file mode 100644 index 0000000..a9dc30a --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusLexer.interp @@ -0,0 +1,163 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +T__7 +T__8 +T__9 +T__10 +T__11 +T__12 +T__13 +T__14 +T__15 +T__16 +T__17 +T__18 +T__19 +T__20 +T__21 +T__22 +T__23 +T__24 +T__25 +T__26 +T__27 +T__28 +T__29 +T__30 +T__31 +T__32 +T__33 +T__34 +T__35 +T__36 +T__37 +T__38 +T__39 +T__40 +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 50, 306, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 4, 35, 9, 35, 4, 36, 9, 36, 4, 37, 9, 37, 4, 38, 9, 38, 4, 39, 9, 39, 4, 40, 9, 40, 4, 41, 9, 41, 4, 42, 9, 42, 4, 43, 9, 43, 4, 44, 9, 44, 4, 45, 9, 45, 4, 46, 9, 46, 4, 47, 9, 47, 4, 48, 9, 48, 4, 49, 9, 49, 4, 50, 9, 50, 4, 51, 9, 51, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 16, 3, 16, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 27, 3, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 29, 3, 30, 3, 30, 3, 31, 3, 31, 3, 32, 3, 32, 3, 32, 3, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 3, 35, 3, 35, 3, 36, 3, 36, 3, 37, 3, 37, 3, 38, 3, 38, 3, 39, 3, 39, 3, 40, 3, 40, 3, 41, 3, 41, 3, 41, 3, 41, 3, 41, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 43, 3, 43, 3, 43, 7, 43, 237, 10, 43, 12, 43, 14, 43, 240, 11, 43, 3, 44, 6, 44, 243, 10, 44, 13, 44, 14, 44, 244, 3, 45, 3, 45, 3, 45, 3, 45, 7, 45, 251, 10, 45, 12, 45, 14, 45, 254, 11, 45, 3, 45, 3, 45, 3, 46, 3, 46, 3, 46, 3, 46, 5, 46, 262, 10, 46, 3, 46, 3, 46, 3, 47, 3, 47, 3, 48, 6, 48, 269, 10, 48, 13, 48, 14, 48, 270, 3, 48, 3, 48, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 279, 10, 49, 12, 49, 14, 49, 282, 11, 49, 3, 49, 5, 49, 285, 10, 49, 3, 49, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 292, 10, 49, 12, 49, 14, 49, 295, 11, 49, 3, 49, 3, 49, 5, 49, 299, 10, 49, 3, 49, 3, 49, 3, 50, 3, 50, 3, 51, 3, 51, 4, 252, 293, 2, 52, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35, 19, 37, 20, 39, 21, 41, 22, 43, 23, 45, 24, 47, 25, 49, 26, 51, 27, 53, 28, 55, 29, 57, 30, 59, 31, 61, 32, 63, 33, 65, 34, 67, 35, 69, 36, 71, 37, 73, 38, 75, 39, 77, 40, 79, 41, 81, 42, 83, 43, 85, 44, 87, 45, 89, 46, 91, 47, 93, 48, 95, 49, 97, 50, 99, 2, 101, 2, 3, 2, 6, 3, 2, 36, 36, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 12, 12, 15, 15, 4, 2, 67, 92, 99, 124, 2, 314, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3, 2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 2, 43, 3, 2, 2, 2, 2, 45, 3, 2, 2, 2, 2, 47, 3, 2, 2, 2, 2, 49, 3, 2, 2, 2, 2, 51, 3, 2, 2, 2, 2, 53, 3, 2, 2, 2, 2, 55, 3, 2, 2, 2, 2, 57, 3, 2, 2, 2, 2, 59, 3, 2, 2, 2, 2, 61, 3, 2, 2, 2, 2, 63, 3, 2, 2, 2, 2, 65, 3, 2, 2, 2, 2, 67, 3, 2, 2, 2, 2, 69, 3, 2, 2, 2, 2, 71, 3, 2, 2, 2, 2, 73, 3, 2, 2, 2, 2, 75, 3, 2, 2, 2, 2, 77, 3, 2, 2, 2, 2, 79, 3, 2, 2, 2, 2, 81, 3, 2, 2, 2, 2, 83, 3, 2, 2, 2, 2, 85, 3, 2, 2, 2, 2, 87, 3, 2, 2, 2, 2, 89, 3, 2, 2, 2, 2, 91, 3, 2, 2, 2, 2, 93, 3, 2, 2, 2, 2, 95, 3, 2, 2, 2, 2, 97, 3, 2, 2, 2, 3, 103, 3, 2, 2, 2, 5, 105, 3, 2, 2, 2, 7, 107, 3, 2, 2, 2, 9, 109, 3, 2, 2, 2, 11, 111, 3, 2, 2, 2, 13, 116, 3, 2, 2, 2, 15, 118, 3, 2, 2, 2, 17, 120, 3, 2, 2, 2, 19, 124, 3, 2, 2, 2, 21, 129, 3, 2, 2, 2, 23, 134, 3, 2, 2, 2, 25, 137, 3, 2, 2, 2, 27, 139, 3, 2, 2, 2, 29, 141, 3, 2, 2, 2, 31, 144, 3, 2, 2, 2, 33, 149, 3, 2, 2, 2, 35, 155, 3, 2, 2, 2, 37, 162, 3, 2, 2, 2, 39, 168, 3, 2, 2, 2, 41, 170, 3, 2, 2, 2, 43, 173, 3, 2, 2, 2, 45, 176, 3, 2, 2, 2, 47, 179, 3, 2, 2, 2, 49, 182, 3, 2, 2, 2, 51, 185, 3, 2, 2, 2, 53, 188, 3, 2, 2, 2, 55, 191, 3, 2, 2, 2, 57, 194, 3, 2, 2, 2, 59, 197, 3, 2, 2, 2, 61, 199, 3, 2, 2, 2, 63, 201, 3, 2, 2, 2, 65, 204, 3, 2, 2, 2, 67, 207, 3, 2, 2, 2, 69, 210, 3, 2, 2, 2, 71, 212, 3, 2, 2, 2, 73, 214, 3, 2, 2, 2, 75, 216, 3, 2, 2, 2, 77, 218, 3, 2, 2, 2, 79, 220, 3, 2, 2, 2, 81, 222, 3, 2, 2, 2, 83, 227, 3, 2, 2, 2, 85, 233, 3, 2, 2, 2, 87, 242, 3, 2, 2, 2, 89, 246, 3, 2, 2, 2, 91, 257, 3, 2, 2, 2, 93, 265, 3, 2, 2, 2, 95, 268, 3, 2, 2, 2, 97, 298, 3, 2, 2, 2, 99, 302, 3, 2, 2, 2, 101, 304, 3, 2, 2, 2, 103, 104, 7, 46, 2, 2, 104, 4, 3, 2, 2, 2, 105, 106, 7, 61, 2, 2, 106, 6, 3, 2, 2, 2, 107, 108, 7, 93, 2, 2, 108, 8, 3, 2, 2, 2, 109, 110, 7, 95, 2, 2, 110, 10, 3, 2, 2, 2, 111, 112, 7, 120, 2, 2, 112, 113, 7, 113, 2, 2, 113, 114, 7, 107, 2, 2, 114, 115, 7, 102, 2, 2, 115, 12, 3, 2, 2, 2, 116, 117, 7, 42, 2, 2, 117, 14, 3, 2, 2, 2, 118, 119, 7, 43, 2, 2, 119, 16, 3, 2, 2, 2, 120, 121, 7, 107, 2, 2, 121, 122, 7, 112, 2, 2, 122, 123, 7, 118, 2, 2, 123, 18, 3, 2, 2, 2, 124, 125, 7, 100, 2, 2, 125, 126, 7, 113, 2, 2, 126, 127, 7, 113, 2, 2, 127, 128, 7, 110, 2, 2, 128, 20, 3, 2, 2, 2, 129, 130, 7, 101, 2, 2, 130, 131, 7, 106, 2, 2, 131, 132, 7, 99, 2, 2, 132, 133, 7, 116, 2, 2, 133, 22, 3, 2, 2, 2, 134, 135, 7, 93, 2, 2, 135, 136, 7, 95, 2, 2, 136, 24, 3, 2, 2, 2, 137, 138, 7, 125, 2, 2, 138, 26, 3, 2, 2, 2, 139, 140, 7, 127, 2, 2, 140, 28, 3, 2, 2, 2, 141, 142, 7, 107, 2, 2, 142, 143, 7, 104, 2, 2, 143, 30, 3, 2, 2, 2, 144, 145, 7, 103, 2, 2, 145, 146, 7, 110, 2, 2, 146, 147, 7, 117, 2, 2, 147, 148, 7, 103, 2, 2, 148, 32, 3, 2, 2, 2, 149, 150, 7, 121, 2, 2, 150, 151, 7, 106, 2, 2, 151, 152, 7, 107, 2, 2, 152, 153, 7, 110, 2, 2, 153, 154, 7, 103, 2, 2, 154, 34, 3, 2, 2, 2, 155, 156, 7, 116, 2, 2, 156, 157, 7, 103, 2, 2, 157, 158, 7, 118, 2, 2, 158, 159, 7, 119, 2, 2, 159, 160, 7, 116, 2, 2, 160, 161, 7, 112, 2, 2, 161, 36, 3, 2, 2, 2, 162, 163, 7, 100, 2, 2, 163, 164, 7, 116, 2, 2, 164, 165, 7, 103, 2, 2, 165, 166, 7, 99, 2, 2, 166, 167, 7, 109, 2, 2, 167, 38, 3, 2, 2, 2, 168, 169, 7, 63, 2, 2, 169, 40, 3, 2, 2, 2, 170, 171, 7, 45, 2, 2, 171, 172, 7, 63, 2, 2, 172, 42, 3, 2, 2, 2, 173, 174, 7, 47, 2, 2, 174, 175, 7, 63, 2, 2, 175, 44, 3, 2, 2, 2, 176, 177, 7, 44, 2, 2, 177, 178, 7, 63, 2, 2, 178, 46, 3, 2, 2, 2, 179, 180, 7, 49, 2, 2, 180, 181, 7, 63, 2, 2, 181, 48, 3, 2, 2, 2, 182, 183, 7, 45, 2, 2, 183, 184, 7, 45, 2, 2, 184, 50, 3, 2, 2, 2, 185, 186, 7, 47, 2, 2, 186, 187, 7, 47, 2, 2, 187, 52, 3, 2, 2, 2, 188, 189, 7, 126, 2, 2, 189, 190, 7, 126, 2, 2, 190, 54, 3, 2, 2, 2, 191, 192, 7, 40, 2, 2, 192, 193, 7, 40, 2, 2, 193, 56, 3, 2, 2, 2, 194, 195, 7, 62, 2, 2, 195, 196, 7, 63, 2, 2, 196, 58, 3, 2, 2, 2, 197, 198, 7, 62, 2, 2, 198, 60, 3, 2, 2, 2, 199, 200, 7, 64, 2, 2, 200, 62, 3, 2, 2, 2, 201, 202, 7, 64, 2, 2, 202, 203, 7, 63, 2, 2, 203, 64, 3, 2, 2, 2, 204, 205, 7, 63, 2, 2, 205, 206, 7, 63, 2, 2, 206, 66, 3, 2, 2, 2, 207, 208, 7, 35, 2, 2, 208, 209, 7, 63, 2, 2, 209, 68, 3, 2, 2, 2, 210, 211, 7, 45, 2, 2, 211, 70, 3, 2, 2, 2, 212, 213, 7, 47, 2, 2, 213, 72, 3, 2, 2, 2, 214, 215, 7, 44, 2, 2, 215, 74, 3, 2, 2, 2, 216, 217, 7, 49, 2, 2, 217, 76, 3, 2, 2, 2, 218, 219, 7, 39, 2, 2, 219, 78, 3, 2, 2, 2, 220, 221, 7, 65, 2, 2, 221, 80, 3, 2, 2, 2, 222, 223, 7, 118, 2, 2, 223, 224, 7, 116, 2, 2, 224, 225, 7, 119, 2, 2, 225, 226, 7, 103, 2, 2, 226, 82, 3, 2, 2, 2, 227, 228, 7, 104, 2, 2, 228, 229, 7, 99, 2, 2, 229, 230, 7, 110, 2, 2, 230, 231, 7, 117, 2, 2, 231, 232, 7, 103, 2, 2, 232, 84, 3, 2, 2, 2, 233, 238, 5, 99, 50, 2, 234, 237, 5, 99, 50, 2, 235, 237, 5, 101, 51, 2, 236, 234, 3, 2, 2, 2, 236, 235, 3, 2, 2, 2, 237, 240, 3, 2, 2, 2, 238, 236, 3, 2, 2, 2, 238, 239, 3, 2, 2, 2, 239, 86, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 241, 243, 5, 101, 51, 2, 242, 241, 3, 2, 2, 2, 243, 244, 3, 2, 2, 2, 244, 242, 3, 2, 2, 2, 244, 245, 3, 2, 2, 2, 245, 88, 3, 2, 2, 2, 246, 252, 7, 36, 2, 2, 247, 248, 7, 94, 2, 2, 248, 251, 7, 36, 2, 2, 249, 251, 10, 2, 2, 2, 250, 247, 3, 2, 2, 2, 250, 249, 3, 2, 2, 2, 251, 254, 3, 2, 2, 2, 252, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 255, 3, 2, 2, 2, 254, 252, 3, 2, 2, 2, 255, 256, 7, 36, 2, 2, 256, 90, 3, 2, 2, 2, 257, 261, 7, 36, 2, 2, 258, 259, 7, 94, 2, 2, 259, 262, 7, 36, 2, 2, 260, 262, 10, 2, 2, 2, 261, 258, 3, 2, 2, 2, 261, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 264, 7, 36, 2, 2, 264, 92, 3, 2, 2, 2, 265, 266, 7, 35, 2, 2, 266, 94, 3, 2, 2, 2, 267, 269, 9, 3, 2, 2, 268, 267, 3, 2, 2, 2, 269, 270, 3, 2, 2, 2, 270, 268, 3, 2, 2, 2, 270, 271, 3, 2, 2, 2, 271, 272, 3, 2, 2, 2, 272, 273, 8, 48, 2, 2, 273, 96, 3, 2, 2, 2, 274, 275, 7, 49, 2, 2, 275, 276, 7, 49, 2, 2, 276, 280, 3, 2, 2, 2, 277, 279, 10, 4, 2, 2, 278, 277, 3, 2, 2, 2, 279, 282, 3, 2, 2, 2, 280, 278, 3, 2, 2, 2, 280, 281, 3, 2, 2, 2, 281, 284, 3, 2, 2, 2, 282, 280, 3, 2, 2, 2, 283, 285, 7, 15, 2, 2, 284, 283, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 286, 3, 2, 2, 2, 286, 299, 7, 12, 2, 2, 287, 288, 7, 49, 2, 2, 288, 289, 7, 44, 2, 2, 289, 293, 3, 2, 2, 2, 290, 292, 11, 2, 2, 2, 291, 290, 3, 2, 2, 2, 292, 295, 3, 2, 2, 2, 293, 294, 3, 2, 2, 2, 293, 291, 3, 2, 2, 2, 294, 296, 3, 2, 2, 2, 295, 293, 3, 2, 2, 2, 296, 297, 7, 44, 2, 2, 297, 299, 7, 49, 2, 2, 298, 274, 3, 2, 2, 2, 298, 287, 3, 2, 2, 2, 299, 300, 3, 2, 2, 2, 300, 301, 8, 49, 2, 2, 301, 98, 3, 2, 2, 2, 302, 303, 9, 5, 2, 2, 303, 100, 3, 2, 2, 2, 304, 305, 4, 50, 59, 2, 305, 102, 3, 2, 2, 2, 14, 2, 236, 238, 244, 250, 252, 261, 270, 280, 284, 293, 298, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusLexer.java b/Homework/cs5300/p4-formatter/parser/CminusLexer.java new file mode 100644 index 0000000..8224ed8 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusLexer.java @@ -0,0 +1,232 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8", + "T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16", + "T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "T__23", "T__24", + "T__25", "T__26", "T__27", "T__28", "T__29", "T__30", "T__31", "T__32", + "T__33", "T__34", "T__35", "T__36", "T__37", "T__38", "T__39", "T__40", + "ID", "NUMCONST", "STRINGCONST", "CHARCONST", "BANG", "WS", "COMMENT", + "LETTER", "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public CminusLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\62\u0132\b\1\4\2"+ + "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+ + "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\3\2"+ + "\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3"+ + "\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3"+ + "\r\3\r\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23"+ + "\3\23\3\23\3\24\3\24\3\25\3\25\3\25\3\26\3\26\3\26\3\27\3\27\3\27\3\30"+ + "\3\30\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\33\3\34\3\34\3\34"+ + "\3\35\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3!\3\"\3\"\3\"\3#\3"+ + "#\3$\3$\3%\3%\3&\3&\3\'\3\'\3(\3(\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3+"+ + "\3+\3+\7+\u00ed\n+\f+\16+\u00f0\13+\3,\6,\u00f3\n,\r,\16,\u00f4\3-\3-"+ + "\3-\3-\7-\u00fb\n-\f-\16-\u00fe\13-\3-\3-\3.\3.\3.\3.\5.\u0106\n.\3.\3"+ + ".\3/\3/\3\60\6\60\u010d\n\60\r\60\16\60\u010e\3\60\3\60\3\61\3\61\3\61"+ + "\3\61\7\61\u0117\n\61\f\61\16\61\u011a\13\61\3\61\5\61\u011d\n\61\3\61"+ + "\3\61\3\61\3\61\3\61\7\61\u0124\n\61\f\61\16\61\u0127\13\61\3\61\3\61"+ + "\5\61\u012b\n\61\3\61\3\61\3\62\3\62\3\63\3\63\4\u00fc\u0125\2\64\3\3"+ + "\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21"+ + "!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!"+ + "A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\2e\2\3\2\6\3\2$$\5\2\13\f"+ + "\16\17\"\"\4\2\f\f\17\17\4\2C\\c|\2\u013a\2\3\3\2\2\2\2\5\3\2\2\2\2\7"+ + "\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2"+ + "\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2"+ + "\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2"+ + "\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2"+ + "\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2"+ + "\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M"+ + "\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y\3\2"+ + "\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\3g\3\2\2\2\5i\3\2\2\2"+ + "\7k\3\2\2\2\tm\3\2\2\2\13o\3\2\2\2\rt\3\2\2\2\17v\3\2\2\2\21x\3\2\2\2"+ + "\23|\3\2\2\2\25\u0081\3\2\2\2\27\u0086\3\2\2\2\31\u0089\3\2\2\2\33\u008b"+ + "\3\2\2\2\35\u008d\3\2\2\2\37\u0090\3\2\2\2!\u0095\3\2\2\2#\u009b\3\2\2"+ + "\2%\u00a2\3\2\2\2\'\u00a8\3\2\2\2)\u00aa\3\2\2\2+\u00ad\3\2\2\2-\u00b0"+ + "\3\2\2\2/\u00b3\3\2\2\2\61\u00b6\3\2\2\2\63\u00b9\3\2\2\2\65\u00bc\3\2"+ + "\2\2\67\u00bf\3\2\2\29\u00c2\3\2\2\2;\u00c5\3\2\2\2=\u00c7\3\2\2\2?\u00c9"+ + "\3\2\2\2A\u00cc\3\2\2\2C\u00cf\3\2\2\2E\u00d2\3\2\2\2G\u00d4\3\2\2\2I"+ + "\u00d6\3\2\2\2K\u00d8\3\2\2\2M\u00da\3\2\2\2O\u00dc\3\2\2\2Q\u00de\3\2"+ + "\2\2S\u00e3\3\2\2\2U\u00e9\3\2\2\2W\u00f2\3\2\2\2Y\u00f6\3\2\2\2[\u0101"+ + "\3\2\2\2]\u0109\3\2\2\2_\u010c\3\2\2\2a\u012a\3\2\2\2c\u012e\3\2\2\2e"+ + "\u0130\3\2\2\2gh\7.\2\2h\4\3\2\2\2ij\7=\2\2j\6\3\2\2\2kl\7]\2\2l\b\3\2"+ + "\2\2mn\7_\2\2n\n\3\2\2\2op\7x\2\2pq\7q\2\2qr\7k\2\2rs\7f\2\2s\f\3\2\2"+ + "\2tu\7*\2\2u\16\3\2\2\2vw\7+\2\2w\20\3\2\2\2xy\7k\2\2yz\7p\2\2z{\7v\2"+ + "\2{\22\3\2\2\2|}\7d\2\2}~\7q\2\2~\177\7q\2\2\177\u0080\7n\2\2\u0080\24"+ + "\3\2\2\2\u0081\u0082\7e\2\2\u0082\u0083\7j\2\2\u0083\u0084\7c\2\2\u0084"+ + "\u0085\7t\2\2\u0085\26\3\2\2\2\u0086\u0087\7]\2\2\u0087\u0088\7_\2\2\u0088"+ + "\30\3\2\2\2\u0089\u008a\7}\2\2\u008a\32\3\2\2\2\u008b\u008c\7\177\2\2"+ + "\u008c\34\3\2\2\2\u008d\u008e\7k\2\2\u008e\u008f\7h\2\2\u008f\36\3\2\2"+ + "\2\u0090\u0091\7g\2\2\u0091\u0092\7n\2\2\u0092\u0093\7u\2\2\u0093\u0094"+ + "\7g\2\2\u0094 \3\2\2\2\u0095\u0096\7y\2\2\u0096\u0097\7j\2\2\u0097\u0098"+ + "\7k\2\2\u0098\u0099\7n\2\2\u0099\u009a\7g\2\2\u009a\"\3\2\2\2\u009b\u009c"+ + "\7t\2\2\u009c\u009d\7g\2\2\u009d\u009e\7v\2\2\u009e\u009f\7w\2\2\u009f"+ + "\u00a0\7t\2\2\u00a0\u00a1\7p\2\2\u00a1$\3\2\2\2\u00a2\u00a3\7d\2\2\u00a3"+ + "\u00a4\7t\2\2\u00a4\u00a5\7g\2\2\u00a5\u00a6\7c\2\2\u00a6\u00a7\7m\2\2"+ + "\u00a7&\3\2\2\2\u00a8\u00a9\7?\2\2\u00a9(\3\2\2\2\u00aa\u00ab\7-\2\2\u00ab"+ + "\u00ac\7?\2\2\u00ac*\3\2\2\2\u00ad\u00ae\7/\2\2\u00ae\u00af\7?\2\2\u00af"+ + ",\3\2\2\2\u00b0\u00b1\7,\2\2\u00b1\u00b2\7?\2\2\u00b2.\3\2\2\2\u00b3\u00b4"+ + "\7\61\2\2\u00b4\u00b5\7?\2\2\u00b5\60\3\2\2\2\u00b6\u00b7\7-\2\2\u00b7"+ + "\u00b8\7-\2\2\u00b8\62\3\2\2\2\u00b9\u00ba\7/\2\2\u00ba\u00bb\7/\2\2\u00bb"+ + "\64\3\2\2\2\u00bc\u00bd\7~\2\2\u00bd\u00be\7~\2\2\u00be\66\3\2\2\2\u00bf"+ + "\u00c0\7(\2\2\u00c0\u00c1\7(\2\2\u00c18\3\2\2\2\u00c2\u00c3\7>\2\2\u00c3"+ + "\u00c4\7?\2\2\u00c4:\3\2\2\2\u00c5\u00c6\7>\2\2\u00c6<\3\2\2\2\u00c7\u00c8"+ + "\7@\2\2\u00c8>\3\2\2\2\u00c9\u00ca\7@\2\2\u00ca\u00cb\7?\2\2\u00cb@\3"+ + "\2\2\2\u00cc\u00cd\7?\2\2\u00cd\u00ce\7?\2\2\u00ceB\3\2\2\2\u00cf\u00d0"+ + "\7#\2\2\u00d0\u00d1\7?\2\2\u00d1D\3\2\2\2\u00d2\u00d3\7-\2\2\u00d3F\3"+ + "\2\2\2\u00d4\u00d5\7/\2\2\u00d5H\3\2\2\2\u00d6\u00d7\7,\2\2\u00d7J\3\2"+ + "\2\2\u00d8\u00d9\7\61\2\2\u00d9L\3\2\2\2\u00da\u00db\7\'\2\2\u00dbN\3"+ + "\2\2\2\u00dc\u00dd\7A\2\2\u00ddP\3\2\2\2\u00de\u00df\7v\2\2\u00df\u00e0"+ + "\7t\2\2\u00e0\u00e1\7w\2\2\u00e1\u00e2\7g\2\2\u00e2R\3\2\2\2\u00e3\u00e4"+ + "\7h\2\2\u00e4\u00e5\7c\2\2\u00e5\u00e6\7n\2\2\u00e6\u00e7\7u\2\2\u00e7"+ + "\u00e8\7g\2\2\u00e8T\3\2\2\2\u00e9\u00ee\5c\62\2\u00ea\u00ed\5c\62\2\u00eb"+ + "\u00ed\5e\63\2\u00ec\u00ea\3\2\2\2\u00ec\u00eb\3\2\2\2\u00ed\u00f0\3\2"+ + "\2\2\u00ee\u00ec\3\2\2\2\u00ee\u00ef\3\2\2\2\u00efV\3\2\2\2\u00f0\u00ee"+ + "\3\2\2\2\u00f1\u00f3\5e\63\2\u00f2\u00f1\3\2\2\2\u00f3\u00f4\3\2\2\2\u00f4"+ + "\u00f2\3\2\2\2\u00f4\u00f5\3\2\2\2\u00f5X\3\2\2\2\u00f6\u00fc\7$\2\2\u00f7"+ + "\u00f8\7^\2\2\u00f8\u00fb\7$\2\2\u00f9\u00fb\n\2\2\2\u00fa\u00f7\3\2\2"+ + "\2\u00fa\u00f9\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc\u00fd\3\2\2\2\u00fc\u00fa"+ + "\3\2\2\2\u00fd\u00ff\3\2\2\2\u00fe\u00fc\3\2\2\2\u00ff\u0100\7$\2\2\u0100"+ + "Z\3\2\2\2\u0101\u0105\7$\2\2\u0102\u0103\7^\2\2\u0103\u0106\7$\2\2\u0104"+ + "\u0106\n\2\2\2\u0105\u0102\3\2\2\2\u0105\u0104\3\2\2\2\u0106\u0107\3\2"+ + "\2\2\u0107\u0108\7$\2\2\u0108\\\3\2\2\2\u0109\u010a\7#\2\2\u010a^\3\2"+ + "\2\2\u010b\u010d\t\3\2\2\u010c\u010b\3\2\2\2\u010d\u010e\3\2\2\2\u010e"+ + "\u010c\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\3\2\2\2\u0110\u0111\b\60"+ + "\2\2\u0111`\3\2\2\2\u0112\u0113\7\61\2\2\u0113\u0114\7\61\2\2\u0114\u0118"+ + "\3\2\2\2\u0115\u0117\n\4\2\2\u0116\u0115\3\2\2\2\u0117\u011a\3\2\2\2\u0118"+ + "\u0116\3\2\2\2\u0118\u0119\3\2\2\2\u0119\u011c\3\2\2\2\u011a\u0118\3\2"+ + "\2\2\u011b\u011d\7\17\2\2\u011c\u011b\3\2\2\2\u011c\u011d\3\2\2\2\u011d"+ + "\u011e\3\2\2\2\u011e\u012b\7\f\2\2\u011f\u0120\7\61\2\2\u0120\u0121\7"+ + ",\2\2\u0121\u0125\3\2\2\2\u0122\u0124\13\2\2\2\u0123\u0122\3\2\2\2\u0124"+ + "\u0127\3\2\2\2\u0125\u0126\3\2\2\2\u0125\u0123\3\2\2\2\u0126\u0128\3\2"+ + "\2\2\u0127\u0125\3\2\2\2\u0128\u0129\7,\2\2\u0129\u012b\7\61\2\2\u012a"+ + "\u0112\3\2\2\2\u012a\u011f\3\2\2\2\u012b\u012c\3\2\2\2\u012c\u012d\b\61"+ + "\2\2\u012db\3\2\2\2\u012e\u012f\t\5\2\2\u012fd\3\2\2\2\u0130\u0131\4\62"+ + ";\2\u0131f\3\2\2\2\16\2\u00ec\u00ee\u00f4\u00fa\u00fc\u0105\u010e\u0118"+ + "\u011c\u0125\u012a\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusLexer.tokens b/Homework/cs5300/p4-formatter/parser/CminusLexer.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusLexer.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/Homework/cs5300/p4-formatter/parser/CminusListener.java b/Homework/cs5300/p4-formatter/parser/CminusListener.java new file mode 100644 index 0000000..bd141e3 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusListener.java @@ -0,0 +1,340 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link CminusParser}. + */ +public interface CminusListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void enterProgram(CminusParser.ProgramContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void exitProgram(CminusParser.ProgramContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void enterDeclaration(CminusParser.DeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void exitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void enterVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void exitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void enterVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void exitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void enterFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void exitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void enterParam(CminusParser.ParamContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void exitParam(CminusParser.ParamContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void enterParamId(CminusParser.ParamIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void exitParamId(CminusParser.ParamIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void enterStatement(CminusParser.StatementContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void exitStatement(CminusParser.StatementContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void enterCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void exitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void enterIfStmt(CminusParser.IfStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void exitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void enterWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void exitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void enterReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void exitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void enterBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void exitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(CminusParser.ExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(CminusParser.ExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void enterOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void exitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void enterAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void exitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void enterRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void exitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void enterRelop(CminusParser.RelopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void exitRelop(CminusParser.RelopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void enterSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void exitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void enterSumop(CminusParser.SumopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void exitSumop(CminusParser.SumopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void enterTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void exitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void enterMulop(CminusParser.MulopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void exitMulop(CminusParser.MulopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void enterUnaryop(CminusParser.UnaryopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void exitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void enterFactor(CminusParser.FactorContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void exitFactor(CminusParser.FactorContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void enterMutable(CminusParser.MutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void exitMutable(CminusParser.MutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void enterImmutable(CminusParser.ImmutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void exitImmutable(CminusParser.ImmutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void enterCall(CminusParser.CallContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void exitCall(CminusParser.CallContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void enterConstant(CminusParser.ConstantContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void exitConstant(CminusParser.ConstantContext ctx); +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusParser.java b/Homework/cs5300/p4-formatter/parser/CminusParser.java new file mode 100644 index 0000000..6749b21 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusParser.java @@ -0,0 +1,2454 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusParser extends Parser { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static final int + RULE_program = 0, RULE_declaration = 1, RULE_varDeclaration = 2, RULE_varDeclId = 3, + RULE_funDeclaration = 4, RULE_typeSpecifier = 5, RULE_param = 6, RULE_paramId = 7, + RULE_statement = 8, RULE_compoundStmt = 9, RULE_expressionStmt = 10, RULE_ifStmt = 11, + RULE_whileStmt = 12, RULE_returnStmt = 13, RULE_breakStmt = 14, RULE_expression = 15, + RULE_simpleExpression = 16, RULE_orExpression = 17, RULE_andExpression = 18, + RULE_unaryRelExpression = 19, RULE_relExpression = 20, RULE_relop = 21, + RULE_sumExpression = 22, RULE_sumop = 23, RULE_termExpression = 24, RULE_mulop = 25, + RULE_unaryExpression = 26, RULE_unaryop = 27, RULE_factor = 28, RULE_mutable = 29, + RULE_immutable = 30, RULE_call = 31, RULE_constant = 32; + private static String[] makeRuleNames() { + return new String[] { + "program", "declaration", "varDeclaration", "varDeclId", "funDeclaration", + "typeSpecifier", "param", "paramId", "statement", "compoundStmt", "expressionStmt", + "ifStmt", "whileStmt", "returnStmt", "breakStmt", "expression", "simpleExpression", + "orExpression", "andExpression", "unaryRelExpression", "relExpression", + "relop", "sumExpression", "sumop", "termExpression", "mulop", "unaryExpression", + "unaryop", "factor", "mutable", "immutable", "call", "constant" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public CminusParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + public static class ProgramContext extends ParserRuleContext { + public List<DeclarationContext> declaration() { + return getRuleContexts(DeclarationContext.class); + } + public DeclarationContext declaration(int i) { + return getRuleContext(DeclarationContext.class,i); + } + public ProgramContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_program; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterProgram(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitProgram(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitProgram(this); + else return visitor.visitChildren(this); + } + } + + public final ProgramContext program() throws RecognitionException { + ProgramContext _localctx = new ProgramContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_program); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(67); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(66); + declaration(); + } + } + setState(69); + _errHandler.sync(this); + _la = _input.LA(1); + } while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__4) | (1L << T__7) | (1L << T__8) | (1L << T__9))) != 0) ); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class DeclarationContext extends ParserRuleContext { + public VarDeclarationContext varDeclaration() { + return getRuleContext(VarDeclarationContext.class,0); + } + public FunDeclarationContext funDeclaration() { + return getRuleContext(FunDeclarationContext.class,0); + } + public DeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_declaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final DeclarationContext declaration() throws RecognitionException { + DeclarationContext _localctx = new DeclarationContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_declaration); + try { + setState(73); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(71); + varDeclaration(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(72); + funDeclaration(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclarationContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List<VarDeclIdContext> varDeclId() { + return getRuleContexts(VarDeclIdContext.class); + } + public VarDeclIdContext varDeclId(int i) { + return getRuleContext(VarDeclIdContext.class,i); + } + public VarDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitVarDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclarationContext varDeclaration() throws RecognitionException { + VarDeclarationContext _localctx = new VarDeclarationContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_varDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(75); + typeSpecifier(); + setState(76); + varDeclId(); + setState(81); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(77); + match(T__0); + setState(78); + varDeclId(); + } + } + setState(83); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(84); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public VarDeclIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclId(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitVarDeclId(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclIdContext varDeclId() throws RecognitionException { + VarDeclIdContext _localctx = new VarDeclIdContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_varDeclId); + try { + setState(91); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(86); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(87); + match(ID); + setState(88); + match(T__2); + setState(89); + match(NUMCONST); + setState(90); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FunDeclarationContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List<ParamContext> param() { + return getRuleContexts(ParamContext.class); + } + public ParamContext param(int i) { + return getRuleContext(ParamContext.class,i); + } + public FunDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_funDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFunDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFunDeclaration(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitFunDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final FunDeclarationContext funDeclaration() throws RecognitionException { + FunDeclarationContext _localctx = new FunDeclarationContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_funDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(95); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__4: + { + setState(93); + match(T__4); + } + break; + case T__7: + case T__8: + case T__9: + { + setState(94); + typeSpecifier(); + } + break; + default: + throw new NoViableAltException(this); + } + setState(97); + match(ID); + setState(98); + match(T__5); + setState(100); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + setState(99); + param(); + } + } + + setState(106); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(102); + match(T__0); + setState(103); + param(); + } + } + setState(108); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(109); + match(T__6); + setState(110); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeSpecifierContext extends ParserRuleContext { + public TypeSpecifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_typeSpecifier; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTypeSpecifier(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTypeSpecifier(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitTypeSpecifier(this); + else return visitor.visitChildren(this); + } + } + + public final TypeSpecifierContext typeSpecifier() throws RecognitionException { + TypeSpecifierContext _localctx = new TypeSpecifierContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_typeSpecifier); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(112); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public ParamIdContext paramId() { + return getRuleContext(ParamIdContext.class,0); + } + public ParamContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_param; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParam(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParam(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitParam(this); + else return visitor.visitChildren(this); + } + } + + public final ParamContext param() throws RecognitionException { + ParamContext _localctx = new ParamContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_param); + try { + enterOuterAlt(_localctx, 1); + { + setState(114); + typeSpecifier(); + setState(115); + paramId(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ParamIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_paramId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParamId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParamId(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitParamId(this); + else return visitor.visitChildren(this); + } + } + + public final ParamIdContext paramId() throws RecognitionException { + ParamIdContext _localctx = new ParamIdContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_paramId); + try { + setState(120); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,7,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(117); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(118); + match(ID); + setState(119); + match(T__10); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class StatementContext extends ParserRuleContext { + public ExpressionStmtContext expressionStmt() { + return getRuleContext(ExpressionStmtContext.class,0); + } + public CompoundStmtContext compoundStmt() { + return getRuleContext(CompoundStmtContext.class,0); + } + public IfStmtContext ifStmt() { + return getRuleContext(IfStmtContext.class,0); + } + public WhileStmtContext whileStmt() { + return getRuleContext(WhileStmtContext.class,0); + } + public ReturnStmtContext returnStmt() { + return getRuleContext(ReturnStmtContext.class,0); + } + public BreakStmtContext breakStmt() { + return getRuleContext(BreakStmtContext.class,0); + } + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_statement; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterStatement(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitStatement(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitStatement(this); + else return visitor.visitChildren(this); + } + } + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_statement); + try { + setState(128); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__1: + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(122); + expressionStmt(); + } + break; + case T__11: + enterOuterAlt(_localctx, 2); + { + setState(123); + compoundStmt(); + } + break; + case T__13: + enterOuterAlt(_localctx, 3); + { + setState(124); + ifStmt(); + } + break; + case T__15: + enterOuterAlt(_localctx, 4); + { + setState(125); + whileStmt(); + } + break; + case T__16: + enterOuterAlt(_localctx, 5); + { + setState(126); + returnStmt(); + } + break; + case T__17: + enterOuterAlt(_localctx, 6); + { + setState(127); + breakStmt(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CompoundStmtContext extends ParserRuleContext { + public List<VarDeclarationContext> varDeclaration() { + return getRuleContexts(VarDeclarationContext.class); + } + public VarDeclarationContext varDeclaration(int i) { + return getRuleContext(VarDeclarationContext.class,i); + } + public List<StatementContext> statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public CompoundStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_compoundStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCompoundStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCompoundStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitCompoundStmt(this); + else return visitor.visitChildren(this); + } + } + + public final CompoundStmtContext compoundStmt() throws RecognitionException { + CompoundStmtContext _localctx = new CompoundStmtContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_compoundStmt); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(130); + match(T__11); + setState(134); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + { + setState(131); + varDeclaration(); + } + } + setState(136); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(140); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__1) | (1L << T__5) | (1L << T__11) | (1L << T__13) | (1L << T__15) | (1L << T__16) | (1L << T__17) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + { + setState(137); + statement(); + } + } + setState(142); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(143); + match(T__12); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ExpressionStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expressionStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpressionStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpressionStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitExpressionStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionStmtContext expressionStmt() throws RecognitionException { + ExpressionStmtContext _localctx = new ExpressionStmtContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_expressionStmt); + try { + setState(149); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(145); + expression(); + setState(146); + match(T__1); + } + break; + case T__1: + enterOuterAlt(_localctx, 2); + { + setState(148); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class IfStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public List<StatementContext> statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public IfStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_ifStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterIfStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitIfStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitIfStmt(this); + else return visitor.visitChildren(this); + } + } + + public final IfStmtContext ifStmt() throws RecognitionException { + IfStmtContext _localctx = new IfStmtContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_ifStmt); + try { + setState(165); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,12,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(151); + match(T__13); + setState(152); + match(T__5); + setState(153); + simpleExpression(); + setState(154); + match(T__6); + setState(155); + statement(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(157); + match(T__13); + setState(158); + match(T__5); + setState(159); + simpleExpression(); + setState(160); + match(T__6); + setState(161); + statement(); + setState(162); + match(T__14); + setState(163); + statement(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class WhileStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public WhileStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_whileStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterWhileStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitWhileStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitWhileStmt(this); + else return visitor.visitChildren(this); + } + } + + public final WhileStmtContext whileStmt() throws RecognitionException { + WhileStmtContext _localctx = new WhileStmtContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_whileStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(167); + match(T__15); + setState(168); + match(T__5); + setState(169); + simpleExpression(); + setState(170); + match(T__6); + setState(171); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ReturnStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ReturnStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_returnStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterReturnStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitReturnStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitReturnStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ReturnStmtContext returnStmt() throws RecognitionException { + ReturnStmtContext _localctx = new ReturnStmtContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_returnStmt); + try { + setState(179); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,13,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(173); + match(T__16); + setState(174); + match(T__1); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(175); + match(T__16); + setState(176); + expression(); + setState(177); + match(T__1); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class BreakStmtContext extends ParserRuleContext { + public BreakStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_breakStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterBreakStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitBreakStmt(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitBreakStmt(this); + else return visitor.visitChildren(this); + } + } + + public final BreakStmtContext breakStmt() throws RecognitionException { + BreakStmtContext _localctx = new BreakStmtContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_breakStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(181); + match(T__17); + setState(182); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionContext extends ParserRuleContext { + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitExpression(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 30, RULE_expression); + try { + setState(211); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,14,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(184); + mutable(); + setState(185); + match(T__18); + setState(186); + expression(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(188); + mutable(); + setState(189); + match(T__19); + setState(190); + expression(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); + { + setState(192); + mutable(); + setState(193); + match(T__20); + setState(194); + expression(); + } + break; + case 4: + enterOuterAlt(_localctx, 4); + { + setState(196); + mutable(); + setState(197); + match(T__21); + setState(198); + expression(); + } + break; + case 5: + enterOuterAlt(_localctx, 5); + { + setState(200); + mutable(); + setState(201); + match(T__22); + setState(202); + expression(); + } + break; + case 6: + enterOuterAlt(_localctx, 6); + { + setState(204); + mutable(); + setState(205); + match(T__23); + } + break; + case 7: + enterOuterAlt(_localctx, 7); + { + setState(207); + mutable(); + setState(208); + match(T__24); + } + break; + case 8: + enterOuterAlt(_localctx, 8); + { + setState(210); + simpleExpression(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SimpleExpressionContext extends ParserRuleContext { + public OrExpressionContext orExpression() { + return getRuleContext(OrExpressionContext.class,0); + } + public SimpleExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_simpleExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSimpleExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSimpleExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSimpleExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SimpleExpressionContext simpleExpression() throws RecognitionException { + SimpleExpressionContext _localctx = new SimpleExpressionContext(_ctx, getState()); + enterRule(_localctx, 32, RULE_simpleExpression); + try { + enterOuterAlt(_localctx, 1); + { + setState(213); + orExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class OrExpressionContext extends ParserRuleContext { + public List<AndExpressionContext> andExpression() { + return getRuleContexts(AndExpressionContext.class); + } + public AndExpressionContext andExpression(int i) { + return getRuleContext(AndExpressionContext.class,i); + } + public OrExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_orExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterOrExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitOrExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitOrExpression(this); + else return visitor.visitChildren(this); + } + } + + public final OrExpressionContext orExpression() throws RecognitionException { + OrExpressionContext _localctx = new OrExpressionContext(_ctx, getState()); + enterRule(_localctx, 34, RULE_orExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(220); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(215); + andExpression(); + setState(216); + match(T__25); + } + } + } + setState(222); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + } + setState(223); + andExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class AndExpressionContext extends ParserRuleContext { + public List<UnaryRelExpressionContext> unaryRelExpression() { + return getRuleContexts(UnaryRelExpressionContext.class); + } + public UnaryRelExpressionContext unaryRelExpression(int i) { + return getRuleContext(UnaryRelExpressionContext.class,i); + } + public AndExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_andExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterAndExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitAndExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitAndExpression(this); + else return visitor.visitChildren(this); + } + } + + public final AndExpressionContext andExpression() throws RecognitionException { + AndExpressionContext _localctx = new AndExpressionContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_andExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(230); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(225); + unaryRelExpression(); + setState(226); + match(T__26); + } + } + } + setState(232); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + } + setState(233); + unaryRelExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryRelExpressionContext extends ParserRuleContext { + public RelExpressionContext relExpression() { + return getRuleContext(RelExpressionContext.class,0); + } + public List<TerminalNode> BANG() { return getTokens(CminusParser.BANG); } + public TerminalNode BANG(int i) { + return getToken(CminusParser.BANG, i); + } + public UnaryRelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryRelExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryRelExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryRelExpressionContext unaryRelExpression() throws RecognitionException { + UnaryRelExpressionContext _localctx = new UnaryRelExpressionContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_unaryRelExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(238); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==BANG) { + { + { + setState(235); + match(BANG); + } + } + setState(240); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(241); + relExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelExpressionContext extends ParserRuleContext { + public List<SumExpressionContext> sumExpression() { + return getRuleContexts(SumExpressionContext.class); + } + public SumExpressionContext sumExpression(int i) { + return getRuleContext(SumExpressionContext.class,i); + } + public List<RelopContext> relop() { + return getRuleContexts(RelopContext.class); + } + public RelopContext relop(int i) { + return getRuleContext(RelopContext.class,i); + } + public RelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final RelExpressionContext relExpression() throws RecognitionException { + RelExpressionContext _localctx = new RelExpressionContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_relExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(248); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(243); + sumExpression(); + setState(244); + relop(); + } + } + } + setState(250); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + } + setState(251); + sumExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelopContext extends ParserRuleContext { + public RelopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitRelop(this); + else return visitor.visitChildren(this); + } + } + + public final RelopContext relop() throws RecognitionException { + RelopContext _localctx = new RelopContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_relop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(253); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__27) | (1L << T__28) | (1L << T__29) | (1L << T__30) | (1L << T__31) | (1L << T__32))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumExpressionContext extends ParserRuleContext { + public List<TermExpressionContext> termExpression() { + return getRuleContexts(TermExpressionContext.class); + } + public TermExpressionContext termExpression(int i) { + return getRuleContext(TermExpressionContext.class,i); + } + public List<SumopContext> sumop() { + return getRuleContexts(SumopContext.class); + } + public SumopContext sumop(int i) { + return getRuleContext(SumopContext.class,i); + } + public SumExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSumExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SumExpressionContext sumExpression() throws RecognitionException { + SumExpressionContext _localctx = new SumExpressionContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_sumExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(260); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(255); + termExpression(); + setState(256); + sumop(); + } + } + } + setState(262); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + } + setState(263); + termExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumopContext extends ParserRuleContext { + public SumopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitSumop(this); + else return visitor.visitChildren(this); + } + } + + public final SumopContext sumop() throws RecognitionException { + SumopContext _localctx = new SumopContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_sumop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(265); + _la = _input.LA(1); + if ( !(_la==T__33 || _la==T__34) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TermExpressionContext extends ParserRuleContext { + public List<UnaryExpressionContext> unaryExpression() { + return getRuleContexts(UnaryExpressionContext.class); + } + public UnaryExpressionContext unaryExpression(int i) { + return getRuleContext(UnaryExpressionContext.class,i); + } + public List<MulopContext> mulop() { + return getRuleContexts(MulopContext.class); + } + public MulopContext mulop(int i) { + return getRuleContext(MulopContext.class,i); + } + public TermExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_termExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTermExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTermExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitTermExpression(this); + else return visitor.visitChildren(this); + } + } + + public final TermExpressionContext termExpression() throws RecognitionException { + TermExpressionContext _localctx = new TermExpressionContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_termExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(272); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(267); + unaryExpression(); + setState(268); + mulop(); + } + } + } + setState(274); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + } + setState(275); + unaryExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MulopContext extends ParserRuleContext { + public MulopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mulop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMulop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMulop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitMulop(this); + else return visitor.visitChildren(this); + } + } + + public final MulopContext mulop() throws RecognitionException { + MulopContext _localctx = new MulopContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_mulop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(277); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__35) | (1L << T__36) | (1L << T__37))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryExpressionContext extends ParserRuleContext { + public FactorContext factor() { + return getRuleContext(FactorContext.class,0); + } + public List<UnaryopContext> unaryop() { + return getRuleContexts(UnaryopContext.class); + } + public UnaryopContext unaryop(int i) { + return getRuleContext(UnaryopContext.class,i); + } + public UnaryExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryExpression(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryExpressionContext unaryExpression() throws RecognitionException { + UnaryExpressionContext _localctx = new UnaryExpressionContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_unaryExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(282); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) { + { + { + setState(279); + unaryop(); + } + } + setState(284); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(285); + factor(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryopContext extends ParserRuleContext { + public UnaryopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryop(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitUnaryop(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryopContext unaryop() throws RecognitionException { + UnaryopContext _localctx = new UnaryopContext(_ctx, getState()); + enterRule(_localctx, 54, RULE_unaryop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(287); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FactorContext extends ParserRuleContext { + public ImmutableContext immutable() { + return getRuleContext(ImmutableContext.class,0); + } + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public FactorContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_factor; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFactor(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFactor(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitFactor(this); + else return visitor.visitChildren(this); + } + } + + public final FactorContext factor() throws RecognitionException { + FactorContext _localctx = new FactorContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_factor); + try { + setState(291); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,22,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(289); + immutable(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(290); + mutable(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MutableContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public MutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMutable(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitMutable(this); + else return visitor.visitChildren(this); + } + } + + public final MutableContext mutable() throws RecognitionException { + MutableContext _localctx = new MutableContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_mutable); + try { + setState(299); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,23,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(293); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(294); + match(ID); + setState(295); + match(T__2); + setState(296); + expression(); + setState(297); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ImmutableContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public CallContext call() { + return getRuleContext(CallContext.class,0); + } + public ConstantContext constant() { + return getRuleContext(ConstantContext.class,0); + } + public ImmutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_immutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterImmutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitImmutable(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitImmutable(this); + else return visitor.visitChildren(this); + } + } + + public final ImmutableContext immutable() throws RecognitionException { + ImmutableContext _localctx = new ImmutableContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_immutable); + try { + setState(307); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + enterOuterAlt(_localctx, 1); + { + setState(301); + match(T__5); + setState(302); + expression(); + setState(303); + match(T__6); + } + break; + case ID: + enterOuterAlt(_localctx, 2); + { + setState(305); + call(); + } + break; + case T__39: + case T__40: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + enterOuterAlt(_localctx, 3); + { + setState(306); + constant(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CallContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public List<ExpressionContext> expression() { + return getRuleContexts(ExpressionContext.class); + } + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class,i); + } + public CallContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_call; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCall(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCall(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitCall(this); + else return visitor.visitChildren(this); + } + } + + public final CallContext call() throws RecognitionException { + CallContext _localctx = new CallContext(_ctx, getState()); + enterRule(_localctx, 62, RULE_call); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(309); + match(ID); + setState(310); + match(T__5); + setState(316); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(311); + expression(); + setState(312); + match(T__0); + } + } + } + setState(318); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + } + setState(320); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__5) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + setState(319); + expression(); + } + } + + setState(322); + match(T__6); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ConstantContext extends ParserRuleContext { + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public TerminalNode CHARCONST() { return getToken(CminusParser.CHARCONST, 0); } + public TerminalNode STRINGCONST() { return getToken(CminusParser.STRINGCONST, 0); } + public ConstantContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_constant; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterConstant(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitConstant(this); + } + @Override + public <T> T accept(ParseTreeVisitor<? extends T> visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor<? extends T>)visitor).visitConstant(this); + else return visitor.visitChildren(this); + } + } + + public final ConstantContext constant() throws RecognitionException { + ConstantContext _localctx = new ConstantContext(_ctx, getState()); + enterRule(_localctx, 64, RULE_constant); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(324); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__39) | (1L << T__40) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\62\u0149\4\2\t\2"+ + "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ + "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!"+ + "\t!\4\"\t\"\3\2\6\2F\n\2\r\2\16\2G\3\3\3\3\5\3L\n\3\3\4\3\4\3\4\3\4\7"+ + "\4R\n\4\f\4\16\4U\13\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\5\5^\n\5\3\6\3\6\5"+ + "\6b\n\6\3\6\3\6\3\6\5\6g\n\6\3\6\3\6\7\6k\n\6\f\6\16\6n\13\6\3\6\3\6\3"+ + "\6\3\7\3\7\3\b\3\b\3\b\3\t\3\t\3\t\5\t{\n\t\3\n\3\n\3\n\3\n\3\n\3\n\5"+ + "\n\u0083\n\n\3\13\3\13\7\13\u0087\n\13\f\13\16\13\u008a\13\13\3\13\7\13"+ + "\u008d\n\13\f\13\16\13\u0090\13\13\3\13\3\13\3\f\3\f\3\f\3\f\5\f\u0098"+ + "\n\f\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\5\r\u00a8"+ + "\n\r\3\16\3\16\3\16\3\16\3\16\3\16\3\17\3\17\3\17\3\17\3\17\3\17\5\17"+ + "\u00b6\n\17\3\20\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\5\21\u00d6\n\21\3\22\3\22\3\23\3\23\3\23\7\23\u00dd"+ + "\n\23\f\23\16\23\u00e0\13\23\3\23\3\23\3\24\3\24\3\24\7\24\u00e7\n\24"+ + "\f\24\16\24\u00ea\13\24\3\24\3\24\3\25\7\25\u00ef\n\25\f\25\16\25\u00f2"+ + "\13\25\3\25\3\25\3\26\3\26\3\26\7\26\u00f9\n\26\f\26\16\26\u00fc\13\26"+ + "\3\26\3\26\3\27\3\27\3\30\3\30\3\30\7\30\u0105\n\30\f\30\16\30\u0108\13"+ + "\30\3\30\3\30\3\31\3\31\3\32\3\32\3\32\7\32\u0111\n\32\f\32\16\32\u0114"+ + "\13\32\3\32\3\32\3\33\3\33\3\34\7\34\u011b\n\34\f\34\16\34\u011e\13\34"+ + "\3\34\3\34\3\35\3\35\3\36\3\36\5\36\u0126\n\36\3\37\3\37\3\37\3\37\3\37"+ + "\3\37\5\37\u012e\n\37\3 \3 \3 \3 \3 \3 \5 \u0136\n \3!\3!\3!\3!\3!\7!"+ + "\u013d\n!\f!\16!\u0140\13!\3!\5!\u0143\n!\3!\3!\3\"\3\"\3\"\2\2#\2\4\6"+ + "\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62\64\668:<>@B\2\b\3\2\n"+ + "\f\3\2\36#\3\2$%\3\2&(\4\2%&))\4\2*+-/\2\u014d\2E\3\2\2\2\4K\3\2\2\2\6"+ + "M\3\2\2\2\b]\3\2\2\2\na\3\2\2\2\fr\3\2\2\2\16t\3\2\2\2\20z\3\2\2\2\22"+ + "\u0082\3\2\2\2\24\u0084\3\2\2\2\26\u0097\3\2\2\2\30\u00a7\3\2\2\2\32\u00a9"+ + "\3\2\2\2\34\u00b5\3\2\2\2\36\u00b7\3\2\2\2 \u00d5\3\2\2\2\"\u00d7\3\2"+ + "\2\2$\u00de\3\2\2\2&\u00e8\3\2\2\2(\u00f0\3\2\2\2*\u00fa\3\2\2\2,\u00ff"+ + "\3\2\2\2.\u0106\3\2\2\2\60\u010b\3\2\2\2\62\u0112\3\2\2\2\64\u0117\3\2"+ + "\2\2\66\u011c\3\2\2\28\u0121\3\2\2\2:\u0125\3\2\2\2<\u012d\3\2\2\2>\u0135"+ + "\3\2\2\2@\u0137\3\2\2\2B\u0146\3\2\2\2DF\5\4\3\2ED\3\2\2\2FG\3\2\2\2G"+ + "E\3\2\2\2GH\3\2\2\2H\3\3\2\2\2IL\5\6\4\2JL\5\n\6\2KI\3\2\2\2KJ\3\2\2\2"+ + "L\5\3\2\2\2MN\5\f\7\2NS\5\b\5\2OP\7\3\2\2PR\5\b\5\2QO\3\2\2\2RU\3\2\2"+ + "\2SQ\3\2\2\2ST\3\2\2\2TV\3\2\2\2US\3\2\2\2VW\7\4\2\2W\7\3\2\2\2X^\7,\2"+ + "\2YZ\7,\2\2Z[\7\5\2\2[\\\7-\2\2\\^\7\6\2\2]X\3\2\2\2]Y\3\2\2\2^\t\3\2"+ + "\2\2_b\7\7\2\2`b\5\f\7\2a_\3\2\2\2a`\3\2\2\2bc\3\2\2\2cd\7,\2\2df\7\b"+ + "\2\2eg\5\16\b\2fe\3\2\2\2fg\3\2\2\2gl\3\2\2\2hi\7\3\2\2ik\5\16\b\2jh\3"+ + "\2\2\2kn\3\2\2\2lj\3\2\2\2lm\3\2\2\2mo\3\2\2\2nl\3\2\2\2op\7\t\2\2pq\5"+ + "\22\n\2q\13\3\2\2\2rs\t\2\2\2s\r\3\2\2\2tu\5\f\7\2uv\5\20\t\2v\17\3\2"+ + "\2\2w{\7,\2\2xy\7,\2\2y{\7\r\2\2zw\3\2\2\2zx\3\2\2\2{\21\3\2\2\2|\u0083"+ + "\5\26\f\2}\u0083\5\24\13\2~\u0083\5\30\r\2\177\u0083\5\32\16\2\u0080\u0083"+ + "\5\34\17\2\u0081\u0083\5\36\20\2\u0082|\3\2\2\2\u0082}\3\2\2\2\u0082~"+ + "\3\2\2\2\u0082\177\3\2\2\2\u0082\u0080\3\2\2\2\u0082\u0081\3\2\2\2\u0083"+ + "\23\3\2\2\2\u0084\u0088\7\16\2\2\u0085\u0087\5\6\4\2\u0086\u0085\3\2\2"+ + "\2\u0087\u008a\3\2\2\2\u0088\u0086\3\2\2\2\u0088\u0089\3\2\2\2\u0089\u008e"+ + "\3\2\2\2\u008a\u0088\3\2\2\2\u008b\u008d\5\22\n\2\u008c\u008b\3\2\2\2"+ + "\u008d\u0090\3\2\2\2\u008e\u008c\3\2\2\2\u008e\u008f\3\2\2\2\u008f\u0091"+ + "\3\2\2\2\u0090\u008e\3\2\2\2\u0091\u0092\7\17\2\2\u0092\25\3\2\2\2\u0093"+ + "\u0094\5 \21\2\u0094\u0095\7\4\2\2\u0095\u0098\3\2\2\2\u0096\u0098\7\4"+ + "\2\2\u0097\u0093\3\2\2\2\u0097\u0096\3\2\2\2\u0098\27\3\2\2\2\u0099\u009a"+ + "\7\20\2\2\u009a\u009b\7\b\2\2\u009b\u009c\5\"\22\2\u009c\u009d\7\t\2\2"+ + "\u009d\u009e\5\22\n\2\u009e\u00a8\3\2\2\2\u009f\u00a0\7\20\2\2\u00a0\u00a1"+ + "\7\b\2\2\u00a1\u00a2\5\"\22\2\u00a2\u00a3\7\t\2\2\u00a3\u00a4\5\22\n\2"+ + "\u00a4\u00a5\7\21\2\2\u00a5\u00a6\5\22\n\2\u00a6\u00a8\3\2\2\2\u00a7\u0099"+ + "\3\2\2\2\u00a7\u009f\3\2\2\2\u00a8\31\3\2\2\2\u00a9\u00aa\7\22\2\2\u00aa"+ + "\u00ab\7\b\2\2\u00ab\u00ac\5\"\22\2\u00ac\u00ad\7\t\2\2\u00ad\u00ae\5"+ + "\22\n\2\u00ae\33\3\2\2\2\u00af\u00b0\7\23\2\2\u00b0\u00b6\7\4\2\2\u00b1"+ + "\u00b2\7\23\2\2\u00b2\u00b3\5 \21\2\u00b3\u00b4\7\4\2\2\u00b4\u00b6\3"+ + "\2\2\2\u00b5\u00af\3\2\2\2\u00b5\u00b1\3\2\2\2\u00b6\35\3\2\2\2\u00b7"+ + "\u00b8\7\24\2\2\u00b8\u00b9\7\4\2\2\u00b9\37\3\2\2\2\u00ba\u00bb\5<\37"+ + "\2\u00bb\u00bc\7\25\2\2\u00bc\u00bd\5 \21\2\u00bd\u00d6\3\2\2\2\u00be"+ + "\u00bf\5<\37\2\u00bf\u00c0\7\26\2\2\u00c0\u00c1\5 \21\2\u00c1\u00d6\3"+ + "\2\2\2\u00c2\u00c3\5<\37\2\u00c3\u00c4\7\27\2\2\u00c4\u00c5\5 \21\2\u00c5"+ + "\u00d6\3\2\2\2\u00c6\u00c7\5<\37\2\u00c7\u00c8\7\30\2\2\u00c8\u00c9\5"+ + " \21\2\u00c9\u00d6\3\2\2\2\u00ca\u00cb\5<\37\2\u00cb\u00cc\7\31\2\2\u00cc"+ + "\u00cd\5 \21\2\u00cd\u00d6\3\2\2\2\u00ce\u00cf\5<\37\2\u00cf\u00d0\7\32"+ + "\2\2\u00d0\u00d6\3\2\2\2\u00d1\u00d2\5<\37\2\u00d2\u00d3\7\33\2\2\u00d3"+ + "\u00d6\3\2\2\2\u00d4\u00d6\5\"\22\2\u00d5\u00ba\3\2\2\2\u00d5\u00be\3"+ + "\2\2\2\u00d5\u00c2\3\2\2\2\u00d5\u00c6\3\2\2\2\u00d5\u00ca\3\2\2\2\u00d5"+ + "\u00ce\3\2\2\2\u00d5\u00d1\3\2\2\2\u00d5\u00d4\3\2\2\2\u00d6!\3\2\2\2"+ + "\u00d7\u00d8\5$\23\2\u00d8#\3\2\2\2\u00d9\u00da\5&\24\2\u00da\u00db\7"+ + "\34\2\2\u00db\u00dd\3\2\2\2\u00dc\u00d9\3\2\2\2\u00dd\u00e0\3\2\2\2\u00de"+ + "\u00dc\3\2\2\2\u00de\u00df\3\2\2\2\u00df\u00e1\3\2\2\2\u00e0\u00de\3\2"+ + "\2\2\u00e1\u00e2\5&\24\2\u00e2%\3\2\2\2\u00e3\u00e4\5(\25\2\u00e4\u00e5"+ + "\7\35\2\2\u00e5\u00e7\3\2\2\2\u00e6\u00e3\3\2\2\2\u00e7\u00ea\3\2\2\2"+ + "\u00e8\u00e6\3\2\2\2\u00e8\u00e9\3\2\2\2\u00e9\u00eb\3\2\2\2\u00ea\u00e8"+ + "\3\2\2\2\u00eb\u00ec\5(\25\2\u00ec\'\3\2\2\2\u00ed\u00ef\7\60\2\2\u00ee"+ + "\u00ed\3\2\2\2\u00ef\u00f2\3\2\2\2\u00f0\u00ee\3\2\2\2\u00f0\u00f1\3\2"+ + "\2\2\u00f1\u00f3\3\2\2\2\u00f2\u00f0\3\2\2\2\u00f3\u00f4\5*\26\2\u00f4"+ + ")\3\2\2\2\u00f5\u00f6\5.\30\2\u00f6\u00f7\5,\27\2\u00f7\u00f9\3\2\2\2"+ + "\u00f8\u00f5\3\2\2\2\u00f9\u00fc\3\2\2\2\u00fa\u00f8\3\2\2\2\u00fa\u00fb"+ + "\3\2\2\2\u00fb\u00fd\3\2\2\2\u00fc\u00fa\3\2\2\2\u00fd\u00fe\5.\30\2\u00fe"+ + "+\3\2\2\2\u00ff\u0100\t\3\2\2\u0100-\3\2\2\2\u0101\u0102\5\62\32\2\u0102"+ + "\u0103\5\60\31\2\u0103\u0105\3\2\2\2\u0104\u0101\3\2\2\2\u0105\u0108\3"+ + "\2\2\2\u0106\u0104\3\2\2\2\u0106\u0107\3\2\2\2\u0107\u0109\3\2\2\2\u0108"+ + "\u0106\3\2\2\2\u0109\u010a\5\62\32\2\u010a/\3\2\2\2\u010b\u010c\t\4\2"+ + "\2\u010c\61\3\2\2\2\u010d\u010e\5\66\34\2\u010e\u010f\5\64\33\2\u010f"+ + "\u0111\3\2\2\2\u0110\u010d\3\2\2\2\u0111\u0114\3\2\2\2\u0112\u0110\3\2"+ + "\2\2\u0112\u0113\3\2\2\2\u0113\u0115\3\2\2\2\u0114\u0112\3\2\2\2\u0115"+ + "\u0116\5\66\34\2\u0116\63\3\2\2\2\u0117\u0118\t\5\2\2\u0118\65\3\2\2\2"+ + "\u0119\u011b\58\35\2\u011a\u0119\3\2\2\2\u011b\u011e\3\2\2\2\u011c\u011a"+ + "\3\2\2\2\u011c\u011d\3\2\2\2\u011d\u011f\3\2\2\2\u011e\u011c\3\2\2\2\u011f"+ + "\u0120\5:\36\2\u0120\67\3\2\2\2\u0121\u0122\t\6\2\2\u01229\3\2\2\2\u0123"+ + "\u0126\5> \2\u0124\u0126\5<\37\2\u0125\u0123\3\2\2\2\u0125\u0124\3\2\2"+ + "\2\u0126;\3\2\2\2\u0127\u012e\7,\2\2\u0128\u0129\7,\2\2\u0129\u012a\7"+ + "\5\2\2\u012a\u012b\5 \21\2\u012b\u012c\7\6\2\2\u012c\u012e\3\2\2\2\u012d"+ + "\u0127\3\2\2\2\u012d\u0128\3\2\2\2\u012e=\3\2\2\2\u012f\u0130\7\b\2\2"+ + "\u0130\u0131\5 \21\2\u0131\u0132\7\t\2\2\u0132\u0136\3\2\2\2\u0133\u0136"+ + "\5@!\2\u0134\u0136\5B\"\2\u0135\u012f\3\2\2\2\u0135\u0133\3\2\2\2\u0135"+ + "\u0134\3\2\2\2\u0136?\3\2\2\2\u0137\u0138\7,\2\2\u0138\u013e\7\b\2\2\u0139"+ + "\u013a\5 \21\2\u013a\u013b\7\3\2\2\u013b\u013d\3\2\2\2\u013c\u0139\3\2"+ + "\2\2\u013d\u0140\3\2\2\2\u013e\u013c\3\2\2\2\u013e\u013f\3\2\2\2\u013f"+ + "\u0142\3\2\2\2\u0140\u013e\3\2\2\2\u0141\u0143\5 \21\2\u0142\u0141\3\2"+ + "\2\2\u0142\u0143\3\2\2\2\u0143\u0144\3\2\2\2\u0144\u0145\7\t\2\2\u0145"+ + "A\3\2\2\2\u0146\u0147\t\7\2\2\u0147C\3\2\2\2\35GKS]aflz\u0082\u0088\u008e"+ + "\u0097\u00a7\u00b5\u00d5\u00de\u00e8\u00f0\u00fa\u0106\u0112\u011c\u0125"+ + "\u012d\u0135\u013e\u0142"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/parser/CminusVisitor.java b/Homework/cs5300/p4-formatter/parser/CminusVisitor.java new file mode 100644 index 0000000..9088695 --- /dev/null +++ b/Homework/cs5300/p4-formatter/parser/CminusVisitor.java @@ -0,0 +1,211 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeVisitor; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link CminusParser}. + * + * @param <T> The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public interface CminusVisitor<T> extends ParseTreeVisitor<T> { + /** + * Visit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitProgram(CminusParser.ProgramContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParam(CminusParser.ParamContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParamId(CminusParser.ParamIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatement(CminusParser.StatementContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpression(CminusParser.ExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelop(CminusParser.RelopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumop(CminusParser.SumopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMulop(CminusParser.MulopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFactor(CminusParser.FactorContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMutable(CminusParser.MutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitImmutable(CminusParser.ImmutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCall(CminusParser.CallContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstant(CminusParser.ConstantContext ctx); +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/submit.zip b/Homework/cs5300/p4-formatter/submit.zip Binary files differnew file mode 100644 index 0000000..bc671f4 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit.zip diff --git a/Homework/cs5300/p4-formatter/submit/ASTVisitor.java b/Homework/cs5300/p4-formatter/submit/ASTVisitor.java new file mode 100644 index 0000000..7f6d020 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ASTVisitor.java @@ -0,0 +1,239 @@ +package submit; + +import parser.CminusBaseVisitor; +import parser.CminusParser; +import submit.ast.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +public class ASTVisitor extends CminusBaseVisitor<Node> { + private final Logger LOGGER; + private SymbolTable symbolTable; + + public ASTVisitor(Logger LOGGER) { + this.LOGGER = LOGGER; + } + + private VarType getVarType(CminusParser.TypeSpecifierContext ctx) { + final String t = ctx.getText(); + return (t.equals("int")) ? VarType.INT : (t.equals("bool")) ? VarType.BOOL : VarType.CHAR; + } + + @Override + public Node visitProgram(CminusParser.ProgramContext ctx) { + symbolTable = new SymbolTable(); + List<Declaration> decls = new ArrayList<>(); + for (CminusParser.DeclarationContext d : ctx.declaration()) + decls.add((Declaration) visitDeclaration(d)); + return new Program(decls); + } + + @Override + public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + List<String> ids = new ArrayList<>(); + List<Integer> arraySizes = new ArrayList<>(); + for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) { + String id = v.ID().getText(); + ids.add(id); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + if (v.NUMCONST() != null) + arraySizes.add(Integer.parseInt(v.NUMCONST().getText())); + else + arraySizes.add(-1); + } + final boolean isStatic = false; + return new VarDeclaration(type, ids, arraySizes, isStatic); + } + + @Override + public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) { + if (ctx.expression() != null) + return new Return((Expression) visitExpression(ctx.expression())); + return new Return(null); + } + + @Override + public Node visitConstant(CminusParser.ConstantContext ctx) { + final Node node; + if (ctx.NUMCONST() != null) + node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText())); + else if (ctx.CHARCONST() != null) + node = new CharConstant(ctx.CHARCONST().getText().charAt(0)); + else if (ctx.STRINGCONST() != null) + node = new StringConstant(ctx.STRINGCONST().getText()); + else + node = new BoolConstant(ctx.getText().equals("true")); + return node; + } + + @Override + public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { + VarType rType = VarType.VOID; + if (ctx.typeSpecifier() != null) + rType = VarType.fromString(ctx.typeSpecifier().getText()); + String id = ctx.ID().getText(); + + symbolTable.addSymbol(id, new SymbolInfo(id, rType, true)); + symbolTable = symbolTable.createChild(); + + List<Param> params = ctx.param().stream().map(pCtx -> (Param) visitParam(pCtx)).collect(Collectors.toList()); + Statement statement = (Statement) visitStatement(ctx.statement()); + + symbolTable = symbolTable.getParent(); + return new FunctionDeclaration(rType, id, params, statement); + } + + @Override + public Node visitParam(CminusParser.ParamContext ctx) { + VarType pType = getVarType(ctx.typeSpecifier()); + + String pId = ctx.paramId().getText(); + Boolean isArray = pId.endsWith("[]"); + if (isArray) + pId = pId.substring(0, pId.length() - 2); + + symbolTable.addSymbol(pId, new SymbolInfo(pId, pType, false)); + + return new Param(pType, pId, isArray); + } + + @Override + public Node visitStatement(CminusParser.StatementContext ctx) { + if (ctx.expressionStmt() != null) + return new ExpressionStatement((Expression) visitExpression(ctx.expressionStmt().expression())); + if (ctx.compoundStmt() != null) { + CminusParser.CompoundStmtContext cCtx = ctx.compoundStmt(); + + symbolTable = symbolTable.createChild(); + + List<VarDeclaration> varDecls = cCtx.varDeclaration().stream() + .map(declCtx -> (VarDeclaration) visitVarDeclaration(declCtx)).collect(Collectors.toList()); + + List<Statement> statements = cCtx.statement().stream().map(stmtCtx -> (Statement) visitStatement(stmtCtx)) + .collect(Collectors.toList()); + + symbolTable = symbolTable.getParent(); + + return new CompoundStatement(varDecls, statements); + } + if (ctx.ifStmt() != null) { + List<Statement> statements = ctx.ifStmt().statement().stream() + .map(stmtCtx -> (Statement) visitStatement(stmtCtx)).collect(Collectors.toList()); + return new IfStatement((Expression) visitSimpleExpression(ctx.ifStmt().simpleExpression()), statements); + } + if (ctx.whileStmt() != null) + return new WhileStatement((Expression) visitSimpleExpression(ctx.whileStmt().simpleExpression()), + (Statement) visitStatement(ctx.whileStmt().statement())); + if (ctx.returnStmt() != null) { + if (ctx.returnStmt().expression() != null) + return new Return((Expression) visitExpression(ctx.returnStmt().expression())); + return new Return(null); + } + if (ctx.breakStmt() != null) + return new Break(); + return null; + } + + @Override + public Node visitExpression(CminusParser.ExpressionContext ctx) { + if (ctx.mutable() != null && ctx.expression() != null) + return new ExpressionExpression((Mutable) visitMutable(ctx.mutable()), + (Expression) visitExpression(ctx.expression()), ctx.getChild(1).getText()); + else if (ctx.mutable() != null) + return new ExpressionExpression((Mutable) visitMutable(ctx.mutable()), ctx.getChild(1).getText()); + return visitSimpleExpression(ctx.simpleExpression()); + } + + @Override + public Node visitMutable(CminusParser.MutableContext ctx) { + String id = ctx.ID().getText(); + + if (symbolTable.find(id) == null) + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + + return new Mutable(id, + ctx.expression() != null ? (Expression) visitExpression(ctx.expression()) : null); + } + + @Override + public Node visitOrExpression(CminusParser.OrExpressionContext ctx) { + List<AndExpression> andExpressions = ctx.andExpression().stream().map(andExpression -> { + List<UnaryRelExpression> uRE = andExpression.unaryRelExpression().stream() + .map(unaryRelExpressionCtx -> (UnaryRelExpression) visitUnaryRelExpression(unaryRelExpressionCtx)) + .collect(Collectors.toList()); + return new AndExpression(uRE); + }).collect(Collectors.toList()); + + return new OrExpression(andExpressions); + } + + @Override + public Node visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { + int bangs = ctx.BANG().size(); + RelExpression relExpression = (RelExpression) visitRelExpression(ctx.relExpression()); + return new UnaryRelExpression(bangs, relExpression); + } + + @Override + public Node visitRelExpression(CminusParser.RelExpressionContext ctx) { + List<BinaryOperatorType> relOps = ctx.relop().stream() + .map(operatorContext -> BinaryOperatorType.fromString(operatorContext.getText())) + .collect(Collectors.toList()); + List<SumExpression> sumExpressions = ctx.sumExpression().stream().map(sumExpressionCtx -> { + List<BinaryOperatorType> sumOps = sumExpressionCtx.sumop().stream() + .map(operatorContext -> BinaryOperatorType.fromString(operatorContext.getText())) + .collect(Collectors.toList()); + List<TermExpression> termExprs = sumExpressionCtx.termExpression().stream() + .map(termExprCtx -> (TermExpression) visitTermExpression(termExprCtx)).collect(Collectors.toList()); + return new SumExpression(sumOps, termExprs); + }).collect(Collectors.toList()); + + return new RelExpression(sumExpressions, relOps); + } + + public Node visitTermExpression(CminusParser.TermExpressionContext ctx) { + List<BinaryOperatorType> mulOps = ctx.mulop().stream() + .map(operatorContext -> BinaryOperatorType.fromString(operatorContext.getText())) + .collect(Collectors.toList()); + List<UnaryExpression> unaryExpressions = ctx.unaryExpression().stream().map(unaryExpressionCtx -> { + List<String> unaryOps = unaryExpressionCtx.unaryop().stream() + .map(operatorContext -> operatorContext.getText()) + .collect(Collectors.toList()); + Factor factor = (Factor) visitFactor(unaryExpressionCtx.factor()); + + return new UnaryExpression(unaryOps, factor); + }).collect(Collectors.toList()); + + return new TermExpression(unaryExpressions, mulOps); + } + + public Node visitFactor(CminusParser.FactorContext ctx) { + if (ctx.immutable() != null) + return new Factor((Immutable) visitImmutable(ctx.immutable())); + return new Factor((Mutable) visitMutable(ctx.mutable())); + } + + public Node visitImmutable(CminusParser.ImmutableContext ctx) { + if (ctx.constant() != null) + return new Immutable((Constant) visitConstant(ctx.constant())); + + if (ctx.call() != null) + return new Immutable((Call) visitCall(ctx.call()), true); + + return new Immutable((Expression) visitExpression(ctx.expression())); + } + + public Node visitCall(CminusParser.CallContext ctx) { + String id = ctx.ID().getText(); + + if (symbolTable.find(id) == null) + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + + return new Call(id, ctx.expression().stream() + .map(exprCtx -> (Expression) visitExpression(exprCtx)).collect(Collectors.toList())); + } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/submit/SymbolInfo.java b/Homework/cs5300/p4-formatter/submit/SymbolInfo.java new file mode 100644 index 0000000..19c3f9b --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/SymbolInfo.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit; + +import submit.ast.VarType; + +/** + * + * @author edwajohn + */ +public class SymbolInfo { + + private final String id; + // In the case of a function, type is the return type + private final VarType type; + private final boolean function; + + public SymbolInfo(String id, VarType type, boolean function) { + this.id = id; + this.type = type; + this.function = function; + } + + @Override + public String toString() { + return "<" + id + ", " + type + '>'; + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/SymbolTable.java b/Homework/cs5300/p4-formatter/submit/SymbolTable.java new file mode 100644 index 0000000..bfa531e --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/SymbolTable.java @@ -0,0 +1,55 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/* + * Code formatter project + * CS 4481 + */ +public class SymbolTable { + + private final HashMap<String, SymbolInfo> table; + private SymbolTable parent; + private final List<SymbolTable> children; + + public SymbolTable() { + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + } + + public void addSymbol(String id, SymbolInfo symbol) { table.put(id, symbol); } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { return parent; } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/AndExpression.java b/Homework/cs5300/p4-formatter/submit/ast/AndExpression.java new file mode 100644 index 0000000..515dad9 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/AndExpression.java @@ -0,0 +1,18 @@ +package submit.ast; + +import java.util.List; +import java.util.ArrayList; + +public class AndExpression implements Expression { + private List<UnaryRelExpression> unaryRelExpressions; + + public AndExpression(List<UnaryRelExpression> unaryRelExpressions) { + this.unaryRelExpressions = new ArrayList<>(unaryRelExpressions); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + Expression.joinExpressions(BinaryOperatorType.AND, new ArrayList<Expression>(unaryRelExpressions), builder); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/BinaryOperator.java b/Homework/cs5300/p4-formatter/submit/ast/BinaryOperator.java new file mode 100644 index 0000000..747b3ba --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/BinaryOperator.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BinaryOperator implements Expression { + + private final Expression lhs, rhs; + private final BinaryOperatorType type; + + public BinaryOperator(Expression lhs, BinaryOperatorType type, Expression rhs) { + this.lhs = lhs; + this.type = type; + this.rhs = rhs; + } + + public BinaryOperator(Expression lhs, String type, Expression rhs) { + this.lhs = lhs; + this.type = BinaryOperatorType.fromString(type); + this.rhs = rhs; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + lhs.toCminus(builder, prefix); + builder.append(" ").append(type).append(" "); + rhs.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/BinaryOperatorType.java b/Homework/cs5300/p4-formatter/submit/ast/BinaryOperatorType.java new file mode 100644 index 0000000..37235c8 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/BinaryOperatorType.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum BinaryOperatorType { + + OR("||"), AND("&&"), + LE("<="), LT("<"), GT(">"), GE(">="), EQ("=="), NE("!="), + PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"), MOD("%"); + + private final String value; + + private BinaryOperatorType(String value) { + this.value = value; + } + + public static BinaryOperatorType fromString(String s) { + for (BinaryOperatorType at : BinaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in OperatorType.fromString(): " + s); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/BoolConstant.java b/Homework/cs5300/p4-formatter/submit/ast/BoolConstant.java new file mode 100644 index 0000000..01a7591 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/BoolConstant.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BoolConstant implements Expression, Constant { + + private final boolean value; + + public BoolConstant(boolean value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (value) { + builder.append("true"); + } else { + builder.append("false"); + } + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Break.java b/Homework/cs5300/p4-formatter/submit/ast/Break.java new file mode 100644 index 0000000..7e86a71 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Break.java @@ -0,0 +1,9 @@ +package submit.ast; + +public class Break implements Statement { + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + + builder.append("break;\n"); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Call.java b/Homework/cs5300/p4-formatter/submit/ast/Call.java new file mode 100644 index 0000000..b5c8a37 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Call.java @@ -0,0 +1,26 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class Call implements Expression { + ArrayList<Expression> expressions; + String id; + + public Call(String id, List<Expression> expressions) { + this.id = id; + this.expressions = new ArrayList<>(expressions); + } + + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + builder.append(id).append("("); + int numExpr = expressions.size(); + if (numExpr > 0) + expressions.get(0).toCminus(builder, ""); + if (numExpr > 1) + for (Expression expr : expressions.subList(1, numExpr)) + expr.toCminus(builder, ", "); + builder.append(")"); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/CharConstant.java b/Homework/cs5300/p4-formatter/submit/ast/CharConstant.java new file mode 100644 index 0000000..a0ccb37 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/CharConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class CharConstant implements Expression, Constant { + + private final char value; + + public CharConstant(char value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("'").append(value).append("'"); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/CompoundStatement.java b/Homework/cs5300/p4-formatter/submit/ast/CompoundStatement.java new file mode 100644 index 0000000..02fa298 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/CompoundStatement.java @@ -0,0 +1,28 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class CompoundStatement implements Statement { + ArrayList<VarDeclaration> varDecls; + ArrayList<Statement> statements; + + public CompoundStatement(List<VarDeclaration> varDecls, List<Statement> statements) { + this.varDecls = new ArrayList<>(varDecls); + this.statements = new ArrayList<>(statements); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + builder.append("{").append("\n"); + for (VarDeclaration varDecl : varDecls) + varDecl.toCminus(builder, prefix + "\t"); + + for (Statement statement : statements) + statement.toCminus(builder, prefix + "\t"); + + builder.append(prefix).append("}").append("\n"); + + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Constant.java b/Homework/cs5300/p4-formatter/submit/ast/Constant.java new file mode 100644 index 0000000..bb30412 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Constant.java @@ -0,0 +1,5 @@ +package submit.ast; + +public interface Constant extends Node { + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Declaration.java b/Homework/cs5300/p4-formatter/submit/ast/Declaration.java new file mode 100644 index 0000000..c70e856 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Declaration.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Declaration extends Statement, Node { + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Expression.java b/Homework/cs5300/p4-formatter/submit/ast/Expression.java new file mode 100644 index 0000000..1c2ba9b --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Expression.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * + * @author edwajohn, simponic :3 + */ +public interface Expression extends Node { + public static void joinExpressionOperatorLists(List<BinaryOperatorType> binaryOperatorTypes, List<Expression> exprs, + StringBuilder builder) { + int numExpr = exprs.size(); + Expression lhs = exprs.get(0); + if (numExpr == 1) + lhs.toCminus(builder, ""); + if (numExpr > 1) { + BinaryOperator binOp = null; + for (int i = 0; i < binaryOperatorTypes.size(); i++) { + binOp = new BinaryOperator(lhs, binaryOperatorTypes.get(i), exprs.get(i + 1)); + lhs = binOp; + } + binOp.toCminus(builder, ""); + } + } + + public static void joinExpressions(BinaryOperatorType binaryOperatorType, List<Expression> exprs, + StringBuilder builder) { + Expression.joinExpressionOperatorLists(Arrays.asList(new BinaryOperatorType[exprs.size() - 1]).stream() + .map(e -> binaryOperatorType).collect(Collectors.toList()), exprs, builder); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/ExpressionExpression.java b/Homework/cs5300/p4-formatter/submit/ast/ExpressionExpression.java new file mode 100644 index 0000000..cc79a1d --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/ExpressionExpression.java @@ -0,0 +1,31 @@ +package submit.ast; + +public class ExpressionExpression implements Expression { + private Mutable mutable; + private Expression expression; + private String op; + + public ExpressionExpression(Mutable mutable, Expression expression, String op) { + this.mutable = mutable; + this.expression = expression; + this.op = op; + } + + public ExpressionExpression(Mutable mutable, String op) { + this.mutable = mutable; + this.op = op; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + mutable.toCminus(builder, ""); + if (op != null && expression != null) + builder.append(" "); + builder.append(op); + if (expression != null) { + builder.append(" "); + expression.toCminus(builder, ""); + } + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/ExpressionStatement.java b/Homework/cs5300/p4-formatter/submit/ast/ExpressionStatement.java new file mode 100644 index 0000000..18feadf --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/ExpressionStatement.java @@ -0,0 +1,17 @@ +package submit.ast; + +public class ExpressionStatement implements Statement { + private Expression expression; + + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expression != null) + expression.toCminus(builder, ""); + builder.append(";\n"); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Factor.java b/Homework/cs5300/p4-formatter/submit/ast/Factor.java new file mode 100644 index 0000000..bda0f53 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Factor.java @@ -0,0 +1,23 @@ +package submit.ast; + +public class Factor implements Node { + private Immutable immutable; + private Mutable mutable; + + public Factor(Immutable immutable) { + this.immutable = immutable; + } + + public Factor(Mutable mutable) { + this.mutable = mutable; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (mutable != null) + mutable.toCminus(builder, ""); + if (immutable != null) + immutable.toCminus(builder, ""); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/FunctionDeclaration.java b/Homework/cs5300/p4-formatter/submit/ast/FunctionDeclaration.java new file mode 100644 index 0000000..56abd14 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/FunctionDeclaration.java @@ -0,0 +1,36 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class FunctionDeclaration implements Declaration { + + private VarType rType; + private Statement statement; + private ArrayList<Param> params; + private String id; + + public FunctionDeclaration(VarType rType, String id, List<Param> params, + Statement statement) { + this.rType = rType; + this.id = id; + this.params = new ArrayList<>(params); + this.statement = statement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append("\n").append(rType).append(" ").append(id).append("("); + int numParams = params.size(); + + if (numParams >= 1) + params.get(0).toCminus(builder, ""); + if (numParams > 1) + for (Param param : params.subList(1, numParams)) + param.toCminus(builder, ", "); + + builder.append(")").append("\n"); + + statement.toCminus(builder, ""); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/IfStatement.java b/Homework/cs5300/p4-formatter/submit/ast/IfStatement.java new file mode 100644 index 0000000..1f9b577 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/IfStatement.java @@ -0,0 +1,33 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class IfStatement implements Statement { + private ArrayList<Statement> statements; + private Expression simpleExpression; + + public IfStatement(Expression simpleExpression, List<Statement> statements) { + this.simpleExpression = simpleExpression; + this.statements = new ArrayList<>(statements); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + builder.append("if").append(" ("); + simpleExpression.toCminus(builder, ""); + builder.append(")\n"); + + Statement ifStmt = statements.get(0); + boolean isIfCompound = ifStmt.getClass() == CompoundStatement.class; + ifStmt.toCminus(builder, prefix + (isIfCompound ? "" : "\t")); + if (statements.size() == 2) { + Statement elseStmt = statements.get(1); + boolean isElseCompound = elseStmt.getClass() == CompoundStatement.class; + + builder.append(prefix).append("else\n"); + elseStmt.toCminus(builder, prefix + (isElseCompound ? "" : "\t")); + } + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Immutable.java b/Homework/cs5300/p4-formatter/submit/ast/Immutable.java new file mode 100644 index 0000000..d94ec2c --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Immutable.java @@ -0,0 +1,34 @@ +package submit.ast; + +public class Immutable implements Expression, Node { + private Expression expression; + private Call call; + private Constant constant; + + public Immutable(Expression expression) { + this.expression = expression; + } + + public Immutable(Call call, boolean isCall) { + this.call = call; + } + + public Immutable(Constant constant) { + this.constant = constant; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (constant != null) + constant.toCminus(builder, ""); + if (expression != null) { + builder.append("("); + expression.toCminus(builder, ""); + builder.append(")"); + } + if (call != null) + call.toCminus(builder, ""); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Mutable.java b/Homework/cs5300/p4-formatter/submit/ast/Mutable.java new file mode 100644 index 0000000..c7ef089 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Mutable.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Mutable implements Expression, Node { + + private final String id; + private final Expression index; + + public Mutable(String id, Expression index) { + this.id = id; + this.index = index; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id); + if (index != null) { + builder.append("["); + index.toCminus(builder, prefix); + builder.append("]"); + } + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Node.java b/Homework/cs5300/p4-formatter/submit/ast/Node.java new file mode 100644 index 0000000..455bb3f --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Node.java @@ -0,0 +1,5 @@ +package submit.ast; + +public interface Node { + void toCminus(StringBuilder builder, final String prefix); +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/NumConstant.java b/Homework/cs5300/p4-formatter/submit/ast/NumConstant.java new file mode 100644 index 0000000..c3eea3c --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/NumConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class NumConstant implements Expression, Node, Constant { + + private final int value; + + public NumConstant(int value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(Integer.toString(value)); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/OrExpression.java b/Homework/cs5300/p4-formatter/submit/ast/OrExpression.java new file mode 100644 index 0000000..f4b4dd7 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/OrExpression.java @@ -0,0 +1,18 @@ +package submit.ast; + +import java.util.List; +import java.util.ArrayList; + +public class OrExpression implements Expression { + private ArrayList<AndExpression> andExpressions; + + public OrExpression(List<AndExpression> andExpressions) { + this.andExpressions = new ArrayList<>(andExpressions); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + Expression.joinExpressions(BinaryOperatorType.OR, new ArrayList<Expression>(andExpressions), builder); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Param.java b/Homework/cs5300/p4-formatter/submit/ast/Param.java new file mode 100644 index 0000000..2afa3d6 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Param.java @@ -0,0 +1,22 @@ +package submit.ast; + +public class Param implements Declaration { + private final VarType type; + private final String id; + private final Boolean isArray; + + public Param(VarType type, String id, Boolean isArray) { + this.type = type; + this.id = id; + this.isArray = isArray; + } + + @Override + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + builder.append(type).append(" ").append(id); + if (this.isArray) { + builder.append("[]"); + } + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Program.java b/Homework/cs5300/p4-formatter/submit/ast/Program.java new file mode 100644 index 0000000..96d1e22 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Program.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Program implements Node { + + private ArrayList<Declaration> declarations; + + public Program(List<Declaration> declarations) { + this.declarations = new ArrayList<>(declarations); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + toCminus(builder, ""); + return builder.toString(); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + for (Declaration declaration : declarations) { + declaration.toCminus(builder, ""); + } + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/RelExpression.java b/Homework/cs5300/p4-formatter/submit/ast/RelExpression.java new file mode 100644 index 0000000..8201dd8 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/RelExpression.java @@ -0,0 +1,20 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class RelExpression implements Expression { + private ArrayList<SumExpression> sumExpressions; + private ArrayList<BinaryOperatorType> relops; + + public RelExpression(List<SumExpression> sumExpressions, List<BinaryOperatorType> relops) { + this.sumExpressions = new ArrayList<>(sumExpressions); + this.relops = new ArrayList<>(relops); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + Expression.joinExpressionOperatorLists(relops, new ArrayList<Expression>(sumExpressions), builder); + } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/submit/ast/Return.java b/Homework/cs5300/p4-formatter/submit/ast/Return.java new file mode 100644 index 0000000..c1f9d4d --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Return.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Return implements Statement { + + private final Expression expr; + + public Return(Expression expr) { + this.expr = expr; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expr == null) { + builder.append("return;\n"); + } else { + builder.append("return "); + expr.toCminus(builder, prefix); + builder.append(";\n"); + } + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/Statement.java b/Homework/cs5300/p4-formatter/submit/ast/Statement.java new file mode 100644 index 0000000..c95db57 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/Statement.java @@ -0,0 +1,14 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; + +/** + * + * @author edwajohn + */ +public interface Statement extends Node { +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/StringConstant.java b/Homework/cs5300/p4-formatter/submit/ast/StringConstant.java new file mode 100644 index 0000000..4bf3738 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/StringConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class StringConstant implements Expression, Constant { + + private final String value; + + public StringConstant(String value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("\"").append(value).append("\""); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/SumExpression.java b/Homework/cs5300/p4-formatter/submit/ast/SumExpression.java new file mode 100644 index 0000000..a7bebfb --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/SumExpression.java @@ -0,0 +1,20 @@ +package submit.ast; + +import java.util.List; +import java.util.ArrayList; + +public class SumExpression implements Expression { + private ArrayList<BinaryOperatorType> sumOps; + private ArrayList<TermExpression> termExpressions; + + public SumExpression(List<BinaryOperatorType> sumOps, List<TermExpression> termExpressions) { + this.sumOps = new ArrayList<>(sumOps); + this.termExpressions = new ArrayList<>(termExpressions); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + Expression.joinExpressionOperatorLists(sumOps, new ArrayList<Expression>(termExpressions), builder); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/TermExpression.java b/Homework/cs5300/p4-formatter/submit/ast/TermExpression.java new file mode 100644 index 0000000..47dc0b2 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/TermExpression.java @@ -0,0 +1,20 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class TermExpression implements Expression { + private ArrayList<UnaryExpression> unaryExpressions; + private ArrayList<BinaryOperatorType> mulOps; + + public TermExpression(List<UnaryExpression> unaryExpressions, List<BinaryOperatorType> mulOps) { + this.unaryExpressions = new ArrayList<>(unaryExpressions); + this.mulOps = new ArrayList<>(mulOps); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + Expression.joinExpressionOperatorLists(mulOps, new ArrayList<Expression>(unaryExpressions), builder); + } +}
\ No newline at end of file diff --git a/Homework/cs5300/p4-formatter/submit/ast/UnaryExpression.java b/Homework/cs5300/p4-formatter/submit/ast/UnaryExpression.java new file mode 100644 index 0000000..0efdb13 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/UnaryExpression.java @@ -0,0 +1,22 @@ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +public class UnaryExpression implements Expression { + private ArrayList<String> unaryOperators; + private Factor factor; + + public UnaryExpression(List<String> unaryOperators, Factor factor) { + this.unaryOperators = new ArrayList<>(unaryOperators); + this.factor = factor; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + for (String unaryOperator : unaryOperators) + builder.append(unaryOperator); + factor.toCminus(builder, ""); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/UnaryRelExpression.java b/Homework/cs5300/p4-formatter/submit/ast/UnaryRelExpression.java new file mode 100644 index 0000000..621f9fb --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/UnaryRelExpression.java @@ -0,0 +1,20 @@ +package submit.ast; + +public class UnaryRelExpression implements Expression { + + private int bangs; + private RelExpression relExpression; + + public UnaryRelExpression(int bangs, RelExpression relExpression) { + this.bangs = bangs; + this.relExpression = relExpression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + for (int i = 0; i < bangs; ++i) + builder.append("!"); + relExpression.toCminus(builder, ""); + } +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/VarDeclaration.java b/Homework/cs5300/p4-formatter/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..90f8ea5 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/VarDeclaration.java @@ -0,0 +1,47 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class VarDeclaration implements Declaration { + + private final VarType type; + private final List<String> ids; + private final List<Integer> arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List<String> ids, List<Integer> arraySizes, boolean isStatic) { + this.type = type; + this.ids = new ArrayList<>(ids); + this.arraySizes = arraySizes; + this.isStatic = isStatic; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + if (isStatic) { + builder.append("static "); + } + builder.append(type).append(" "); + for (int i = 0; i < ids.size(); ++i) { + final String id = ids.get(i); + final int arraySize = arraySizes.get(i); + if (arraySize >= 0) { + builder.append(id).append("[").append(arraySize).append("]").append(", "); + } else { + builder.append(id).append(", "); + } + } + builder.delete(builder.length() - 2, builder.length()); + builder.append(";\n"); + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/VarType.java b/Homework/cs5300/p4-formatter/submit/ast/VarType.java new file mode 100644 index 0000000..85296d9 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/VarType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum VarType { + + VOID("void"), INT("int"), BOOL("bool"), CHAR("char"); + + private final String value; + + private VarType(String value) { + this.value = value; + } + + public static VarType fromString(String s) { + for (VarType vt : VarType.values()) { + if (vt.value.equals(s)) { + return vt; + } + } + throw new RuntimeException("Illegal string in VarType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/p4-formatter/submit/ast/WhileStatement.java b/Homework/cs5300/p4-formatter/submit/ast/WhileStatement.java new file mode 100644 index 0000000..cd99d51 --- /dev/null +++ b/Homework/cs5300/p4-formatter/submit/ast/WhileStatement.java @@ -0,0 +1,22 @@ +package submit.ast; + +public class WhileStatement implements Statement { + Expression simpleExpression; + Statement statement; + + public WhileStatement(Expression simpleExpression, Statement statement) { + this.simpleExpression = simpleExpression; + this.statement = statement; + } + + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + + builder.append("while ("); + simpleExpression.toCminus(builder, ""); + builder.append(")\n"); + + boolean isBodyCompound = statement.getClass() == CompoundStatement.class; + statement.toCminus(builder, prefix + (isBodyCompound ? "" : "\t")); + } +} diff --git a/Homework/cs5300/project-one/p1-scanner(1).zip b/Homework/cs5300/project-one/p1-scanner(1).zip Binary files differnew file mode 100644 index 0000000..6f6bb69 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner(1).zip diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/Main.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Main.class Binary files differnew file mode 100644 index 0000000..9318350 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Main.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/ScanStream.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/ScanStream.class Binary files differnew file mode 100644 index 0000000..cad01c8 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/ScanStream.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/Scanner.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Scanner.class Binary files differnew file mode 100644 index 0000000..b401fcb --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Scanner.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$CharCat.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$CharCat.class Binary files differnew file mode 100644 index 0000000..10cfa45 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$CharCat.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$TokenType.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$TokenType.class Binary files differnew file mode 100644 index 0000000..9c19786 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$TokenType.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$Transition.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$Transition.class Binary files differnew file mode 100644 index 0000000..9e75001 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$Transition.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$WhichTable.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$WhichTable.class Binary files differnew file mode 100644 index 0000000..9f26273 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader$WhichTable.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader.class Binary files differnew file mode 100644 index 0000000..bfff22d --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/TableReader.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/Tests.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Tests.class Binary files differnew file mode 100644 index 0000000..00a8224 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Tests.class diff --git a/Homework/cs5300/project-one/p1-scanner/classes/scanner/Token.class b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Token.class Binary files differnew file mode 100644 index 0000000..f99a2c0 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/classes/scanner/Token.class diff --git a/Homework/cs5300/project-one/p1-scanner/data/err1-hex.txt b/Homework/cs5300/project-one/p1-scanner/data/err1-hex.txt new file mode 100644 index 0000000..2c136b2 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err1-hex.txt @@ -0,0 +1 @@ +302
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/err1-reg.txt b/Homework/cs5300/project-one/p1-scanner/data/err1-reg.txt new file mode 100644 index 0000000..2c136b2 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err1-reg.txt @@ -0,0 +1 @@ +302
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/err2-hex.txt b/Homework/cs5300/project-one/p1-scanner/data/err2-hex.txt new file mode 100644 index 0000000..b963787 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err2-hex.txt @@ -0,0 +1 @@ +0y diff --git a/Homework/cs5300/project-one/p1-scanner/data/err2-reg.txt b/Homework/cs5300/project-one/p1-scanner/data/err2-reg.txt new file mode 100644 index 0000000..eefe985 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err2-reg.txt @@ -0,0 +1 @@ +ra
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/err3-hex.txt b/Homework/cs5300/project-one/p1-scanner/data/err3-hex.txt new file mode 100644 index 0000000..64a955e --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err3-hex.txt @@ -0,0 +1 @@ +0x3Ax diff --git a/Homework/cs5300/project-one/p1-scanner/data/err3-reg.txt b/Homework/cs5300/project-one/p1-scanner/data/err3-reg.txt new file mode 100644 index 0000000..5a27b08 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/err3-reg.txt @@ -0,0 +1 @@ +r33r
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/hexadecimal.table b/Homework/cs5300/project-one/p1-scanner/data/hexadecimal.table new file mode 100644 index 0000000..c4764fd --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/hexadecimal.table @@ -0,0 +1,42 @@ +// Special tokens are \space, \t and \n. +ClassifierTable +x x +0 zero +1 digit +2 digit +3 digit +4 digit +5 digit +6 digit +7 digit +8 digit +9 digit +A digit +B digit +C digit +D digit +E digit +F digit +\space whitespace +\t whitespace +\n whitespace + +// In the transition table, the first state +// is considered the input state. +TransitionTable +s0 zero s1 +s1 x s2 +s2 zero s3 +s2 digit s3 +s3 zero s3 +s3 digit s3 +s0 whitespace s4 +s4 whitespace s4 + +// If a state has an entry in this table then +// it is considered an accept state. +// "ignore" is a reserved type which indicates +// that the token should be ignored. +TokenTypeTable +s3 hexnumber +s4 ignore
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/register.table b/Homework/cs5300/project-one/p1-scanner/data/register.table new file mode 100644 index 0000000..f22b15e --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/register.table @@ -0,0 +1,33 @@ +// Special tokens are \space, \t and \n. +ClassifierTable +r register +0 digit +1 digit +2 digit +3 digit +4 digit +5 digit +6 digit +7 digit +8 digit +9 digit +\space whitespace +\t whitespace +\n whitespace + +// In the transition table, the first state +// is considered the input state. +TransitionTable +s0 register s1 +s1 digit s2 +s2 digit s2 +s0 whitespace s3 +s3 whitespace s3 + +// If a state has an entry in this table then +// it is considered an accept state. +// "ignore" is a reserved type which indicates +// that the token should be ignored. +TokenTypeTable +s2 register +s3 ignore
\ No newline at end of file diff --git a/Homework/cs5300/project-one/p1-scanner/data/test1-hex.txt b/Homework/cs5300/project-one/p1-scanner/data/test1-hex.txt new file mode 100644 index 0000000..565b558 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/test1-hex.txt @@ -0,0 +1,3 @@ +0x3F +0x1234 +0x1234567890ABCDEF diff --git a/Homework/cs5300/project-one/p1-scanner/data/test1-reg.txt b/Homework/cs5300/project-one/p1-scanner/data/test1-reg.txt new file mode 100644 index 0000000..aaa53cf --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/data/test1-reg.txt @@ -0,0 +1,3 @@ +r302 +r02 +r233987 diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/Main.java b/Homework/cs5300/project-one/p1-scanner/scanner/Main.java new file mode 100644 index 0000000..a1d5f9c --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/Main.java @@ -0,0 +1,235 @@ +/* + * CS 4481 Compilers + * Project 1 - scanner part 1 + * + * This file contains the method runTests(). You will uncomment tests + * as you develop your scanner code + */ +package scanner; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * A different Main file will be used for grading, so you may make modifications + * as needed, but before submitting you should download a fresh copy of this + * file, uncomment the tests and make sure you pass them all. + */ +public class Main { + + public static void main(String[] args) + throws FileNotFoundException, IOException { + if (args.length > 0) { + scanInputFile(args[0], args[1]); + } else { + runRegisterTests(); + runHexTests(); + + System.out.println(tests.getSuccesses() + "/" + tests.getN() + + " tests succeeded"); + int failures = tests.getFailures(); + + System.exit(failures); + } + } + + private static void runRegisterTests() + throws FileNotFoundException, IOException { + // TODO: uncomment tests as you develop code + + //------------------------------------------------------------ + // Test register.table reading, table building and token + // parsing. These tests should pass once you have the Scanner + // constructor implemented. + //------------------------------------------------------------ + + String tableFile = "data/register.table"; + TableReader tableReader = new TableReader(tableFile); + Scanner scanner = new Scanner(tableReader); + + tests.test(scanner.getCategory('r').equals("register")); + tests.test(scanner.getCategory('3').equals("digit")); + tests.test(scanner.getCategory('8').equals("digit")); + tests.test(scanner.getCategory(' ').equals("whitespace")); + tests.test(scanner.getCategory('x').equals("not in alphabet")); + + tests.test(scanner.getNewState("s0", "register").equals("s1")); + tests.test(scanner.getNewState("s1", "digit").equals("s2")); + tests.test(scanner.getNewState("s2", "digit").equals("s2")); + tests.test(scanner.getNewState("s0", "whitespace").equals("s3")); + tests.test(scanner.getNewState("s0", "digit").equals("error")); + tests.test(scanner.getNewState("s1", "register").equals("error")); + tests.test(scanner.getNewState("s2", "register").equals("error")); + + tests.test(scanner.getTokenType("s0").equals("error")); + tests.test(scanner.getTokenType("s1").equals("error")); + tests.test(scanner.getTokenType("s2").equals("register")); + tests.test(scanner.getTokenType("s3").equals("ignore")); + + //------------------------------------------------------------ + // The following tests should pass once you have + // Scanner.nextToken() implemented. + //------------------------------------------------------------ + { + ScanStream ss = getDataStream("data/test1-reg.txt"); + testToken(scanner, ss, "register", "r302"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "register", "r02"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "register", "r233987"); + } + { + ScanStream ss = getDataStream("data/err1-reg.txt"); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err2-reg.txt"); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err3-reg.txt"); + testToken(scanner, ss, "register", "r33"); + testToken(scanner, ss, null); + } + } + + private static void runHexTests() throws FileNotFoundException, IOException { + // TODO: uncomment tests as you develop code + + //------------------------------------------------------------ + // Test register.table reading, table building and token + // parsing. These tests should pass once you have the Scanner + // constructor implemented. + //------------------------------------------------------------ + + String tableFile = "data/hexadecimal.table"; + TableReader tableReader = new TableReader(tableFile); + Scanner scanner = new Scanner(tableReader); + + tests.test(scanner.getCategory('x').equals("x")); + tests.test(scanner.getCategory('0').equals("zero")); + tests.test(scanner.getCategory('8').equals("digit")); + tests.test(scanner.getCategory('B').equals("digit")); + tests.test(scanner.getCategory(' ').equals("whitespace")); + tests.test(scanner.getCategory('z').equals("not in alphabet")); + + tests.test(scanner.getNewState("s0", "zero").equals("s1")); + tests.test(scanner.getNewState("s1", "x").equals("s2")); + tests.test(scanner.getNewState("s2", "zero").equals("s3")); + tests.test(scanner.getNewState("s2", "digit").equals("s3")); + tests.test(scanner.getNewState("s3", "zero").equals("s3")); + tests.test(scanner.getNewState("s3", "digit").equals("s3")); + tests.test(scanner.getNewState("s0", "whitespace").equals("s4")); + tests.test(scanner.getNewState("s4", "whitespace").equals("s4")); + tests.test(scanner.getNewState("s0", "digit").equals("error")); + tests.test(scanner.getNewState("s1", "zero").equals("error")); + tests.test(scanner.getNewState("s2", "x").equals("error")); + + tests.test(scanner.getTokenType("s0").equals("error")); + tests.test(scanner.getTokenType("s1").equals("error")); + tests.test(scanner.getTokenType("s2").equals("error")); + tests.test(scanner.getTokenType("s3").equals("hexnumber")); + tests.test(scanner.getTokenType("s4").equals("ignore")); + + //------------------------------------------------------------ + // The following tests should pass once you have + // Scanner.nextToken() implemented. + //------------------------------------------------------------ + { + ScanStream ss = getDataStream("data/test1-hex.txt"); + testToken(scanner, ss, "hexnumber", "0x3F"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "hexnumber", "0x1234"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "hexnumber", "0x1234567890ABCDEF"); + } + { + ScanStream ss = getDataStream("data/err1-hex.txt"); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err2-hex.txt"); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err3-hex.txt"); + testToken(scanner, ss, "hexnumber", "0x3A"); + testToken(scanner, ss, null); + } + } + + //-------------------------------------------------------------------------------- + // Utility stuff + // You can ignore code from here to the end of the file. + //-------------------------------------------------------------------------------- + + static Tests tests = new Tests(); + + private static void testToken(Scanner scanner, ScanStream ss, String type, + String lexeme) { + try { + Token t = scanner.nextToken(ss); + if (type == null) { + tests.test(t == null, "expected lexical error"); + } else { + tests.test(t.getType().equals(type), + t.getType() + " not equal to " + type); + } + + if (lexeme != null) { + tests.test(t.getLexeme().equals(lexeme), + t.getLexeme() + " not equal to " + lexeme); + } + } catch (Exception e) { + System.out.println(e.toString()); + tests.addFailure(); + } + } + + private static void testToken(Scanner scanner, ScanStream ss, String type) { + testToken(scanner, ss, type, null); + } + + private static ScanStream getDataStream(String testFile) throws IOException { + File file = new File(testFile); + FileInputStream fis = new FileInputStream(file); + byte[] data = new byte[(int)file.length()]; + fis.read(data); + fis.close(); + ScanStream ss = new ScanStream(data); + return ss; + } + + public static void scanInputFile(String tableFile, String testFile) + throws FileNotFoundException, IOException { + TableReader tableReader = new TableReader(tableFile); + + File file = new File(testFile); + FileInputStream fis = new FileInputStream(file); + byte[] data = new byte[(int)file.length()]; + fis.read(data); + fis.close(); + + PrintWriter writer = new PrintWriter(testFile + ".out", "UTF-8"); + + Scanner scanner = new Scanner(tableReader); + ScanStream ss = new ScanStream(data); + Token t; + do { + t = scanner.nextToken(ss); + if (t == null) { + System.out.println("Lexical error: " + ss.next()); + ss.rollback(); + writer.println("Lexical error: " + ss.next()); + } else if (!t.getType().equals("ignore")) { + System.out.println(t.toString()); + writer.println(t.toString()); + } + } while (t != null && !ss.eof()); + + writer.close(); + } +} diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/ScanStream.java b/Homework/cs5300/project-one/p1-scanner/scanner/ScanStream.java new file mode 100644 index 0000000..04cff68 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/ScanStream.java @@ -0,0 +1,36 @@ +/* + * DO NOT MODIFY THIS FILE + */ +package scanner; + +/** + * Abstracts a stream reader that reads one byte at a time with possible + * rollback. + * + * Do not modify this file. + */ +public final class ScanStream { + + final private byte[] data; + private int i; + + public ScanStream(byte[] data) { + this.data = data; + i = 0; + } + + public boolean eof() { + return i >= data.length; + } + + public char next() { + if (eof()) { + throw new RuntimeException("EOF reached"); + } + return (char) data[i++]; + } + + void rollback() { + --i; + } +} diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/Scanner.java b/Homework/cs5300/project-one/p1-scanner/scanner/Scanner.java new file mode 100644 index 0000000..2581dcb --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/Scanner.java @@ -0,0 +1,121 @@ +package scanner; + +import java.util.HashMap; +import java.util.Stack; + +public class Scanner { + HashMap<Character, String> charCategories; + HashMap<String, HashMap<String, String>> transitions; + HashMap<String, String> tokenTypes; + String s0; + + /** + * Builds the tables needed for the scanner. + */ + public Scanner(TableReader tableReader) { + charCategories = new HashMap<>(); + transitions = new HashMap<>(); + tokenTypes = new HashMap<>(); + + // Build catMap, mapping a character to a category. + for (TableReader.CharCat cat : tableReader.getClassifier()) + charCategories.put(cat.getC(), cat.getCategory()); + + // Build the transition table. Given a state and a character category, + // give a new state. + for (TableReader.Transition t : tableReader.getTransitions()) { + if (s0 == null) + s0 = t.getFromStateName(); + if (!transitions.containsKey(t.getFromStateName())) + transitions.put(t.getFromStateName(), new HashMap<String, String>()); + + transitions.get(t.getFromStateName()) + .put(t.getCategory(), t.getToStateName()); + } + + // Build the token types table + for (TableReader.TokenType tt : tableReader.getTokens()) + tokenTypes.put(tt.getState(), tt.getType()); + } + + /** + * Returns the category for c or "not in alphabet" if c has no category. Do + * not hardcode this. That is, this function should have nothing more than a + * table lookup or two. You should not have any character literals in here + * such as 'r' or '3'. + */ + public String getCategory(Character c) { + if (charCategories.containsKey(c)) + return charCategories.get(c); + + return "not in alphabet"; + } + + /** + * Returns the new state given a current state and category. This models + * the transition table. Returns "error" if there is no transition. + * Do not hardcode any state names or categories. You should have only + * table lookups here. + */ + public String getNewState(String state, String category) { + if (transitionExists(state, category)) + return transitions.get(state).get(category); + + return "error"; + } + + private boolean transitionExists(String state, String category) { + if (transitions.containsKey(state)) { + HashMap<String, String> transitionTable = transitions.get(state); + return transitionTable.containsKey(category); + } + return false; + } + + /** + * Returns the type of token corresponding to a given state. If the state + * is not accepting then return "error". + * Do not hardcode any state names or token types. + */ + public String getTokenType(String state) { + if (tokenTypes.containsKey(state)) + return tokenTypes.get(state); + return "error"; + } + + /** + * Return the next token or null if there's a lexical error. + */ + public Token nextToken(ScanStream ss) { + String state = s0; + String lexeme = ""; + + Stack<String> stack = new Stack<>(); + stack.push("bad"); + + while (!state.equals("error") && !ss.eof()) { + char c = ss.next(); + lexeme += c; + + if (tokenTypes.containsKey(state)) + stack.clear(); + + stack.push(state); + + state = getNewState(state, getCategory(c)); + } + + while (!tokenTypes.containsKey(state) && !state.equals("bad")) { + state = stack.pop(); + if (lexeme.length() > 0) + lexeme = lexeme.substring(0, lexeme.length() - 1); + + ss.rollback(); + } + + if (tokenTypes.containsKey(state)) + return new Token(tokenTypes.get(state), lexeme); + + return null; + } +} diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java b/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java new file mode 100644 index 0000000..a074547 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java @@ -0,0 +1,160 @@ +/* + * Do not modify this file. + */ +package scanner; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * TableParser provides all functionality for parsing the table definition file. + * + * Do not modify this file. + */ +public final class TableReader { + + // CharCat class + // Used in the classifier table + public class CharCat { + + final private char c; + final private String category; + + public CharCat(char c, String category) { + this.c = c; + this.category = category; + } + + public char getC() { + return c; + } + + public String getCategory() { + return category; + } + + } + + // Transition class + // Used in the transition table + public class Transition { + + final private String fromState; + final private String category; + final private String toState; + + public Transition(String fromState, String category, String toState) { + this.fromState = fromState; + this.category = category; + this.toState = toState; + } + + public String getFromStateName() { + return fromState; + } + + public String getCategory() { + return category; + } + + public String getToStateName() { + return toState; + } + } + + // TokenType class + // Used in the token type table + public class TokenType { + + final private String state; + final private String type; + + public TokenType(String state, String type) { + this.state = state; + this.type = type; + } + + public String getState() { + return state; + } + + public String getType() { + return type; + } + } + + private enum WhichTable { + + CLASS, TRANS, TOKEN + }; + + private ArrayList<CharCat> classifier; + private ArrayList<Transition> transitions; + private ArrayList<TokenType> tokens; + + public TableReader(String filename) throws IOException { + parse(filename); + } + + public Iterable<CharCat> getClassifier() { + return classifier; + } + + public Iterable<Transition> getTransitions() { + return transitions; + } + + public Iterable<TokenType> getTokens() { + return tokens; + } + + public void parse(String fn) throws FileNotFoundException, IOException { + this.classifier = new ArrayList<>(); + this.transitions = new ArrayList<>(); + this.tokens = new ArrayList<>(); + + WhichTable table = WhichTable.CLASS; + try (BufferedReader br = new BufferedReader(new FileReader(fn))) { + String line; + while ((line = br.readLine()) != null) { + final int idx = line.indexOf("//"); + if (idx > -1) { + line = line.substring(0, idx); + } + line = line.trim(); + if (!line.isEmpty()) { + if (line.equals("ClassifierTable")) { + table = WhichTable.CLASS; + } else if (line.equals("TransitionTable")) { + table = WhichTable.TRANS; + } else if (line.equals("TokenTypeTable")) { + table = WhichTable.TOKEN; + } else { + String[] lineTokens = line.split(" "); + if (table == WhichTable.CLASS) { + char c = lineTokens[0].charAt(0); + if (lineTokens[0].equals("\\space")) { + c = ' '; + } else if (lineTokens[0].equals("\\t")) { + c = '\t'; + } else if (lineTokens[0].equals("\\n")) { + c = '\n'; + } + CharCat cc = new CharCat(c, lineTokens[1]); + classifier.add(cc); + } else if (table == WhichTable.TRANS) { + Transition t = new Transition(lineTokens[0], lineTokens[1], lineTokens[2]); + transitions.add(t); + } else if (table == WhichTable.TOKEN) { + TokenType tt = new TokenType(lineTokens[0], lineTokens[1]); + tokens.add(tt); + } + } + } + } + } + } +} diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/Tests.java b/Homework/cs5300/project-one/p1-scanner/scanner/Tests.java new file mode 100644 index 0000000..bb51147 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/Tests.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package scanner; + +/** + * A test utility class. + * + * Do not modify this file. + */ +public class Tests { + + private int N = 0; + private int failures = 0; + + public void test(boolean b) { + N++; + if (!b) { + System.err.println("Failed test " + N); + failures++; + } + } + + public void test(boolean b, String message) { + test(b); + if (!b) { + System.err.println(message); + } + } + + public void addFailure() { + N++; + failures++; + } + + public int getN() { + return N; + } + + public int getSuccesses() { + return N - failures; + } + + public int getFailures() { + return failures; + } +} diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/Token.java b/Homework/cs5300/project-one/p1-scanner/scanner/Token.java new file mode 100644 index 0000000..4c0151a --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/Token.java @@ -0,0 +1,33 @@ +/* + * DO NOT MODIFY THIS FILE + */ +package scanner; + +/** + * A class that stores a token. + * + * Do not modify this file. + */ +public class Token { + + private final String type; + private final String lexeme; + + public Token(String type, String lexeme) { + this.type = type; + this.lexeme = lexeme; + } + + public String toString() { + return type + ": " + lexeme; + } + + public String getType() { + return type; + } + + public String getLexeme() { + return lexeme; + } + +} diff --git a/Homework/cs5300/project-three/antlr-4.9.1-complete.jar b/Homework/cs5300/project-three/antlr-4.9.1-complete.jar Binary files differnew file mode 100644 index 0000000..1bb7e8c --- /dev/null +++ b/Homework/cs5300/project-three/antlr-4.9.1-complete.jar diff --git a/Homework/cs5300/project-three/cfgparser/Grammar.java b/Homework/cs5300/project-three/cfgparser/Grammar.java new file mode 100644 index 0000000..4de9ae4 --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/Grammar.java @@ -0,0 +1,65 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package cfgparser; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import org.antlr.v4.runtime.ANTLRFileStream; +import org.antlr.v4.runtime.BufferedTokenStream; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import parser.Rule; + +/** + * + * @author edwajohn + */ +public class Grammar { + + private String name; + private ArrayList<Rule> rules; + private String startSymbol; + + public Grammar(String filename) throws IOException { + GrammarLexer cfglexer = new GrammarLexer(new ANTLRFileStream(filename)); + GrammarParser cfgparser = new GrammarParser(new BufferedTokenStream(cfglexer)); + ParserRuleContext tree = cfgparser.getContext(); // parse + + GrammarParser.GoalContext context = cfgparser.goal(); + + // Walk it and attach our listener + GrammarListenerImpl listener = new GrammarListenerImpl(); + ParseTreeWalker walker = new ParseTreeWalker(); + walker.walk(listener, context); + name = listener.name; + rules = listener.rules; + startSymbol = listener.startSymbol; + } + + public ArrayList<Rule> getRules() { + return rules; + } + + public String getName() { + return name; + } + + public ArrayList<String> getSymbols() { + HashSet<String> symbols = new HashSet<>(); + for (Rule rule : rules) { + symbols.add(rule.getLhs()); + for (String symbol : rule.getRhs()) { + symbols.add(symbol); + } + } + return new ArrayList<>(symbols); + } + + public String getStartSymbol() { + return startSymbol; + } +} diff --git a/Homework/cs5300/project-three/cfgparser/GrammarBaseListener.java b/Homework/cs5300/project-three/cfgparser/GrammarBaseListener.java new file mode 100644 index 0000000..29038f1 --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/GrammarBaseListener.java @@ -0,0 +1,135 @@ +// Generated from Grammar.g4 by ANTLR 4.9.1 +package cfgparser; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link GrammarListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class GrammarBaseListener implements GrammarListener { + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterGoal(GrammarParser.GoalContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitGoal(GrammarParser.GoalContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterName(GrammarParser.NameContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitName(GrammarParser.NameContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRule_list(GrammarParser.Rule_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRule_list(GrammarParser.Rule_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterLhs(GrammarParser.LhsContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitLhs(GrammarParser.LhsContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterRhs(GrammarParser.RhsContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitRhs(GrammarParser.RhsContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterOr_list(GrammarParser.Or_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitOr_list(GrammarParser.Or_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSymbol_list(GrammarParser.Symbol_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSymbol_list(GrammarParser.Symbol_listContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterSymbol(GrammarParser.SymbolContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitSymbol(GrammarParser.SymbolContext ctx) { } + + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + * <p>The default implementation does nothing.</p> + */ + @Override public void visitErrorNode(ErrorNode node) { } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/cfgparser/GrammarLexer.java b/Homework/cs5300/project-three/cfgparser/GrammarLexer.java new file mode 100644 index 0000000..653cc9c --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/GrammarLexer.java @@ -0,0 +1,137 @@ +// Generated from Grammar.g4 by ANTLR 4.9.1 +package cfgparser; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class GrammarLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, GRAMMAR=4, IDENT=5, WS=6, COMMENT=7; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "T__0", "T__1", "T__2", "GRAMMAR", "IDENT", "WS", "COMMENT", "LETTER", + "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "';'", "':'", "'|'", "'grammar'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, "GRAMMAR", "IDENT", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public GrammarLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "Grammar.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\tR\b\1\4\2\t\2\4"+ + "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\3\2\3\2"+ + "\3\3\3\3\3\4\3\4\3\5\3\5\3\5\3\5\3\5\3\5\3\5\3\5\3\6\3\6\3\6\7\6\'\n\6"+ + "\f\6\16\6*\13\6\3\7\6\7-\n\7\r\7\16\7.\3\7\3\7\3\b\3\b\3\b\3\b\7\b\67"+ + "\n\b\f\b\16\b:\13\b\3\b\5\b=\n\b\3\b\3\b\3\b\3\b\3\b\7\bD\n\b\f\b\16\b"+ + "G\13\b\3\b\3\b\5\bK\n\b\3\b\3\b\3\t\3\t\3\n\3\n\3E\2\13\3\3\5\4\7\5\t"+ + "\6\13\7\r\b\17\t\21\2\23\2\3\2\5\5\2\13\f\16\17\"\"\4\2\f\f\17\17\4\2"+ + "C\\c|\2V\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2"+ + "\2\r\3\2\2\2\2\17\3\2\2\2\3\25\3\2\2\2\5\27\3\2\2\2\7\31\3\2\2\2\t\33"+ + "\3\2\2\2\13#\3\2\2\2\r,\3\2\2\2\17J\3\2\2\2\21N\3\2\2\2\23P\3\2\2\2\25"+ + "\26\7=\2\2\26\4\3\2\2\2\27\30\7<\2\2\30\6\3\2\2\2\31\32\7~\2\2\32\b\3"+ + "\2\2\2\33\34\7i\2\2\34\35\7t\2\2\35\36\7c\2\2\36\37\7o\2\2\37 \7o\2\2"+ + " !\7c\2\2!\"\7t\2\2\"\n\3\2\2\2#(\5\21\t\2$\'\5\21\t\2%\'\5\23\n\2&$\3"+ + "\2\2\2&%\3\2\2\2\'*\3\2\2\2(&\3\2\2\2()\3\2\2\2)\f\3\2\2\2*(\3\2\2\2+"+ + "-\t\2\2\2,+\3\2\2\2-.\3\2\2\2.,\3\2\2\2./\3\2\2\2/\60\3\2\2\2\60\61\b"+ + "\7\2\2\61\16\3\2\2\2\62\63\7\61\2\2\63\64\7\61\2\2\648\3\2\2\2\65\67\n"+ + "\3\2\2\66\65\3\2\2\2\67:\3\2\2\28\66\3\2\2\289\3\2\2\29<\3\2\2\2:8\3\2"+ + "\2\2;=\7\17\2\2<;\3\2\2\2<=\3\2\2\2=>\3\2\2\2>K\7\f\2\2?@\7\61\2\2@A\7"+ + ",\2\2AE\3\2\2\2BD\13\2\2\2CB\3\2\2\2DG\3\2\2\2EF\3\2\2\2EC\3\2\2\2FH\3"+ + "\2\2\2GE\3\2\2\2HI\7,\2\2IK\7\61\2\2J\62\3\2\2\2J?\3\2\2\2KL\3\2\2\2L"+ + "M\b\b\2\2M\20\3\2\2\2NO\t\4\2\2O\22\3\2\2\2PQ\4\62;\2Q\24\3\2\2\2\n\2"+ + "&(.8<EJ\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/cfgparser/GrammarListener.java b/Homework/cs5300/project-three/cfgparser/GrammarListener.java new file mode 100644 index 0000000..e24999c --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/GrammarListener.java @@ -0,0 +1,90 @@ +// Generated from Grammar.g4 by ANTLR 4.9.1 +package cfgparser; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link GrammarParser}. + */ +public interface GrammarListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link GrammarParser#goal}. + * @param ctx the parse tree + */ + void enterGoal(GrammarParser.GoalContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#goal}. + * @param ctx the parse tree + */ + void exitGoal(GrammarParser.GoalContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#name}. + * @param ctx the parse tree + */ + void enterName(GrammarParser.NameContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#name}. + * @param ctx the parse tree + */ + void exitName(GrammarParser.NameContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#rule_list}. + * @param ctx the parse tree + */ + void enterRule_list(GrammarParser.Rule_listContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#rule_list}. + * @param ctx the parse tree + */ + void exitRule_list(GrammarParser.Rule_listContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#lhs}. + * @param ctx the parse tree + */ + void enterLhs(GrammarParser.LhsContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#lhs}. + * @param ctx the parse tree + */ + void exitLhs(GrammarParser.LhsContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#rhs}. + * @param ctx the parse tree + */ + void enterRhs(GrammarParser.RhsContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#rhs}. + * @param ctx the parse tree + */ + void exitRhs(GrammarParser.RhsContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#or_list}. + * @param ctx the parse tree + */ + void enterOr_list(GrammarParser.Or_listContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#or_list}. + * @param ctx the parse tree + */ + void exitOr_list(GrammarParser.Or_listContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#symbol_list}. + * @param ctx the parse tree + */ + void enterSymbol_list(GrammarParser.Symbol_listContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#symbol_list}. + * @param ctx the parse tree + */ + void exitSymbol_list(GrammarParser.Symbol_listContext ctx); + /** + * Enter a parse tree produced by {@link GrammarParser#symbol}. + * @param ctx the parse tree + */ + void enterSymbol(GrammarParser.SymbolContext ctx); + /** + * Exit a parse tree produced by {@link GrammarParser#symbol}. + * @param ctx the parse tree + */ + void exitSymbol(GrammarParser.SymbolContext ctx); +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/cfgparser/GrammarListenerImpl.java b/Homework/cs5300/project-three/cfgparser/GrammarListenerImpl.java new file mode 100644 index 0000000..560b412 --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/GrammarListenerImpl.java @@ -0,0 +1,71 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package cfgparser; + +import cfgparser.GrammarBaseListener; +import cfgparser.GrammarParser; +import java.util.ArrayList; +import org.antlr.v4.runtime.tree.ErrorNode; +import parser.Rule; + +/** + * + * @author edwajohn + */ +public class GrammarListenerImpl extends GrammarBaseListener { + + String name; + String curLhs; + Rule curRule; + ArrayList<Rule> rules; + String startSymbol; + + public GrammarListenerImpl() { + rules = new ArrayList<>(); + startSymbol = null; + } + + public void print() { + for (Rule rule : rules) { + System.out.println(rule); + } + } + + @Override + public void enterName(GrammarParser.NameContext ctx) { + name = ctx.getText(); + } + + @Override + public void enterLhs(GrammarParser.LhsContext ctx) { + curLhs = ctx.getText(); + if (startSymbol == null) { + startSymbol = curLhs; + } + } + + @Override + public void enterRhs(GrammarParser.RhsContext ctx) { + curRule = new Rule(curLhs); + } + + @Override + public void exitRhs(GrammarParser.RhsContext ctx) { + curRule.setName(rules.size()); + rules.add(curRule); + } + + @Override + public void enterSymbol(GrammarParser.SymbolContext ctx) { + curRule.addRhs(ctx.getText()); + } + + @Override + public void visitErrorNode(ErrorNode node) { + System.out.println("Error!"); + } + +} diff --git a/Homework/cs5300/project-three/cfgparser/GrammarParser.java b/Homework/cs5300/project-three/cfgparser/GrammarParser.java new file mode 100644 index 0000000..20c523c --- /dev/null +++ b/Homework/cs5300/project-three/cfgparser/GrammarParser.java @@ -0,0 +1,503 @@ +// Generated from Grammar.g4 by ANTLR 4.9.1 +package cfgparser; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class GrammarParser extends Parser { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, GRAMMAR=4, IDENT=5, WS=6, COMMENT=7; + public static final int + RULE_goal = 0, RULE_name = 1, RULE_rule_list = 2, RULE_lhs = 3, RULE_rhs = 4, + RULE_or_list = 5, RULE_symbol_list = 6, RULE_symbol = 7; + private static String[] makeRuleNames() { + return new String[] { + "goal", "name", "rule_list", "lhs", "rhs", "or_list", "symbol_list", + "symbol" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "';'", "':'", "'|'", "'grammar'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, "GRAMMAR", "IDENT", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "Grammar.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public GrammarParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + public static class GoalContext extends ParserRuleContext { + public TerminalNode GRAMMAR() { return getToken(GrammarParser.GRAMMAR, 0); } + public NameContext name() { + return getRuleContext(NameContext.class,0); + } + public Rule_listContext rule_list() { + return getRuleContext(Rule_listContext.class,0); + } + public GoalContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_goal; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterGoal(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitGoal(this); + } + } + + public final GoalContext goal() throws RecognitionException { + GoalContext _localctx = new GoalContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_goal); + try { + enterOuterAlt(_localctx, 1); + { + setState(16); + match(GRAMMAR); + setState(17); + name(); + setState(18); + match(T__0); + setState(19); + rule_list(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class NameContext extends ParserRuleContext { + public TerminalNode IDENT() { return getToken(GrammarParser.IDENT, 0); } + public NameContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_name; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterName(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitName(this); + } + } + + public final NameContext name() throws RecognitionException { + NameContext _localctx = new NameContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_name); + try { + enterOuterAlt(_localctx, 1); + { + setState(21); + match(IDENT); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class Rule_listContext extends ParserRuleContext { + public LhsContext lhs() { + return getRuleContext(LhsContext.class,0); + } + public RhsContext rhs() { + return getRuleContext(RhsContext.class,0); + } + public Or_listContext or_list() { + return getRuleContext(Or_listContext.class,0); + } + public Rule_listContext rule_list() { + return getRuleContext(Rule_listContext.class,0); + } + public Rule_listContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_rule_list; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterRule_list(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitRule_list(this); + } + } + + public final Rule_listContext rule_list() throws RecognitionException { + Rule_listContext _localctx = new Rule_listContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_rule_list); + try { + setState(31); + _errHandler.sync(this); + switch (_input.LA(1)) { + case IDENT: + enterOuterAlt(_localctx, 1); + { + setState(23); + lhs(); + setState(24); + match(T__1); + setState(25); + rhs(); + setState(26); + or_list(); + setState(27); + match(T__0); + setState(28); + rule_list(); + } + break; + case EOF: + enterOuterAlt(_localctx, 2); + { + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class LhsContext extends ParserRuleContext { + public TerminalNode IDENT() { return getToken(GrammarParser.IDENT, 0); } + public LhsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_lhs; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterLhs(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitLhs(this); + } + } + + public final LhsContext lhs() throws RecognitionException { + LhsContext _localctx = new LhsContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_lhs); + try { + enterOuterAlt(_localctx, 1); + { + setState(33); + match(IDENT); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RhsContext extends ParserRuleContext { + public Symbol_listContext symbol_list() { + return getRuleContext(Symbol_listContext.class,0); + } + public RhsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_rhs; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterRhs(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitRhs(this); + } + } + + public final RhsContext rhs() throws RecognitionException { + RhsContext _localctx = new RhsContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_rhs); + try { + enterOuterAlt(_localctx, 1); + { + setState(35); + symbol_list(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class Or_listContext extends ParserRuleContext { + public RhsContext rhs() { + return getRuleContext(RhsContext.class,0); + } + public Or_listContext or_list() { + return getRuleContext(Or_listContext.class,0); + } + public Or_listContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_or_list; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterOr_list(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitOr_list(this); + } + } + + public final Or_listContext or_list() throws RecognitionException { + Or_listContext _localctx = new Or_listContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_or_list); + try { + setState(42); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__2: + enterOuterAlt(_localctx, 1); + { + setState(37); + match(T__2); + setState(38); + rhs(); + setState(39); + or_list(); + } + break; + case T__0: + enterOuterAlt(_localctx, 2); + { + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class Symbol_listContext extends ParserRuleContext { + public SymbolContext symbol() { + return getRuleContext(SymbolContext.class,0); + } + public Symbol_listContext symbol_list() { + return getRuleContext(Symbol_listContext.class,0); + } + public Symbol_listContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_symbol_list; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterSymbol_list(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitSymbol_list(this); + } + } + + public final Symbol_listContext symbol_list() throws RecognitionException { + Symbol_listContext _localctx = new Symbol_listContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_symbol_list); + try { + setState(48); + _errHandler.sync(this); + switch (_input.LA(1)) { + case IDENT: + enterOuterAlt(_localctx, 1); + { + setState(44); + symbol(); + setState(45); + symbol_list(); + } + break; + case T__0: + case T__2: + enterOuterAlt(_localctx, 2); + { + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SymbolContext extends ParserRuleContext { + public TerminalNode IDENT() { return getToken(GrammarParser.IDENT, 0); } + public SymbolContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_symbol; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).enterSymbol(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof GrammarListener ) ((GrammarListener)listener).exitSymbol(this); + } + } + + public final SymbolContext symbol() throws RecognitionException { + SymbolContext _localctx = new SymbolContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_symbol); + try { + enterOuterAlt(_localctx, 1); + { + setState(50); + match(IDENT); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\t\67\4\2\t\2\4\3"+ + "\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\2\3\2\3"+ + "\2\3\3\3\3\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\5\4\"\n\4\3\5\3\5\3\6\3\6\3"+ + "\7\3\7\3\7\3\7\3\7\5\7-\n\7\3\b\3\b\3\b\3\b\5\b\63\n\b\3\t\3\t\3\t\2\2"+ + "\n\2\4\6\b\n\f\16\20\2\2\2\61\2\22\3\2\2\2\4\27\3\2\2\2\6!\3\2\2\2\b#"+ + "\3\2\2\2\n%\3\2\2\2\f,\3\2\2\2\16\62\3\2\2\2\20\64\3\2\2\2\22\23\7\6\2"+ + "\2\23\24\5\4\3\2\24\25\7\3\2\2\25\26\5\6\4\2\26\3\3\2\2\2\27\30\7\7\2"+ + "\2\30\5\3\2\2\2\31\32\5\b\5\2\32\33\7\4\2\2\33\34\5\n\6\2\34\35\5\f\7"+ + "\2\35\36\7\3\2\2\36\37\5\6\4\2\37\"\3\2\2\2 \"\3\2\2\2!\31\3\2\2\2! \3"+ + "\2\2\2\"\7\3\2\2\2#$\7\7\2\2$\t\3\2\2\2%&\5\16\b\2&\13\3\2\2\2\'(\7\5"+ + "\2\2()\5\n\6\2)*\5\f\7\2*-\3\2\2\2+-\3\2\2\2,\'\3\2\2\2,+\3\2\2\2-\r\3"+ + "\2\2\2./\5\20\t\2/\60\5\16\b\2\60\63\3\2\2\2\61\63\3\2\2\2\62.\3\2\2\2"+ + "\62\61\3\2\2\2\63\17\3\2\2\2\64\65\7\7\2\2\65\21\3\2\2\2\5!,\62"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/Expr.cfg b/Homework/cs5300/project-three/data/Expr.cfg new file mode 100644 index 0000000..7493e3e --- /dev/null +++ b/Homework/cs5300/project-three/data/Expr.cfg @@ -0,0 +1,25 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ExprLexer.tokens. + +grammar Expr; + +goal: expr; + +expr: expr PLUS term + | expr MINUS term + | term + ; + +term : term MULTIPLY factor + | term DIVIDE factor + | factor + ; + +factor: OPAREN expr CPAREN + | INT + | FLOAT + | IDENTIFIER + ;
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/ExprLexer.tokens b/Homework/cs5300/project-three/data/ExprLexer.tokens new file mode 100644 index 0000000..ccb798a --- /dev/null +++ b/Homework/cs5300/project-three/data/ExprLexer.tokens @@ -0,0 +1,39 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Expr.cfg. + +lexer grammar ExprLexer; + +PLUS: '+' ; +MINUS: '-' ; +MULTIPLY: '*' ; +DIVIDE: '/' ; +OPAREN: '(' ; +CPAREN: ')' ; + +FLOAT + : DIGIT+ '.' DIGIT+ + ; + +INT + : DIGIT+ + ; + +IDENTIFIER + : LETTER (LETTER | DIGIT)* + ; + +COMMENT + : ( '//' ~[\r\n]* '\r'? '\n' + | '/*' .*? '*/' + ) -> skip + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; + +fragment LETTER : ('a'..'z' | 'A'..'Z'); +fragment DIGIT : ('0'..'9');
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/Paren.cfg b/Homework/cs5300/project-three/data/Paren.cfg new file mode 100644 index 0000000..184b11c --- /dev/null +++ b/Homework/cs5300/project-three/data/Paren.cfg @@ -0,0 +1,17 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ParenLexer.tokens. + +grammar Paren; + +goal: list; + +list: list pair + | pair + ; + +pair: OPAREN list CPAREN + | OPAREN CPAREN + ; diff --git a/Homework/cs5300/project-three/data/ParenLexer.tokens b/Homework/cs5300/project-three/data/ParenLexer.tokens new file mode 100644 index 0000000..51e5258 --- /dev/null +++ b/Homework/cs5300/project-three/data/ParenLexer.tokens @@ -0,0 +1,19 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Paren.cfg. + +lexer grammar ParenLexer; + +OPAREN + : '(' + ; + +CPAREN + : ')' + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; diff --git a/Homework/cs5300/project-three/data/Simple.cfg b/Homework/cs5300/project-three/data/Simple.cfg new file mode 100644 index 0000000..80c1e18 --- /dev/null +++ b/Homework/cs5300/project-three/data/Simple.cfg @@ -0,0 +1,13 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ParenLexer.tokens. + +grammar Simple; + +G: N; + +N: N X + | X + ; diff --git a/Homework/cs5300/project-three/data/SimpleLexer.tokens b/Homework/cs5300/project-three/data/SimpleLexer.tokens new file mode 100644 index 0000000..64ec3c1 --- /dev/null +++ b/Homework/cs5300/project-three/data/SimpleLexer.tokens @@ -0,0 +1,15 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Simple.cfg. + +lexer grammar SimpleLexer; + +X + : 'x' + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; diff --git a/Homework/cs5300/project-three/data/Tiny copy.cfg b/Homework/cs5300/project-three/data/Tiny copy.cfg new file mode 100644 index 0000000..71fb8d9 --- /dev/null +++ b/Homework/cs5300/project-three/data/Tiny copy.cfg @@ -0,0 +1,63 @@ +// +// WRITE A CORRECT GRAMMAR FOR THE TINY LANGUAGE IN THIS FILE. +// +// The grammar is described in data/tinyGrammar.pdf. Do not use dashes +// in your non-terminal names. I recommend you use mixed case naming for your +// non-terminals instead (e.g. stmtSeq). +// +// Do not put any string literals in this grammar (e.g. "="). Use only token +// names given in TinyLexer.tokens. +// +// This file contains an expression grammar that uses tokens described in +// TinyLexer.tokens. +// +// NOTE ONE ERROR in the pdf: the second production rule should read: +// stmt-seq -> stmt-seq stmt | stmt + +grammar Tiny; + +goal: program; + +program: stmtseq; + +stmtseq: stmtseq stmt + | stmt; + +stmt: ifstmt + | repeatstmt + | assignstmt + | readstmt + | writestmt + ; + +ifstmt: IF exp THEN stmtseq END + | IF exp THEN stmtseq ELSE stmtseq END + ; + +repeatstmt: REPEAT stmtseq UNTIL exp; + +assignstmt: ID EQUAL exp; + +readstmt: READ ID; + +writestmt: WRITE exp; + +exp: simpleexp LT simpleexp + | simpleexp EQUAL simpleexp + | simpleexp + ; + +simpleexp: simpleexp PLUS term + | simpleexp MINUS term + | term + ; + +term: term MULTIPLY factor + | term DIVIDE factor + | factor + ; + +factor: OPAREN exp CPAREN + | NUM + | ID + ;
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/Tiny.cfg b/Homework/cs5300/project-three/data/Tiny.cfg new file mode 100644 index 0000000..71fb8d9 --- /dev/null +++ b/Homework/cs5300/project-three/data/Tiny.cfg @@ -0,0 +1,63 @@ +// +// WRITE A CORRECT GRAMMAR FOR THE TINY LANGUAGE IN THIS FILE. +// +// The grammar is described in data/tinyGrammar.pdf. Do not use dashes +// in your non-terminal names. I recommend you use mixed case naming for your +// non-terminals instead (e.g. stmtSeq). +// +// Do not put any string literals in this grammar (e.g. "="). Use only token +// names given in TinyLexer.tokens. +// +// This file contains an expression grammar that uses tokens described in +// TinyLexer.tokens. +// +// NOTE ONE ERROR in the pdf: the second production rule should read: +// stmt-seq -> stmt-seq stmt | stmt + +grammar Tiny; + +goal: program; + +program: stmtseq; + +stmtseq: stmtseq stmt + | stmt; + +stmt: ifstmt + | repeatstmt + | assignstmt + | readstmt + | writestmt + ; + +ifstmt: IF exp THEN stmtseq END + | IF exp THEN stmtseq ELSE stmtseq END + ; + +repeatstmt: REPEAT stmtseq UNTIL exp; + +assignstmt: ID EQUAL exp; + +readstmt: READ ID; + +writestmt: WRITE exp; + +exp: simpleexp LT simpleexp + | simpleexp EQUAL simpleexp + | simpleexp + ; + +simpleexp: simpleexp PLUS term + | simpleexp MINUS term + | term + ; + +term: term MULTIPLY factor + | term DIVIDE factor + | factor + ; + +factor: OPAREN exp CPAREN + | NUM + | ID + ;
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/TinyLexer.tokens b/Homework/cs5300/project-three/data/TinyLexer.tokens new file mode 100644 index 0000000..ad6b2a7 --- /dev/null +++ b/Homework/cs5300/project-three/data/TinyLexer.tokens @@ -0,0 +1,41 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Tiny.cfg. + +lexer grammar TinyLexer; + +IF : 'if' ; +THEN : 'then' ; +ELSE : 'else' ; +END : 'end' ; +REPEAT : 'repeat' ; +UNTIL : 'until' ; +READ : 'read' ; +WRITE : 'write' ; +OPAREN : '(' ; +CPAREN : ')' ; + +PLUS: '+' ; +MINUS: '-' ; +MULTIPLY: '*' ; +DIVIDE: '/' ; +LT: '<' ; +EQUAL: '=' ; + +NUM : DIGIT+ ; +ID : LETTER+ ; + +COMMENT + : ( '//' ~[\r\n]* '\r'? '\n' + | '/*' .*? '*/' + ) -> skip + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; + +fragment LETTER : ('a'..'z' | 'A'..'Z'); +fragment DIGIT : ('0'..'9');
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/expr1.dat b/Homework/cs5300/project-three/data/expr1.dat new file mode 100644 index 0000000..846e7d3 --- /dev/null +++ b/Homework/cs5300/project-three/data/expr1.dat @@ -0,0 +1 @@ +3 + 4 * 5 / 8
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/expr2.dat b/Homework/cs5300/project-three/data/expr2.dat new file mode 100644 index 0000000..8e0c0b7 --- /dev/null +++ b/Homework/cs5300/project-three/data/expr2.dat @@ -0,0 +1 @@ +3 + + 42 * 34
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/expr3.dat b/Homework/cs5300/project-three/data/expr3.dat new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/Homework/cs5300/project-three/data/expr3.dat @@ -0,0 +1 @@ +3 diff --git a/Homework/cs5300/project-three/data/expr4.dat b/Homework/cs5300/project-three/data/expr4.dat new file mode 100644 index 0000000..84a357a --- /dev/null +++ b/Homework/cs5300/project-three/data/expr4.dat @@ -0,0 +1 @@ +(a + b) * ((c-def)/43)
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/expr5.dat b/Homework/cs5300/project-three/data/expr5.dat new file mode 100644 index 0000000..c2c433b --- /dev/null +++ b/Homework/cs5300/project-three/data/expr5.dat @@ -0,0 +1 @@ +(a + b) * ((c-d)/43 diff --git a/Homework/cs5300/project-three/data/expr6.dat b/Homework/cs5300/project-three/data/expr6.dat new file mode 100644 index 0000000..f46d387 --- /dev/null +++ b/Homework/cs5300/project-three/data/expr6.dat @@ -0,0 +1 @@ +(
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/paren0.dat b/Homework/cs5300/project-three/data/paren0.dat new file mode 100644 index 0000000..dd626a0 --- /dev/null +++ b/Homework/cs5300/project-three/data/paren0.dat @@ -0,0 +1 @@ +()
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/paren1.dat b/Homework/cs5300/project-three/data/paren1.dat new file mode 100644 index 0000000..f7e9493 --- /dev/null +++ b/Homework/cs5300/project-three/data/paren1.dat @@ -0,0 +1,3 @@ +(( +))( +)(((()))()(()))
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/paren2.dat b/Homework/cs5300/project-three/data/paren2.dat new file mode 100644 index 0000000..4f879eb --- /dev/null +++ b/Homework/cs5300/project-three/data/paren2.dat @@ -0,0 +1,3 @@ +( +))( +)
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/paren3.dat b/Homework/cs5300/project-three/data/paren3.dat new file mode 100644 index 0000000..e4a2fe8 --- /dev/null +++ b/Homework/cs5300/project-three/data/paren3.dat @@ -0,0 +1 @@ +)()( diff --git a/Homework/cs5300/project-three/data/paren4.dat b/Homework/cs5300/project-three/data/paren4.dat new file mode 100644 index 0000000..3077d74 --- /dev/null +++ b/Homework/cs5300/project-three/data/paren4.dat @@ -0,0 +1 @@ +()()(()()) diff --git a/Homework/cs5300/project-three/data/tiny1.dat b/Homework/cs5300/project-three/data/tiny1.dat new file mode 100644 index 0000000..7f9078f --- /dev/null +++ b/Homework/cs5300/project-three/data/tiny1.dat @@ -0,0 +1,3 @@ +a = 3 +b = 4 +c = a + b diff --git a/Homework/cs5300/project-three/data/tiny2.dat b/Homework/cs5300/project-three/data/tiny2.dat new file mode 100644 index 0000000..09195b8 --- /dev/null +++ b/Homework/cs5300/project-three/data/tiny2.dat @@ -0,0 +1,7 @@ +a = 3 +b = 4 +if a = b then + write (a + b * (c - d)) +else + write a +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/tiny3.dat b/Homework/cs5300/project-three/data/tiny3.dat new file mode 100644 index 0000000..768addd --- /dev/null +++ b/Homework/cs5300/project-three/data/tiny3.dat @@ -0,0 +1,4 @@ +a = 3 +if a = then + write a +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/tiny4.dat b/Homework/cs5300/project-three/data/tiny4.dat new file mode 100644 index 0000000..1d66283 --- /dev/null +++ b/Homework/cs5300/project-three/data/tiny4.dat @@ -0,0 +1,4 @@ +a = 3 +if (a = b) then + write a = 3) +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/data/tinyGrammar.pdf b/Homework/cs5300/project-three/data/tinyGrammar.pdf Binary files differnew file mode 100644 index 0000000..855d45b --- /dev/null +++ b/Homework/cs5300/project-three/data/tinyGrammar.pdf diff --git a/Homework/cs5300/project-three/lexer/ExprLexer.interp b/Homework/cs5300/project-three/lexer/ExprLexer.interp new file mode 100644 index 0000000..c9ec8c0 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ExprLexer.interp @@ -0,0 +1,52 @@ +token literal names: +null +'+' +'-' +'*' +'/' +'(' +')' +null +null +null +null +null + +token symbolic names: +null +PLUS +MINUS +MULTIPLY +DIVIDE +OPAREN +CPAREN +FLOAT +INT +IDENTIFIER +COMMENT +WS + +rule names: +PLUS +MINUS +MULTIPLY +DIVIDE +OPAREN +CPAREN +FLOAT +INT +IDENTIFIER +COMMENT +WS +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 13, 104, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 6, 8, 43, 10, 8, 13, 8, 14, 8, 44, 3, 8, 3, 8, 6, 8, 49, 10, 8, 13, 8, 14, 8, 50, 3, 9, 6, 9, 54, 10, 9, 13, 9, 14, 9, 55, 3, 10, 3, 10, 3, 10, 7, 10, 61, 10, 10, 12, 10, 14, 10, 64, 11, 10, 3, 11, 3, 11, 3, 11, 3, 11, 7, 11, 70, 10, 11, 12, 11, 14, 11, 73, 11, 11, 3, 11, 5, 11, 76, 10, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 7, 11, 83, 10, 11, 12, 11, 14, 11, 86, 11, 11, 3, 11, 3, 11, 5, 11, 90, 10, 11, 3, 11, 3, 11, 3, 12, 6, 12, 95, 10, 12, 13, 12, 14, 12, 96, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 84, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 2, 27, 2, 3, 2, 5, 4, 2, 12, 12, 15, 15, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 67, 92, 99, 124, 2, 111, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 31, 3, 2, 2, 2, 7, 33, 3, 2, 2, 2, 9, 35, 3, 2, 2, 2, 11, 37, 3, 2, 2, 2, 13, 39, 3, 2, 2, 2, 15, 42, 3, 2, 2, 2, 17, 53, 3, 2, 2, 2, 19, 57, 3, 2, 2, 2, 21, 89, 3, 2, 2, 2, 23, 94, 3, 2, 2, 2, 25, 100, 3, 2, 2, 2, 27, 102, 3, 2, 2, 2, 29, 30, 7, 45, 2, 2, 30, 4, 3, 2, 2, 2, 31, 32, 7, 47, 2, 2, 32, 6, 3, 2, 2, 2, 33, 34, 7, 44, 2, 2, 34, 8, 3, 2, 2, 2, 35, 36, 7, 49, 2, 2, 36, 10, 3, 2, 2, 2, 37, 38, 7, 42, 2, 2, 38, 12, 3, 2, 2, 2, 39, 40, 7, 43, 2, 2, 40, 14, 3, 2, 2, 2, 41, 43, 5, 27, 14, 2, 42, 41, 3, 2, 2, 2, 43, 44, 3, 2, 2, 2, 44, 42, 3, 2, 2, 2, 44, 45, 3, 2, 2, 2, 45, 46, 3, 2, 2, 2, 46, 48, 7, 48, 2, 2, 47, 49, 5, 27, 14, 2, 48, 47, 3, 2, 2, 2, 49, 50, 3, 2, 2, 2, 50, 48, 3, 2, 2, 2, 50, 51, 3, 2, 2, 2, 51, 16, 3, 2, 2, 2, 52, 54, 5, 27, 14, 2, 53, 52, 3, 2, 2, 2, 54, 55, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 55, 56, 3, 2, 2, 2, 56, 18, 3, 2, 2, 2, 57, 62, 5, 25, 13, 2, 58, 61, 5, 25, 13, 2, 59, 61, 5, 27, 14, 2, 60, 58, 3, 2, 2, 2, 60, 59, 3, 2, 2, 2, 61, 64, 3, 2, 2, 2, 62, 60, 3, 2, 2, 2, 62, 63, 3, 2, 2, 2, 63, 20, 3, 2, 2, 2, 64, 62, 3, 2, 2, 2, 65, 66, 7, 49, 2, 2, 66, 67, 7, 49, 2, 2, 67, 71, 3, 2, 2, 2, 68, 70, 10, 2, 2, 2, 69, 68, 3, 2, 2, 2, 70, 73, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 72, 3, 2, 2, 2, 72, 75, 3, 2, 2, 2, 73, 71, 3, 2, 2, 2, 74, 76, 7, 15, 2, 2, 75, 74, 3, 2, 2, 2, 75, 76, 3, 2, 2, 2, 76, 77, 3, 2, 2, 2, 77, 90, 7, 12, 2, 2, 78, 79, 7, 49, 2, 2, 79, 80, 7, 44, 2, 2, 80, 84, 3, 2, 2, 2, 81, 83, 11, 2, 2, 2, 82, 81, 3, 2, 2, 2, 83, 86, 3, 2, 2, 2, 84, 85, 3, 2, 2, 2, 84, 82, 3, 2, 2, 2, 85, 87, 3, 2, 2, 2, 86, 84, 3, 2, 2, 2, 87, 88, 7, 44, 2, 2, 88, 90, 7, 49, 2, 2, 89, 65, 3, 2, 2, 2, 89, 78, 3, 2, 2, 2, 90, 91, 3, 2, 2, 2, 91, 92, 8, 11, 2, 2, 92, 22, 3, 2, 2, 2, 93, 95, 9, 3, 2, 2, 94, 93, 3, 2, 2, 2, 95, 96, 3, 2, 2, 2, 96, 94, 3, 2, 2, 2, 96, 97, 3, 2, 2, 2, 97, 98, 3, 2, 2, 2, 98, 99, 8, 12, 2, 2, 99, 24, 3, 2, 2, 2, 100, 101, 9, 4, 2, 2, 101, 26, 3, 2, 2, 2, 102, 103, 4, 50, 59, 2, 103, 28, 3, 2, 2, 2, 13, 2, 44, 50, 55, 60, 62, 71, 75, 84, 89, 96, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/ExprLexer.java b/Homework/cs5300/project-three/lexer/ExprLexer.java new file mode 100644 index 0000000..b9d760b --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ExprLexer.java @@ -0,0 +1,145 @@ +// Generated from ExprLexer.tokens by ANTLR 4.9.1 +package lexer; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class ExprLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + PLUS=1, MINUS=2, MULTIPLY=3, DIVIDE=4, OPAREN=5, CPAREN=6, FLOAT=7, INT=8, + IDENTIFIER=9, COMMENT=10, WS=11; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "OPAREN", "CPAREN", "FLOAT", "INT", + "IDENTIFIER", "COMMENT", "WS", "LETTER", "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'+'", "'-'", "'*'", "'/'", "'('", "')'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "OPAREN", "CPAREN", "FLOAT", + "INT", "IDENTIFIER", "COMMENT", "WS" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public ExprLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "ExprLexer.tokens"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\rh\b\1\4\2\t\2\4"+ + "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+ + "\13\4\f\t\f\4\r\t\r\4\16\t\16\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6"+ + "\3\7\3\7\3\b\6\b+\n\b\r\b\16\b,\3\b\3\b\6\b\61\n\b\r\b\16\b\62\3\t\6\t"+ + "\66\n\t\r\t\16\t\67\3\n\3\n\3\n\7\n=\n\n\f\n\16\n@\13\n\3\13\3\13\3\13"+ + "\3\13\7\13F\n\13\f\13\16\13I\13\13\3\13\5\13L\n\13\3\13\3\13\3\13\3\13"+ + "\3\13\7\13S\n\13\f\13\16\13V\13\13\3\13\3\13\5\13Z\n\13\3\13\3\13\3\f"+ + "\6\f_\n\f\r\f\16\f`\3\f\3\f\3\r\3\r\3\16\3\16\3T\2\17\3\3\5\4\7\5\t\6"+ + "\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\2\33\2\3\2\5\4\2\f\f\17\17\5\2"+ + "\13\f\16\17\"\"\4\2C\\c|\2o\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3"+ + "\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2"+ + "\2\25\3\2\2\2\2\27\3\2\2\2\3\35\3\2\2\2\5\37\3\2\2\2\7!\3\2\2\2\t#\3\2"+ + "\2\2\13%\3\2\2\2\r\'\3\2\2\2\17*\3\2\2\2\21\65\3\2\2\2\239\3\2\2\2\25"+ + "Y\3\2\2\2\27^\3\2\2\2\31d\3\2\2\2\33f\3\2\2\2\35\36\7-\2\2\36\4\3\2\2"+ + "\2\37 \7/\2\2 \6\3\2\2\2!\"\7,\2\2\"\b\3\2\2\2#$\7\61\2\2$\n\3\2\2\2%"+ + "&\7*\2\2&\f\3\2\2\2\'(\7+\2\2(\16\3\2\2\2)+\5\33\16\2*)\3\2\2\2+,\3\2"+ + "\2\2,*\3\2\2\2,-\3\2\2\2-.\3\2\2\2.\60\7\60\2\2/\61\5\33\16\2\60/\3\2"+ + "\2\2\61\62\3\2\2\2\62\60\3\2\2\2\62\63\3\2\2\2\63\20\3\2\2\2\64\66\5\33"+ + "\16\2\65\64\3\2\2\2\66\67\3\2\2\2\67\65\3\2\2\2\678\3\2\2\28\22\3\2\2"+ + "\29>\5\31\r\2:=\5\31\r\2;=\5\33\16\2<:\3\2\2\2<;\3\2\2\2=@\3\2\2\2><\3"+ + "\2\2\2>?\3\2\2\2?\24\3\2\2\2@>\3\2\2\2AB\7\61\2\2BC\7\61\2\2CG\3\2\2\2"+ + "DF\n\2\2\2ED\3\2\2\2FI\3\2\2\2GE\3\2\2\2GH\3\2\2\2HK\3\2\2\2IG\3\2\2\2"+ + "JL\7\17\2\2KJ\3\2\2\2KL\3\2\2\2LM\3\2\2\2MZ\7\f\2\2NO\7\61\2\2OP\7,\2"+ + "\2PT\3\2\2\2QS\13\2\2\2RQ\3\2\2\2SV\3\2\2\2TU\3\2\2\2TR\3\2\2\2UW\3\2"+ + "\2\2VT\3\2\2\2WX\7,\2\2XZ\7\61\2\2YA\3\2\2\2YN\3\2\2\2Z[\3\2\2\2[\\\b"+ + "\13\2\2\\\26\3\2\2\2]_\t\3\2\2^]\3\2\2\2_`\3\2\2\2`^\3\2\2\2`a\3\2\2\2"+ + "ab\3\2\2\2bc\b\f\2\2c\30\3\2\2\2de\t\4\2\2e\32\3\2\2\2fg\4\62;\2g\34\3"+ + "\2\2\2\r\2,\62\67<>GKTY`\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/ExprLexer.tokens b/Homework/cs5300/project-three/lexer/ExprLexer.tokens new file mode 100644 index 0000000..295e180 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ExprLexer.tokens @@ -0,0 +1,17 @@ +PLUS=1 +MINUS=2 +MULTIPLY=3 +DIVIDE=4 +OPAREN=5 +CPAREN=6 +FLOAT=7 +INT=8 +IDENTIFIER=9 +COMMENT=10 +WS=11 +'+'=1 +'-'=2 +'*'=3 +'/'=4 +'('=5 +')'=6 diff --git a/Homework/cs5300/project-three/lexer/ParenLexer.interp b/Homework/cs5300/project-three/lexer/ParenLexer.interp new file mode 100644 index 0000000..d973cba --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ParenLexer.interp @@ -0,0 +1,26 @@ +token literal names: +null +'(' +')' +null + +token symbolic names: +null +OPAREN +CPAREN +WS + +rule names: +OPAREN +CPAREN +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 5, 20, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 6, 4, 15, 10, 4, 13, 4, 14, 4, 16, 3, 4, 3, 4, 2, 2, 5, 3, 3, 5, 4, 7, 5, 3, 2, 3, 5, 2, 11, 12, 14, 15, 34, 34, 2, 20, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 3, 9, 3, 2, 2, 2, 5, 11, 3, 2, 2, 2, 7, 14, 3, 2, 2, 2, 9, 10, 7, 42, 2, 2, 10, 4, 3, 2, 2, 2, 11, 12, 7, 43, 2, 2, 12, 6, 3, 2, 2, 2, 13, 15, 9, 2, 2, 2, 14, 13, 3, 2, 2, 2, 15, 16, 3, 2, 2, 2, 16, 14, 3, 2, 2, 2, 16, 17, 3, 2, 2, 2, 17, 18, 3, 2, 2, 2, 18, 19, 8, 4, 2, 2, 19, 8, 3, 2, 2, 2, 4, 2, 16, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/ParenLexer.java b/Homework/cs5300/project-three/lexer/ParenLexer.java new file mode 100644 index 0000000..3ac9511 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ParenLexer.java @@ -0,0 +1,121 @@ +// Generated from ParenLexer.tokens by ANTLR 4.9.1 +package lexer; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class ParenLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + OPAREN=1, CPAREN=2, WS=3; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "OPAREN", "CPAREN", "WS" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'('", "')'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "OPAREN", "CPAREN", "WS" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public ParenLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "ParenLexer.tokens"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\5\24\b\1\4\2\t\2"+ + "\4\3\t\3\4\4\t\4\3\2\3\2\3\3\3\3\3\4\6\4\17\n\4\r\4\16\4\20\3\4\3\4\2"+ + "\2\5\3\3\5\4\7\5\3\2\3\5\2\13\f\16\17\"\"\2\24\2\3\3\2\2\2\2\5\3\2\2\2"+ + "\2\7\3\2\2\2\3\t\3\2\2\2\5\13\3\2\2\2\7\16\3\2\2\2\t\n\7*\2\2\n\4\3\2"+ + "\2\2\13\f\7+\2\2\f\6\3\2\2\2\r\17\t\2\2\2\16\r\3\2\2\2\17\20\3\2\2\2\20"+ + "\16\3\2\2\2\20\21\3\2\2\2\21\22\3\2\2\2\22\23\b\4\2\2\23\b\3\2\2\2\4\2"+ + "\20\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/ParenLexer.tokens b/Homework/cs5300/project-three/lexer/ParenLexer.tokens new file mode 100644 index 0000000..a29ddff --- /dev/null +++ b/Homework/cs5300/project-three/lexer/ParenLexer.tokens @@ -0,0 +1,5 @@ +OPAREN=1 +CPAREN=2 +WS=3 +'('=1 +')'=2 diff --git a/Homework/cs5300/project-three/lexer/SimpleLexer.interp b/Homework/cs5300/project-three/lexer/SimpleLexer.interp new file mode 100644 index 0000000..8d4c41c --- /dev/null +++ b/Homework/cs5300/project-three/lexer/SimpleLexer.interp @@ -0,0 +1,23 @@ +token literal names: +null +'x' +null + +token symbolic names: +null +X +WS + +rule names: +X +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 4, 16, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 3, 2, 3, 2, 3, 3, 6, 3, 11, 10, 3, 13, 3, 14, 3, 12, 3, 3, 3, 3, 2, 2, 4, 3, 3, 5, 4, 3, 2, 3, 5, 2, 11, 12, 14, 15, 34, 34, 2, 16, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 3, 7, 3, 2, 2, 2, 5, 10, 3, 2, 2, 2, 7, 8, 7, 122, 2, 2, 8, 4, 3, 2, 2, 2, 9, 11, 9, 2, 2, 2, 10, 9, 3, 2, 2, 2, 11, 12, 3, 2, 2, 2, 12, 10, 3, 2, 2, 2, 12, 13, 3, 2, 2, 2, 13, 14, 3, 2, 2, 2, 14, 15, 8, 3, 2, 2, 15, 6, 3, 2, 2, 2, 4, 2, 12, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/SimpleLexer.java b/Homework/cs5300/project-three/lexer/SimpleLexer.java new file mode 100644 index 0000000..f6ce847 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/SimpleLexer.java @@ -0,0 +1,120 @@ +// Generated from SimpleLexer.tokens by ANTLR 4.9.1 +package lexer; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class SimpleLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + X=1, WS=2; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "X", "WS" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'x'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "X", "WS" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public SimpleLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "SimpleLexer.tokens"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\4\20\b\1\4\2\t\2"+ + "\4\3\t\3\3\2\3\2\3\3\6\3\13\n\3\r\3\16\3\f\3\3\3\3\2\2\4\3\3\5\4\3\2\3"+ + "\5\2\13\f\16\17\"\"\2\20\2\3\3\2\2\2\2\5\3\2\2\2\3\7\3\2\2\2\5\n\3\2\2"+ + "\2\7\b\7z\2\2\b\4\3\2\2\2\t\13\t\2\2\2\n\t\3\2\2\2\13\f\3\2\2\2\f\n\3"+ + "\2\2\2\f\r\3\2\2\2\r\16\3\2\2\2\16\17\b\3\2\2\17\6\3\2\2\2\4\2\f\3\b\2"+ + "\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/SimpleLexer.tokens b/Homework/cs5300/project-three/lexer/SimpleLexer.tokens new file mode 100644 index 0000000..dc07319 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/SimpleLexer.tokens @@ -0,0 +1,3 @@ +X=1 +WS=2 +'x'=1 diff --git a/Homework/cs5300/project-three/lexer/TinyLexer.interp b/Homework/cs5300/project-three/lexer/TinyLexer.interp new file mode 100644 index 0000000..a2db21e --- /dev/null +++ b/Homework/cs5300/project-three/lexer/TinyLexer.interp @@ -0,0 +1,79 @@ +token literal names: +null +'if' +'then' +'else' +'end' +'repeat' +'until' +'read' +'write' +'(' +')' +'+' +'-' +'*' +'/' +'<' +'=' +null +null +null +null + +token symbolic names: +null +IF +THEN +ELSE +END +REPEAT +UNTIL +READ +WRITE +OPAREN +CPAREN +PLUS +MINUS +MULTIPLY +DIVIDE +LT +EQUAL +NUM +ID +COMMENT +WS + +rule names: +IF +THEN +ELSE +END +REPEAT +UNTIL +READ +WRITE +OPAREN +CPAREN +PLUS +MINUS +MULTIPLY +DIVIDE +LT +EQUAL +NUM +ID +COMMENT +WS +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 22, 153, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 3, 16, 3, 16, 3, 17, 3, 17, 3, 18, 6, 18, 106, 10, 18, 13, 18, 14, 18, 107, 3, 19, 6, 19, 111, 10, 19, 13, 19, 14, 19, 112, 3, 20, 3, 20, 3, 20, 3, 20, 7, 20, 119, 10, 20, 12, 20, 14, 20, 122, 11, 20, 3, 20, 5, 20, 125, 10, 20, 3, 20, 3, 20, 3, 20, 3, 20, 3, 20, 7, 20, 132, 10, 20, 12, 20, 14, 20, 135, 11, 20, 3, 20, 3, 20, 5, 20, 139, 10, 20, 3, 20, 3, 20, 3, 21, 6, 21, 144, 10, 21, 13, 21, 14, 21, 145, 3, 21, 3, 21, 3, 22, 3, 22, 3, 23, 3, 23, 3, 133, 2, 24, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35, 19, 37, 20, 39, 21, 41, 22, 43, 2, 45, 2, 3, 2, 5, 4, 2, 12, 12, 15, 15, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 67, 92, 99, 124, 2, 157, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3, 2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 3, 47, 3, 2, 2, 2, 5, 50, 3, 2, 2, 2, 7, 55, 3, 2, 2, 2, 9, 60, 3, 2, 2, 2, 11, 64, 3, 2, 2, 2, 13, 71, 3, 2, 2, 2, 15, 77, 3, 2, 2, 2, 17, 82, 3, 2, 2, 2, 19, 88, 3, 2, 2, 2, 21, 90, 3, 2, 2, 2, 23, 92, 3, 2, 2, 2, 25, 94, 3, 2, 2, 2, 27, 96, 3, 2, 2, 2, 29, 98, 3, 2, 2, 2, 31, 100, 3, 2, 2, 2, 33, 102, 3, 2, 2, 2, 35, 105, 3, 2, 2, 2, 37, 110, 3, 2, 2, 2, 39, 138, 3, 2, 2, 2, 41, 143, 3, 2, 2, 2, 43, 149, 3, 2, 2, 2, 45, 151, 3, 2, 2, 2, 47, 48, 7, 107, 2, 2, 48, 49, 7, 104, 2, 2, 49, 4, 3, 2, 2, 2, 50, 51, 7, 118, 2, 2, 51, 52, 7, 106, 2, 2, 52, 53, 7, 103, 2, 2, 53, 54, 7, 112, 2, 2, 54, 6, 3, 2, 2, 2, 55, 56, 7, 103, 2, 2, 56, 57, 7, 110, 2, 2, 57, 58, 7, 117, 2, 2, 58, 59, 7, 103, 2, 2, 59, 8, 3, 2, 2, 2, 60, 61, 7, 103, 2, 2, 61, 62, 7, 112, 2, 2, 62, 63, 7, 102, 2, 2, 63, 10, 3, 2, 2, 2, 64, 65, 7, 116, 2, 2, 65, 66, 7, 103, 2, 2, 66, 67, 7, 114, 2, 2, 67, 68, 7, 103, 2, 2, 68, 69, 7, 99, 2, 2, 69, 70, 7, 118, 2, 2, 70, 12, 3, 2, 2, 2, 71, 72, 7, 119, 2, 2, 72, 73, 7, 112, 2, 2, 73, 74, 7, 118, 2, 2, 74, 75, 7, 107, 2, 2, 75, 76, 7, 110, 2, 2, 76, 14, 3, 2, 2, 2, 77, 78, 7, 116, 2, 2, 78, 79, 7, 103, 2, 2, 79, 80, 7, 99, 2, 2, 80, 81, 7, 102, 2, 2, 81, 16, 3, 2, 2, 2, 82, 83, 7, 121, 2, 2, 83, 84, 7, 116, 2, 2, 84, 85, 7, 107, 2, 2, 85, 86, 7, 118, 2, 2, 86, 87, 7, 103, 2, 2, 87, 18, 3, 2, 2, 2, 88, 89, 7, 42, 2, 2, 89, 20, 3, 2, 2, 2, 90, 91, 7, 43, 2, 2, 91, 22, 3, 2, 2, 2, 92, 93, 7, 45, 2, 2, 93, 24, 3, 2, 2, 2, 94, 95, 7, 47, 2, 2, 95, 26, 3, 2, 2, 2, 96, 97, 7, 44, 2, 2, 97, 28, 3, 2, 2, 2, 98, 99, 7, 49, 2, 2, 99, 30, 3, 2, 2, 2, 100, 101, 7, 62, 2, 2, 101, 32, 3, 2, 2, 2, 102, 103, 7, 63, 2, 2, 103, 34, 3, 2, 2, 2, 104, 106, 5, 45, 23, 2, 105, 104, 3, 2, 2, 2, 106, 107, 3, 2, 2, 2, 107, 105, 3, 2, 2, 2, 107, 108, 3, 2, 2, 2, 108, 36, 3, 2, 2, 2, 109, 111, 5, 43, 22, 2, 110, 109, 3, 2, 2, 2, 111, 112, 3, 2, 2, 2, 112, 110, 3, 2, 2, 2, 112, 113, 3, 2, 2, 2, 113, 38, 3, 2, 2, 2, 114, 115, 7, 49, 2, 2, 115, 116, 7, 49, 2, 2, 116, 120, 3, 2, 2, 2, 117, 119, 10, 2, 2, 2, 118, 117, 3, 2, 2, 2, 119, 122, 3, 2, 2, 2, 120, 118, 3, 2, 2, 2, 120, 121, 3, 2, 2, 2, 121, 124, 3, 2, 2, 2, 122, 120, 3, 2, 2, 2, 123, 125, 7, 15, 2, 2, 124, 123, 3, 2, 2, 2, 124, 125, 3, 2, 2, 2, 125, 126, 3, 2, 2, 2, 126, 139, 7, 12, 2, 2, 127, 128, 7, 49, 2, 2, 128, 129, 7, 44, 2, 2, 129, 133, 3, 2, 2, 2, 130, 132, 11, 2, 2, 2, 131, 130, 3, 2, 2, 2, 132, 135, 3, 2, 2, 2, 133, 134, 3, 2, 2, 2, 133, 131, 3, 2, 2, 2, 134, 136, 3, 2, 2, 2, 135, 133, 3, 2, 2, 2, 136, 137, 7, 44, 2, 2, 137, 139, 7, 49, 2, 2, 138, 114, 3, 2, 2, 2, 138, 127, 3, 2, 2, 2, 139, 140, 3, 2, 2, 2, 140, 141, 8, 20, 2, 2, 141, 40, 3, 2, 2, 2, 142, 144, 9, 3, 2, 2, 143, 142, 3, 2, 2, 2, 144, 145, 3, 2, 2, 2, 145, 143, 3, 2, 2, 2, 145, 146, 3, 2, 2, 2, 146, 147, 3, 2, 2, 2, 147, 148, 8, 21, 2, 2, 148, 42, 3, 2, 2, 2, 149, 150, 9, 4, 2, 2, 150, 44, 3, 2, 2, 2, 151, 152, 4, 50, 59, 2, 152, 46, 3, 2, 2, 2, 10, 2, 107, 112, 120, 124, 133, 138, 145, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/TinyLexer.java b/Homework/cs5300/project-three/lexer/TinyLexer.java new file mode 100644 index 0000000..f1213fb --- /dev/null +++ b/Homework/cs5300/project-three/lexer/TinyLexer.java @@ -0,0 +1,164 @@ +// Generated from TinyLexer.tokens by ANTLR 4.9.1 +package lexer; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class TinyLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + IF=1, THEN=2, ELSE=3, END=4, REPEAT=5, UNTIL=6, READ=7, WRITE=8, OPAREN=9, + CPAREN=10, PLUS=11, MINUS=12, MULTIPLY=13, DIVIDE=14, LT=15, EQUAL=16, + NUM=17, ID=18, COMMENT=19, WS=20; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "IF", "THEN", "ELSE", "END", "REPEAT", "UNTIL", "READ", "WRITE", "OPAREN", + "CPAREN", "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "LT", "EQUAL", "NUM", + "ID", "COMMENT", "WS", "LETTER", "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'if'", "'then'", "'else'", "'end'", "'repeat'", "'until'", "'read'", + "'write'", "'('", "')'", "'+'", "'-'", "'*'", "'/'", "'<'", "'='" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "IF", "THEN", "ELSE", "END", "REPEAT", "UNTIL", "READ", "WRITE", + "OPAREN", "CPAREN", "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "LT", "EQUAL", + "NUM", "ID", "COMMENT", "WS" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = "<INVALID>"; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public TinyLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "TinyLexer.tokens"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\26\u0099\b\1\4\2"+ + "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+ + "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\3\2\3\2\3\2\3"+ + "\3\3\3\3\3\3\3\3\3\3\4\3\4\3\4\3\4\3\4\3\5\3\5\3\5\3\5\3\6\3\6\3\6\3\6"+ + "\3\6\3\6\3\6\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\t\3\t\3\t\3"+ + "\t\3\t\3\t\3\n\3\n\3\13\3\13\3\f\3\f\3\r\3\r\3\16\3\16\3\17\3\17\3\20"+ + "\3\20\3\21\3\21\3\22\6\22j\n\22\r\22\16\22k\3\23\6\23o\n\23\r\23\16\23"+ + "p\3\24\3\24\3\24\3\24\7\24w\n\24\f\24\16\24z\13\24\3\24\5\24}\n\24\3\24"+ + "\3\24\3\24\3\24\3\24\7\24\u0084\n\24\f\24\16\24\u0087\13\24\3\24\3\24"+ + "\5\24\u008b\n\24\3\24\3\24\3\25\6\25\u0090\n\25\r\25\16\25\u0091\3\25"+ + "\3\25\3\26\3\26\3\27\3\27\3\u0085\2\30\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21"+ + "\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\2-\2"+ + "\3\2\5\4\2\f\f\17\17\5\2\13\f\16\17\"\"\4\2C\\c|\2\u009d\2\3\3\2\2\2\2"+ + "\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2"+ + "\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2"+ + "\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2"+ + "\2\'\3\2\2\2\2)\3\2\2\2\3/\3\2\2\2\5\62\3\2\2\2\7\67\3\2\2\2\t<\3\2\2"+ + "\2\13@\3\2\2\2\rG\3\2\2\2\17M\3\2\2\2\21R\3\2\2\2\23X\3\2\2\2\25Z\3\2"+ + "\2\2\27\\\3\2\2\2\31^\3\2\2\2\33`\3\2\2\2\35b\3\2\2\2\37d\3\2\2\2!f\3"+ + "\2\2\2#i\3\2\2\2%n\3\2\2\2\'\u008a\3\2\2\2)\u008f\3\2\2\2+\u0095\3\2\2"+ + "\2-\u0097\3\2\2\2/\60\7k\2\2\60\61\7h\2\2\61\4\3\2\2\2\62\63\7v\2\2\63"+ + "\64\7j\2\2\64\65\7g\2\2\65\66\7p\2\2\66\6\3\2\2\2\678\7g\2\289\7n\2\2"+ + "9:\7u\2\2:;\7g\2\2;\b\3\2\2\2<=\7g\2\2=>\7p\2\2>?\7f\2\2?\n\3\2\2\2@A"+ + "\7t\2\2AB\7g\2\2BC\7r\2\2CD\7g\2\2DE\7c\2\2EF\7v\2\2F\f\3\2\2\2GH\7w\2"+ + "\2HI\7p\2\2IJ\7v\2\2JK\7k\2\2KL\7n\2\2L\16\3\2\2\2MN\7t\2\2NO\7g\2\2O"+ + "P\7c\2\2PQ\7f\2\2Q\20\3\2\2\2RS\7y\2\2ST\7t\2\2TU\7k\2\2UV\7v\2\2VW\7"+ + "g\2\2W\22\3\2\2\2XY\7*\2\2Y\24\3\2\2\2Z[\7+\2\2[\26\3\2\2\2\\]\7-\2\2"+ + "]\30\3\2\2\2^_\7/\2\2_\32\3\2\2\2`a\7,\2\2a\34\3\2\2\2bc\7\61\2\2c\36"+ + "\3\2\2\2de\7>\2\2e \3\2\2\2fg\7?\2\2g\"\3\2\2\2hj\5-\27\2ih\3\2\2\2jk"+ + "\3\2\2\2ki\3\2\2\2kl\3\2\2\2l$\3\2\2\2mo\5+\26\2nm\3\2\2\2op\3\2\2\2p"+ + "n\3\2\2\2pq\3\2\2\2q&\3\2\2\2rs\7\61\2\2st\7\61\2\2tx\3\2\2\2uw\n\2\2"+ + "\2vu\3\2\2\2wz\3\2\2\2xv\3\2\2\2xy\3\2\2\2y|\3\2\2\2zx\3\2\2\2{}\7\17"+ + "\2\2|{\3\2\2\2|}\3\2\2\2}~\3\2\2\2~\u008b\7\f\2\2\177\u0080\7\61\2\2\u0080"+ + "\u0081\7,\2\2\u0081\u0085\3\2\2\2\u0082\u0084\13\2\2\2\u0083\u0082\3\2"+ + "\2\2\u0084\u0087\3\2\2\2\u0085\u0086\3\2\2\2\u0085\u0083\3\2\2\2\u0086"+ + "\u0088\3\2\2\2\u0087\u0085\3\2\2\2\u0088\u0089\7,\2\2\u0089\u008b\7\61"+ + "\2\2\u008ar\3\2\2\2\u008a\177\3\2\2\2\u008b\u008c\3\2\2\2\u008c\u008d"+ + "\b\24\2\2\u008d(\3\2\2\2\u008e\u0090\t\3\2\2\u008f\u008e\3\2\2\2\u0090"+ + "\u0091\3\2\2\2\u0091\u008f\3\2\2\2\u0091\u0092\3\2\2\2\u0092\u0093\3\2"+ + "\2\2\u0093\u0094\b\25\2\2\u0094*\3\2\2\2\u0095\u0096\t\4\2\2\u0096,\3"+ + "\2\2\2\u0097\u0098\4\62;\2\u0098.\3\2\2\2\n\2kpx|\u0085\u008a\u0091\3"+ + "\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/lexer/TinyLexer.tokens b/Homework/cs5300/project-three/lexer/TinyLexer.tokens new file mode 100644 index 0000000..2d6fdc7 --- /dev/null +++ b/Homework/cs5300/project-three/lexer/TinyLexer.tokens @@ -0,0 +1,36 @@ +IF=1 +THEN=2 +ELSE=3 +END=4 +REPEAT=5 +UNTIL=6 +READ=7 +WRITE=8 +OPAREN=9 +CPAREN=10 +PLUS=11 +MINUS=12 +MULTIPLY=13 +DIVIDE=14 +LT=15 +EQUAL=16 +NUM=17 +ID=18 +COMMENT=19 +WS=20 +'if'=1 +'then'=2 +'else'=3 +'end'=4 +'repeat'=5 +'until'=6 +'read'=7 +'write'=8 +'('=9 +')'=10 +'+'=11 +'-'=12 +'*'=13 +'/'=14 +'<'=15 +'='=16 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/antlr-4.9.1-complete.jar b/Homework/cs5300/project-three/out/production/p3-parser/antlr-4.9.1-complete.jar Binary files differnew file mode 100644 index 0000000..1bb7e8c --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/antlr-4.9.1-complete.jar diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/Grammar.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/Grammar.class Binary files differnew file mode 100644 index 0000000..34912da --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/Grammar.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarBaseListener.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarBaseListener.class Binary files differnew file mode 100644 index 0000000..b3e115e --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarBaseListener.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarLexer.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarLexer.class Binary files differnew file mode 100644 index 0000000..9fc843f --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarLexer.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListener.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListener.class Binary files differnew file mode 100644 index 0000000..fa64aeb --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListener.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListenerImpl.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListenerImpl.class Binary files differnew file mode 100644 index 0000000..2b52242 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarListenerImpl.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$GoalContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$GoalContext.class Binary files differnew file mode 100644 index 0000000..25e5c2e --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$GoalContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$LhsContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$LhsContext.class Binary files differnew file mode 100644 index 0000000..c5580c2 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$LhsContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$NameContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$NameContext.class Binary files differnew file mode 100644 index 0000000..32cd8ee --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$NameContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Or_listContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Or_listContext.class Binary files differnew file mode 100644 index 0000000..82b869a --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Or_listContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$RhsContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$RhsContext.class Binary files differnew file mode 100644 index 0000000..90dd7d3 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$RhsContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Rule_listContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Rule_listContext.class Binary files differnew file mode 100644 index 0000000..7501d1f --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Rule_listContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$SymbolContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$SymbolContext.class Binary files differnew file mode 100644 index 0000000..ed3da97 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$SymbolContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Symbol_listContext.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Symbol_listContext.class Binary files differnew file mode 100644 index 0000000..34d9e0c --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser$Symbol_listContext.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser.class b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser.class Binary files differnew file mode 100644 index 0000000..6f2d6ae --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/cfgparser/GrammarParser.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/Expr.cfg b/Homework/cs5300/project-three/out/production/p3-parser/data/Expr.cfg new file mode 100644 index 0000000..7493e3e --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/Expr.cfg @@ -0,0 +1,25 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ExprLexer.tokens. + +grammar Expr; + +goal: expr; + +expr: expr PLUS term + | expr MINUS term + | term + ; + +term : term MULTIPLY factor + | term DIVIDE factor + | factor + ; + +factor: OPAREN expr CPAREN + | INT + | FLOAT + | IDENTIFIER + ;
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/ExprLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/data/ExprLexer.tokens new file mode 100644 index 0000000..ccb798a --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/ExprLexer.tokens @@ -0,0 +1,39 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Expr.cfg. + +lexer grammar ExprLexer; + +PLUS: '+' ; +MINUS: '-' ; +MULTIPLY: '*' ; +DIVIDE: '/' ; +OPAREN: '(' ; +CPAREN: ')' ; + +FLOAT + : DIGIT+ '.' DIGIT+ + ; + +INT + : DIGIT+ + ; + +IDENTIFIER + : LETTER (LETTER | DIGIT)* + ; + +COMMENT + : ( '//' ~[\r\n]* '\r'? '\n' + | '/*' .*? '*/' + ) -> skip + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; + +fragment LETTER : ('a'..'z' | 'A'..'Z'); +fragment DIGIT : ('0'..'9');
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/Paren.cfg b/Homework/cs5300/project-three/out/production/p3-parser/data/Paren.cfg new file mode 100644 index 0000000..184b11c --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/Paren.cfg @@ -0,0 +1,17 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ParenLexer.tokens. + +grammar Paren; + +goal: list; + +list: list pair + | pair + ; + +pair: OPAREN list CPAREN + | OPAREN CPAREN + ; diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/ParenLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/data/ParenLexer.tokens new file mode 100644 index 0000000..51e5258 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/ParenLexer.tokens @@ -0,0 +1,19 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Paren.cfg. + +lexer grammar ParenLexer; + +OPAREN + : '(' + ; + +CPAREN + : ')' + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/Simple.cfg b/Homework/cs5300/project-three/out/production/p3-parser/data/Simple.cfg new file mode 100644 index 0000000..80c1e18 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/Simple.cfg @@ -0,0 +1,13 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file contains an expression grammar that uses tokens described in +// ParenLexer.tokens. + +grammar Simple; + +G: N; + +N: N X + | X + ; diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/SimpleLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/data/SimpleLexer.tokens new file mode 100644 index 0000000..64ec3c1 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/SimpleLexer.tokens @@ -0,0 +1,15 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Simple.cfg. + +lexer grammar SimpleLexer; + +X + : 'x' + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/Tiny.cfg b/Homework/cs5300/project-three/out/production/p3-parser/data/Tiny.cfg new file mode 100644 index 0000000..a7a28a3 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/Tiny.cfg @@ -0,0 +1,19 @@ +// +// WRITE A CORRECT GRAMMAR FOR THE TINY LANGUAGE IN THIS FILE. +// +// The grammar is described in data/tinyGrammar.pdf. Do not use dashes +// in your non-terminal names. I recommend you use mixed case naming for your +// non-terminals instead (e.g. stmtSeq). +// +// Do not put any string literals in this grammar (e.g. "="). Use only token +// names given in TinyLexer.tokens. +// +// This file contains an expression grammar that uses tokens described in +// TinyLexer.tokens. +// +// NOTE ONE ERROR in the pdf: the second production rule should read: +// stmt-seq -> stmt-seq stmt | stmt + +grammar Tiny; + +goal: program; diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/TinyLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/data/TinyLexer.tokens new file mode 100644 index 0000000..ad6b2a7 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/TinyLexer.tokens @@ -0,0 +1,41 @@ +// +// DO NOT MODIFY THIS FILE. +// +// This file describes the tokens that will be available to the parser +// that uses the grammar described in Tiny.cfg. + +lexer grammar TinyLexer; + +IF : 'if' ; +THEN : 'then' ; +ELSE : 'else' ; +END : 'end' ; +REPEAT : 'repeat' ; +UNTIL : 'until' ; +READ : 'read' ; +WRITE : 'write' ; +OPAREN : '(' ; +CPAREN : ')' ; + +PLUS: '+' ; +MINUS: '-' ; +MULTIPLY: '*' ; +DIVIDE: '/' ; +LT: '<' ; +EQUAL: '=' ; + +NUM : DIGIT+ ; +ID : LETTER+ ; + +COMMENT + : ( '//' ~[\r\n]* '\r'? '\n' + | '/*' .*? '*/' + ) -> skip + ; + +WS + : (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip + ; + +fragment LETTER : ('a'..'z' | 'A'..'Z'); +fragment DIGIT : ('0'..'9');
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr1.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr1.dat new file mode 100644 index 0000000..846e7d3 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr1.dat @@ -0,0 +1 @@ +3 + 4 * 5 / 8
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr2.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr2.dat new file mode 100644 index 0000000..8e0c0b7 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr2.dat @@ -0,0 +1 @@ +3 + + 42 * 34
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr3.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr3.dat new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr3.dat @@ -0,0 +1 @@ +3 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr4.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr4.dat new file mode 100644 index 0000000..84a357a --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr4.dat @@ -0,0 +1 @@ +(a + b) * ((c-def)/43)
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr5.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr5.dat new file mode 100644 index 0000000..c2c433b --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr5.dat @@ -0,0 +1 @@ +(a + b) * ((c-d)/43 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/expr6.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/expr6.dat new file mode 100644 index 0000000..f46d387 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/expr6.dat @@ -0,0 +1 @@ +(
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/paren0.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/paren0.dat new file mode 100644 index 0000000..dd626a0 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/paren0.dat @@ -0,0 +1 @@ +()
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/paren1.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/paren1.dat new file mode 100644 index 0000000..f7e9493 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/paren1.dat @@ -0,0 +1,3 @@ +(( +))( +)(((()))()(()))
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/paren2.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/paren2.dat new file mode 100644 index 0000000..4f879eb --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/paren2.dat @@ -0,0 +1,3 @@ +( +))( +)
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/paren3.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/paren3.dat new file mode 100644 index 0000000..e4a2fe8 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/paren3.dat @@ -0,0 +1 @@ +)()( diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/paren4.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/paren4.dat new file mode 100644 index 0000000..3077d74 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/paren4.dat @@ -0,0 +1 @@ +()()(()()) diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/tiny1.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny1.dat new file mode 100644 index 0000000..7f9078f --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny1.dat @@ -0,0 +1,3 @@ +a = 3 +b = 4 +c = a + b diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/tiny2.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny2.dat new file mode 100644 index 0000000..09195b8 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny2.dat @@ -0,0 +1,7 @@ +a = 3 +b = 4 +if a = b then + write (a + b * (c - d)) +else + write a +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/tiny3.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny3.dat new file mode 100644 index 0000000..768addd --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny3.dat @@ -0,0 +1,4 @@ +a = 3 +if a = then + write a +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/tiny4.dat b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny4.dat new file mode 100644 index 0000000..1d66283 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/tiny4.dat @@ -0,0 +1,4 @@ +a = 3 +if (a = b) then + write a = 3) +end
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/data/tinyGrammar.pdf b/Homework/cs5300/project-three/out/production/p3-parser/data/tinyGrammar.pdf Binary files differnew file mode 100644 index 0000000..855d45b --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/data/tinyGrammar.pdf diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.class b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.class Binary files differnew file mode 100644 index 0000000..34e7d89 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.interp b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.interp new file mode 100644 index 0000000..c9ec8c0 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.interp @@ -0,0 +1,52 @@ +token literal names: +null +'+' +'-' +'*' +'/' +'(' +')' +null +null +null +null +null + +token symbolic names: +null +PLUS +MINUS +MULTIPLY +DIVIDE +OPAREN +CPAREN +FLOAT +INT +IDENTIFIER +COMMENT +WS + +rule names: +PLUS +MINUS +MULTIPLY +DIVIDE +OPAREN +CPAREN +FLOAT +INT +IDENTIFIER +COMMENT +WS +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 13, 104, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 6, 8, 43, 10, 8, 13, 8, 14, 8, 44, 3, 8, 3, 8, 6, 8, 49, 10, 8, 13, 8, 14, 8, 50, 3, 9, 6, 9, 54, 10, 9, 13, 9, 14, 9, 55, 3, 10, 3, 10, 3, 10, 7, 10, 61, 10, 10, 12, 10, 14, 10, 64, 11, 10, 3, 11, 3, 11, 3, 11, 3, 11, 7, 11, 70, 10, 11, 12, 11, 14, 11, 73, 11, 11, 3, 11, 5, 11, 76, 10, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 7, 11, 83, 10, 11, 12, 11, 14, 11, 86, 11, 11, 3, 11, 3, 11, 5, 11, 90, 10, 11, 3, 11, 3, 11, 3, 12, 6, 12, 95, 10, 12, 13, 12, 14, 12, 96, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 84, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 2, 27, 2, 3, 2, 5, 4, 2, 12, 12, 15, 15, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 67, 92, 99, 124, 2, 111, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 31, 3, 2, 2, 2, 7, 33, 3, 2, 2, 2, 9, 35, 3, 2, 2, 2, 11, 37, 3, 2, 2, 2, 13, 39, 3, 2, 2, 2, 15, 42, 3, 2, 2, 2, 17, 53, 3, 2, 2, 2, 19, 57, 3, 2, 2, 2, 21, 89, 3, 2, 2, 2, 23, 94, 3, 2, 2, 2, 25, 100, 3, 2, 2, 2, 27, 102, 3, 2, 2, 2, 29, 30, 7, 45, 2, 2, 30, 4, 3, 2, 2, 2, 31, 32, 7, 47, 2, 2, 32, 6, 3, 2, 2, 2, 33, 34, 7, 44, 2, 2, 34, 8, 3, 2, 2, 2, 35, 36, 7, 49, 2, 2, 36, 10, 3, 2, 2, 2, 37, 38, 7, 42, 2, 2, 38, 12, 3, 2, 2, 2, 39, 40, 7, 43, 2, 2, 40, 14, 3, 2, 2, 2, 41, 43, 5, 27, 14, 2, 42, 41, 3, 2, 2, 2, 43, 44, 3, 2, 2, 2, 44, 42, 3, 2, 2, 2, 44, 45, 3, 2, 2, 2, 45, 46, 3, 2, 2, 2, 46, 48, 7, 48, 2, 2, 47, 49, 5, 27, 14, 2, 48, 47, 3, 2, 2, 2, 49, 50, 3, 2, 2, 2, 50, 48, 3, 2, 2, 2, 50, 51, 3, 2, 2, 2, 51, 16, 3, 2, 2, 2, 52, 54, 5, 27, 14, 2, 53, 52, 3, 2, 2, 2, 54, 55, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 55, 56, 3, 2, 2, 2, 56, 18, 3, 2, 2, 2, 57, 62, 5, 25, 13, 2, 58, 61, 5, 25, 13, 2, 59, 61, 5, 27, 14, 2, 60, 58, 3, 2, 2, 2, 60, 59, 3, 2, 2, 2, 61, 64, 3, 2, 2, 2, 62, 60, 3, 2, 2, 2, 62, 63, 3, 2, 2, 2, 63, 20, 3, 2, 2, 2, 64, 62, 3, 2, 2, 2, 65, 66, 7, 49, 2, 2, 66, 67, 7, 49, 2, 2, 67, 71, 3, 2, 2, 2, 68, 70, 10, 2, 2, 2, 69, 68, 3, 2, 2, 2, 70, 73, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 72, 3, 2, 2, 2, 72, 75, 3, 2, 2, 2, 73, 71, 3, 2, 2, 2, 74, 76, 7, 15, 2, 2, 75, 74, 3, 2, 2, 2, 75, 76, 3, 2, 2, 2, 76, 77, 3, 2, 2, 2, 77, 90, 7, 12, 2, 2, 78, 79, 7, 49, 2, 2, 79, 80, 7, 44, 2, 2, 80, 84, 3, 2, 2, 2, 81, 83, 11, 2, 2, 2, 82, 81, 3, 2, 2, 2, 83, 86, 3, 2, 2, 2, 84, 85, 3, 2, 2, 2, 84, 82, 3, 2, 2, 2, 85, 87, 3, 2, 2, 2, 86, 84, 3, 2, 2, 2, 87, 88, 7, 44, 2, 2, 88, 90, 7, 49, 2, 2, 89, 65, 3, 2, 2, 2, 89, 78, 3, 2, 2, 2, 90, 91, 3, 2, 2, 2, 91, 92, 8, 11, 2, 2, 92, 22, 3, 2, 2, 2, 93, 95, 9, 3, 2, 2, 94, 93, 3, 2, 2, 2, 95, 96, 3, 2, 2, 2, 96, 94, 3, 2, 2, 2, 96, 97, 3, 2, 2, 2, 97, 98, 3, 2, 2, 2, 98, 99, 8, 12, 2, 2, 99, 24, 3, 2, 2, 2, 100, 101, 9, 4, 2, 2, 101, 26, 3, 2, 2, 2, 102, 103, 4, 50, 59, 2, 103, 28, 3, 2, 2, 2, 13, 2, 44, 50, 55, 60, 62, 71, 75, 84, 89, 96, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.tokens new file mode 100644 index 0000000..295e180 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ExprLexer.tokens @@ -0,0 +1,17 @@ +PLUS=1 +MINUS=2 +MULTIPLY=3 +DIVIDE=4 +OPAREN=5 +CPAREN=6 +FLOAT=7 +INT=8 +IDENTIFIER=9 +COMMENT=10 +WS=11 +'+'=1 +'-'=2 +'*'=3 +'/'=4 +'('=5 +')'=6 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.class b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.class Binary files differnew file mode 100644 index 0000000..f499161 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.interp b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.interp new file mode 100644 index 0000000..d973cba --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.interp @@ -0,0 +1,26 @@ +token literal names: +null +'(' +')' +null + +token symbolic names: +null +OPAREN +CPAREN +WS + +rule names: +OPAREN +CPAREN +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 5, 20, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 6, 4, 15, 10, 4, 13, 4, 14, 4, 16, 3, 4, 3, 4, 2, 2, 5, 3, 3, 5, 4, 7, 5, 3, 2, 3, 5, 2, 11, 12, 14, 15, 34, 34, 2, 20, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 3, 9, 3, 2, 2, 2, 5, 11, 3, 2, 2, 2, 7, 14, 3, 2, 2, 2, 9, 10, 7, 42, 2, 2, 10, 4, 3, 2, 2, 2, 11, 12, 7, 43, 2, 2, 12, 6, 3, 2, 2, 2, 13, 15, 9, 2, 2, 2, 14, 13, 3, 2, 2, 2, 15, 16, 3, 2, 2, 2, 16, 14, 3, 2, 2, 2, 16, 17, 3, 2, 2, 2, 17, 18, 3, 2, 2, 2, 18, 19, 8, 4, 2, 2, 19, 8, 3, 2, 2, 2, 4, 2, 16, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.tokens new file mode 100644 index 0000000..a29ddff --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/ParenLexer.tokens @@ -0,0 +1,5 @@ +OPAREN=1 +CPAREN=2 +WS=3 +'('=1 +')'=2 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.class b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.class Binary files differnew file mode 100644 index 0000000..d9ec81d --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.interp b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.interp new file mode 100644 index 0000000..8d4c41c --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.interp @@ -0,0 +1,23 @@ +token literal names: +null +'x' +null + +token symbolic names: +null +X +WS + +rule names: +X +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 4, 16, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 3, 2, 3, 2, 3, 3, 6, 3, 11, 10, 3, 13, 3, 14, 3, 12, 3, 3, 3, 3, 2, 2, 4, 3, 3, 5, 4, 3, 2, 3, 5, 2, 11, 12, 14, 15, 34, 34, 2, 16, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 3, 7, 3, 2, 2, 2, 5, 10, 3, 2, 2, 2, 7, 8, 7, 122, 2, 2, 8, 4, 3, 2, 2, 2, 9, 11, 9, 2, 2, 2, 10, 9, 3, 2, 2, 2, 11, 12, 3, 2, 2, 2, 12, 10, 3, 2, 2, 2, 12, 13, 3, 2, 2, 2, 13, 14, 3, 2, 2, 2, 14, 15, 8, 3, 2, 2, 15, 6, 3, 2, 2, 2, 4, 2, 12, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.tokens new file mode 100644 index 0000000..dc07319 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/SimpleLexer.tokens @@ -0,0 +1,3 @@ +X=1 +WS=2 +'x'=1 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.class b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.class Binary files differnew file mode 100644 index 0000000..ce45364 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.interp b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.interp new file mode 100644 index 0000000..a2db21e --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.interp @@ -0,0 +1,79 @@ +token literal names: +null +'if' +'then' +'else' +'end' +'repeat' +'until' +'read' +'write' +'(' +')' +'+' +'-' +'*' +'/' +'<' +'=' +null +null +null +null + +token symbolic names: +null +IF +THEN +ELSE +END +REPEAT +UNTIL +READ +WRITE +OPAREN +CPAREN +PLUS +MINUS +MULTIPLY +DIVIDE +LT +EQUAL +NUM +ID +COMMENT +WS + +rule names: +IF +THEN +ELSE +END +REPEAT +UNTIL +READ +WRITE +OPAREN +CPAREN +PLUS +MINUS +MULTIPLY +DIVIDE +LT +EQUAL +NUM +ID +COMMENT +WS +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 22, 153, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 3, 16, 3, 16, 3, 17, 3, 17, 3, 18, 6, 18, 106, 10, 18, 13, 18, 14, 18, 107, 3, 19, 6, 19, 111, 10, 19, 13, 19, 14, 19, 112, 3, 20, 3, 20, 3, 20, 3, 20, 7, 20, 119, 10, 20, 12, 20, 14, 20, 122, 11, 20, 3, 20, 5, 20, 125, 10, 20, 3, 20, 3, 20, 3, 20, 3, 20, 3, 20, 7, 20, 132, 10, 20, 12, 20, 14, 20, 135, 11, 20, 3, 20, 3, 20, 5, 20, 139, 10, 20, 3, 20, 3, 20, 3, 21, 6, 21, 144, 10, 21, 13, 21, 14, 21, 145, 3, 21, 3, 21, 3, 22, 3, 22, 3, 23, 3, 23, 3, 133, 2, 24, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35, 19, 37, 20, 39, 21, 41, 22, 43, 2, 45, 2, 3, 2, 5, 4, 2, 12, 12, 15, 15, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 67, 92, 99, 124, 2, 157, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3, 2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 3, 47, 3, 2, 2, 2, 5, 50, 3, 2, 2, 2, 7, 55, 3, 2, 2, 2, 9, 60, 3, 2, 2, 2, 11, 64, 3, 2, 2, 2, 13, 71, 3, 2, 2, 2, 15, 77, 3, 2, 2, 2, 17, 82, 3, 2, 2, 2, 19, 88, 3, 2, 2, 2, 21, 90, 3, 2, 2, 2, 23, 92, 3, 2, 2, 2, 25, 94, 3, 2, 2, 2, 27, 96, 3, 2, 2, 2, 29, 98, 3, 2, 2, 2, 31, 100, 3, 2, 2, 2, 33, 102, 3, 2, 2, 2, 35, 105, 3, 2, 2, 2, 37, 110, 3, 2, 2, 2, 39, 138, 3, 2, 2, 2, 41, 143, 3, 2, 2, 2, 43, 149, 3, 2, 2, 2, 45, 151, 3, 2, 2, 2, 47, 48, 7, 107, 2, 2, 48, 49, 7, 104, 2, 2, 49, 4, 3, 2, 2, 2, 50, 51, 7, 118, 2, 2, 51, 52, 7, 106, 2, 2, 52, 53, 7, 103, 2, 2, 53, 54, 7, 112, 2, 2, 54, 6, 3, 2, 2, 2, 55, 56, 7, 103, 2, 2, 56, 57, 7, 110, 2, 2, 57, 58, 7, 117, 2, 2, 58, 59, 7, 103, 2, 2, 59, 8, 3, 2, 2, 2, 60, 61, 7, 103, 2, 2, 61, 62, 7, 112, 2, 2, 62, 63, 7, 102, 2, 2, 63, 10, 3, 2, 2, 2, 64, 65, 7, 116, 2, 2, 65, 66, 7, 103, 2, 2, 66, 67, 7, 114, 2, 2, 67, 68, 7, 103, 2, 2, 68, 69, 7, 99, 2, 2, 69, 70, 7, 118, 2, 2, 70, 12, 3, 2, 2, 2, 71, 72, 7, 119, 2, 2, 72, 73, 7, 112, 2, 2, 73, 74, 7, 118, 2, 2, 74, 75, 7, 107, 2, 2, 75, 76, 7, 110, 2, 2, 76, 14, 3, 2, 2, 2, 77, 78, 7, 116, 2, 2, 78, 79, 7, 103, 2, 2, 79, 80, 7, 99, 2, 2, 80, 81, 7, 102, 2, 2, 81, 16, 3, 2, 2, 2, 82, 83, 7, 121, 2, 2, 83, 84, 7, 116, 2, 2, 84, 85, 7, 107, 2, 2, 85, 86, 7, 118, 2, 2, 86, 87, 7, 103, 2, 2, 87, 18, 3, 2, 2, 2, 88, 89, 7, 42, 2, 2, 89, 20, 3, 2, 2, 2, 90, 91, 7, 43, 2, 2, 91, 22, 3, 2, 2, 2, 92, 93, 7, 45, 2, 2, 93, 24, 3, 2, 2, 2, 94, 95, 7, 47, 2, 2, 95, 26, 3, 2, 2, 2, 96, 97, 7, 44, 2, 2, 97, 28, 3, 2, 2, 2, 98, 99, 7, 49, 2, 2, 99, 30, 3, 2, 2, 2, 100, 101, 7, 62, 2, 2, 101, 32, 3, 2, 2, 2, 102, 103, 7, 63, 2, 2, 103, 34, 3, 2, 2, 2, 104, 106, 5, 45, 23, 2, 105, 104, 3, 2, 2, 2, 106, 107, 3, 2, 2, 2, 107, 105, 3, 2, 2, 2, 107, 108, 3, 2, 2, 2, 108, 36, 3, 2, 2, 2, 109, 111, 5, 43, 22, 2, 110, 109, 3, 2, 2, 2, 111, 112, 3, 2, 2, 2, 112, 110, 3, 2, 2, 2, 112, 113, 3, 2, 2, 2, 113, 38, 3, 2, 2, 2, 114, 115, 7, 49, 2, 2, 115, 116, 7, 49, 2, 2, 116, 120, 3, 2, 2, 2, 117, 119, 10, 2, 2, 2, 118, 117, 3, 2, 2, 2, 119, 122, 3, 2, 2, 2, 120, 118, 3, 2, 2, 2, 120, 121, 3, 2, 2, 2, 121, 124, 3, 2, 2, 2, 122, 120, 3, 2, 2, 2, 123, 125, 7, 15, 2, 2, 124, 123, 3, 2, 2, 2, 124, 125, 3, 2, 2, 2, 125, 126, 3, 2, 2, 2, 126, 139, 7, 12, 2, 2, 127, 128, 7, 49, 2, 2, 128, 129, 7, 44, 2, 2, 129, 133, 3, 2, 2, 2, 130, 132, 11, 2, 2, 2, 131, 130, 3, 2, 2, 2, 132, 135, 3, 2, 2, 2, 133, 134, 3, 2, 2, 2, 133, 131, 3, 2, 2, 2, 134, 136, 3, 2, 2, 2, 135, 133, 3, 2, 2, 2, 136, 137, 7, 44, 2, 2, 137, 139, 7, 49, 2, 2, 138, 114, 3, 2, 2, 2, 138, 127, 3, 2, 2, 2, 139, 140, 3, 2, 2, 2, 140, 141, 8, 20, 2, 2, 141, 40, 3, 2, 2, 2, 142, 144, 9, 3, 2, 2, 143, 142, 3, 2, 2, 2, 144, 145, 3, 2, 2, 2, 145, 143, 3, 2, 2, 2, 145, 146, 3, 2, 2, 2, 146, 147, 3, 2, 2, 2, 147, 148, 8, 21, 2, 2, 148, 42, 3, 2, 2, 2, 149, 150, 9, 4, 2, 2, 150, 44, 3, 2, 2, 2, 151, 152, 4, 50, 59, 2, 152, 46, 3, 2, 2, 2, 10, 2, 107, 112, 120, 124, 133, 138, 145, 3, 8, 2, 2]
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.tokens b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.tokens new file mode 100644 index 0000000..2d6fdc7 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/lexer/TinyLexer.tokens @@ -0,0 +1,36 @@ +IF=1 +THEN=2 +ELSE=3 +END=4 +REPEAT=5 +UNTIL=6 +READ=7 +WRITE=8 +OPAREN=9 +CPAREN=10 +PLUS=11 +MINUS=12 +MULTIPLY=13 +DIVIDE=14 +LT=15 +EQUAL=16 +NUM=17 +ID=18 +COMMENT=19 +WS=20 +'if'=1 +'then'=2 +'else'=3 +'end'=4 +'repeat'=5 +'until'=6 +'read'=7 +'write'=8 +'('=9 +')'=10 +'+'=11 +'-'=12 +'*'=13 +'/'=14 +'<'=15 +'='=16 diff --git a/Homework/cs5300/project-three/out/production/p3-parser/p3-parser.iml b/Homework/cs5300/project-three/out/production/p3-parser/p3-parser.iml new file mode 100644 index 0000000..8c27921 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/p3-parser.iml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" name="antlr-4.9.1-complete" level="project" /> + </component> +</module>
\ No newline at end of file diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Action$Act.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Action$Act.class Binary files differnew file mode 100644 index 0000000..2c89b29 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Action$Act.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Action.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Action.class Binary files differnew file mode 100644 index 0000000..9cde3de --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Action.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Grammar.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Grammar.class Binary files differnew file mode 100644 index 0000000..309a19a --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Grammar.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Item.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Item.class Binary files differnew file mode 100644 index 0000000..980e93c --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Item.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Main.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Main.class Binary files differnew file mode 100644 index 0000000..64b8ee0 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Main.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Parser.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Parser.class Binary files differnew file mode 100644 index 0000000..2804ef3 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Parser.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/ParserException.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/ParserException.class Binary files differnew file mode 100644 index 0000000..3c2a702 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/ParserException.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Rule.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Rule.class Binary files differnew file mode 100644 index 0000000..048edb2 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Rule.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/State.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/State.class Binary files differnew file mode 100644 index 0000000..720f6c8 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/State.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/States.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/States.class Binary files differnew file mode 100644 index 0000000..fb1c4c1 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/States.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Tests.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Tests.class Binary files differnew file mode 100644 index 0000000..74aa50e --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Tests.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/Util.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/Util.class Binary files differnew file mode 100644 index 0000000..14780ec --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/Util.class diff --git a/Homework/cs5300/project-three/out/production/p3-parser/parser/util/SymbolComparator.class b/Homework/cs5300/project-three/out/production/p3-parser/parser/util/SymbolComparator.class Binary files differnew file mode 100644 index 0000000..fd23969 --- /dev/null +++ b/Homework/cs5300/project-three/out/production/p3-parser/parser/util/SymbolComparator.class diff --git a/Homework/cs5300/project-three/parser/Action.java b/Homework/cs5300/project-three/parser/Action.java new file mode 100644 index 0000000..040daab --- /dev/null +++ b/Homework/cs5300/project-three/parser/Action.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +/** + * + * @author edwajohn + */ +public class Action { + + enum Act { + SHIFT, REDUCE, ACCEPT + }; + + private final Act act; + private final Integer state; + private final Rule rule; + + static Action createShift(Integer state) { + return new Action(Act.SHIFT, state, null); + } + + static Action createReduce(Rule rule) { + return new Action(Act.REDUCE, null, rule); + } + + static Action createAccept() { + return new Action(Act.ACCEPT, null, null); + } + + private Action(Act act, Integer state, Rule rule) { + this.act = act; + this.state = state; + this.rule = rule; + } + + public boolean isShift() { + return act == Act.SHIFT; + } + + public boolean isReduce() { + return act == Act.REDUCE; + } + + public boolean isAccept() { + return act == Act.ACCEPT; + } + + public Integer getState() { + return state; + } + + public Rule getRule() { + return rule; + } + + @Override + public String toString() { + if (isShift()) { + return "S" + state; + } + if (isReduce()) { + return "R" + rule.getName(); + } + return "acc"; + } +} diff --git a/Homework/cs5300/project-three/parser/Grammar.java b/Homework/cs5300/project-three/parser/Grammar.java new file mode 100644 index 0000000..0050cd1 --- /dev/null +++ b/Homework/cs5300/project-three/parser/Grammar.java @@ -0,0 +1,125 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +import java.io.IOException; +import java.util.*; + +import parser.util.SymbolComparator; + +/** + * + * @author edwajohn + */ +public class Grammar { + + /** + * Name of the grammar from the grammar file. This is computed for you. + */ + final String grammarName; + /** + * The list of rules found in the input grammar file. This is computed for + * you. + */ + final ArrayList<Rule> rules; + /** + * The list of all symbols found in the input grammar file. This is computed + * for you. + */ + final ArrayList<String> symbols; + /** + * The set of all terminal symbols. To see if a symbol is terminal or not: + * terminals.contains(symbol). This is computed for you. + */ + final HashSet<String> terminals; + /** + * The set of all nonterminal symbols. To see if a symbol is nonterminal: + * nonterminals.contains(symbol). This is computed for you. + */ + final HashSet<String> nonterminals; + /** + * Start symbol that is provided for you. + */ + final String startSymbol; + /** + * Start production rule that is provided for you. + */ + final Rule startRule; + /** + * Maps non-terminals to all rules that have the non-terminal on the lhs. + */ + HashMap<String, ArrayList<Rule>> nt2rules; + + final HashMap<String, HashSet<String>> first; + final HashMap<String, HashSet<String>> follow; + final HashMap<Rule, HashSet<String>> firstPlus; + + public Grammar(String grammarFilename) throws IOException { + System.out.println("Reading grammar " + grammarFilename); + cfgparser.Grammar grammar = new cfgparser.Grammar(grammarFilename); + + this.grammarName = grammar.getName(); + this.rules = grammar.getRules(); + this.symbols = grammar.getSymbols(); + this.startSymbol = grammar.getStartSymbol(); + this.terminals = new HashSet<>(symbols); + this.nonterminals = new HashSet<>(); + + Rule start = null; + for (Rule rule : this.rules) { + if (rule.getLhs().equals(this.startSymbol)) { + start = rule; + } + } + if (start == null) { + throw new RuntimeException("Unexpected null start production rule"); + } + this.startRule = start; + + nt2rules = new HashMap<>(); + System.out.println("\nRules"); + for (Rule rule : rules) { + System.out.println(rule.toString()); + final String lhs = rule.getLhs(); + terminals.remove(lhs); + nonterminals.add(lhs); + if (!nt2rules.containsKey(lhs)) { + nt2rules.put(lhs, new ArrayList<>()); + } + nt2rules.get(lhs).add(rule); + } + + // Sort the list of symbols for easy reading of output. + symbols.sort(new SymbolComparator(terminals)); + + // Compute first, follow and firstPlus sets. + first = Util.computeFirst(symbols, terminals, rules); + follow = Util.computeFollow(symbols, terminals, rules, first); + firstPlus = Util.computeFirstPlus(symbols, terminals, rules, first, follow); + } + + public boolean isTerminal(String symbol) { + return terminals.contains(symbol); + } + + public boolean isNonterminal(String symbol) { + return nonterminals.contains(symbol); + } + + public Rule findRule(String lhs, List<String> rhs) { + for (Rule rule : rules) { + if (rule.getLhs().equals(lhs) && rule.getRhs().equals(rhs)) { + return rule; + } + } + return null; + } + + public Rule findRule(String s) { + List<String> symbols = Arrays.asList(s.split(" ")); + return findRule(symbols.get(0), symbols.subList(2, symbols.size())); + } +} diff --git a/Homework/cs5300/project-three/parser/Item.java b/Homework/cs5300/project-three/parser/Item.java new file mode 100644 index 0000000..1cdb134 --- /dev/null +++ b/Homework/cs5300/project-three/parser/Item.java @@ -0,0 +1,151 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * An LR(1) item. + * + */ +public class Item implements Comparable { + + private Rule rule; + /** + * Position of the stack top in the production's RHS. + */ + private int dot; + /** + * The lookahead symbol + */ + private String a; + + /** + * Creates a set of items given the rule and lookahead, one item per potential + * stack position placement. The number of item is the number of symbols on + * the rhs of the rule plus one. + * + * @param rule + * @param a + * @return + */ + static ArrayList<Item> create(Rule rule, String a) { + ArrayList<Item> ret = new ArrayList<>(); + final ArrayList<String> rhs = rule.getRhs(); + for (int i = 0; i < rhs.size(); ++i) { + ret.add(new Item(rule, i, a)); + } + ret.add(new Item(rule, rhs.size(), a)); + return ret; + } + + public Item(Rule rule, int dot, String a) { + this.rule = rule; + this.dot = dot; + this.a = a; + } + + public Rule getRule() { + return rule; + } + + public int getDot() { + return dot; + } + + /** + * @return the lookahead symbol + */ + public String getA() { + return a; + } + + public String getLookahead() { + return getA(); + } + + /** + * Returns the symbol following the stack pointer. Returns null if the pointer + * is at the end of the rhs list. + * + * @return + */ + public String getNextSymbol() { + if (dot < rule.getRhs().size()) { + return rule.getRhs().get(dot); + } + return null; + } + + public String getNextNextSymbol() { + if (dot < rule.getRhs().size() - 1) { + return rule.getRhs().get(dot + 1); + } + return null; + } + + public Item advance() { + return new Item(rule, dot + 1, a); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 37 * hash + Objects.hashCode(this.rule); + hash = 37 * hash + this.dot; + hash = 37 * hash + Objects.hashCode(this.a); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Item other = (Item) obj; + if (!Objects.equals(this.rule, other.rule)) { + return false; + } + if (this.dot != other.dot) { + return false; + } + if (!Objects.equals(this.a, other.a)) { + return false; + } + return true; + } + + @Override + public String toString() { + String ret = "[" + rule.getLhs() + " -> "; + final ArrayList<String> rhs = rule.getRhs(); + for (int i = 0; i < dot; ++i) { + ret += rhs.get(i) + " "; + } + ret += Character.toString((char) 0x25CF); + if (dot < rhs.size()) { + ret += " "; + } + for (int i = dot; i < rhs.size(); ++i) { + ret += rhs.get(i); + if (i < rhs.size() - 1) { + ret += " "; + } + } + ret += ", " + a + "]"; + return ret; + } + + @Override + public int compareTo(Object o) { + return toString().compareTo(o.toString()); + } + +} diff --git a/Homework/cs5300/project-three/parser/Main.java b/Homework/cs5300/project-three/parser/Main.java new file mode 100644 index 0000000..62ca0a3 --- /dev/null +++ b/Homework/cs5300/project-three/parser/Main.java @@ -0,0 +1,337 @@ +package parser; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.*; + +/** + * + */ +public class Main { + static final String dot = Character.toString((char) 0x25CF); + + /** + * @param args the command line arguments + * @throws java.io.IOException + */ + public static void main(String[] args) throws IOException { + Tests tests = new Tests(); + testClosure(tests); + testStates(tests); + testTables(tests); + testParser(tests); + + // ------------------------------------------------------------ + // Output number of tests that succeeded + // ------------------------------------------------------------ + System.out.println(tests.getSuccesses() + "/" + tests.getN() + + " tests succeeded"); + // return tests.getFailures(); + + } + + public static void testClosure(Tests tests) throws FileNotFoundException, IOException { + // TODO: uncomment tests as you develop code + { + // TODO: Become familiar with the Grammar class. You will use it a lot. + Grammar grammar = new Grammar("data/Simple.cfg"); + + // Find the closure of [N -> ● X, $] + Rule rule = grammar.findRule("N -> X"); + State state = Parser.computeClosure(new Item(rule, 0, Util.EOF), grammar); + state.setName(0); + tests.test(state, "0: [[N -> ● X, $]]"); + + // Find the closure of [N -> ● N X, $] + rule = grammar.findRule("N -> N X"); + state = Parser.computeClosure(new Item(rule, 0, Util.EOF), grammar); + state.setName(0); + // [[N -> ● N X, $], [N -> ● N X, X], [N -> ● X, X]] + tests.test(state.size(), 3); + + // Find the start state + Item head = new Item(grammar.startRule, 0, Util.EOF); + state = Parser.computeClosure(head, grammar); + // [[G -> ● N, $], [N -> ● N X, $], [N -> ● N X, X], [N -> ● X, X], [N -> ● X, + // $]] + tests.test(state.size(), 5); + } + { + Grammar grammar = new Grammar("data/Paren.cfg"); + + // Find the closure of [list -> list ● pair, $] + Rule rule = grammar.findRule("list -> list pair"); + State state = Parser.computeClosure(new Item(rule, 1, Util.EOF), grammar); + // [[list -> list ● pair, $], [pair -> ● OPAREN list CPAREN, $], [pair -> ● + // OPAREN CPAREN, $]] + tests.test(state.size(), 3); + + // Find the closure of [pair -> OPAREN ● list CPAREN, $] + rule = grammar.findRule("pair -> OPAREN list CPAREN"); + state = Parser.computeClosure(new Item(rule, 1, Util.EOF), grammar); + // [[pair -> OPAREN ● list CPAREN, $], [list -> ● list pair, CPAREN] + // [list -> ● list pair, OPAREN], [list -> ● pair, OPAREN], [pair -> ● OPAREN + // list CPAREN, OPAREN] + // [pair -> ● OPAREN CPAREN, OPAREN], [list -> ● pair, CPAREN], [pair -> ● + // OPAREN list CPAREN, CPAREN] + // [pair -> ● OPAREN CPAREN, CPAREN]] + tests.test(state.size(), 9); + + // Find the start state + Item head = new Item(grammar.startRule, 0, Util.EOF); + state = Parser.computeClosure(head, grammar); + // [[goal -> ● list, $], [list -> ● list pair, $], [list -> ● list pair, OPAREN] + // [list -> ● pair, OPAREN], [pair -> ● OPAREN list CPAREN, OPAREN], [pair -> ● + // OPAREN CPAREN, OPAREN] + // [list -> ● pair, $], [pair -> ● OPAREN list CPAREN, $], [pair -> ● OPAREN + // CPAREN, $]] + tests.test(state.size(), 9); + } + { + Grammar grammar = new Grammar("data/Expr.cfg"); + + // Find the start state + Item head = new Item(grammar.startRule, 0, Util.EOF); + State state = Parser.computeClosure(head, grammar); + // [[goal -> ● expr, $], [expr -> ● expr PLUS term, $], [expr -> ● expr PLUS + // term, PLUS], [expr -> ● expr MINUS term, PLUS], [expr -> ● expr PLUS term, + // MINUS], [expr -> ● term, PLUS], [term -> ● term MULTIPLY factor, PLUS], [term + // -> ● term MULTIPLY factor, MULTIPLY], [term -> ● term DIVIDE factor, + // MULTIPLY], [term -> ● term MULTIPLY factor, DIVIDE], [term -> ● factor, + // MULTIPLY], [factor -> ● OPAREN expr CPAREN, MULTIPLY], [factor -> ● INT, + // MULTIPLY], [factor -> ● FLOAT, MULTIPLY], [factor -> ● IDENTIFIER, MULTIPLY], + // [term -> ● term DIVIDE factor, DIVIDE], [term -> ● factor, DIVIDE], [factor + // -> ● OPAREN expr CPAREN, DIVIDE], [factor -> ● INT, DIVIDE], [factor -> ● + // FLOAT, DIVIDE], [factor -> ● IDENTIFIER, DIVIDE], [term -> ● term DIVIDE + // factor, PLUS], [term -> ● factor, PLUS], [factor -> ● OPAREN expr CPAREN, + // PLUS], [factor -> ● INT, PLUS], [factor -> ● FLOAT, PLUS], [factor -> ● + // IDENTIFIER, PLUS], [expr -> ● expr MINUS term, MINUS], [expr -> ● term, + // MINUS], [term -> ● term MULTIPLY factor, MINUS], [term -> ● term DIVIDE + // factor, MINUS], [term -> ● factor, MINUS], [factor -> ● OPAREN expr CPAREN, + // MINUS], [factor -> ● INT, MINUS], [factor -> ● FLOAT, MINUS], [factor -> ● + // IDENTIFIER, MINUS], [expr -> ● expr MINUS term, $], [expr -> ● term, $], + // [term -> ● term MULTIPLY factor, $], [term -> ● term DIVIDE factor, $], [term + // -> ● factor, $], [factor -> ● OPAREN expr CPAREN, $], [factor -> ● INT, $], + // [factor -> ● FLOAT, $], [factor -> ● IDENTIFIER, $]] + tests.test(state.size(), 45); + } + } + + public static void testStates(Tests tests) throws FileNotFoundException, IOException { + { + Parser parser = new Parser("data/Simple.cfg"); + States states = parser.getStates(); + tests.test(states.size(), 4); + } + { + Parser parser = new Parser("data/Paren.cfg"); + States states = parser.getStates(); + tests.test(states.size(), 14); + } + { + Parser parser = new Parser("data/Expr.cfg"); + States states = parser.getStates(); + tests.test(states.size(), 34); + } + } + + public static void testTables(Tests tests) throws FileNotFoundException, IOException { + { + Parser parser = new Parser("data/Simple.cfg"); + String actionTable = parser.actionTableToString(); + tests.test(countMatches(actionTable, 'S'), 2); // 2 shifts + tests.test(countMatches(actionTable, 'R'), 4); // 4 reduces + tests.test(countMatches(actionTable, "acc"), 1); // 1 accept + } + { + Parser parser = new Parser("data/Paren.cfg"); + String actionTable = parser.actionTableToString(); + tests.test(countMatches(actionTable, 'S'), 10); // 10 shifts + tests.test(countMatches(actionTable, 'R'), 18); // 18 reduces (2 of the Rs are in the header) + tests.test(countMatches(actionTable, "acc"), 1); // 1 accept + } + { + Parser parser = new Parser("data/Expr.cfg"); + String actionTable = parser.actionTableToString(); + tests.test(countMatches(actionTable, 'S'), 66); + tests.test(countMatches(actionTable, 'R'), 91); + tests.test(countMatches(actionTable, "acc"), 1); + } + } + + public static void testParser(Tests tests) throws FileNotFoundException, IOException { + { + Parser parser = new Parser("data/Simple.cfg"); + + String s = "xxx"; + try { + List<Action> actions = parser.parseFromString(s); + tests.test(countMatches(actions.toString(), 'S'), 3); // 3 shift actions + tests.test(countMatches(actions.toString(), 'R'), 3); // 3 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + s + ": " + e.getMessage()); + } + + s = "xxxxx"; + try { + List<Action> actions = parser.parseFromString(s); + tests.test(countMatches(actions.toString(), 'S'), 5); // 5 shift actions + tests.test(countMatches(actions.toString(), 'R'), 5); // 5 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + s + ": " + e.getMessage()); + } + + s = ""; + try { + List<Action> actions = parser.parseFromString(s); + tests.addFailure("Should have failed to parse"); + } catch (ParserException e) { + } + } + + { + Parser parser = new Parser("data/Paren.cfg"); + + String fn = "data/paren0.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 2); // 2 shift actions + tests.test(countMatches(actions.toString(), 'R'), 2); // 2 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/paren1.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 20); // 20 shift actions + tests.test(countMatches(actions.toString(), 'R'), 20); // 20 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/paren2.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + fn = "data/paren3.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + fn = "data/paren4.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 10); // 10 shift actions + tests.test(countMatches(actions.toString(), 'R'), 10); // 10 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + } + { + Parser parser = new Parser("data/Expr.cfg"); + + String fn = "data/expr1.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 7); // 7 shift actions + tests.test(countMatches(actions.toString(), 'R'), 10); // 10 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/expr2.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + fn = "data/expr3.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 1); // 1 shift actions + tests.test(countMatches(actions.toString(), 'R'), 3); // 3 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/expr4.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 15); // 15 shift actions + tests.test(countMatches(actions.toString(), 'R'), 22); // 22 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/expr5.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + fn = "data/expr6.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + } + + { + // TODO: To pass these tests you must implement data/Tiny.cfg + // according to the grammar given in data/tinyGrammar.pdf. + // See notes in data/Tiny.cfg (be sure to read all of them!). + Parser parser = new Parser("data/Tiny.cfg"); + + String fn = "data/tiny1.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 11); // 11 shift actions + tests.test(countMatches(actions.toString(), 'R'), 25); // 25 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/tiny2.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.test(countMatches(actions.toString(), 'S'), 27); // 27 shift actions + tests.test(countMatches(actions.toString(), 'R'), 55); // 55 reduce actions + } catch (ParserException e) { + tests.addFailure("Failed to parse " + fn + ": " + e.getMessage()); + } + + fn = "data/tiny3.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + fn = "data/tiny4.dat"; + try { + List<Action> actions = parser.parseFromFile(fn); + tests.addFailure("Should have failed to parse " + fn); + } catch (ParserException e) { + } + + } + } + + private static int countMatches(String s, char c) { + return (int) s.chars().filter(ch -> ch == c).count(); + } + + private static int countMatches(String s, String c) { + return s.split(c, -1).length - 1; + } +}
\ No newline at end of file diff --git a/Homework/cs5300/project-three/parser/Parser.java b/Homework/cs5300/project-three/parser/Parser.java new file mode 100644 index 0000000..67d723a --- /dev/null +++ b/Homework/cs5300/project-three/parser/Parser.java @@ -0,0 +1,341 @@ +/* + * Look for TODO comments in this file for suggestions on how to implement + * your parser. + */ +package parser; + +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +import javax.lang.model.util.ElementScanner6; + +import lexer.ExprLexer; +import lexer.ParenLexer; +import lexer.SimpleLexer; +import lexer.TinyLexer; +import org.antlr.v4.runtime.*; + +/** + * + */ +public class Parser { + + final Grammar grammar; + + /** + * All states in the parser. + */ + private final States states; + + /** + * Action table for bottom-up parsing. Accessed as + * actionTable.get(state).get(terminal). You may replace + * the Integer with a State class if you choose. + */ + private final HashMap<Integer, HashMap<String, Action>> actionTable; + /** + * Goto table for bottom-up parsing. Accessed as + * gotoTable.get(state).get(nonterminal). + * You may replace the Integers with State classes if you choose. + */ + private final HashMap<Integer, HashMap<String, Integer>> gotoTable; + + public Parser(String grammarFilename) throws IOException { + actionTable = new HashMap<>(); + gotoTable = new HashMap<>(); + + grammar = new Grammar(grammarFilename); + + states = new States(); + + initializeStates(); + constructActionTable(); + constructGotoTable(); + } + + private int findState(State state) { + for (State f_state : states.getStateList()) + if (f_state.equals(state)) + return f_state.getName(); + return -1; + } + + private void constructActionTable() { + for (State state : states.getStateList()) { + int name = state.getName(); + actionTable.computeIfAbsent(name, k -> new HashMap<String, Action>()); + + for (Item item : state.getItems()) { + String nextSymbol = item.getNextSymbol(); + + if (nextSymbol != null && grammar.isTerminal(nextSymbol)) + actionTable.get(name).put(nextSymbol, Action.createShift(findState(Parser.GOTO(state, nextSymbol, grammar)))); + else if (nextSymbol == null) { + Rule rule = item.getRule(); + if (rule.equals(grammar.startRule) && item.getLookahead() == Util.EOF) + actionTable.get(name).put(Util.EOF, Action.createAccept()); + else + actionTable.get(name).put(item.getLookahead(), Action.createReduce(item.getRule())); + } + } + } + } + + private void constructGotoTable() { + for (State state : states.getStateList()) { + int name = state.getName(); + gotoTable.computeIfAbsent(name, k -> new HashMap<String, Integer>()); + for (String nonTerminal : grammar.nonterminals) { + int goTo = findState(Parser.GOTO(state, nonTerminal, grammar)); + if (goTo != -1) + gotoTable.get(name).put(nonTerminal, goTo); + } + } + } + + private void initializeStates() { + int stateId = 0; + + State closure = Parser.computeClosure(new Item(grammar.startRule, 0, Util.EOF), grammar); + closure.setName(stateId++); + states.addState(closure); + + boolean added = true; + while (added) { + added = false; + + ArrayList<State> stateList = new ArrayList<>(states.getStateList()); + for (State state : stateList) + for (String symbol : grammar.symbols) { + State goTo = Parser.GOTO(state, symbol, grammar); + if (goTo.getItems().size() > 0 && states.addState(goTo)) { + goTo.setName(stateId++); + added = true; + } + } + } + } + + public States getStates() { + return states; + } + + static public State computeClosure(Item I, Grammar grammar) { + State closure = new State(I); + + boolean added = true; + while (added) { + added = false; + + List<Item> items = new ArrayList<Item>(closure.getItems()); + + for (Item item : items) { + String symbol = item.getNextSymbol(); + + if (grammar.isNonterminal(symbol)) { + List<Rule> symbolRules = grammar.rules.stream().filter(rule -> rule.getLhs().equals(symbol)) + .collect(Collectors.toList()); + + ArrayList<String> firstOperand = new ArrayList<>(); + if (item.getNextNextSymbol() != null) { + List<String> rhs = item.getRule().getRhs(); + firstOperand.addAll(rhs.subList(item.getDot() + 1, rhs.size())); + } + firstOperand.add(item.getLookahead()); + + HashSet<String> firstSet = new HashSet<>(); + for (String firstSymbol : firstOperand) { + if (grammar.first.containsKey(firstSymbol)) { + firstSet.addAll(grammar.first.get(firstSymbol)); + if (!grammar.first.get(firstSymbol).contains(Util.EPSILON)) + break; + } else if (firstSymbol.equals(Util.EOF)) { + firstSet.add(firstSymbol); + } + } + + for (Rule rule : symbolRules) + for (String terminal : firstSet) + added = closure.addItem(new Item(rule, 0, terminal)) || added; + } + } + } + + return closure; + } + + // This returns a new state that represents the transition from + // the given state on the symbol X. + static public State GOTO(State state, String X, Grammar grammar) { + State gotoState = new State(); + + for (Item item : state.getItems()) + if (item.getNextSymbol() != null && item.getNextSymbol().equals(X)) + gotoState.addItem(new Item(item.getRule(), item.getDot() + 1, item.getLookahead())); + + List<Item> gotoItems = new ArrayList<>(gotoState.getItems()); + for (Item gotoItem : gotoItems) + for (Item closureItem : Parser.computeClosure(gotoItem, grammar).getItems()) + gotoState.addItem(closureItem); + + return gotoState; + } + + // You will want to use StringBuilder. Another useful method will be + // String.format: for + // printing a value in the table, use + // String.format("%8s", value) + // How much whitespace you have shouldn't matter with regard to the tests, but + // it will + // help you debug if you can format it nicely. + public String actionTableToString() { + StringBuilder builder = new StringBuilder(); + + List<String> terminalsPlusEof = new ArrayList<String>(grammar.terminals); + terminalsPlusEof.add(Util.EOF); + + builder.append("state"); + for (String terminal : terminalsPlusEof) + builder.append(String.format("%8s", terminal)); + builder.append("\n"); + + for (State state : states.getStateList()) { + int stateName = state.getName(); + HashMap<String, Action> actionRow = actionTable.get(stateName); + builder.append(String.format("%5s", stateName)); + + for (String terminal : terminalsPlusEof) { + Action action = actionRow.get(terminal); + builder.append(String.format("%8s", action != null ? action.toString() : "")); + } + builder.append("\n"); + } + return builder.toString(); + } + + // You will want to use StringBuilder. Another useful method will be + // String.format: for + // printing a value in the table, use + // String.format("%8s", value) + // How much whitespace you have shouldn't matter with regard to the tests, but + // it will + // help you debug if you can format it nicely. + public String gotoTableToString() { + StringBuilder builder = new StringBuilder(); + + builder.append("state"); + for (String nonTerminal : grammar.nonterminals) + builder.append(String.format("%8s", nonTerminal)); + builder.append("\n"); + + for (State state : states.getStateList()) { + int stateName = state.getName(); + HashMap<String, Integer> gotoTableRow = gotoTable.get(stateName); + builder.append(String.format("%5s", stateName)); + + for (String nonTerminal : grammar.nonterminals) { + if (gotoTableRow.containsKey(nonTerminal)) + builder.append(String.format("%8s", gotoTableRow.get(nonTerminal))); + else + builder.append(String.format("%8s", "")); + } + + builder.append("\n"); + } + return builder.toString(); + } + + // You should return a list of the actions taken. + public List<Action> parse(Lexer scanner) throws ParserException { + // tokens is the output from the scanner. It is the list of tokens + // scanned from the input file. + // To get the token type: v.getSymbolicName(t.getType()) + // To get the token lexeme: t.getText() + ArrayList<? extends Token> tokens = new ArrayList<>(scanner.getAllTokens()); + Vocabulary v = scanner.getVocabulary(); + + Stack<String> input = new Stack<>(); + Collections.reverse(tokens); + input.add(Util.EOF); + for (Token t : tokens) { + input.push(v.getSymbolicName(t.getType())); + } + Collections.reverse(tokens); + + List<Action> actions = new ArrayList<>(); + + Stack<State> stack = new Stack<>(); + stack.push(states.getState(0)); + + String currentInput = input.pop(); + while (true) { + State s = stack.peek(); + Action action = actionTable.get(s.getName()).get(currentInput); + + if (action == null) + throw ParserException.create(tokens, input.size()); + + actions.add(action); + + if (action.isShift()) { + stack.push(states.getState(action.getState())); + currentInput = input.pop(); + } else if (action.isReduce()) { + Rule rule = action.getRule(); + for (int i = 0; i < rule.getRhs().size(); i++) + stack.pop(); + + s = stack.peek(); + stack.push(states.getState(gotoTable.get(s.getName()).get(rule.getLhs()))); + } else if (action.isAccept()) { + break; + } else { + throw ParserException.create(tokens, input.size()); + } + } + + return actions; + } + + // ------------------------------------------------------------------- + // Convenience functions + // ------------------------------------------------------------------- + + public List<Action> parseFromFile(String filename) throws IOException, ParserException { + // System.out.println("\nReading input file " + filename + "\n"); + final CharStream charStream = CharStreams.fromFileName(filename); + Lexer scanner = scanFile(charStream); + return parse(scanner); + } + + public List<Action> parseFromString(String program) throws ParserException { + Lexer scanner = scanFile(CharStreams.fromString(program)); + return parse(scanner); + } + + private Lexer scanFile(CharStream charStream) { + // We use ANTLR's scanner (lexer) to produce the tokens. + Lexer scanner = null; + switch (grammar.grammarName) { + case "Simple": + scanner = new SimpleLexer(charStream); + break; + case "Paren": + scanner = new ParenLexer(charStream); + break; + case "Expr": + scanner = new ExprLexer(charStream); + break; + case "Tiny": + scanner = new TinyLexer(charStream); + break; + default: + System.out.println("Unknown scanner"); + break; + } + + return scanner; + } + +} diff --git a/Homework/cs5300/project-three/parser/ParserException.java b/Homework/cs5300/project-three/parser/ParserException.java new file mode 100644 index 0000000..1c32080 --- /dev/null +++ b/Homework/cs5300/project-three/parser/ParserException.java @@ -0,0 +1,81 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +import java.util.ArrayList; +import org.antlr.v4.runtime.Token; + +/** + * + * @author edwajohn + */ +public class ParserException extends Exception { + + /** + * Create a ParserException. This is a convenience factory method to easily + * format your output to meet project specification. + * + * @param words the words being parsed + * @param i the index to the current word being parsed + * @return + */ + public static ParserException create(final ArrayList<? extends Token> words, final int i) { + String msg; + if (i < words.size()) { + msg = "Error parsing token \"" + words.get(i).getText() + "\" in \n "; + int preLen = 4; + for (int j = 0; j < i; ++j) { + Token token = words.get(j); + msg = msg + token.getText() + " "; + preLen += token.getText().length() + 1; + } + final int curLen = words.get(i).getText().length(); + msg = msg + words.get(i).getText(); + if (i < words.size() - 1) { + msg = msg + " "; + } + for (int j = i + 1; j < words.size(); ++j) { + Token token = words.get(j); + msg = msg + token.getText(); + if (j < words.size() - 1) { + msg = msg + " "; + } + } + + msg = msg + "\n"; + for (int j = 0; j < preLen; ++j) { + msg += " "; + } + for (int j = 0; j < curLen; ++j) { + msg += "^"; + } + } else { + msg = "Unexpectedly reached end of file in\n"; + for (int j = 0; j < words.size(); ++j) { + Token token = words.get(j); + msg = msg + token.getText(); + if (j < words.size() - 1) { + msg = msg + " "; + } + } + + } + return new ParserException(msg); + } + + /** + * Constructor. You will most likely NOT use this constructor directly, but + * rather use the convenience factory method create(). You may choose to use + * this constructor, however, if your data structures are set up differently + * than what the factory method expects. + * + * @param msg + */ + public ParserException(String msg) { + super(msg); + } + +} diff --git a/Homework/cs5300/project-three/parser/Rule.java b/Homework/cs5300/project-three/parser/Rule.java new file mode 100644 index 0000000..b251e84 --- /dev/null +++ b/Homework/cs5300/project-three/parser/Rule.java @@ -0,0 +1,93 @@ +/* + * Do not modify this file. + */ +package parser; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * + */ +public class Rule { + + private int name; + private final String lhs; + private ArrayList<String> rhs; + + public Rule(String lhs) { + this.name = 0; + this.lhs = lhs; + rhs = new ArrayList<>(); + } + + public void setName(int name) { + this.name = name; + } + + public int getName() { + return this.name; + } + + @Override + public String toString() { + String ret = "R" + name + " " + lhs + " -> "; + for (String symbol : getRhs()) { + ret = ret + symbol + " "; + } + return ret; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 37 * hash + Objects.hashCode(this.lhs); + hash = 37 * hash + this.name; + for (int i = 0; i < this.rhs.size(); ++i) { + hash = 37 * hash + Objects.hashCode(this.rhs.get(i)); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Rule other = (Rule) obj; + if (!this.lhs.equals(other.lhs)) { + return false; + } + if (this.name != other.name) { + return false; + } + if (this.rhs.size() != other.rhs.size()) { + return false; + } + for (int i = 0; i < this.rhs.size(); ++i) { + if (!this.rhs.get(i).equals(other.rhs.get(i))) { + return false; + } + } + return true; + } + + public String getLhs() { + return lhs; + } + + public ArrayList<String> getRhs() { + if (rhs.isEmpty()) { + rhs.add("EPSILON"); + } + return rhs; + } + + public void addRhs(String symbol) { + rhs.add(symbol); + } + +} diff --git a/Homework/cs5300/project-three/parser/State.java b/Homework/cs5300/project-three/parser/State.java new file mode 100644 index 0000000..90c9c33 --- /dev/null +++ b/Homework/cs5300/project-three/parser/State.java @@ -0,0 +1,94 @@ +package parser; + +import java.util.*; + +public class State implements Comparable<State> { + private Set<Item> itemSet; + private List<Item> items; + private int name; + + public State(int name) { + this.itemSet = new HashSet<>(); + this.items = new ArrayList<>(); + this.name = name; + } + + public State() { + this(0); + } + + public State(Item item) { + this(); + addItem(item); + } + + public void setName(int name) { + this.name = name; + } + + public int getName() { + return this.name; + } + + public List<Item> getItems() { + return this.items; + } + + public int size() { + return items.size(); + } + + /* + * true if the item was added, false if it was already in the set + */ + public boolean addItem(Item item) { + if (!itemSet.contains(item)) { + itemSet.add(item); + items.add(item); + return true; + } + return false; + } + + @Override + public int hashCode() { + int hash = 7; + ArrayList<Item> sortedList = new ArrayList<>(items); + sortedList.sort(Comparator.comparingInt(Item::hashCode)); + for (Item item : sortedList) { + hash = 37 * hash + Objects.hashCode(item); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final State other = (State) obj; + if (items.size() != other.items.size()) { + return false; + } + for (Item item : items) { + if (!other.itemSet.contains(item)) { + return false; + } + } + return true; + } + + @Override + public String toString() { + return this.name + ": " + items.toString(); + } + + @Override + public int compareTo(State o) { + return Integer.valueOf(this.name).compareTo(Integer.valueOf(o.name)); + } + +} diff --git a/Homework/cs5300/project-three/parser/States.java b/Homework/cs5300/project-three/parser/States.java new file mode 100644 index 0000000..7002f53 --- /dev/null +++ b/Homework/cs5300/project-three/parser/States.java @@ -0,0 +1,44 @@ +package parser; + +import java.util.*; + +public class States { + private Set<State> stateSet; + private List<State> states; + private int id = 0; + + public States() { + this.stateSet = new HashSet<>(); + this.states = new ArrayList<>(); + } + + public int size() { + return this.states.size(); + } + + public boolean addState(State state) { + if (!this.stateSet.contains(state)) { + this.stateSet.add(state); + this.states.add(state); + return true; + } + return false; + } + + public List<State> getStateList() { + return states; + } + + public State getState(int name) { + if (name >= states.size()) { + return null; + } + return states.get(name); + } + + @Override + public String toString() { + return states.toString(); + } + +} diff --git a/Homework/cs5300/project-three/parser/Tests.java b/Homework/cs5300/project-three/parser/Tests.java new file mode 100644 index 0000000..73732af --- /dev/null +++ b/Homework/cs5300/project-three/parser/Tests.java @@ -0,0 +1,69 @@ +package parser; + +/** + * + * @author edwajohn + */ +public class Tests { + + private int N = 0; + private int failures = 0; + + public void test(boolean b) { + N++; + if (!b) { + System.err.println("Failed test " + N); + failures++; + } + } + + public void test(boolean b, String message) { + test(b); + if (!b) { + System.err.println(message); + } + } + + public void test(Object a, Object target) { + N++; + if (a == null && target == null) { + return; + } + if (a == null || target == null || !a.equals(target)) { + System.err.println("Failed test " + N); + System.err.println(" " + a + " not equal to target " + target); + failures++; + } + } + + public void test(Object a, String target) { + N++; + if (a == null && target == null) { + return; + } + if (a == null || target == null || !a.toString().equals(target)) { + System.err.println("Failed test " + N); + System.err.println(" " + a + " not equal to target " + target); + failures++; + } + } + + public void addFailure(String msg) { + N++; + System.err.println("Failed test " + N); + System.err.println(msg); + failures++; + } + + public int getN() { + return N; + } + + public int getSuccesses() { + return N - failures; + } + + public int getFailures() { + return failures; + } +} diff --git a/Homework/cs5300/project-three/parser/Util.java b/Homework/cs5300/project-three/parser/Util.java new file mode 100644 index 0000000..fe7d9ab --- /dev/null +++ b/Homework/cs5300/project-three/parser/Util.java @@ -0,0 +1,132 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Util { + + public static final String EPSILON = "EPSILON"; + public static final String EOF = "$"; + + public static HashMap<String, HashSet<String>> computeFirst(ArrayList<String> symbols, HashSet<String> terminals, ArrayList<Rule> rules) { + HashMap<String, HashSet<String>> first = new HashMap<>(); + for (String symbol : symbols) { + first.put(symbol, new HashSet<>()); + } + for (String terminal : terminals) { + first.get(terminal).add(terminal); + } + // Algorithm 3.7 + boolean changed = true; + while (changed) { + changed = false; + for (Rule rule : rules) { + ArrayList<String> rhsSymbols = rule.getRhs(); + HashSet<String> rhs = new HashSet<>(); + final String B0 = rhsSymbols.get(0); + rhs.addAll(noEpsilon(first.get(B0))); + boolean stop = !first.get(B0).contains(EPSILON); + for (int i = 0; !stop && i < rhsSymbols.size() - 1; ++i) { + final String B = rhsSymbols.get(i + 1); + rhs.addAll(noEpsilon(first.get(B0))); + stop = !first.get(B).contains(EPSILON); + } + if (!stop) { + rhs.add(EPSILON); + } + final String A = rule.getLhs(); + if (!first.get(A).containsAll(rhs)) { + first.get(A).addAll(rhs); + changed = true; + } + } + } +// System.out.println("\n"); +// for (String symbol : symbols) { +// final String A = symbol; +// System.out.println("FIRST(" + A + ") = " + first.get(A)); +// } + return first; + } + + public static HashMap<String, HashSet<String>> computeFollow( + ArrayList<String> symbols, HashSet<String> terminals, ArrayList<Rule> rules, + HashMap<String, HashSet<String>> first) { + HashMap<String, HashSet<String>> follow = new HashMap<>(); + for (String symbol : symbols) { + follow.put(symbol, new HashSet<>()); + } + follow.get(rules.get(0).getLhs()).add(EOF); + boolean changed = true; + while (changed) { + changed = false; + for (Rule rule : rules) { + HashSet<String> trailer = new HashSet<>(follow.get(rule.getLhs())); + final ArrayList<String> rhsSymbols = rule.getRhs(); + for (int i = rhsSymbols.size() - 1; i >= 0; --i) { + final String Bi = rhsSymbols.get(i); + if (!terminals.contains(Bi)) { + if (!follow.get(Bi).containsAll(trailer)) { + follow.get(Bi).addAll(trailer); + changed = true; + } + if (first.get(Bi).contains(EPSILON)) { + trailer.addAll(noEpsilon(first.get(Bi))); + } else { + trailer = first.get(Bi); + } + } else { + trailer = first.get(Bi); + } + } + } + } +// System.out.println("\n"); +// for (String symbol : symbols) { +// if (!terminals.contains(symbol)) { +// System.out.println("FOLLOW(" + symbol + ") = " + follow.get(symbol)); +// } +// } + return follow; + } + + public static HashMap<Rule, HashSet<String>> computeFirstPlus( + ArrayList<String> symbols, HashSet<String> terminals, ArrayList<Rule> rules, + HashMap<String, HashSet<String>> first, HashMap<String, HashSet<String>> follow) { + HashMap<Rule, HashSet<String>> firstPlus = new HashMap<>(); + for (Rule rule : rules) { + final String A = rule.getLhs(); + final String B = rule.getRhs().get(0); + HashSet<String> firstPlusA = new HashSet<>(); + if (!first.get(B).contains(EPSILON)) { + firstPlusA.addAll(first.get(B)); + } else { + firstPlusA.addAll(first.get(B)); + firstPlusA.addAll(follow.get(A)); + } + firstPlus.put(rule, firstPlusA); + } +// System.out.println("\n"); +// for (Rule rule : rules) { +// System.out.println("FIRST+(" + rule + ") = " + firstPlus.get(rule)); +// } + return firstPlus; + } + + private static HashSet<String> noEpsilon(final HashSet<String> set) { + HashSet<String> s = new HashSet<>(set); + s.remove(EPSILON); + return s; + } +} diff --git a/Homework/cs5300/project-three/parser/util/SymbolComparator.java b/Homework/cs5300/project-three/parser/util/SymbolComparator.java new file mode 100644 index 0000000..e131c79 --- /dev/null +++ b/Homework/cs5300/project-three/parser/util/SymbolComparator.java @@ -0,0 +1,38 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package parser.util; + +import java.util.Comparator; +import java.util.HashSet; + +/** + * + * @author edwajohn + */ +public class SymbolComparator implements Comparator<String> { + + private final HashSet<String> terminals; + + public SymbolComparator(HashSet<String> terminals) { + this.terminals = terminals; + } + + @Override + public int compare(String o1, String o2) { + // Slightly less efficient code for clarity. + if (terminals.contains(o1) && terminals.contains(o2)) { + return o1.compareTo(o2); + } + if (terminals.contains(o1)) { + return 1; + } + if (terminals.contains(o2)) { + return -1; + } + return o1.compareTo(o2); + } + +} diff --git a/Homework/cs5300/project-two/Pasca-definition.pdf b/Homework/cs5300/project-two/Pasca-definition.pdf Binary files differnew file mode 100644 index 0000000..8f0ea79 --- /dev/null +++ b/Homework/cs5300/project-two/Pasca-definition.pdf diff --git a/Homework/cs5300/project-two/classes/scanner/Main.class b/Homework/cs5300/project-two/classes/scanner/Main.class Binary files differnew file mode 100644 index 0000000..48b49a7 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/Main.class diff --git a/Homework/cs5300/project-two/classes/scanner/ScanStream.class b/Homework/cs5300/project-two/classes/scanner/ScanStream.class Binary files differnew file mode 100644 index 0000000..cad01c8 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/ScanStream.class diff --git a/Homework/cs5300/project-two/classes/scanner/Scanner.class b/Homework/cs5300/project-two/classes/scanner/Scanner.class Binary files differnew file mode 100644 index 0000000..fdd31bc --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/Scanner.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableParser$CharCat.class b/Homework/cs5300/project-two/classes/scanner/TableParser$CharCat.class Binary files differnew file mode 100644 index 0000000..a21dff7 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableParser$CharCat.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableParser$TokenType.class b/Homework/cs5300/project-two/classes/scanner/TableParser$TokenType.class Binary files differnew file mode 100644 index 0000000..6f6b500 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableParser$TokenType.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableParser$Transition.class b/Homework/cs5300/project-two/classes/scanner/TableParser$Transition.class Binary files differnew file mode 100644 index 0000000..2c8ead3 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableParser$Transition.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableParser$WhichTable.class b/Homework/cs5300/project-two/classes/scanner/TableParser$WhichTable.class Binary files differnew file mode 100644 index 0000000..4241bfe --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableParser$WhichTable.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableParser.class b/Homework/cs5300/project-two/classes/scanner/TableParser.class Binary files differnew file mode 100644 index 0000000..102aeea --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableParser.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableReader$CharCat.class b/Homework/cs5300/project-two/classes/scanner/TableReader$CharCat.class Binary files differnew file mode 100644 index 0000000..10cfa45 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableReader$CharCat.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableReader$TokenType.class b/Homework/cs5300/project-two/classes/scanner/TableReader$TokenType.class Binary files differnew file mode 100644 index 0000000..9c19786 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableReader$TokenType.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableReader$Transition.class b/Homework/cs5300/project-two/classes/scanner/TableReader$Transition.class Binary files differnew file mode 100644 index 0000000..9e75001 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableReader$Transition.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableReader$WhichTable.class b/Homework/cs5300/project-two/classes/scanner/TableReader$WhichTable.class Binary files differnew file mode 100644 index 0000000..9f26273 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableReader$WhichTable.class diff --git a/Homework/cs5300/project-two/classes/scanner/TableReader.class b/Homework/cs5300/project-two/classes/scanner/TableReader.class Binary files differnew file mode 100644 index 0000000..bfff22d --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/TableReader.class diff --git a/Homework/cs5300/project-two/classes/scanner/Tests.class b/Homework/cs5300/project-two/classes/scanner/Tests.class Binary files differnew file mode 100644 index 0000000..fd6e411 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/Tests.class diff --git a/Homework/cs5300/project-two/classes/scanner/Token.class b/Homework/cs5300/project-two/classes/scanner/Token.class Binary files differnew file mode 100644 index 0000000..f99a2c0 --- /dev/null +++ b/Homework/cs5300/project-two/classes/scanner/Token.class diff --git a/Homework/cs5300/project-two/data/err1-pasca.txt b/Homework/cs5300/project-two/data/err1-pasca.txt new file mode 100644 index 0000000..3316517 --- /dev/null +++ b/Homework/cs5300/project-two/data/err1-pasca.txt @@ -0,0 +1 @@ +(* a comment *) *) diff --git a/Homework/cs5300/project-two/data/err2-pasca.txt b/Homework/cs5300/project-two/data/err2-pasca.txt new file mode 100644 index 0000000..ba3383f --- /dev/null +++ b/Homework/cs5300/project-two/data/err2-pasca.txt @@ -0,0 +1 @@ +(* a comment diff --git a/Homework/cs5300/project-two/data/err3-pasca.txt b/Homework/cs5300/project-two/data/err3-pasca.txt new file mode 100644 index 0000000..40381e2 --- /dev/null +++ b/Homework/cs5300/project-two/data/err3-pasca.txt @@ -0,0 +1 @@ +0123 diff --git a/Homework/cs5300/project-two/data/pasca.table b/Homework/cs5300/project-two/data/pasca.table new file mode 100644 index 0000000..e3a5733 --- /dev/null +++ b/Homework/cs5300/project-two/data/pasca.table @@ -0,0 +1,122 @@ +// Modify this file to create your tables for the Pasca language. + +// Special tokens are \space, \t and \n. +ClassifierTable +a letter +b letter +c letter +d letter +e letter +f letter +g letter +h letter +i letter +j letter +k letter +l letter +m letter +n letter +o letter +p letter +q letter +r letter +s letter +t letter +u letter +v letter +w letter +x letter +y letter +z letter +0 zero +1 nonzero +2 nonzero +3 nonzero +4 nonzero +5 nonzero +6 nonzero +7 nonzero +8 nonzero +9 nonzero +' quote +* asterisk +{ delim +} delim +; delim +. delim +( lp +) rp +\space whitespace +\t whitespace +\n whitespace + + +// In the transition table, the first state +// is considered the input state. +TransitionTable +// Identifiers +s0 letter id +id letter id +id zero id +id nonzero id + +// Integers +s0 nonzero int +int zero int +int nonzero int + +// Strings +s0 quote q1 +q1 quote str +str quote q1 +// Everything besides quotes +q1 zero q1 +q1 nonzero q1 +q1 letter q1 +q1 delim q1 +q1 asterisk q1 +q1 lp q1 +q1 rp q1 +q1 whitespace q1 + +// Delimiters +s0 delim delimiter + +// Comments +s0 lp c1 +c1 asterisk c2 +c2 asterisk c3 +c3 asterisk c3 +c3 rp ignore + +c3 zero c2 +c3 nonzero c2 +c3 letter c2 +c3 delim c2 +c3 lp c2 +c3 quote c2 +c3 whitespace c2 + +c2 zero c2 +c2 nonzero c2 +c2 letter c2 +c2 delim c2 +c2 rp c2 +c2 lp c2 +c2 whitespace c2 + +// whitespace +s0 whitespace s1 +s1 whitespace s1 + +// If a state has an entry in this table then +// it is considered an accept state. +// "ignore" is a reserved type which indicates +// that the token should be ignored. +TokenTypeTable +id identifier +int integer +str string +delimiter delimiter +ignore ignore +s1 ignore diff --git a/Homework/cs5300/project-two/data/register.table b/Homework/cs5300/project-two/data/register.table new file mode 100644 index 0000000..f22b15e --- /dev/null +++ b/Homework/cs5300/project-two/data/register.table @@ -0,0 +1,33 @@ +// Special tokens are \space, \t and \n. +ClassifierTable +r register +0 digit +1 digit +2 digit +3 digit +4 digit +5 digit +6 digit +7 digit +8 digit +9 digit +\space whitespace +\t whitespace +\n whitespace + +// In the transition table, the first state +// is considered the input state. +TransitionTable +s0 register s1 +s1 digit s2 +s2 digit s2 +s0 whitespace s3 +s3 whitespace s3 + +// If a state has an entry in this table then +// it is considered an accept state. +// "ignore" is a reserved type which indicates +// that the token should be ignored. +TokenTypeTable +s2 register +s3 ignore
\ No newline at end of file diff --git a/Homework/cs5300/project-two/data/test1-pasca.txt b/Homework/cs5300/project-two/data/test1-pasca.txt new file mode 100644 index 0000000..060b972 --- /dev/null +++ b/Homework/cs5300/project-two/data/test1-pasca.txt @@ -0,0 +1,14 @@ +begin +(**) +(* a straightforward comment *) +(** a crazy com*ment02 {}() ****) +program a1 { + var0 is 8; + (* some crazy identifier names *) + var1 var2 variable var1able +} +(* some integers *) +123 1203 +' a crazy string abc(){}*''4013' +end. + diff --git a/Homework/cs5300/project-two/data/test2-pasca.txt b/Homework/cs5300/project-two/data/test2-pasca.txt new file mode 100644 index 0000000..247411a --- /dev/null +++ b/Homework/cs5300/project-two/data/test2-pasca.txt @@ -0,0 +1 @@ +'alks dfl;alsk k''jk{}' diff --git a/Homework/cs5300/project-two/scanner/Main.java b/Homework/cs5300/project-two/scanner/Main.java new file mode 100644 index 0000000..6025afa --- /dev/null +++ b/Homework/cs5300/project-two/scanner/Main.java @@ -0,0 +1,189 @@ +/* + * CS 4481 Compilers + * Project 1 - scanner part 1 + * + * This file contains the method runTests(). You will uncomment tests + * as you develop your scanner code + */ +package scanner; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * A different Main file will be used for grading, so you may make modifications + * as needed, but before submitting you should download a fresh copy of this + * file, uncomment the tests and make sure you pass them all. + */ +public class Main { + + public static void main(String[] args) + throws FileNotFoundException, IOException { + if (args.length > 0) { + scanInputFile(args[0], args[1]); + } else { + runPascaTests(); + + System.out.println(tests.getSuccesses() + "/" + tests.getN() + + " tests succeeded"); + int failures = tests.getFailures(); + + System.exit(failures); + } + } + + private static void runPascaTests() + throws FileNotFoundException, IOException { + // TODO: uncomment tests as you develop code + + //------------------------------------------------------------ + // Test register.table reading, table building and token + // parsing. These tests should pass once you have the Scanner + // constructor implemented. + //------------------------------------------------------------ + + String tableFile = "data/pasca.table"; + TableReader tableReader = new TableReader(tableFile); + Scanner scanner = new Scanner(tableReader); + + { + ScanStream ss = getDataStream("data/test1-pasca.txt"); + + testToken(scanner, ss, "identifier", "begin"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "program"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "a1"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "delimiter", "{"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "var0"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "is"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "integer", "8"); + testToken(scanner, ss, "delimiter", ";"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "var1"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "var2"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "variable"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "var1able"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "delimiter", "}"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "integer", "123"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "integer", "1203"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "string", "' a crazy string abc(){}*''4013'"); + testToken(scanner, ss, "ignore"); + testToken(scanner, ss, "identifier", "end"); + testToken(scanner, ss, "delimiter", "."); + } + { + ScanStream ss = getDataStream("data/test2-pasca.txt"); + testToken(scanner, ss, "string", "'alks dfl;alsk k''jk{}'"); + } + { + ScanStream ss = getDataStream("data/err1-pasca.txt"); + testToken(scanner, ss, "ignore", "(* a comment *)"); + testToken(scanner, ss, "ignore", " "); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err2-pasca.txt"); + testToken(scanner, ss, null); + } + { + ScanStream ss = getDataStream("data/err3-pasca.txt"); + testToken(scanner, ss, null); + } + } + + //-------------------------------------------------------------------------------- + // Utility stuff + // You can ignore code from here to the end of the file. + //-------------------------------------------------------------------------------- + + static Tests tests = new Tests(); + + private static void testToken(Scanner scanner, ScanStream ss, String type, + String lexeme) { + try { + Token t = scanner.nextToken(ss); + if (type == null) { + tests.test(t == null, "expected lexical error"); + } else { + tests.test(t.getType().equals(type), + t.getType() + " not equal to " + type); + } + + if (lexeme != null) { + tests.test(t.getLexeme().equals(lexeme), + t.getLexeme() + " not equal to " + lexeme); + } + } catch (Exception e) { + tests.addFailure(e); + } + } + + private static void testToken(Scanner scanner, ScanStream ss, String type) { + testToken(scanner, ss, type, null); + } + + private static ScanStream getDataStream(String testFile) throws IOException { + File file = new File(testFile); + FileInputStream fis = new FileInputStream(file); + byte[] data = new byte[(int)file.length()]; + fis.read(data); + fis.close(); + ScanStream ss = new ScanStream(data); + return ss; + } + + public static void scanInputFile(String tableFile, String testFile) + throws FileNotFoundException, IOException { + TableReader tableReader = new TableReader(tableFile); + + File file = new File(testFile); + FileInputStream fis = new FileInputStream(file); + byte[] data = new byte[(int)file.length()]; + fis.read(data); + fis.close(); + + PrintWriter writer = new PrintWriter(testFile + ".out", "UTF-8"); + + Scanner scanner = new Scanner(tableReader); + ScanStream ss = new ScanStream(data); + Token t; + do { + t = scanner.nextToken(ss); + if (t == null) { + System.out.println("Lexical error: " + ss.next()); + ss.rollback(); + writer.println("Lexical error: " + ss.next()); + } else if (!t.getType().equals("ignore")) { + System.out.println(t.toString()); + writer.println(t.toString()); + } + } while (t != null && !ss.eof()); + + writer.close(); + } +} diff --git a/Homework/cs5300/project-two/scanner/ScanStream.java b/Homework/cs5300/project-two/scanner/ScanStream.java new file mode 100644 index 0000000..04cff68 --- /dev/null +++ b/Homework/cs5300/project-two/scanner/ScanStream.java @@ -0,0 +1,36 @@ +/* + * DO NOT MODIFY THIS FILE + */ +package scanner; + +/** + * Abstracts a stream reader that reads one byte at a time with possible + * rollback. + * + * Do not modify this file. + */ +public final class ScanStream { + + final private byte[] data; + private int i; + + public ScanStream(byte[] data) { + this.data = data; + i = 0; + } + + public boolean eof() { + return i >= data.length; + } + + public char next() { + if (eof()) { + throw new RuntimeException("EOF reached"); + } + return (char) data[i++]; + } + + void rollback() { + --i; + } +} diff --git a/Homework/cs5300/project-two/scanner/Scanner.java b/Homework/cs5300/project-two/scanner/Scanner.java new file mode 100644 index 0000000..5681932 --- /dev/null +++ b/Homework/cs5300/project-two/scanner/Scanner.java @@ -0,0 +1,166 @@ +package scanner; + +import java.util.HashMap; +import java.util.Stack; + +/** + * This is the file you will modify. + */ +public class Scanner { + + //------------------------------------------------------------ + // TODO: declare the HashMaps that you will use to store + // your tables. Also declare the start state. + //------------------------------------------------------------ + + // Category table + final HashMap<Character, String> char2category = new HashMap<>(); + // Transition table + final HashMap<String, HashMap<String, String>> state2category2state = new HashMap<>(); + // Token type table + final HashMap<String, String> state2tokenType = new HashMap<>(); + // Utility + final HashMap<String, String> name2state = new HashMap<>(); + // Start state + private String s0; + + //------------------------------------------------------------ + // TODO: build your tables in the constructor and implement + // the get methods. + //------------------------------------------------------------ + + /** + * Builds the tables needed for the scanner. + */ + public Scanner(TableReader tableReader) { + // TODO: starting with the skeleton code below, build the + // classifer, transition and token type tables. You will need + // to also implement the test functions below once you have your + // tables built. + + // Build catMap, mapping a character to a category. + for (TableReader.CharCat cat : tableReader.getClassifier()) { +// System.out.println("Character " + cat.getC() + " is of category " +// + cat.getCategory()); + char2category.put(cat.getC(), cat.getCategory()); + } + + // Build the transition table. Given a state and a character category, + // give a new state. + for (TableReader.Transition t : tableReader.getTransitions()) { + String from = t.getFromStateName(); + String to = t.getToStateName(); +// System.out.println(from + " -- " + t.getCategory() +// + " --> " + to); + + if (s0 == null) + s0 = from; + + HashMap<String, String> cat2state; + if (state2category2state.containsKey(from)) { + cat2state = state2category2state.get(from); + } else { + cat2state = new HashMap<>(); + state2category2state.put(from, cat2state); + } + cat2state.put(t.getCategory(), to); + } + + // Build the token types + for (TableReader.TokenType tt : tableReader.getTokens()) { +// System.out.println("State " + tt.getState() +// + " accepts with the lexeme being of type " + tt.getType()); + + state2tokenType.put(tt.getState(), tt.getType()); + } + + } + + /** + * Returns the category for c or "not in alphabet" if c has no category. Do not hardcode + * this. That is, this function should have nothing more than a table lookup + * or two. You should not have any character literals in here such as 'r' or '3'. + */ + public String getCategory(Character c) { + if (!char2category.containsKey(c)) return "not in alphabet"; + return char2category.get(c); + } + + /** + * Returns the new state given a current state and category. This models + * the transition table. Returns "error" if there is no transition. + * Do not hardcode any state names or categories. You should have only + * table lookups here. + */ + public String getNewState(String state, String category) { + if (!state2category2state.containsKey(state)) return "error"; + if (!state2category2state.get(state).containsKey(category)) return "error"; + return state2category2state.get(state).get(category); + } + + /** + * Returns the type of token corresponding to a given state. If the state + * is not accepting then return "error". + * Do not hardcode any state names or token types. + */ + public String getTokenType(String state) { + if (!state2tokenType.containsKey(state)) return "error"; + return state2tokenType.get(state); + } + + //------------------------------------------------------------ + // TODO: implement nextToken + //------------------------------------------------------------ + + /** + * Return the next token or null if there's a lexical error. + */ + public Token nextToken(ScanStream ss) { + // TODO: get a single token. This is an implementation of the nextToken + // algorithm given in class. You may *not* use TableReader in this + // function. Return null if there is a lexical error. + + String state = s0; + String lexeme = ""; + Stack<String> stack = new Stack<>(); + String bad = "bad"; + String error = "error"; + stack.push(bad); + while (state != error) { + char c; + try { + c = ss.next(); + System.out.println("current state is " + state + " and next is " + c); + } catch(Exception e) { + state = error; + continue; + } + lexeme += c; + if (state2tokenType.containsKey(state)) + stack.clear(); + stack.push(state); + String category = char2category.get(c); + if (state2category2state.containsKey(state)) { + state = state2category2state.get(state).get(category); + } else { + state = null; + } + if (state == null) { + state = error; + } + System.out.println("Moving to state " + state); + } + while (state != bad && !state2tokenType.containsKey(state)) { + state = stack.pop(); + if (!lexeme.isEmpty()) { + lexeme = lexeme.substring(0, lexeme.length() - 1); + ss.rollback(); + } + } + if (state2tokenType.containsKey(state)) { + return new Token(state2tokenType.get(state), lexeme); + } + return null; + } + +} diff --git a/Homework/cs5300/project-two/scanner/TableParser.java b/Homework/cs5300/project-two/scanner/TableParser.java new file mode 100644 index 0000000..760e312 --- /dev/null +++ b/Homework/cs5300/project-two/scanner/TableParser.java @@ -0,0 +1,160 @@ +/* + * DO NOT MODIFY THIS FILE + */ +package scanner; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * TableParser provides all functionality for parsing the table definition file. + * + * Do not modify this file. + */ +public final class TableParser { + + // CharCat class + // Used in the classifier table + public class CharCat { + + final private char c; + final private String category; + + public CharCat(char c, String category) { + this.c = c; + this.category = category; + } + + public char getC() { + return c; + } + + public String getCategory() { + return category; + } + + } + + // Transition class + // Used in the transition table + public class Transition { + + final private String fromState; + final private String category; + final private String toState; + + public Transition(String fromState, String category, String toState) { + this.fromState = fromState; + this.category = category; + this.toState = toState; + } + + public String getFromStateName() { + return fromState; + } + + public String getCategory() { + return category; + } + + public String getToStateName() { + return toState; + } + } + + // TokenType class + // Used in the token type table + public class TokenType { + + final private String state; + final private String type; + + public TokenType(String state, String type) { + this.state = state; + this.type = type; + } + + public String getState() { + return state; + } + + public String getType() { + return type; + } + } + + private enum WhichTable { + + CLASS, TRANS, TOKEN + }; + + private ArrayList<CharCat> classifier; + private ArrayList<Transition> transitions; + private ArrayList<TokenType> tokens; + + public TableParser(String filename) throws IOException { + parse(filename); + } + + public Iterable<CharCat> getClassifier() { + return classifier; + } + + public Iterable<Transition> getTransitions() { + return transitions; + } + + public Iterable<TokenType> getTokens() { + return tokens; + } + + public void parse(String fn) throws FileNotFoundException, IOException { + this.classifier = new ArrayList<>(); + this.transitions = new ArrayList<>(); + this.tokens = new ArrayList<>(); + + WhichTable table = WhichTable.CLASS; + try (BufferedReader br = new BufferedReader(new FileReader(fn))) { + String line; + while ((line = br.readLine()) != null) { + final int idx = line.indexOf("//"); + if (idx > -1) { + line = line.substring(0, idx); + } + line = line.trim(); + if (!line.isEmpty()) { + if (line.equals("ClassifierTable")) { + table = WhichTable.CLASS; + } else if (line.equals("TransitionTable")) { + table = WhichTable.TRANS; + } else if (line.equals("TokenTypeTable")) { + table = WhichTable.TOKEN; + } else { + String[] lineTokens = line.split(" "); + if (table == WhichTable.CLASS) { + char c = lineTokens[0].charAt(0); + if (lineTokens[0].equals("\\space")) { + c = ' '; + } else if (lineTokens[0].equals("\\t")) { + c = '\t'; + } else if (lineTokens[0].equals("\\n")) { + c = '\n'; + } + CharCat cc = new CharCat(c, lineTokens[1]); + classifier.add(cc); + } else if (table == WhichTable.TRANS) { + Transition t = new Transition(lineTokens[0], lineTokens[1], lineTokens[2]); + transitions.add(t); + } else if (table == WhichTable.TOKEN) { + TokenType tt = new TokenType(lineTokens[0], lineTokens[1]); + tokens.add(tt); + } + } + } + } + } + } +} diff --git a/Homework/cs5300/project-two/scanner/TableReader.java b/Homework/cs5300/project-two/scanner/TableReader.java new file mode 100644 index 0000000..a074547 --- /dev/null +++ b/Homework/cs5300/project-two/scanner/TableReader.java @@ -0,0 +1,160 @@ +/* + * Do not modify this file. + */ +package scanner; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * TableParser provides all functionality for parsing the table definition file. + * + * Do not modify this file. + */ +public final class TableReader { + + // CharCat class + // Used in the classifier table + public class CharCat { + + final private char c; + final private String category; + + public CharCat(char c, String category) { + this.c = c; + this.category = category; + } + + public char getC() { + return c; + } + + public String getCategory() { + return category; + } + + } + + // Transition class + // Used in the transition table + public class Transition { + + final private String fromState; + final private String category; + final private String toState; + + public Transition(String fromState, String category, String toState) { + this.fromState = fromState; + this.category = category; + this.toState = toState; + } + + public String getFromStateName() { + return fromState; + } + + public String getCategory() { + return category; + } + + public String getToStateName() { + return toState; + } + } + + // TokenType class + // Used in the token type table + public class TokenType { + + final private String state; + final private String type; + + public TokenType(String state, String type) { + this.state = state; + this.type = type; + } + + public String getState() { + return state; + } + + public String getType() { + return type; + } + } + + private enum WhichTable { + + CLASS, TRANS, TOKEN + }; + + private ArrayList<CharCat> classifier; + private ArrayList<Transition> transitions; + private ArrayList<TokenType> tokens; + + public TableReader(String filename) throws IOException { + parse(filename); + } + + public Iterable<CharCat> getClassifier() { + return classifier; + } + + public Iterable<Transition> getTransitions() { + return transitions; + } + + public Iterable<TokenType> getTokens() { + return tokens; + } + + public void parse(String fn) throws FileNotFoundException, IOException { + this.classifier = new ArrayList<>(); + this.transitions = new ArrayList<>(); + this.tokens = new ArrayList<>(); + + WhichTable table = WhichTable.CLASS; + try (BufferedReader br = new BufferedReader(new FileReader(fn))) { + String line; + while ((line = br.readLine()) != null) { + final int idx = line.indexOf("//"); + if (idx > -1) { + line = line.substring(0, idx); + } + line = line.trim(); + if (!line.isEmpty()) { + if (line.equals("ClassifierTable")) { + table = WhichTable.CLASS; + } else if (line.equals("TransitionTable")) { + table = WhichTable.TRANS; + } else if (line.equals("TokenTypeTable")) { + table = WhichTable.TOKEN; + } else { + String[] lineTokens = line.split(" "); + if (table == WhichTable.CLASS) { + char c = lineTokens[0].charAt(0); + if (lineTokens[0].equals("\\space")) { + c = ' '; + } else if (lineTokens[0].equals("\\t")) { + c = '\t'; + } else if (lineTokens[0].equals("\\n")) { + c = '\n'; + } + CharCat cc = new CharCat(c, lineTokens[1]); + classifier.add(cc); + } else if (table == WhichTable.TRANS) { + Transition t = new Transition(lineTokens[0], lineTokens[1], lineTokens[2]); + transitions.add(t); + } else if (table == WhichTable.TOKEN) { + TokenType tt = new TokenType(lineTokens[0], lineTokens[1]); + tokens.add(tt); + } + } + } + } + } + } +} diff --git a/Homework/cs5300/project-two/scanner/Tests.java b/Homework/cs5300/project-two/scanner/Tests.java new file mode 100644 index 0000000..19413fa --- /dev/null +++ b/Homework/cs5300/project-two/scanner/Tests.java @@ -0,0 +1,50 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package scanner; + +/** + * A test utility class. + * + * Do not modify this file. + */ +public class Tests { + + private int N = 0; + private int failures = 0; + + public void test(boolean b) { + N++; + if (!b) { + System.err.println("Failed test " + N); + failures++; + } + } + + public void test(boolean b, String message) { + test(b); + if (!b) { + System.err.println(message); + } + } + + public void addFailure(Exception e) { + N++; + failures++; + System.err.println("Failed test " + N + ": " + e); + } + + public int getN() { + return N; + } + + public int getSuccesses() { + return N - failures; + } + + public int getFailures() { + return failures; + } +} diff --git a/Homework/cs5300/project-two/scanner/Token.java b/Homework/cs5300/project-two/scanner/Token.java new file mode 100644 index 0000000..4c0151a --- /dev/null +++ b/Homework/cs5300/project-two/scanner/Token.java @@ -0,0 +1,33 @@ +/* + * DO NOT MODIFY THIS FILE + */ +package scanner; + +/** + * A class that stores a token. + * + * Do not modify this file. + */ +public class Token { + + private final String type; + private final String lexeme; + + public Token(String type, String lexeme) { + this.type = type; + this.lexeme = lexeme; + } + + public String toString() { + return type + ": " + lexeme; + } + + public String getType() { + return type; + } + + public String getLexeme() { + return lexeme; + } + +} |
