summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/parser.hpp28
-rw-r--r--parser.cpp119
2 files changed, 147 insertions, 0 deletions
diff --git a/include/parser.hpp b/include/parser.hpp
new file mode 100644
index 0000000..02471d8
--- /dev/null
+++ b/include/parser.hpp
@@ -0,0 +1,28 @@
+/* PIA - Lenguajes Modernos de Programación
+ * FACULTAD DE CIENCIAS FÍSICO MATEMÁTICAS
+ * Luis Sebastián Martínez Vega - LCC */
+
+#include <string>
+#include <vector>
+#include "lexer.hpp"
+#ifndef PARSER_H
+#define PARSER_H
+
+class Parser{
+private:
+ std::string text;
+ Lexer tokenizer;
+ void setText(std::string text);
+ void parseExpr();
+ void parsePrimeExpr();
+ void parseTerm();
+ void parsePrimeTerm();
+ void parsePower();
+ void parsePrimePower();
+ void parseFactor();
+ bool checkToken(); // Checa el token actual del lexer.
+public:
+ void parse(std::string expr);
+};
+
+#endif /* PARSER_H */
diff --git a/parser.cpp b/parser.cpp
new file mode 100644
index 0000000..8cba19e
--- /dev/null
+++ b/parser.cpp
@@ -0,0 +1,119 @@
+#include <string>
+#include "include/parser.hpp"
+#include "include/lexer.hpp"
+#include "include/exceptions.hpp"
+
+using std::string;
+
+void Parser::setText(string text){
+ tokenizer.setText(text); // Se prepara el lexer.
+}
+
+bool Parser::checkToken(){
+ if(tokenizer.getCurrentToken().type == nil)
+ return false;
+
+ return true;
+}
+
+void Parser::parse(string expr){
+ setText(expr);
+ parseExpr();
+}
+
+void Parser::parseExpr(){
+ parseTerm();
+ parsePrimeExpr();
+}
+
+void Parser::parsePrimeExpr(){
+ if(tokenizer.getCurrentToken().type == sum
+ || tokenizer.getCurrentToken().type == substraction)
+ {
+ tokenizer.nextToken();
+
+ if(!checkToken())
+ throw ParserException("Carácter inválido.");
+
+ parseTerm();
+ parsePrimeExpr();
+ }
+}
+
+void Parser::parseTerm(){
+ parsePower();
+ parsePrimeTerm();
+}
+
+void Parser::parsePrimeTerm(){
+ if(tokenizer.getCurrentToken().type == multiplication
+ || tokenizer.getCurrentToken().type == division)
+ {
+ tokenizer.nextToken();
+
+ if(!checkToken())
+ throw ParserException("Carácter inválido.");
+
+ parsePower();
+ parsePrimeTerm();
+ }
+}
+
+void Parser::parsePower(){
+ parseFactor();
+ parsePrimePower();
+}
+
+void Parser::parsePrimePower(){
+ if(tokenizer.getCurrentToken().type == power)
+ {
+ tokenizer.nextToken();
+
+ if(tokenizer.getCurrentToken().type != number)
+ throw ParserException("Sintaxis incorrecta.");
+
+ parsePrimePower();
+ }
+}
+
+void Parser::parseFactor(){
+ switch(tokenizer.getCurrentToken().type)
+ {
+ case substraction:
+ tokenizer.nextToken();
+ parseExpr();
+ break;
+ case left_parens:
+ tokenizer.nextToken();
+ parseExpr();
+
+ // Consumimos paréntesis derecho.
+ if(tokenizer.getCurrentToken().type != right_parens)
+ throw ParserException("Sintaxis incorrecta.");
+ break;
+ case function:
+ tokenizer.nextToken();
+
+ if(tokenizer.getCurrentToken().type != left_parens)
+ throw ParserException("Sintaxis incorrecta.");
+
+ parseExpr();
+
+ if(tokenizer.getCurrentToken().type != right_parens)
+ throw ParserException("Sintaxis incorrecta.");
+
+ break;
+ case variable:
+ tokenizer.nextToken();
+
+ break;
+ case number:
+ tokenizer.nextToken();
+
+ break;
+ default:
+ throw ParserException("Sintaxis incorrecta.");
+
+ break;
+ }
+}