From 02505f273ea3002bf307242ed93d4e504320a4fc Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Tue, 29 Sep 2020 23:30:58 +0530 Subject: [PATCH] add statement support --- grammar/bf.g4 | 14 ++- lib/generated/bf.tokens | 30 +++--- lib/generated/bfBaseListener.h | 3 + lib/generated/bfBaseVisitor.h | 4 + lib/generated/bfLexer.cpp | 55 ++++++----- lib/generated/bfLexer.h | 3 +- lib/generated/bfLexer.tokens | 30 +++--- lib/generated/bfListener.h | 3 + lib/generated/bfParser.cpp | 161 +++++++++++++++++++++++++++------ lib/generated/bfParser.h | 30 +++++- lib/generated/bfVisitor.h | 2 + src/main.cpp | 4 +- 12 files changed, 251 insertions(+), 88 deletions(-) diff --git a/grammar/bf.g4 b/grammar/bf.g4 index fc91c00..b548dc8 100644 --- a/grammar/bf.g4 +++ b/grammar/bf.g4 @@ -1,9 +1,19 @@ grammar bf; -program: statements; +program: + statements* EOF; -statements: INC | DEC | INPUT|OUTPUT; +statements: + statement+; + +statement: + INC + | DEC + | INPUT + | OUTPUT + ; +NEWLINE: '\n' -> skip; COMMENT: '\\\\.*?\\\\' -> skip; INPUT: '?'; OUTPUT: '.'; diff --git a/lib/generated/bf.tokens b/lib/generated/bf.tokens index bc770ec..f7fd997 100644 --- a/lib/generated/bf.tokens +++ b/lib/generated/bf.tokens @@ -1,14 +1,16 @@ -COMMENT=1 -INPUT=2 -OUTPUT=3 -DEC=4 -INC=5 -LEFT=6 -RIGHT=7 -'\\\\.*?\\\\'=1 -'?'=2 -'.'=3 -'-'=4 -'+'=5 -'>'=6 -'<'=7 +NEWLINE=1 +COMMENT=2 +INPUT=3 +OUTPUT=4 +DEC=5 +INC=6 +LEFT=7 +RIGHT=8 +'\n'=1 +'\\\\.*?\\\\'=2 +'?'=3 +'.'=4 +'-'=5 +'+'=6 +'>'=7 +'<'=8 diff --git a/lib/generated/bfBaseListener.h b/lib/generated/bfBaseListener.h index 9acf9d4..3a8bdcb 100644 --- a/lib/generated/bfBaseListener.h +++ b/lib/generated/bfBaseListener.h @@ -22,6 +22,9 @@ public: virtual void enterStatements(bfParser::StatementsContext * /*ctx*/) override { } virtual void exitStatements(bfParser::StatementsContext * /*ctx*/) override { } + virtual void enterStatement(bfParser::StatementContext * /*ctx*/) override { } + virtual void exitStatement(bfParser::StatementContext * /*ctx*/) override { } + virtual void enterEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { } virtual void exitEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { } diff --git a/lib/generated/bfBaseVisitor.h b/lib/generated/bfBaseVisitor.h index ef1b6a1..7434500 100644 --- a/lib/generated/bfBaseVisitor.h +++ b/lib/generated/bfBaseVisitor.h @@ -23,6 +23,10 @@ public: return visitChildren(ctx); } + virtual antlrcpp::Any visitStatement(bfParser::StatementContext *ctx) override { + return visitChildren(ctx); + } + }; diff --git a/lib/generated/bfLexer.cpp b/lib/generated/bfLexer.cpp index 260dcb0..ef380fd 100644 --- a/lib/generated/bfLexer.cpp +++ b/lib/generated/bfLexer.cpp @@ -60,7 +60,8 @@ atn::ATN bfLexer::_atn; std::vector bfLexer::_serializedATN; std::vector bfLexer::_ruleNames = { - u8"COMMENT", u8"INPUT", u8"OUTPUT", u8"DEC", u8"INC", u8"LEFT", u8"RIGHT" + u8"NEWLINE", u8"COMMENT", u8"INPUT", u8"OUTPUT", u8"DEC", u8"INC", u8"LEFT", + u8"RIGHT" }; std::vector bfLexer::_channelNames = { @@ -72,11 +73,13 @@ std::vector bfLexer::_modeNames = { }; std::vector bfLexer::_literalNames = { - "", u8"'\\\\.*?\\\\'", u8"'?'", u8"'.'", u8"'-'", u8"'+'", u8"'>'", u8"'<'" + "", u8"'\n'", u8"'\\\\.*?\\\\'", u8"'?'", u8"'.'", u8"'-'", u8"'+'", u8"'>'", + u8"'<'" }; std::vector bfLexer::_symbolicNames = { - "", u8"COMMENT", u8"INPUT", u8"OUTPUT", u8"DEC", u8"INC", u8"LEFT", u8"RIGHT" + "", u8"NEWLINE", u8"COMMENT", u8"INPUT", u8"OUTPUT", u8"DEC", u8"INC", + u8"LEFT", u8"RIGHT" }; dfa::Vocabulary bfLexer::_vocabulary(_literalNames, _symbolicNames); @@ -100,30 +103,34 @@ bfLexer::Initializer::Initializer() { _serializedATN = { 0x3, 0x608b, 0xa72a, 0x8133, 0xb9ed, 0x417c, 0x3be7, 0x7786, 0x5964, - 0x2, 0x9, 0x27, 0x8, 0x1, 0x4, 0x2, 0x9, 0x2, 0x4, 0x3, 0x9, 0x3, 0x4, + 0x2, 0xa, 0x2d, 0x8, 0x1, 0x4, 0x2, 0x9, 0x2, 0x4, 0x3, 0x9, 0x3, 0x4, 0x4, 0x9, 0x4, 0x4, 0x5, 0x9, 0x5, 0x4, 0x6, 0x9, 0x6, 0x4, 0x7, 0x9, - 0x7, 0x4, 0x8, 0x9, 0x8, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, - 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x3, 0x3, - 0x3, 0x3, 0x4, 0x3, 0x4, 0x3, 0x5, 0x3, 0x5, 0x3, 0x6, 0x3, 0x6, 0x3, - 0x7, 0x3, 0x7, 0x3, 0x8, 0x3, 0x8, 0x2, 0x2, 0x9, 0x3, 0x3, 0x5, 0x4, - 0x7, 0x5, 0x9, 0x6, 0xb, 0x7, 0xd, 0x8, 0xf, 0x9, 0x3, 0x2, 0x2, 0x2, - 0x26, 0x2, 0x3, 0x3, 0x2, 0x2, 0x2, 0x2, 0x5, 0x3, 0x2, 0x2, 0x2, 0x2, + 0x7, 0x4, 0x8, 0x9, 0x8, 0x4, 0x9, 0x9, 0x9, 0x3, 0x2, 0x3, 0x2, 0x3, + 0x2, 0x3, 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, + 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 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1f, 0x20, 0x8, 0x3, 0x2, + 0x2, 0x20, 0x6, 0x3, 0x2, 0x2, 0x2, 0x21, 0x22, 0x7, 0x41, 0x2, 0x2, + 0x22, 0x8, 0x3, 0x2, 0x2, 0x2, 0x23, 0x24, 0x7, 0x30, 0x2, 0x2, 0x24, + 0xa, 0x3, 0x2, 0x2, 0x2, 0x25, 0x26, 0x7, 0x2f, 0x2, 0x2, 0x26, 0xc, + 0x3, 0x2, 0x2, 0x2, 0x27, 0x28, 0x7, 0x2d, 0x2, 0x2, 0x28, 0xe, 0x3, + 0x2, 0x2, 0x2, 0x29, 0x2a, 0x7, 0x40, 0x2, 0x2, 0x2a, 0x10, 0x3, 0x2, + 0x2, 0x2, 0x2b, 0x2c, 0x7, 0x3e, 0x2, 0x2, 0x2c, 0x12, 0x3, 0x2, 0x2, + 0x2, 0x3, 0x2, 0x3, 0x8, 0x2, 0x2, }; atn::ATNDeserializer deserializer; diff --git a/lib/generated/bfLexer.h b/lib/generated/bfLexer.h index dd4f275..ae7ccb9 100644 --- a/lib/generated/bfLexer.h +++ b/lib/generated/bfLexer.h @@ -12,7 +12,8 @@ class bfLexer : public antlr4::Lexer { public: enum { - COMMENT = 1, INPUT = 2, OUTPUT = 3, DEC = 4, INC = 5, LEFT = 6, RIGHT = 7 + NEWLINE = 1, COMMENT = 2, INPUT = 3, OUTPUT = 4, DEC = 5, INC = 6, LEFT = 7, + RIGHT = 8 }; bfLexer(antlr4::CharStream *input); diff --git a/lib/generated/bfLexer.tokens b/lib/generated/bfLexer.tokens index bc770ec..f7fd997 100644 --- a/lib/generated/bfLexer.tokens +++ b/lib/generated/bfLexer.tokens @@ -1,14 +1,16 @@ -COMMENT=1 -INPUT=2 -OUTPUT=3 -DEC=4 -INC=5 -LEFT=6 -RIGHT=7 -'\\\\.*?\\\\'=1 -'?'=2 -'.'=3 -'-'=4 -'+'=5 -'>'=6 -'<'=7 +NEWLINE=1 +COMMENT=2 +INPUT=3 +OUTPUT=4 +DEC=5 +INC=6 +LEFT=7 +RIGHT=8 +'\n'=1 +'\\\\.*?\\\\'=2 +'?'=3 +'.'=4 +'-'=5 +'+'=6 +'>'=7 +'<'=8 diff --git a/lib/generated/bfListener.h b/lib/generated/bfListener.h index f13ad17..014935d 100644 --- a/lib/generated/bfListener.h +++ b/lib/generated/bfListener.h @@ -20,6 +20,9 @@ public: virtual void enterStatements(bfParser::StatementsContext *ctx) = 0; virtual void exitStatements(bfParser::StatementsContext *ctx) = 0; + virtual void enterStatement(bfParser::StatementContext *ctx) = 0; + virtual void exitStatement(bfParser::StatementContext *ctx) = 0; + }; diff --git a/lib/generated/bfParser.cpp b/lib/generated/bfParser.cpp index 9c806ae..d0628cc 100644 --- a/lib/generated/bfParser.cpp +++ b/lib/generated/bfParser.cpp @@ -38,8 +38,16 @@ bfParser::ProgramContext::ProgramContext(ParserRuleContext *parent, size_t invok : ParserRuleContext(parent, invokingState) { } -bfParser::StatementsContext* bfParser::ProgramContext::statements() { - return getRuleContext(0); +tree::TerminalNode* bfParser::ProgramContext::EOF() { + return getToken(bfParser::EOF, 0); +} + +std::vector bfParser::ProgramContext::statements() { + return getRuleContexts(); +} + +bfParser::StatementsContext* bfParser::ProgramContext::statements(size_t i) { + return getRuleContext(i); } @@ -70,14 +78,29 @@ antlrcpp::Any bfParser::ProgramContext::accept(tree::ParseTreeVisitor *visitor) bfParser::ProgramContext* bfParser::program() { ProgramContext *_localctx = _tracker.createInstance(_ctx, getState()); enterRule(_localctx, 0, bfParser::RuleProgram); + size_t _la = 0; auto onExit = finally([=] { exitRule(); }); try { enterOuterAlt(_localctx, 1); - setState(4); - statements(); + setState(9); + _errHandler->sync(this); + _la = _input->LA(1); + while ((((_la & ~ 0x3fULL) == 0) && + ((1ULL << _la) & ((1ULL << bfParser::INPUT) + | (1ULL << bfParser::OUTPUT) + | (1ULL << bfParser::DEC) + | (1ULL << bfParser::INC))) != 0)) { + setState(6); + statements(); + setState(11); + _errHandler->sync(this); + _la = _input->LA(1); + } + setState(12); + match(bfParser::EOF); } catch (RecognitionException &e) { @@ -95,20 +118,12 @@ bfParser::StatementsContext::StatementsContext(ParserRuleContext *parent, size_t : ParserRuleContext(parent, invokingState) { } -tree::TerminalNode* bfParser::StatementsContext::INC() { - return getToken(bfParser::INC, 0); +std::vector bfParser::StatementsContext::statement() { + return getRuleContexts(); } -tree::TerminalNode* bfParser::StatementsContext::DEC() { - return getToken(bfParser::DEC, 0); -} - -tree::TerminalNode* bfParser::StatementsContext::INPUT() { - return getToken(bfParser::INPUT, 0); -} - -tree::TerminalNode* bfParser::StatementsContext::OUTPUT() { - return getToken(bfParser::OUTPUT, 0); +bfParser::StatementContext* bfParser::StatementsContext::statement(size_t i) { + return getRuleContext(i); } @@ -139,6 +154,92 @@ antlrcpp::Any bfParser::StatementsContext::accept(tree::ParseTreeVisitor *visito bfParser::StatementsContext* bfParser::statements() { StatementsContext *_localctx = _tracker.createInstance(_ctx, getState()); enterRule(_localctx, 2, bfParser::RuleStatements); + + auto onExit = finally([=] { + exitRule(); + }); + try { + size_t alt; + enterOuterAlt(_localctx, 1); + setState(15); + _errHandler->sync(this); + alt = 1; + do { + switch (alt) { + case 1: { + setState(14); + statement(); + break; + } + + default: + throw NoViableAltException(this); + } + setState(17); + _errHandler->sync(this); + alt = getInterpreter()->adaptivePredict(_input, 1, _ctx); + } while (alt != 2 && alt != atn::ATN::INVALID_ALT_NUMBER); + + } + catch (RecognitionException &e) { + _errHandler->reportError(this, e); + _localctx->exception = std::current_exception(); + _errHandler->recover(this, _localctx->exception); + } + + return _localctx; +} + +//----------------- StatementContext ------------------------------------------------------------------ + +bfParser::StatementContext::StatementContext(ParserRuleContext *parent, size_t invokingState) + : ParserRuleContext(parent, invokingState) { +} + +tree::TerminalNode* bfParser::StatementContext::INC() { + return getToken(bfParser::INC, 0); +} + +tree::TerminalNode* bfParser::StatementContext::DEC() { + return getToken(bfParser::DEC, 0); +} + +tree::TerminalNode* bfParser::StatementContext::INPUT() { + return getToken(bfParser::INPUT, 0); +} + +tree::TerminalNode* bfParser::StatementContext::OUTPUT() { + return getToken(bfParser::OUTPUT, 0); +} + + +size_t bfParser::StatementContext::getRuleIndex() const { + return bfParser::RuleStatement; +} + +void bfParser::StatementContext::enterRule(tree::ParseTreeListener *listener) { + auto parserListener = dynamic_cast(listener); + if (parserListener != nullptr) + parserListener->enterStatement(this); +} + +void bfParser::StatementContext::exitRule(tree::ParseTreeListener *listener) { + auto parserListener = dynamic_cast(listener); + if (parserListener != nullptr) + parserListener->exitStatement(this); +} + + +antlrcpp::Any bfParser::StatementContext::accept(tree::ParseTreeVisitor *visitor) { + if (auto parserVisitor = dynamic_cast(visitor)) + return parserVisitor->visitStatement(this); + else + return visitor->visitChildren(this); +} + +bfParser::StatementContext* bfParser::statement() { + StatementContext *_localctx = _tracker.createInstance(_ctx, getState()); + enterRule(_localctx, 4, bfParser::RuleStatement); size_t _la = 0; auto onExit = finally([=] { @@ -146,7 +247,7 @@ bfParser::StatementsContext* bfParser::statements() { }); try { enterOuterAlt(_localctx, 1); - setState(6); + setState(19); _la = _input->LA(1); if (!((((_la & ~ 0x3fULL) == 0) && ((1ULL << _la) & ((1ULL << bfParser::INPUT) @@ -179,15 +280,15 @@ atn::ATN bfParser::_atn; std::vector bfParser::_serializedATN; std::vector bfParser::_ruleNames = { - "program", "statements" + "program", "statements", "statement" }; std::vector bfParser::_literalNames = { - "", "'\\\\.*?\\\\'", "'?'", "'.'", "'-'", "'+'", "'>'", "'<'" + "", "'\n'", "'\\\\.*?\\\\'", "'?'", "'.'", "'-'", "'+'", "'>'", "'<'" }; std::vector bfParser::_symbolicNames = { - "", "COMMENT", "INPUT", "OUTPUT", "DEC", "INC", "LEFT", "RIGHT" + "", "NEWLINE", "COMMENT", "INPUT", "OUTPUT", "DEC", "INC", "LEFT", "RIGHT" }; dfa::Vocabulary bfParser::_vocabulary(_literalNames, _symbolicNames); @@ -210,12 +311,20 @@ bfParser::Initializer::Initializer() { _serializedATN = { 0x3, 0x608b, 0x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0x2, 0x2, 0x2, 0xe, + 0xf, 0x7, 0x2, 0x2, 0x3, 0xf, 0x3, 0x3, 0x2, 0x2, 0x2, 0x10, 0x12, 0x5, + 0x6, 0x4, 0x2, 0x11, 0x10, 0x3, 0x2, 0x2, 0x2, 0x12, 0x13, 0x3, 0x2, + 0x2, 0x2, 0x13, 0x11, 0x3, 0x2, 0x2, 0x2, 0x13, 0x14, 0x3, 0x2, 0x2, + 0x2, 0x14, 0x5, 0x3, 0x2, 0x2, 0x2, 0x15, 0x16, 0x9, 0x2, 0x2, 0x2, + 0x16, 0x7, 0x3, 0x2, 0x2, 0x2, 0x4, 0xb, 0x13, }; atn::ATNDeserializer deserializer; diff --git a/lib/generated/bfParser.h b/lib/generated/bfParser.h index 7c908c4..292db46 100644 --- a/lib/generated/bfParser.h +++ b/lib/generated/bfParser.h @@ -12,11 +12,12 @@ class bfParser : public antlr4::Parser { public: enum { - COMMENT = 1, INPUT = 2, OUTPUT = 3, DEC = 4, INC = 5, LEFT = 6, RIGHT = 7 + NEWLINE = 1, COMMENT = 2, INPUT = 3, OUTPUT = 4, DEC = 5, INC = 6, LEFT = 7, + RIGHT = 8 }; enum { - RuleProgram = 0, RuleStatements = 1 + RuleProgram = 0, RuleStatements = 1, RuleStatement = 2 }; bfParser(antlr4::TokenStream *input); @@ -30,13 +31,16 @@ public: class ProgramContext; - class StatementsContext; + class StatementsContext; + class StatementContext; class ProgramContext : public antlr4::ParserRuleContext { public: ProgramContext(antlr4::ParserRuleContext *parent, size_t invokingState); virtual size_t getRuleIndex() const override; - StatementsContext *statements(); + antlr4::tree::TerminalNode *EOF(); + std::vector statements(); + StatementsContext* statements(size_t i); virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override; virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override; @@ -51,6 +55,22 @@ public: public: StatementsContext(antlr4::ParserRuleContext *parent, size_t invokingState); virtual size_t getRuleIndex() const override; + std::vector statement(); + StatementContext* statement(size_t i); + + virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override; + virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override; + + virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override; + + }; + + StatementsContext* statements(); + + class StatementContext : public antlr4::ParserRuleContext { + public: + StatementContext(antlr4::ParserRuleContext *parent, size_t invokingState); + virtual size_t getRuleIndex() const override; antlr4::tree::TerminalNode *INC(); antlr4::tree::TerminalNode *DEC(); antlr4::tree::TerminalNode *INPUT(); @@ -63,7 +83,7 @@ public: }; - StatementsContext* statements(); + StatementContext* statement(); private: diff --git a/lib/generated/bfVisitor.h b/lib/generated/bfVisitor.h index eada3a7..5a234aa 100644 --- a/lib/generated/bfVisitor.h +++ b/lib/generated/bfVisitor.h @@ -23,6 +23,8 @@ public: virtual antlrcpp::Any visitStatements(bfParser::StatementsContext *context) = 0; + virtual antlrcpp::Any visitStatement(bfParser::StatementContext *context) = 0; + }; diff --git a/src/main.cpp b/src/main.cpp index d4d8c4f..77ad96a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,8 @@ using namespace antlr4; class expressionListener : public bfBaseListener{ public: - void enterProgram(bfParser::ProgramContext *ctx) override{ - std::cout<getText(); + void enterStatements(bfParser::StatementsContext *ctx) override{ + std::cout<getText()<