diff --git a/assignment2/Expected_results.txt b/assign2/Expected_results.txt similarity index 100% rename from assignment2/Expected_results.txt rename to assign2/Expected_results.txt diff --git a/assignment2/assign2.pdf b/assign2/assign2.pdf similarity index 100% rename from assignment2/assign2.pdf rename to assign2/assign2.pdf diff --git a/assignment2/matfunc.yy.lex b/assign2/matfunc.lex similarity index 76% rename from assignment2/matfunc.yy.lex rename to assign2/matfunc.lex index f6d0fde..b4e1c4d 100644 --- a/assignment2/matfunc.yy.lex +++ b/assign2/matfunc.lex @@ -18,19 +18,19 @@ digits [0-9] number [\-]?(0|([1-9]+{digits}*)) /************* Start: add your definitions here*/ - /************* End: add your definitions here */ %% \n return *yytext; /* Start: add your rules here*/ + +REV yylval = "REV";printf("%s",yytext);return REV; +NEG yylval = "NEG";printf("%s",yytext);return NEG; +{number} yylval = (char*)malloc(sizeof(char)*MAXL);strcpy(yylval, yytext);printf("%s",yytext);return NUM; -REV yyval = "REV";return REV; -NEG yyval = "NEG";return NEG; -{number} yylval = (char*)malloc(sizeof(char)*MAXL);strcpy(yylval, yytext);return NUM; -. printf("%c",*yytext); - - /* End: add your rules here*/ +[+\-*\/,\[\]\n()] printf("%s",yytext);return *yytext; {ws} +. printf("missed: '%s'", yytext); + /* End: add your rules here*/ %% void RmWs(char* str){ diff --git a/assignment2/matfunc.y b/assign2/matfunc.y similarity index 97% rename from assignment2/matfunc.y rename to assign2/matfunc.y index b4ef000..e35ff14 100644 --- a/assignment2/matfunc.y +++ b/assign2/matfunc.y @@ -50,7 +50,7 @@ line: '\n' expr: s1 {$$=$1;} ; -s1: s1 '+' s2 {$$=Add($1,$3);printf("plus:%s + %s",$1,$3);} +s1: s1 '+' s2 {$$=Add($1,$3);} | s1 '-' s2 {$$=Minus($1,$3);} | s2 {$$=$1;} ; @@ -68,7 +68,7 @@ s4: MATRIX {$$=$1} MATRIX: '[' COLS ']'{$$=malloc(sizeof(char*)*MAXL);sprintf($$,"[%s]",$2);} ; COLS: ROW {$$=$1;} - | ROW ',' {$$=malloc(sizeof(char*)*(strlen($1)+1));sprintf($$,"%s,",$1);} + | ROW ',' COLS {$$=malloc(sizeof(char*)*(strlen($1)+1));sprintf($$,"%s,%s",$1,$3);} ROW: '[' VEC ']' {$$=malloc(sizeof(char*)*MAXL);sprintf($$,"[%s]",$2);} ; VEC: NUM ',' VEC {$$=malloc(sizeof(char*)*MAXL);sprintf($$,"%s,%s",$1,$3);} @@ -102,10 +102,10 @@ int main(int argc, char *argv[]){ int yyerror(const char* s) { -// extern int yylineno; -// extern char *yytext; -// printf("\n^%d: %s at %s #%d\n", yylineno, s, yytext, (int)(*yytext)); -// return 0; + /*extern int yylineno; + extern char *yytext; + printf("\n^%d: %s at %s #%d\n", yylineno, s, yytext, (int)(*yytext)); + return 0;*/ } char *FFormat(const char* str){ diff --git a/assign2/results.txt b/assign2/results.txt new file mode 100644 index 0000000..f29f7a2 --- /dev/null +++ b/assign2/results.txt @@ -0,0 +1,13 @@ +[[2]] +[[2]] +[[2,4]] +[[2,4],[12,14],[10,10]] +[[5,205,19],[-223,13,25]] +[[6,271,22],[201,25,45]] +[[6,5,4],[3,2,1]] +[[3,6],[9,12]] +[[2,4],[6,8]] +[[2,4,6],[4,6,8]] +[[5,207,19],[1931,13,43]] +[[0,0,0],[0,0,0]] +[[-2,-4,-6],[-2,-4,-6]] diff --git a/assignment2/run.sh b/assign2/run.sh similarity index 59% rename from assignment2/run.sh rename to assign2/run.sh index aab3543..3fc1673 100644 --- a/assignment2/run.sh +++ b/assign2/run.sh @@ -1,7 +1,8 @@ #!/bin/bash +#cd ../../mnt/c/Users/dipsy/Desktop/course/COMP3031/assign2 + bison -d matfunc.y -#flex -o matfunc.lex.yy.c matfunc.lex flex matfunc.lex gcc *.c -o test -lm ./test testcase.txt diff --git a/assignment2/test.sh b/assign2/test.sh similarity index 66% rename from assignment2/test.sh rename to assign2/test.sh index d7d618c..7e4cb7c 100644 --- a/assignment2/test.sh +++ b/assign2/test.sh @@ -1,7 +1,6 @@ #!/bin/bash bison -d matfunc.y -#flex -o matfunc.lex.yy.c matfunc.lex flex matfunc.lex gcc *.c -o test -lm ./test diff --git a/assignment2/testcase.txt b/assign2/testcase.txt similarity index 100% rename from assignment2/testcase.txt rename to assign2/testcase.txt diff --git a/assignment2/matfunc.lex b/assignment2/matfunc.lex deleted file mode 100644 index d115978..0000000 --- a/assignment2/matfunc.lex +++ /dev/null @@ -1,52 +0,0 @@ -%option noyywrap - -%{ -#define YYSTYPE char* -#include "matfunc.tab.h" -#include -#include -#include -#include -#define MAXL 5000 -#include -void RmWs(char* str); - -%} - -ws [ \t]+ -digits [0-9] -number [\-]?(0|([1-9]+{digits}*)) - /************* Start: add your definitions here*/ -matric \[ *( *\[ *( *-?[0-9]+ *, *)* *-?[0-9]+ *\] *, *)*( *\[ *( *-?[0-9]+ *, *)* *-?[0-9]+ *\] *) *\] - - /************* End: add your definitions here */ -%% -\n return *yytext; - /* Start: add your rules here*/ - -REV yylval = "REV";printf("%s",yytext);return REV; -NEG yylval = "NEG";printf("%s",yytext);return NEG; -{number} yylval = (char*)malloc(sizeof(char)*MAXL);strcpy(yylval, yytext);printf("%s",yytext);return NUM; -\[ printf("%c",*yytext); return *yytext; -\] printf("%c",*yytext); return *yytext; -, printf("%c",*yytext); return *yytext; -[+\-*\/] printf("%c",*yytext); return *yytext; -. if(*yytext==' '||*yytext=='\t'){REJECT;}else{ printf("missed:%c",*yytext);} - - /* End: add your rules here*/ -{ws} -%% - -void RmWs(char* str){ - int i=0,j=0; - char temp[strlen(str)+1]; - double x,y; - strcpy(temp,str); - while (temp[i]!='\0'){ - while (temp[i]==' '||temp[i]=='\t'){i++;} - str[j]=temp[i]; - i++; j++; - } - str[j]='\0'; -} - diff --git a/assignment2/results.txt b/assignment2/results.txt deleted file mode 100644 index e69de29..0000000 diff --git a/assignment2/test b/assignment2/test deleted file mode 100644 index cb3f803..0000000 Binary files a/assignment2/test and /dev/null differ