From 404826e78a56e15e20d3938aed80945295921745 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 22 Jun 2024 16:45:04 -0600 Subject: Add parser --- src/include/calculator.hpp | 6 ++++++ src/include/operand.hpp | 10 ++++++++++ src/include/parser.hpp | 24 ++++++++++++++++++++++++ src/include/product.hpp | 9 +++++++++ src/include/substraction.hpp | 8 ++++++++ src/include/sum.hpp | 8 ++++++++ src/include/syntax_tree.hpp | 12 ++++++++++++ src/include/tokenizer.hpp | 17 +++++++++-------- 8 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 src/include/operand.hpp create mode 100644 src/include/parser.hpp create mode 100644 src/include/product.hpp create mode 100644 src/include/substraction.hpp create mode 100644 src/include/sum.hpp create mode 100644 src/include/syntax_tree.hpp (limited to 'src/include') diff --git a/src/include/calculator.hpp b/src/include/calculator.hpp index 8c5e97a..2362258 100644 --- a/src/include/calculator.hpp +++ b/src/include/calculator.hpp @@ -8,9 +8,11 @@ class Calculator { private: + // Private attributes. LCD_I2C *display; Keypad *default_keypad; Tokenizer tokenizer = Tokenizer(); + Token **tokens; char default_keypad_chars[4][4] = { {'1', '2', '3', '+'}, {'4', '5', '6', '-'}, @@ -25,6 +27,10 @@ private: }; uint keypad_col_pins[4] = {6, 7, 8, 9}; uint keypad_row_pins[4] = {16, 17, 18, 19}; + // End of private attributes. + // Private methods. + void processInputs(); + // End of private methods. public: Calculator(); ~Calculator(); diff --git a/src/include/operand.hpp b/src/include/operand.hpp new file mode 100644 index 0000000..cc638de --- /dev/null +++ b/src/include/operand.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "syntax_tree.hpp" + + +class Operand: SyntaxTree { + int value; +public: + Operand(int value); + float eval() const; +}; diff --git a/src/include/parser.hpp b/src/include/parser.hpp new file mode 100644 index 0000000..7bc7997 --- /dev/null +++ b/src/include/parser.hpp @@ -0,0 +1,24 @@ +/* Grammar + * Expression --> Term PrimeExpression + * PrimeExpression --> (+|-)Expression | Null + * Term --> Factor PrimeTerm + * PrimeTerm --> (*|/)Term | Null + * Factor --> -Expression | (Expression) | operand */ +#pragma once +#include "tokenizer.hpp" +#include "syntax_tree.hpp" + + +class Parser { +private: + // Private methods. + void parseExpression(); + void parsePrimeExpression(); + void parseTerm(); + void parsePrimeTerm(); + void parseFactor(); + // End of private methods. +public: + Parser(); + const SyntaxTree *parse(); +}; diff --git a/src/include/product.hpp b/src/include/product.hpp new file mode 100644 index 0000000..1a87f80 --- /dev/null +++ b/src/include/product.hpp @@ -0,0 +1,9 @@ +#pragma once +#include "syntax_tree.hpp" + + +class Product: SyntaxTree { +public: + Product(SyntaxTree *left, SyntaxTree *right); + float eval() const; +}; diff --git a/src/include/substraction.hpp b/src/include/substraction.hpp new file mode 100644 index 0000000..4a5a8c7 --- /dev/null +++ b/src/include/substraction.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "syntax_tree.hpp" + +class Substraction: SyntaxTree { +public: + Substraction(SyntaxTree *left, SyntaxTree *right); + float eval() const; +}; diff --git a/src/include/sum.hpp b/src/include/sum.hpp new file mode 100644 index 0000000..35d6736 --- /dev/null +++ b/src/include/sum.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "syntax_tree.hpp" + +class Sum: SyntaxTree { +public: + Sum(SyntaxTree *left, SyntaxTree *right); + float eval() const; +}; diff --git a/src/include/syntax_tree.hpp b/src/include/syntax_tree.hpp new file mode 100644 index 0000000..9edf23c --- /dev/null +++ b/src/include/syntax_tree.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "tokenizer.hpp" + +class SyntaxTree { +protected: + const SyntaxTree *left {}; + const SyntaxTree *right {}; +public: + SyntaxTree(SyntaxTree *left = nullptr, SyntaxTree *right = nullptr); + virtual ~SyntaxTree(); + virtual float eval() const = 0; +}; diff --git a/src/include/tokenizer.hpp b/src/include/tokenizer.hpp index a3f9998..7d00055 100644 --- a/src/include/tokenizer.hpp +++ b/src/include/tokenizer.hpp @@ -1,7 +1,7 @@ #pragma once #include #include -#include +#define TOKENS_ARRAY_LEN 16 enum Type { operand, @@ -11,7 +11,8 @@ enum Type { division, left_parens, right_parens, - equals + equals, + nil }; struct Token { @@ -22,14 +23,14 @@ struct Token { class Tokenizer { private: // Private attributes - std::array tokens; + Token *tokens[17]; const std::string *to_tokenize; size_t current_char_index; short tokens_head; // Regexes - std::regex operand_regex = std::regex("^0{1}|\\d+"); - std::regex operation_regex = std::regex("\\+|-|/|\\*"); - std::regex parens_regex = std::regex("(|)"); + std::regex operand_regex = std::regex("^0{1}|^\\d+"); + std::regex operation_regex = std::regex("^\\+|^-|/|^\\*"); + std::regex parens_regex = std::regex("^\\(|^\\)"); // End of private attributes // Private methods void clearTokens(); @@ -42,6 +43,6 @@ private: public: Tokenizer(); ~Tokenizer(); - const std::array &tokenize(const std::string &operation); - const std::array &getTokens(); + Token **tokenize(const std::string &operation); + Token **getTokens(); }; -- cgit v1.2.3