Use strings
This commit is contained in:
		
							parent
							
								
									1c3233d272
								
							
						
					
					
						commit
						148abaf5ab
					
				| @ -1,5 +1,6 @@ | |||||||
| %{ | %{ | ||||||
| #include <stack> | #include <stack> | ||||||
|  | #include <string> | ||||||
| #include "syntax.h" | #include "syntax.h" | ||||||
| #include "parser.h" | #include "parser.h" | ||||||
| using namespace std; | using namespace std; | ||||||
| @ -36,12 +37,12 @@ if (s.top() == yyleng) { | |||||||
| } | } | ||||||
| 	       			} | 	       			} | ||||||
| #[^\n]*					// Eat comments | #[^\n]*					// Eat comments | ||||||
| \"[^"]*\"				yylval.sym = yytext; return STRING; | \"[^"]*\"				yylval.sym = new string(yytext); return STRING; | ||||||
| if					return IF; | if					return IF; | ||||||
| else					return ELSE; | else					return ELSE; | ||||||
| elif 					return ELIF; | elif 					return ELIF; | ||||||
| {digit}+|{digit}*\.{digit}+		yylval.num = atof(yytext); return NUM; | {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 GETS; | ||||||
| "->"					return MAPS; | "->"					return MAPS; | ||||||
| ".."					return CAT; | ".."					return CAT; | ||||||
|  | |||||||
| @ -1,5 +1,7 @@ | |||||||
| %{ | %{ | ||||||
|  | 
 | ||||||
| #include <cstdio> | #include <cstdio> | ||||||
|  | #include <string> | ||||||
| 
 | 
 | ||||||
| #include "syntax.h" | #include "syntax.h" | ||||||
| 
 | 
 | ||||||
| @ -7,13 +9,14 @@ using namespace std; | |||||||
| 
 | 
 | ||||||
| int yylex(); | int yylex(); | ||||||
| void yyerror(const char* p) { fprintf(stderr, p); } | void yyerror(const char* p) { fprintf(stderr, p); } | ||||||
|  | 
 | ||||||
| %} | %} | ||||||
| 
 | 
 | ||||||
| %define parse.trace | %define parse.trace | ||||||
| 
 | 
 | ||||||
| %union { | %union { | ||||||
|        float num; |        float num; | ||||||
|        char *sym; |        std::string *sym; | ||||||
|        Node *expr; |        Node *expr; | ||||||
|        NodeList *exprlist; |        NodeList *exprlist; | ||||||
| } | } | ||||||
| @ -29,7 +32,7 @@ void yyerror(const char* p) { fprintf(stderr, p); } | |||||||
| %right GETS | %right GETS | ||||||
| %left CAT | %left CAT | ||||||
| %left '+' '-' | %left '+' '-' | ||||||
| %left '/' '*'n | %left '/' '*' | ||||||
| %left MAPS | %left MAPS | ||||||
| 
 | 
 | ||||||
| %nterm <expr> expr | %nterm <expr> expr | ||||||
| @ -62,7 +65,7 @@ param:	 	ID | '_' | // null production; | |||||||
| funcall: 	ID '(' exprlist ')' { print_expression_list($3); } | funcall: 	ID '(' exprlist ')' { print_expression_list($3); } | ||||||
| exprlist:	exprlist ',' expr { $1 -> push_back($3); $$ = $1; } | exprlist:	exprlist ',' expr { $1 -> push_back($3); $$ = $1; } | ||||||
| 		| expr { $$ = new NodeList(); $$ -> push_back($1); } | 		| expr { $$ = new NodeList(); $$ -> push_back($1); } | ||||||
| 		| { $$ = NULL; } // null production; | 		| { $$ = new NodeList(); } // null production; | ||||||
| 
 | 
 | ||||||
| conditional: 	IF expr block elifs | conditional: 	IF expr block elifs | ||||||
| 		| IF expr block elifs ELSE block; | 		| IF expr block elifs ELSE block; | ||||||
|  | |||||||
| @ -6,12 +6,12 @@ | |||||||
| 
 | 
 | ||||||
| using namespace std; | using namespace std; | ||||||
| 
 | 
 | ||||||
| IdNode::IdNode(char * id) { | IdNode::IdNode(string *id) { | ||||||
|   this->id = string(id); |   this->id = id; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void IdNode::display() { | void IdNode::display() { | ||||||
|   cout << id; |   cout << *id; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| NumNode::NumNode(float num) { | NumNode::NumNode(float num) { | ||||||
| @ -22,8 +22,8 @@ void NumNode::display() { | |||||||
|    |    | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| StringNode::StringNode(char *str) { | StringNode::StringNode(string* stringVal) { | ||||||
|   this->stringVal = str; |   this->stringVal = stringVal; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void StringNode::display() { | void StringNode::display() { | ||||||
|  | |||||||
| @ -11,9 +11,9 @@ public: | |||||||
| 
 | 
 | ||||||
| class IdNode : public Node { | class IdNode : public Node { | ||||||
| private: | private: | ||||||
|   std::string id; |   std::string *id; | ||||||
| public: | public: | ||||||
|   IdNode(char*); |   IdNode(std::string*); | ||||||
|   virtual void display(); |   virtual void display(); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -27,9 +27,9 @@ public: | |||||||
| 
 | 
 | ||||||
| class StringNode : public Node { | class StringNode : public Node { | ||||||
| private: | private: | ||||||
|   char* stringVal; |   std::string* stringVal; | ||||||
| public: | public: | ||||||
|   StringNode(char*); |   StringNode(std::string*); | ||||||
|   virtual void display(); |   virtual void display(); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user