diff --git a/LAB5/ErrorTest.decaf b/LAB5/ErrorTest.decaf index 41091f4..90b6efd 100644 --- a/LAB5/ErrorTest.decaf +++ b/LAB5/ErrorTest.decaf @@ -34,13 +34,13 @@ package foo { } func main() int { foo(1); - for ( i=1,j=2; i <= k; i=i+1, j=j+2) // for loop - { - var x,y,z int; - var x,y,z bool; + // for ( i=1,j=2; i <= k; i=i+1, j=j+2) // for loop + // { + // var x,y,z int; + // var x,y,z bool; i = print_int(i); - x = 0xBad0FF; // HEX - } + // x = 0xBad0FF; // HEX + // } } diff --git a/LAB5/GoodTest.decaf b/LAB5/GoodTest.decaf index e665baf..17a840c 100644 --- a/LAB5/GoodTest.decaf +++ b/LAB5/GoodTest.decaf @@ -4,7 +4,6 @@ extern func print_int(int) void; extern func print_string(string) void; package foo { - var x int; var y int = 100; var A[100] bool; diff --git a/LAB5/lab5.l b/LAB5/lab5.l index 0e3b91b..aaece4c 100644 --- a/LAB5/lab5.l +++ b/LAB5/lab5.l @@ -12,7 +12,28 @@ #include "y.tab.h" int yylex(); -int mydebug=1; +int mydebug=0; + +int readHexValue(char * inch) +{ + int val; + if (*inch >= '0' && *inch <= '9') val = (*inch - '0') * 16; + else if (*inch >= 'a' && *inch <= 'f') val = (*inch - 'a' + 10) * 16; + else if (*inch >= 'A' && *inch <='F') val = (*inch - 'A' + 10) * 16; + + + + char *inchptr; + for (inchptr = inch; *inchptr != '\0'; inchptr++){ + if (*inchptr >= '0' && *inchptr <= '9') val += (*inchptr - '0'); + else if (*inchptr >= 'a' && *inchptr <= 'f') val += (*inchptr - 'a' + 10); + else if (*inchptr >= 'A' && *inchptr <='F') val += (*inchptr - 'A' + 10); + } + + printf("The hex value is: %d\n", val); + + return val; +} %} @@ -42,7 +63,8 @@ CHANGES: "func" {return(T_FUNC);} ">=" {return(T_GEQ);} ">" {return(T_GT);} -"if" {return(T_IF);} +"<" {return(T_LT);} +"if" {if (mydebug) fprintf(stderr,"T_IF found\n");return(T_IF);} "int" { if (mydebug) fprintf(stderr,"T_INTTYPE found\n"); @@ -59,7 +81,7 @@ CHANGES: ">>" {return(T_RIGHTSHIFT);} "string" {return(T_STRINGTYPE);} "true" {return(T_TRUE);} -"var" {return(T_VAR);} +"var" {if (mydebug) fprintf(stderr,"T_VAR found\n");return(T_VAR);} "void" {if (mydebug) fprintf(stderr,"T_VOID found\n");return(T_VOID);} "while" {return(T_WHILE);} @@ -75,13 +97,13 @@ CHANGES: [a-zA-Z][a-zA-Z0-9_]* { if (mydebug) { - fprintf(stderr,"Letter found %s\n", yytext); + fprintf(stderr,"Identifier found %s\n", yytext); } yylval.string=strdup(yytext); return(T_ID); } [0-9][0-9]* { - if (mydebug) fprintf(stderr,"Digit found\n"); + if (mydebug) fprintf(stderr,"Integer found %s\n", yytext); yylval.value=atoi((const char *)yytext); return(T_INTCONSTANT); } @@ -90,7 +112,7 @@ CHANGES: if (mydebug) fprintf(stderr,"Whitespace found\n"); } -[\-+*/%();{},\[\]<>!] { +[\-+*/%();{},\[\]!] { if (mydebug) fprintf(stderr,"return a token %c\n",*yytext); return (*yytext); } @@ -102,9 +124,4 @@ CHANGES: %% int yywrap(void) -{ return 1;} - -/* void yyerror(const char *str) -{ - fprintf(stderr,"Error | Line: %d\n%s\n",yylineno,str); -} */ \ No newline at end of file +{ return 1;} \ No newline at end of file diff --git a/LAB5/lab5.y b/LAB5/lab5.y index 8abd1a8..74316ad 100644 --- a/LAB5/lab5.y +++ b/LAB5/lab5.y @@ -1,5 +1,6 @@ %{ #include +#include #include int yylex(); @@ -7,8 +8,8 @@ extern int yylineno; void yyerror (s) /* Called by yyparse on error */ char *s; { - printf ("%s \n", s); - printf ("FUCK SHIT %d \n", yylineno); + fprintf(stderr, "%s \n", s); + fprintf(stderr, "Error on line: %d \n", yylineno); } @@ -40,6 +41,7 @@ from LEX and how the operators are associated */ %token T_FUNC %token T_GEQ %token T_GT +%token T_LT %token T_ID %token T_IF %token T_INTCONSTANT @@ -112,7 +114,7 @@ Block : '{' VarDecls Statements '}' ; VarDecls : /* empty */ - | VarDecl + | VarDecl VarDecls ; VarDecl : T_VAR T_ID Type ';' @@ -122,7 +124,7 @@ VarDecl : T_VAR T_ID ArrayType ';' ; Statements : /* empty */ - | Statement + | Statement Statements ; Statement : Block @@ -145,13 +147,19 @@ MethodCall : T_ID '(' MethodArgs ')' ; MethodArg : Expr + | T_STRINGCONSTANT ; MethodArgs : /* empty */ - | MethodArg MethodArgs + | FullMethodArgs + ; + +FullMethodArgs : MethodArg + | MethodArg ',' MethodArgs ; Statement : T_IF '(' Expr ')' Block T_ELSE Block + | T_IF '(' Expr ')' Block ; Statement : T_WHILE '(' Expr ')' Block @@ -175,7 +183,7 @@ Simpleexpression : Additiveexpression | Simpleexpression Relop Additiveexpression ; -Relop : T_LEQ | '<' | '>' | T_GEQ | T_EQ | T_NEQ +Relop : T_LEQ | T_LT | T_GT | T_GEQ | T_EQ | T_NEQ ; Additiveexpression : Term @@ -189,7 +197,7 @@ Term : Factor | Term Multop Factor ; -Multop : '*' | '/' | T_AND | T_OR | T_LEFTSHIFT | T_RIGHTSHIFT +Multop : '*' | '/' | '%' | T_AND | T_OR | T_LEFTSHIFT | T_RIGHTSHIFT ; Factor : T_ID @@ -224,6 +232,7 @@ ArrayType : '[' T_INTCONSTANT ']' Type ; Constant : T_INTCONSTANT + | T_STRINGCONSTANT | BoolConstant ; %% /* end of rules, start of program */