Fixed some of the spacing and added a harder test t#36
This commit is contained in:
@ -1,7 +1,3 @@
|
||||
/* Syntax Analyzer with Bison (3.8.1) */
|
||||
/* (referenced Bison manual for file boilerplate [3.1]) */
|
||||
|
||||
// Prologue
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include "../src/symbol_table.c"
|
||||
@ -22,7 +18,7 @@
|
||||
int integ;
|
||||
char * words;
|
||||
}
|
||||
//precedence order
|
||||
|
||||
//need subtraction only here
|
||||
%type <words> id_or_types
|
||||
%type <words> types
|
||||
@ -72,6 +68,8 @@
|
||||
%token RESERVE 613
|
||||
%token RELEASE 614
|
||||
%token COMMENT 700
|
||||
|
||||
//precedence order
|
||||
%precedence RESERVE RELEASE
|
||||
%precedence DOT
|
||||
%precedence UMINUS
|
||||
@ -84,11 +82,11 @@
|
||||
%left OR
|
||||
%left ASSIGN
|
||||
|
||||
|
||||
%%
|
||||
|
||||
program:
|
||||
prototype_or_definition_list;
|
||||
prototype_or_definition_list
|
||||
;
|
||||
|
||||
prototype_or_definition_list:
|
||||
prototype prototype_or_definition_list
|
||||
@ -96,86 +94,55 @@ prototype_or_definition_list:
|
||||
| prototype
|
||||
| definition
|
||||
;
|
||||
|
||||
prototype:
|
||||
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID;
|
||||
|
||||
definition:
|
||||
TYPE ID COLON dblock
|
||||
| TYPE ID COLON constant ARROW ID
|
||||
//| TYPE ID COLON types ARROW ID
|
||||
| function_declaration
|
||||
| TYPE ID COLON id_or_types ARROW id_or_types
|
||||
| ID parameter ASSIGN sblock
|
||||
;
|
||||
|
||||
function_declaration:
|
||||
FUNCTION { printf("found a function def\n"); }ID COLON ID
|
||||
| EXTERNAL { printf("found an external function def\n"); }FUNCTION ID COLON ID
|
||||
FUNCTION ID COLON ID
|
||||
| EXTERNAL FUNCTION ID COLON ID
|
||||
;
|
||||
|
||||
parameter:
|
||||
L_PAREN { printf("found a param def\n"); } ID R_PAREN
|
||||
| AS{ printf("found a param_as_list def\n"); } L_PAREN idlist R_PAREN
|
||||
L_PAREN ID R_PAREN
|
||||
| AS L_PAREN idlist R_PAREN
|
||||
;
|
||||
|
||||
idlist:
|
||||
ID COMMA idlist
|
||||
|ID
|
||||
| ID
|
||||
;
|
||||
|
||||
sblock:
|
||||
L_BRACE {printf("open brace\n"); cur = CreateScope(cur,2,2);} statement_list {printf("close brace\n"); cur = getParent(cur);} R_BRACE
|
||||
| L_BRACE {printf("open brace\n"); cur = CreateScope(cur,2,2);} dblock statement_list {printf("close brace\n"); cur = getParent(cur);} R_BRACE
|
||||
L_BRACE {cur = CreateScope(cur,2,2);} statement_list {cur = getParent(cur);} R_BRACE
|
||||
| L_BRACE {cur = CreateScope(cur,2,2);} dblock statement_list {cur = getParent(cur);} R_BRACE
|
||||
;
|
||||
|
||||
dblock:
|
||||
L_BRACKET declaration_list R_BRACKET;
|
||||
|
||||
declaration_list:
|
||||
declaration
|
||||
{printf(
|
||||
"found a decleration of form dec;dec_list\n");
|
||||
//CreateEntry(cur,cur_type,cur_value);
|
||||
}
|
||||
SEMI_COLON declaration_list
|
||||
declaration SEMI_COLON declaration_list
|
||||
| declaration
|
||||
{printf(
|
||||
"found a decleration of form dec\n");
|
||||
//CreateEntry(cur,cur_type,cur_value);
|
||||
}
|
||||
;
|
||||
|
||||
declaration:
|
||||
id_or_types COLON ID {
|
||||
|
||||
CreateEntry(cur,$1,$3);
|
||||
// printf("declaration rule encountered");
|
||||
// if(cur_value != NULL){
|
||||
// char* delete1 = cur_value;
|
||||
// printf("delete1 var assigned to cur_value");
|
||||
// free(delete1);
|
||||
// printf("delete1 var freed");
|
||||
// }
|
||||
// if(cur_type != NULL){
|
||||
// char* delete2 = cur_type;
|
||||
// free(delete2);}
|
||||
// int len = strlen($1);
|
||||
// printf("length determined");
|
||||
// cur_value = malloc(len + 1);
|
||||
// printf("space allocated");
|
||||
// strcpy(cur_value, $1);
|
||||
// printf("string copied over");
|
||||
|
||||
// len = strlen($3);
|
||||
// cur_type = malloc(len + 1);
|
||||
// strcpy(cur_type, $3);
|
||||
// printf("value var is %s type var is %s\n",cur_value,cur_type);
|
||||
}
|
||||
// | types COLON ID
|
||||
id_or_types COLON ID {CreateEntry(cur,$1,$3); }
|
||||
;
|
||||
|
||||
id_or_types:
|
||||
ID
|
||||
| types
|
||||
;
|
||||
|
||||
statement_list:
|
||||
compound_statement statement_list
|
||||
| compound_statement
|
||||
@ -184,20 +151,14 @@ statement_list:
|
||||
;
|
||||
|
||||
compound_statement:
|
||||
WHILE {printf("found an while statment\n"); } L_PAREN expression R_PAREN sblock
|
||||
| IF {printf("found an if statment\n"); }L_PAREN expression R_PAREN THEN sblock ELSE sblock
|
||||
WHILE L_PAREN expression R_PAREN sblock
|
||||
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
|
||||
| sblock
|
||||
;
|
||||
|
||||
// Ref Compilers Principles, Techniques, & Tools
|
||||
|
||||
|
||||
|
||||
|
||||
simple_statement:
|
||||
assignable ASSIGN {printf("found an assignment statment\n"); } expression
|
||||
// I think that we need to add memOp stuff here | assignable ASSIGN expression {printf("found an assignment statment\n"); }
|
||||
| RETURN {printf("found an return statment\n"); } expression
|
||||
assignable ASSIGN expression
|
||||
| RETURN expression
|
||||
;
|
||||
|
||||
assignable:
|
||||
@ -223,16 +184,13 @@ expression:
|
||||
| expression LESS_THAN expression
|
||||
| expression EQUAL_TO expression
|
||||
| assignable
|
||||
//| constant binaryOperator expression
|
||||
//| ID binaryOperator expression
|
||||
| L_PAREN expression R_PAREN
|
||||
| memOp assignable
|
||||
;
|
||||
|
||||
|
||||
|
||||
ablock:
|
||||
L_PAREN argument_list R_PAREN;
|
||||
L_PAREN argument_list R_PAREN
|
||||
;
|
||||
|
||||
argument_list:
|
||||
expression COMMA argument_list
|
||||
@ -262,7 +220,7 @@ types:
|
||||
| T_CHARACTER
|
||||
| T_BOOLEAN
|
||||
;
|
||||
//
|
||||
|
||||
%%
|
||||
|
||||
void yyerror(const char *err) {
|
||||
|
Reference in New Issue
Block a user