edited some grammar rules. Still running into syntax errors

This commit is contained in:
Partho Bhattacharya
2025-02-28 14:03:53 -05:00
parent 32e123beb7
commit 2808a75dc9
5 changed files with 38 additions and 2111 deletions

BIN
.Makefile.swp Normal file

Binary file not shown.

View File

@ -3,6 +3,13 @@ FLEX := flex
LEX := lexicalStructure.lex
EXE := alpha
CFLAGS :=
YACC := bison
parser : lex.yy.c grammar.tab.c
$(CC) -o parser lex.yy.c grammar.tab.c
grammar.tab.c: grammar.y
$(YACC) -d grammar.y
compiler: lex.yy.c runner.o runner

View File

@ -12,6 +12,9 @@
SymbolTable * st;
char* cur_value;
char* cur_type;
int token_tracker;
extern int line_number;
extern int column_number;
%}
%union {
@ -100,6 +103,8 @@ prototype:
definition:
TYPE ID COLON dblock
| TYPE ID COLON constant ARROW ID
| TYPE ID COLON types ARROW ID
| FUNCTION ID COLON ID
| TYPE ID COLON ID ARROW ID
| ID parameter ASSIGN sblock
;
@ -115,20 +120,22 @@ idlist:
;
sblock:
L_BRACE {st = CreateScope(st,1,1);} statement_list {st = getParent(st);} R_BRACE
| L_BRACE {st = CreateScope(st,1,1);} dblock statement_list {st = getParent(st);} R_BRACE
L_BRACE /*{st = CreateScope(st,1,1);}*/ statement_list /*{st = getParent(st);}*/ R_BRACE
| L_BRACE /*{st = CreateScope(st,1,1);}*/ dblock statement_list /*{st = getParent(st);}*/ R_BRACE
;
dblock:
L_BRACKET declaration_list R_BRACKET;
declaration_list:
declaration {CreateEntry(st,cur_type,cur_value);} SEMI_COLON declaration_list
| declaration {CreateEntry(st,cur_type,cur_value);}
declaration /*{CreateEntry(st,cur_type,cur_value);}*/ SEMI_COLON declaration_list
| declaration //{CreateEntry(st,cur_type,cur_value);}
;
declaration:
ID COLON ID {cur_value = strdup($1);cur_type = strdup($3);};
ID COLON ID //{cur_value = strdup($1);cur_type = strdup($3);}
| types COLON ID
;
statement_list:
compound_statement statement_list
@ -201,22 +208,35 @@ constant:
| C_TRUE
| C_FALSE
;
types:
T_STRING
| T_INTEGER
| T_ADDRESS
| T_CHARACTER
| T_BOOLEAN
;
//
%%
void yyerror(const char *err) {
fprintf(stderr, "Error: %s\n", err);
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,line_number,column_number);
}
int main() {
cur_value = NULL;
cur_type = NULL;
//cur_value = NULL;
//cur_type = NULL;
token_tracker = 1;
st=CreateScope(NULL,1,1);
int a;
while ((a = yyparse()) != EOF){
printf("%d = a: yytext = %s: yychar = %d\n", a, yytext, yychar);
if(yytext[0] == '!'){
print_symbol_table(getAncestor(st),stdout);
token_tracker++;
//printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker);
if(yytext[0] == '\n'){
FILE* f = fdopen(1,"w");
print_symbol_table(getAncestor(st),f);
fclose(f);
break;
}
}

2100
lex.yy.c

File diff suppressed because it is too large Load Diff

BIN
parser

Binary file not shown.