Skip to content

Commit

Permalink
We now build a syntax tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtahu committed Apr 26, 2013
1 parent 4537c91 commit 0b8cdf4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
24 changes: 13 additions & 11 deletions LangS.l
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <iostream>
#include <stdlib.h>

#define YYSTYPE std::string
#define YYSTYPE ParseTree::Node*

#include "ParseTree.h"
#include "LangS.tab.hpp"

void yyerror(std::string s);
Expand All @@ -17,18 +18,19 @@ void yyerror(std::string s);
exit {return Exit; }
print {return Print; }
'.*' {
yylval = yytext;
yylval.erase(yylval.begin());
yylval.erase(yylval.end()-1);
std::string text=yytext;
text.erase(text.begin());
text.erase(text.end()-1);
yylval = new ParseTree::StringValue(text);
return STRING;
}
[[:digit:]]+ {yylval = yytext; return STRING; }
$[[:alpha:]]+ {yylval = yytext; return Scalar; }
%[[:alpha:]]+ {yylval = yytext; return Vector; }
[-] {yylval = '-'; return BinOP; }
[+] {yylval = '+'; return BinOP; }
[\*] {yylval = '*'; return BinOP; }
[\\/] {yylval = '/'; return BinOP; }
[[:digit:]]+ {yylval = new ParseTree::StringValue(yytext); return STRING; }
$[[:alpha:]]+ {yylval = new ParseTree::Identifier(yytext); return Scalar; }
%[[:alpha:]]+ {yylval = new ParseTree::Identifier(yytext); return Vector; }
[-] {yylval = new ParseTree::Identifier("-"); return BinOP; }
[+] {yylval = new ParseTree::Identifier("+"); return BinOP; }
[\*] {yylval = new ParseTree::Identifier("*"); return BinOP; }
[\\/] {yylval = new ParseTree::Identifier("/"); return BinOP; }
[[:punct:]] {return *yytext; }
[[:space:]]+ ; /* ignore whitespace */
. { yyerror("Unhandeled character "+std::string(yytext)); };
Expand Down
14 changes: 8 additions & 6 deletions LangS.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <iostream>

#include "LangS.h"
#include "ParseTree.h"

#define YYSTYPE string
#define YYSTYPE Node*
using namespace std;
using namespace ParseTree;

std::ofstream out;
void yyerror(std::string s);
Expand All @@ -29,15 +31,15 @@ int yylex (void);
Prog: Stm ';' Prog
| Stm ';'

Stm: Exit { }
| Scalar '=' Value { setValue($1, $3); }
Stm: Exit { $$ = new ExitStatement();}
| Scalar '=' Value { $$ = new AssingmentStatement((Identifier*)$1,(Value*)$3); }
| Vector '=' List { }
| Value { }
| Print Value { cout<< $2 <<endl; }
| Print Value { $$ = new PrintStatement((Value*)$2); }

Value: Scalar { $$ = getValue($1); }
Value: Scalar { $$ = $1; }
| STRING { $$ = $1; }
| Value BinOP Value { $$ = binOP($2,$1,$3); }
| Value BinOP Value { $$ = new BinOpValue(((Identifier*)$2)->getName(),(Value*)$1,(Value*)$3); }

List: '[' Items ']'

Expand Down
21 changes: 21 additions & 0 deletions ParseTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* ParseTree.h
*
* Created on: Apr 26, 2013
* Author: jimtahu
*/

#ifndef PARSETREE_H_
#define PARSETREE_H_

#include "Node.h"
#include "Value.h"
#include "Identifier.h"
#include "StringValue.h"
#include "BinOpValue.h"
#include "Statement.h"
#include "AssingmentStatement.h"
#include "ExitStatement.h"
#include "PrintStatement.h"

#endif /* PARSETREE_H_ */
1 change: 0 additions & 1 deletion Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace ParseTree {
using namespace std;

Statement::Statement() {
cerr << "Creation of abstract statement" << endl;
}

void Statement::Execute(){
Expand Down

0 comments on commit 0b8cdf4

Please sign in to comment.