Skip to content

Commit

Permalink
Split vars into Scalar and Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtahu committed Apr 25, 2013
1 parent 73dbbe6 commit abe845a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 5 additions & 1 deletion LangS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using namespace std;

void yyerror(std::string s);
int yylex (void);
int yyparse (void);

map<string, string> varTable;

Expand Down Expand Up @@ -87,3 +87,7 @@ string binOP(string op, string a, string b){
}else return a+b;
}//end binOP

int main(int argc, char *argv[]) {
yyparse();
return 0;
}//end main
6 changes: 4 additions & 2 deletions LangS.l
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ void yyerror(std::string s);

%%

exit {return Exit; }
print {return Print; }
'.*' {
yylval = yytext;
yylval.erase(yylval.begin());
yylval.erase(yylval.end()-1);
return STRING;
}
$[[:alpha:]]+ {yylval = yytext; return VAR; }
[[:digit:]]+ {yylval = yytext; return NUM; }
[[:digit:]]+ {yylval = yytext; return STRING; }
$[[:alpha:]]+ {yylval = yytext; return Scalar; }
%[[:alpha:]]+ {yylval = yytext; return Vector; }
[-] {yylval = '-'; return BinOP; }
[+] {yylval = '+'; return BinOP; }
[\*] {yylval = '*'; return BinOP; }
Expand Down
23 changes: 13 additions & 10 deletions LangS.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ int yylex (void);

%}

%token VAR
%token NUM
%token Scalar
%token Vector
%token STRING
%token BinOP
%token Print
%token Exit

%left BinOP

Expand All @@ -28,15 +29,21 @@ int yylex (void);
Prog: Stm ';' Prog
| Stm ';'

Stm: VAR '=' Value { setValue($1, $3); }
| Value { }
Stm: Exit { }
| Scalar '=' Value { setValue($1, $3); }
| Vector '=' List { }
| Value { }
| Print Value { cout<< $2 <<endl; }

Value: VAR { $$ = getValue($1); }
| NUM { $$ = $1; }
Value: Scalar { $$ = getValue($1); }
| STRING { $$ = $1; }
| Value BinOP Value { $$ = binOP($2,$1,$3); }

List: '[' Items ']'

Items: Items ',' Value
| Value

%%

using namespace std;
Expand All @@ -45,8 +52,4 @@ void yyerror(std::string s) {
cerr<<s<<endl;
}

int main(int argc, char *argv[]) {
yyparse();
return 0;
}

0 comments on commit abe845a

Please sign in to comment.