mirror of
https://github.com/RustyRaptor/compilers.git
synced 2026-12-03 15:15:17 -07:00
finished lab5
This commit is contained in:
1 parent
53a3860ae5
commit
e84aecb1d0
9 files changed
+1995
-61
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
+2
-2
@@ -4,12 +4,12 @@ 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;
|
||||
|
||||
func foo(x int) void {
|
||||
var x int;
|
||||
func foo(x int) void { var x int;
|
||||
var x int;
|
||||
var y bool;
|
||||
while ( x <= y )
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
+1
-1
@@ -10,7 +10,7 @@ all: lab5
|
||||
# Compile all the THINGS!
|
||||
lab5: lab5.l lab5.y symtable.c symtable.h
|
||||
lex lab5.l
|
||||
yacc -d lab5.y
|
||||
yacc -d --verbose --debug lab5.y
|
||||
gcc $(FLAGS) y.tab.c lex.yy.c symtable.c -o lab5 -lm
|
||||
|
||||
# remove the binary and other generated stuff
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# CS370 Compilers Lab 5
|
||||
## Ziad Arafat - Mar 8 2021
|
||||
|
||||
In this lab we take our previous lab code and implement the proper syntax rules from the DECAF programming language.
|
||||
|
||||
## Screenshots
|
||||
#### Good Test
|
||||

|
||||
|
||||
#### Error Test
|
||||
|
||||

|
||||
|
||||
## Changelog
|
||||
|
||||
#### LEX
|
||||
- added the DECAF tokens
|
||||
- Added some debug prints to the tokens
|
||||
- Added square brackets to the lex directive
|
||||
- Added '<' token to the directive
|
||||
- Added modulus '%'
|
||||
- Added directive for HEX numbers
|
||||
- Created function to convert hex numbers to their int value
|
||||
- Disabled the newline return token
|
||||
|
||||
#### YACC
|
||||
- added all the rules from DECAF and reformatted them to work with Bison */
|
||||
- for each of the rules with a list of tokens I created a recursive rule that allows for one or more of the token.
|
||||
- Replaced any instance of '=' with T_ASSIGN
|
||||
+78
-44
@@ -8,31 +8,47 @@
|
||||
January 2015
|
||||
*/
|
||||
|
||||
/* Ziad Arafat Mar 8 2021 */
|
||||
|
||||
%{
|
||||
|
||||
#include "y.tab.h"
|
||||
#include <string.h>
|
||||
int yylex();
|
||||
int mydebug=0;
|
||||
|
||||
int readHexValue(char * inch)
|
||||
int aHexToInt(char * hexstr)
|
||||
{
|
||||
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;
|
||||
// I used the code we used in one of the 273 labs and adapted it to
|
||||
// work on any number of chars.
|
||||
|
||||
// Uses the difference of the ASCII table values of the chars to get
|
||||
// the value.
|
||||
|
||||
size_t len = strlen(hexstr)-1;
|
||||
int mult = 1;
|
||||
int val = 0;
|
||||
|
||||
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);
|
||||
}
|
||||
// since we already check to make sure it's 0-9 we don't need to
|
||||
// exclude the x in 0x and obviously the 0 will make no change to the
|
||||
// value.
|
||||
if (*(hexstr + len) >= '0' && *(hexstr + len) <= '9') val = (*(hexstr + len) - '0') * mult;
|
||||
else if (*(hexstr + len) >= 'a' && *(hexstr + len) <= 'f') val = (*(hexstr + len) - 'a' + 10) * mult;
|
||||
else if (*(hexstr + len) >= 'A' && *(hexstr + len) <='F') val = (*(hexstr + len) - 'A' + 10) * mult;
|
||||
|
||||
printf("The hex value is: %d\n", val);
|
||||
|
||||
return val;
|
||||
mult *= 16;
|
||||
|
||||
char *hexstrptr;
|
||||
for (hexstrptr = hexstr+(len-1); hexstrptr != hexstr; hexstrptr--){
|
||||
if (*hexstrptr >= '0' && *hexstrptr <= '9') val += (*hexstrptr - '0') * mult;
|
||||
else if (*hexstrptr >= 'a' && *hexstrptr <= 'f') val += (*hexstrptr - 'a' + 10) * mult;
|
||||
else if (*hexstrptr >= 'A' && *hexstrptr <='F') val += (*hexstrptr - 'A' + 10) * mult;
|
||||
mult *= 16;
|
||||
}
|
||||
|
||||
// printf("The hex value is: %d\n", val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
%}
|
||||
@@ -40,50 +56,58 @@ int readHexValue(char * inch)
|
||||
/*
|
||||
CHANGES:
|
||||
- added the DECAF tokens
|
||||
- Added some debug prints to the tokens
|
||||
- Added square brackets to the lex directive
|
||||
- Added '<' token to the directive
|
||||
- Added modulus '%'
|
||||
- Added directive for HEX numbers
|
||||
- Created function to convert hex numbers to their int value
|
||||
- Disabled the newline return token
|
||||
*/
|
||||
|
||||
|
||||
%%
|
||||
"&&" {return(T_AND);}
|
||||
"&&" {return(T_AND);}
|
||||
|
||||
"=" {
|
||||
if (mydebug) fprintf(stderr,"T_ASSIGN found\n");
|
||||
return(T_ASSIGN);
|
||||
}
|
||||
|
||||
"bool" {return(T_BOOLTYPE);}
|
||||
"break" {return(T_BREAK);}
|
||||
"continue" {return(T_CONTINUE);}
|
||||
\. {return(T_DOT);}
|
||||
"else" {return(T_ELSE);}
|
||||
"==" {return(T_EQ);}
|
||||
"extern" {return(T_EXTERN);}
|
||||
"false" {return(T_FALSE);}
|
||||
"for" {return(T_FOR);}
|
||||
"func" {return(T_FUNC);}
|
||||
">=" {return(T_GEQ);}
|
||||
">" {return(T_GT);}
|
||||
"<" {return(T_LT);}
|
||||
"if" {if (mydebug) fprintf(stderr,"T_IF found\n");return(T_IF);}
|
||||
"bool" {return(T_BOOLTYPE);}
|
||||
"break" {return(T_BREAK);}
|
||||
"continue" {return(T_CONTINUE);}
|
||||
\. {return(T_DOT);}
|
||||
"else" {return(T_ELSE);}
|
||||
"==" {return(T_EQ);}
|
||||
"extern" {return(T_EXTERN);}
|
||||
"false" {return(T_FALSE);}
|
||||
"for" {return(T_FOR);}
|
||||
"func" {return(T_FUNC);}
|
||||
">=" {return(T_GEQ);}
|
||||
">" {return(T_GT);}
|
||||
|
||||
"if" {if (mydebug) fprintf(stderr,"T_IF found\n");return(T_IF);}
|
||||
|
||||
"int" {
|
||||
if (mydebug) fprintf(stderr,"T_INTTYPE found\n");
|
||||
return(T_INTTYPE);
|
||||
|
||||
}
|
||||
"<<" {return(T_LEFTSHIFT);}
|
||||
"<=" {return(T_LEQ);}
|
||||
"!=" {return(T_NEQ);}
|
||||
"null" {return(T_NULL);}
|
||||
"||" {return(T_OR);}
|
||||
"package" {return(T_PACKAGE);}
|
||||
"return" {return(T_RETURN);}
|
||||
">>" {return(T_RIGHTSHIFT);}
|
||||
"string" {return(T_STRINGTYPE);}
|
||||
"true" {return(T_TRUE);}
|
||||
"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);}
|
||||
|
||||
"<<" {return(T_LEFTSHIFT);}
|
||||
"<=" {return(T_LEQ);}
|
||||
"!=" {return(T_NEQ);}
|
||||
"null" {return(T_NULL);}
|
||||
"||" {return(T_OR);}
|
||||
"package" {return(T_PACKAGE);}
|
||||
"return" {return(T_RETURN);}
|
||||
">>" {return(T_RIGHTSHIFT);}
|
||||
"string" {return(T_STRINGTYPE);}
|
||||
"true" {return(T_TRUE);}
|
||||
"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);}
|
||||
|
||||
\'.\' {
|
||||
yylval.value=yytext[1];
|
||||
@@ -102,6 +126,14 @@ CHANGES:
|
||||
yylval.string=strdup(yytext);
|
||||
return(T_ID);
|
||||
}
|
||||
|
||||
0x[0-9A-Za-f][0-9A-Za-f]* {
|
||||
if (mydebug) fprintf(stderr,"Hex found %s\n", yytext);
|
||||
yylval.value=aHexToInt((char *)yytext);
|
||||
return(T_INTCONSTANT);
|
||||
}
|
||||
|
||||
|
||||
[0-9][0-9]* {
|
||||
if (mydebug) fprintf(stderr,"Integer found %s\n", yytext);
|
||||
yylval.value=atoi((const char *)yytext);
|
||||
@@ -112,7 +144,7 @@ CHANGES:
|
||||
if (mydebug) fprintf(stderr,"Whitespace found\n");
|
||||
}
|
||||
|
||||
[\-+*/%();{},\[\]!] {
|
||||
[\-+*/%();{},\[\]!<] {
|
||||
if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
|
||||
return (*yytext);
|
||||
}
|
||||
@@ -124,4 +156,6 @@ CHANGES:
|
||||
%%
|
||||
|
||||
int yywrap(void)
|
||||
{ return 1;}
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
+28
-8
@@ -1,15 +1,23 @@
|
||||
/*
|
||||
Ziad Arafat - Mar 8 2021
|
||||
Lab 5
|
||||
*/
|
||||
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int yylex();
|
||||
|
||||
extern int yylineno;
|
||||
extern int yylineno; // import the yylineno variable
|
||||
void yyerror (s) /* Called by yyparse on error */
|
||||
char *s;
|
||||
{
|
||||
// changed these to output to stderr
|
||||
fprintf(stderr, "%s \n", s);
|
||||
fprintf(stderr, "Error on line: %d \n", yylineno);
|
||||
|
||||
// print out the line number of the syntax error.
|
||||
fprintf(stderr, "Error on line: %d \n", yylineno);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +34,7 @@ from LEX and how the operators are associated */
|
||||
char* string;
|
||||
}
|
||||
|
||||
// Added a token for each of the lex tokens
|
||||
%token T_AND
|
||||
%token T_ASSIGN
|
||||
%token T_BOOLTYPE
|
||||
@@ -41,7 +50,6 @@ 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
|
||||
@@ -61,7 +69,13 @@ from LEX and how the operators are associated */
|
||||
%token T_VOID
|
||||
%token T_WHILE
|
||||
|
||||
/* added all the rules from DECAF and reformatted them to work with Bison */
|
||||
|
||||
/* for each of the rules with a list of tokens I created a recursive rule
|
||||
that allows for one or more of the token.
|
||||
*/
|
||||
|
||||
/* Replaced any instance of '=' with T_ASSIGN */
|
||||
|
||||
%% /* end specs, begin rules */
|
||||
Program : Externs T_PACKAGE T_ID '{' FieldDecls MethodDecls '}'
|
||||
@@ -135,6 +149,9 @@ Statement : Assign ';'
|
||||
|
||||
Assign : Lvalue T_ASSIGN Expr
|
||||
;
|
||||
/* Assigns : Assign
|
||||
| Assign ',' Assigns
|
||||
; */
|
||||
|
||||
Lvalue : T_ID
|
||||
| T_ID '[' Expr ']'
|
||||
@@ -147,7 +164,6 @@ MethodCall : T_ID '(' MethodArgs ')'
|
||||
;
|
||||
|
||||
MethodArg : Expr
|
||||
| T_STRINGCONSTANT
|
||||
;
|
||||
|
||||
MethodArgs : /* empty */
|
||||
@@ -165,6 +181,9 @@ Statement : T_IF '(' Expr ')' Block T_ELSE Block
|
||||
Statement : T_WHILE '(' Expr ')' Block
|
||||
;
|
||||
|
||||
/* Statement : T_FOR '(' Assigns ';' Expr ';' Assigns ')' Block
|
||||
; */
|
||||
|
||||
Statement : T_RETURN ';'
|
||||
| T_RETURN '(' ')' ';'
|
||||
| T_RETURN '(' Expr ')' ';'
|
||||
@@ -183,7 +202,7 @@ Simpleexpression : Additiveexpression
|
||||
| Simpleexpression Relop Additiveexpression
|
||||
;
|
||||
|
||||
Relop : T_LEQ | T_LT | T_GT | T_GEQ | T_EQ | T_NEQ
|
||||
Relop : T_LEQ | '<' | T_GT | T_GEQ | T_EQ | T_NEQ
|
||||
;
|
||||
|
||||
Additiveexpression : Term
|
||||
@@ -196,7 +215,7 @@ Addop : '+' | '-'
|
||||
Term : Factor
|
||||
| Term Multop Factor
|
||||
;
|
||||
|
||||
/* added modulus as a multi operator */
|
||||
Multop : '*' | '/' | '%' | T_AND | T_OR | T_LEFTSHIFT | T_RIGHTSHIFT
|
||||
;
|
||||
|
||||
@@ -230,9 +249,10 @@ BoolConstant : T_TRUE
|
||||
|
||||
ArrayType : '[' T_INTCONSTANT ']' Type
|
||||
;
|
||||
|
||||
/* added String and Char as constants */
|
||||
Constant : T_INTCONSTANT
|
||||
| T_STRINGCONSTANT
|
||||
| T_CHARCONSTANT
|
||||
| BoolConstant
|
||||
;
|
||||
%% /* end of rules, start of program */
|
||||
|
||||
+1851
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user