diff --git a/src/lexer.l b/src/lexer.l index f7fb4db..162a4bc 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -1,5 +1,6 @@ %{ #include +#include #include "syntax.h" #include "parser.h" using namespace std; @@ -36,12 +37,12 @@ if (s.top() == yyleng) { } } #[^\n]* // Eat comments -\"[^"]*\" yylval.sym = yytext; return STRING; +\"[^"]*\" yylval.sym = new string(yytext); return STRING; if return IF; else return ELSE; elif return ELIF; {digit}+|{digit}*\.{digit}+ yylval.num = atof(yytext); return NUM; -{letter}({letter}|{digit}|[?.-])* yylval.sym = yytext; return ID; +{letter}({letter}|{digit}|[?.-])* yylval.sym = new string(yytext); return ID; "<-" return GETS; "->" return MAPS; ".." return CAT; diff --git a/src/parser.y b/src/parser.y index 0dcc3bb..371c9b3 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,5 +1,7 @@ %{ + #include +#include #include "syntax.h" @@ -7,13 +9,14 @@ using namespace std; int yylex(); void yyerror(const char* p) { fprintf(stderr, p); } + %} %define parse.trace %union { float num; - char *sym; + std::string *sym; Node *expr; NodeList *exprlist; } @@ -29,7 +32,7 @@ void yyerror(const char* p) { fprintf(stderr, p); } %right GETS %left CAT %left '+' '-' -%left '/' '*'n +%left '/' '*' %left MAPS %nterm expr @@ -62,7 +65,7 @@ param: ID | '_' | // null production; funcall: ID '(' exprlist ')' { print_expression_list($3); } exprlist: exprlist ',' expr { $1 -> push_back($3); $$ = $1; } | expr { $$ = new NodeList(); $$ -> push_back($1); } - | { $$ = NULL; } // null production; + | { $$ = new NodeList(); } // null production; conditional: IF expr block elifs | IF expr block elifs ELSE block; diff --git a/src/syntax.cpp b/src/syntax.cpp index 2272fb2..a6733d4 100644 --- a/src/syntax.cpp +++ b/src/syntax.cpp @@ -6,12 +6,12 @@ using namespace std; -IdNode::IdNode(char * id) { - this->id = string(id); +IdNode::IdNode(string *id) { + this->id = id; } void IdNode::display() { - cout << id; + cout << *id; } NumNode::NumNode(float num) { @@ -22,8 +22,8 @@ void NumNode::display() { } -StringNode::StringNode(char *str) { - this->stringVal = str; +StringNode::StringNode(string* stringVal) { + this->stringVal = stringVal; } void StringNode::display() { diff --git a/src/syntax.h b/src/syntax.h index fffa45a..fe75316 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -11,9 +11,9 @@ public: class IdNode : public Node { private: - std::string id; + std::string *id; public: - IdNode(char*); + IdNode(std::string*); virtual void display(); }; @@ -27,9 +27,9 @@ public: class StringNode : public Node { private: - char* stringVal; + std::string* stringVal; public: - StringNode(char*); + StringNode(std::string*); virtual void display(); };