From 86359ebc54f39de4684061f8d33d54db5181e434 Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Wed, 30 Sep 2020 16:54:58 +0530 Subject: [PATCH] Add more samples, simplify grammar, add groups --- .vscode/launch.json | 39 +++++++++++--- grammar/bf.g4 | 61 +++++++++++++++++----- include/main.hpp | 22 +++++++- samples/group.bfe | 2 + sample.bfe => samples/simple.bfe | 1 + src/main.cpp | 88 +++++++++++++++++++++----------- 6 files changed, 164 insertions(+), 49 deletions(-) create mode 100644 samples/group.bfe rename sample.bfe => samples/simple.bfe (99%) diff --git a/.vscode/launch.json b/.vscode/launch.json index efdd34b..3ff6721 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,17 +7,44 @@ { "type": "antlr-debug", "request": "launch", - "name": "Debug Current Grammar", - "input": "sample.bfe", + "name": "Grammar - simple.bfe", + "input": "samples/simple.bfe", "visualParseTree": true, - "grammar": "${file}" + "grammar": "${workspaceFolder}/grammar/bf.g4" }, { - "name": "(gdb) Launch", + "type": "antlr-debug", + "request": "launch", + "name": "Grammar - group.bfe", + "input": "samples/group.bfe", + "visualParseTree": true, + "grammar": "${workspaceFolder}/grammar/bf.g4" + }, + { + "name": "GDB - simple.bfe", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/debug/main.out", - "args": ["sample.txt"], + "program": "${workspaceFolder}/build/main.out", + "args": ["samples/simple.bfe"], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + }, + { + "name": "GDB - group.bfe", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/main.out", + "args": ["samples/group.bfe"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], diff --git a/grammar/bf.g4 b/grammar/bf.g4 index 95a7104..5b66dd7 100644 --- a/grammar/bf.g4 +++ b/grammar/bf.g4 @@ -1,28 +1,62 @@ grammar bf; program - : statements* EOF; + : statements? EOF; statements - : (statement|numberedStatement|loopStmt)+; + : eligibleStmt+; + +eligibleStmt + : stmt + | numberedStmt + ; + +numberedStmt + : stmt NUMBER + ; + + +stmt + : basicStmt + | groupedStmt + | loopStmt + ; + + +groupedStmt + : GRPSTART statements GRPEND + ; loopStmt - : LOOPSTART statements LOOPEND + : LOOPSTART statements LOOPEND ; -numberedStatement - : statement NUMBER - ; - -statement - : INC - | DEC - | LEFT - | RIGHT +basicStmt + : ptrIncr + | ptrDecr + | ptrLeft + | ptrRight | inputStmt | outputStmt ; + +ptrIncr + : INC + ; + +ptrDecr + : DEC + ; + +ptrLeft + : LEFT + ; + +ptrRight + : RIGHT + ; + inputStmt : INPUT ; @@ -34,8 +68,11 @@ outputStmt NEWLINE: '\n' -> skip; COMMENT: '//' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN); WS: [ \r\n] -> skip; +DEF: '#'; LOOPSTART: '['; LOOPEND:']'; +GRPSTART:'('; +GRPEND:')'; NUMBER: [0-9]+; INPUT: '?'; OUTPUT: '.'; diff --git a/include/main.hpp b/include/main.hpp index 3d6de1d..d30286d 100644 --- a/include/main.hpp +++ b/include/main.hpp @@ -1,3 +1,23 @@ #pragma once -#define FOO 1 +#include +#include "bfBaseListener.h" + + +class expressionPrintingListener : public bfBaseListener { + protected: + std::vector printStack; + public: + void enterProgram(bfParser::ProgramContext *ctx) override; + void exitProgram(bfParser::ProgramContext *ctx) override ; + void enterPtrIncr(bfParser::PtrIncrContext *ctx) override; + void enterPtrDecr(bfParser::PtrDecrContext *ctx) override; + void enterPtrLeft(bfParser::PtrLeftContext *ctx) override; + void enterPtrRight(bfParser::PtrRightContext *ctx) override; + void enterNumberedStmt(bfParser::NumberedStmtContext *ctx) override; + void exitNumberedStmt(bfParser::NumberedStmtContext *ctx) override; + void enterLoopStmt(bfParser::LoopStmtContext *ctx) override; + void exitLoopStmt(bfParser::LoopStmtContext *ctx) override; + void enterGroupedStmt(bfParser::GroupedStmtContext *ctx) override; + void exitGroupedStmt(bfParser::GroupedStmtContext *ctx) override; +}; \ No newline at end of file diff --git a/samples/group.bfe b/samples/group.bfe new file mode 100644 index 0000000..8160e0e --- /dev/null +++ b/samples/group.bfe @@ -0,0 +1,2 @@ +//should print ++++++ +(+2)3 \ No newline at end of file diff --git a/sample.bfe b/samples/simple.bfe similarity index 99% rename from sample.bfe rename to samples/simple.bfe index 517b87c..b2b764b 100644 --- a/sample.bfe +++ b/samples/simple.bfe @@ -2,4 +2,5 @@ -[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8. + // courtesy of https://copy.sh/brainfuck/text.html \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 636d697..201f6c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include "main.hpp" #include "bfLexer.h" @@ -8,44 +10,70 @@ using namespace antlr4; -class expressionPrintingListener : public bfBaseListener +void expressionPrintingListener::enterProgram(bfParser::ProgramContext *ctx) { -public: - void enterStatement(bfParser::StatementContext *ctx) override + printStack.push_back(""); +} +void expressionPrintingListener::exitProgram(bfParser::ProgramContext *ctx) +{ + std::cout << printStack.front() << std::endl; +} +void expressionPrintingListener::enterPtrIncr(bfParser::PtrIncrContext *ctx) +{ + printStack.back() += "+"; +} +void expressionPrintingListener::enterPtrDecr(bfParser::PtrDecrContext *ctx) +{ + printStack.back() += ("-"); +} +void expressionPrintingListener::enterPtrLeft(bfParser::PtrLeftContext *ctx) +{ + printStack.back() += ("<"); +} +void expressionPrintingListener::enterPtrRight(bfParser::PtrRightContext *ctx) +{ + printStack.back() += (">"); +} + +void expressionPrintingListener::enterNumberedStmt(bfParser::NumberedStmtContext *ctx) +{ + printStack.push_back(""); +} +void expressionPrintingListener::exitNumberedStmt(bfParser::NumberedStmtContext *ctx) +{ + std::string s = printStack.back(); + printStack.pop_back(); + int n = stoi(ctx->NUMBER()->getText()); + for (int i = 0; i < n; i++) { - std::cout << ctx->getText(); + printStack.back() += s; } - void enterNumberedStatement(bfParser::NumberedStatementContext *ctx) override - { - int num = 0; - try{ - num = stoi(ctx->NUMBER()->getText()); - }catch(std::exception e){ - num = 1; - } - for (int i = 0; i < num - 1; i++) - { - std::cout << ctx->statement()->getText(); - } - } - void exitProgram(bfParser::ProgramContext *ctx) override{ - std::cout<