Fixed formatting in lex; Updated Makefile t#30

This commit is contained in:
Scarlett
2025-03-04 16:14:01 -05:00
parent b22f0149c7
commit c26253c20f
6 changed files with 240 additions and 235 deletions

1
.gitignore vendored
View File

@ -6,5 +6,4 @@ grammar.tab.c
grammar.tab.h grammar.tab.h
.vscode .vscode
out out
src
tmp tmp

View File

@ -5,8 +5,7 @@ EXE := alpha
CFLAGS := CFLAGS :=
YACC := bison YACC := bison
compiler: tmp/grammar.tab.c tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o compiler: runner
$(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/lex.yy.c tmp/symbol_table.o
tmp/grammar.tab.c: src/grammar.y tmp/grammar.tab.c: src/grammar.y
mkdir -p tmp mkdir -p tmp
@ -14,7 +13,7 @@ tmp/grammar.tab.c: src/grammar.y
mv grammar.tab.c tmp/ mv grammar.tab.c tmp/
mv grammar.tab.h tmp/ mv grammar.tab.h tmp/
tmp/lex.yy.c: src/lexicalStructure.lex tmp/lex.yy.c: src/lexicalStructure.lex tmp/grammar.tab.c
$(FLEX) -o tmp/lex.yy.c $(LEX) $(FLEX) -o tmp/lex.yy.c $(LEX)
mv flex.h tmp/ mv flex.h tmp/
@ -25,21 +24,17 @@ tmp/symbol_table.o: src/symbol_table.c src/symbol_table.h
$(CC) $(CFLAGS) -o tmp/symbol_table.o -c src/symbol_table.c $(CC) $(CFLAGS) -o tmp/symbol_table.o -c src/symbol_table.c
parser : tmp/lex.yy.c tmp/grammar.tab.c parser : tmp/lex.yy.c tmp/grammar.tab.c
$(CC) -o parser tmp/lex.yy.c tmp/grammar.tab.c src/symbol_table.c $(CC) -o parser tmp/lex.yy.c tmp/grammar.tab.c
runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o
$(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/lex.yy.c tmp/symbol_table.o $(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/lex.yy.c tmp/symbol_table.o
debug: CFLAGS += -DDEBUG=1 debug: CFLAGS += -DDEBUG=1
debug: clean compiler debug: clean compiler
test: test: test-s1 test-s2
test-s1:
./$(EXE) -tok ./tests/sprint1/test_comment_fix1.alpha ./$(EXE) -tok ./tests/sprint1/test_comment_fix1.alpha
./$(EXE) -tok ./tests/sprint1/test_comment_fix2.alpha ./$(EXE) -tok ./tests/sprint1/test_comment_fix2.alpha
./$(EXE) -tok ./tests/sprint1/test_comment_issues.alpha ./$(EXE) -tok ./tests/sprint1/test_comment_issues.alpha
@ -55,6 +50,11 @@ test:
./$(EXE) -tok ./tests/sprint1/test_simple_literals.alpha ./$(EXE) -tok ./tests/sprint1/test_simple_literals.alpha
./$(EXE) -tok ./tests/sprint1/test_variables.alpha ./$(EXE) -tok ./tests/sprint1/test_variables.alpha
test-s2:
./$(EXE) -tok ./tests/sprint2/alpha/test_library.alpha
./$(EXE) -tok ./tests/sprint2/alpha/test_one_line.alpha
./$(EXE) -tok ./tests/sprint2/alpha/test_simple.alpha
clean: clean:
rm -f *.o rm -f *.o
rm -f lex.yy.c rm -f lex.yy.c
@ -66,3 +66,4 @@ clean:
rm -f *.st rm -f *.st
rm -rf out rm -rf out
rm -rf tmp rm -rf tmp
rm -f parser

View File

@ -4,7 +4,7 @@
// Prologue // Prologue
%{ %{
#include <stdio.h> #include <stdio.h>
#include "symbol_table.c" #include "../src/symbol_table.c"
extern int yylex(void); extern int yylex(void);
void yyerror(const char *err); void yyerror(const char *err);
extern char* yytext; extern char* yytext;

View File

@ -1,6 +1,4 @@
/* Lexical Analysis with Flex (1.6.0) We used some of the code from this manual */ /* Lexical Analyzer with Flex (1.6.0) */
/* so we placed the citation here. */
/* definitions */
%option noyywrap %option noyywrap
%option header-file="flex.h" %option header-file="flex.h"
@ -12,18 +10,15 @@
#ifndef DEBUG #ifndef DEBUG
#define DEBUG 0 #define DEBUG 0
#endif #endif
%} %}
STARCOM [^\*]|\*+[^\)\*]+ STARCOM [^\*]|\*+[^\)\*]+
PARENCOM [^\)]|[^\*\)]+\)+ PARENCOM [^\)]|[^\*\)]+\)+
COMMENT \(\*{STARCOM}*\*\)|\(\*{PARENCOM}*\*\)
ID [A-Za-z_][0-9A-Za-z_]* ID [A-Za-z_][0-9A-Za-z_]*
DIGIT [0-9] DIGIT [0-9]
CHAR \\n|\\t|\\'|[^'\n\t\\]|\\\\ CHAR \\n|\\t|\\'|[^'\n\t\\]|\\\\
/* char can be a newline, tab, an escaped quote, or anything but a single quote, an actual line break, an actual tab, or a backslash by itself (to prevent confusion from escaped quote */
SCHAR \\n|\\t|\\\"|[^\"\n\\] SCHAR \\n|\\t|\\\"|[^\"\n\\]
/* similar to above, a string Char (SCHAR) is the same as a CHAR except we cannot have double quotes instead of single quotes. Double quotes need to be escaped in Flex unlike single quotes based on documentation */
%% %%
@ -66,14 +61,12 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}} {DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}}
'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}} '{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}}
\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}} \"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}}
\(\*{STARCOM}*\*\)|\(\*{PARENCOM}*\*\) {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {return COMMENT;}} {COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {return COMMENT;}}
"(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}} "(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}}
")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}} ")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}}
"[" {if(DEBUG) {printf( "L_BRACKET: %s (%d)\n", yytext, L_BRACKET);} else {return L_BRACKET;}} "[" {if(DEBUG) {printf( "L_BRACKET: %s (%d)\n", yytext, L_BRACKET);} else {return L_BRACKET;}}
"]" {if(DEBUG) {printf( "R_BRACKET: %s (%d)\n", yytext, R_BRACKET);} else {return R_BRACKET;}} "]" {if(DEBUG) {printf( "R_BRACKET: %s (%d)\n", yytext, R_BRACKET);} else {return R_BRACKET;}}
"{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {return L_BRACE;}} "{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {return L_BRACE;}}
"}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {return R_BRACE;}} "}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {return R_BRACE;}}
@ -81,7 +74,7 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {return C_FALSE;}} "false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {return C_FALSE;}}
"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {return C_NULL;}} "null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {return C_NULL;}}
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {yylval.words = strdup(yytext); return ID;}} {ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {/*yylval.words = strdup(yytext);*/ return ID;}}
\n {line_number++; column_number = 1;} \n {line_number++; column_number = 1;}
\t {column_number++;} \t {column_number++;}

View File

@ -1,30 +1,37 @@
//#include "symbol_table.h" // #include "symbol_table.h"
#include "runner.h" #include "runner.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 1) { if (argc == 1) {
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n"); fprintf(
stderr,
"INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
return -1; return -1;
} else if (argc == 2) { } else if (argc == 2) {
//can be either -help or .alpha file // can be either -help or .alpha file
if (is_help(argv[1])) { if (is_help(argv[1])) {
return 0; return 0;
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) { } else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
//run flex for now // run flex for now
no_flag = SET_FLAG; //no argument but valid input no_flag = SET_FLAG; // no argument but valid input
alpha_file = fopen(argv[1], "r"); alpha_file = fopen(argv[1], "r");
} else { } else {
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs\n"); fprintf(stderr,
"INVALID INPUT: Include a .alpha file or use -help for more "
"inputs\n");
return -1; return -1;
} }
} else { } else {
//last input must be .alpha // last input must be .alpha
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) { if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n"); fprintf(stderr,
"INVALID INPUT: Include a .alpha file at end of input or use "
"-help for more inputs \n");
return -1; return -1;
} else { } else {
//now check that other args are valid (flags will not be null if flag is present) // now check that other args are valid (flags will not be null if flag is
// present)
for (int i = 1; i < argc - 1; i++) { for (int i = 1; i < argc - 1; i++) {
if (check_flag(argv[i], argv[argc - 1]) != 0) { if (check_flag(argv[i], argv[argc - 1]) != 0) {
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n"); fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
@ -37,7 +44,7 @@ int main(int argc, char *argv[]) {
return run(alpha_file); return run(alpha_file);
} }
int check_flag(char *arg, char* alpha) { int check_flag(char *arg, char *alpha) {
if (strcmp("-tok", arg) == 0) { if (strcmp("-tok", arg) == 0) {
if (tok_flag == NULL) { if (tok_flag == NULL) {
return new_file(arg, alpha); return new_file(arg, alpha);
@ -56,10 +63,9 @@ int check_flag(char *arg, char* alpha) {
} }
} }
int run(FILE *alpha) { int run(FILE *alpha) {
int token; int token;
//check that file exists // check that file exists
if (alpha == NULL) { if (alpha == NULL) {
fprintf(stderr, "INPUT FILE NOT FOUND\n"); fprintf(stderr, "INPUT FILE NOT FOUND\n");
return -1; return -1;
@ -67,11 +73,12 @@ int run(FILE *alpha) {
yyin = alpha; yyin = alpha;
while (0 != (token = yylex())) { while (0 != (token = yylex())) {
if (tok_flag != NULL) { if (tok_flag != NULL) {
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext); fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token,
yytext);
} }
if(token == COMMENT){ if (token == COMMENT) {
for (int i = 0; i < yyleng; i++) { for (int i = 0; i < yyleng; i++) {
if(yytext[i] == '\n'){ if (yytext[i] == '\n') {
line_number++; line_number++;
column_number = 0; column_number = 0;
} }
@ -79,16 +86,19 @@ int run(FILE *alpha) {
} }
continue; continue;
} }
if(token == 1999){ if (token == 1999) {
printf("On line number %d and column number %d we have an invalid character:%s\n",line_number,column_number,yytext); printf(
//return -1; "On line number %d and column number %d we have an invalid "
} "character:%s\n",
line_number, column_number, yytext);
// return -1;
}
column_number += yyleng; column_number += yyleng;
} }
if (st_flag != NULL) { if (st_flag != NULL) {
//output symbol table, file pointer is // output symbol table, file pointer is
//print_symbol_table(top,st_flag); // print_symbol_table(top,st_flag);
} }
if (yyin != NULL) { if (yyin != NULL) {
@ -102,9 +112,7 @@ int run(FILE *alpha) {
return 0; return 0;
} }
bool is_help(char *input) {
bool is_help(char * input) {
if (strcmp(input, "-help") == 0) { if (strcmp(input, "-help") == 0) {
printf("%s", HELP); printf("%s", HELP);
return true; return true;
@ -138,17 +146,16 @@ int new_file(char *arg, char *alpha) {
return -1; return -1;
} }
// calculate lengths // calculate lengths
int basename_len = strlen(basename); int basename_len = strlen(basename);
char *file_name = calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char)); char *file_name =
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
//coy filename and add extension // coy filename and add extension
strncpy(file_name, basename, basename_len - ALPHA_OFFSET); strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
strcat(file_name, "."); strcat(file_name, ".");
strcat(file_name, arg + 1); strcat(file_name, arg + 1);
if (strcmp(arg, "-tok") == 0) { if (strcmp(arg, "-tok") == 0) {
tok_flag = fopen(file_name, "w"); tok_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-st") == 0) { } else if (strcmp(arg, "-st") == 0) {
@ -157,19 +164,18 @@ int new_file(char *arg, char *alpha) {
return 0; return 0;
} }
int is_alpha_file(char *alpha, int file_len) { int is_alpha_file(char *alpha, int file_len) {
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) { if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
return -1; //not alpha file return -1; // not alpha file
} }
return 0; //is alpha file return 0; // is alpha file
} }
void enter_scope(int line, int column){ void enter_scope(int line, int column) {
curr = CreateScope(curr, line, column); curr = CreateScope(curr, line, column);
} }
void exit_scope() { void exit_scope() {
if(curr->Parent_Scope == NULL){ if (curr->Parent_Scope == NULL) {
printf("Can't close top"); printf("Can't close top");
return; return;
} }

View File

@ -1,29 +1,35 @@
#define ALPHA_OFFSET 6 #define ALPHA_OFFSET 6
#define TOK_LEN 3 #define TOK_LEN 3
#define ST_LEN 2 #define ST_LEN 2
#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n-st output the symbol table for the program to the .st file\n-help print this message and exit the alpha compiler\n" #define HELP \
//use to set flags for arg types "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid " \
"options:\n-tok output the token number, token, line number, and column " \
"number for each of the tokens to the .tok file\n-st output the symbol " \
"table for the program to the .st file\n-help print this message and exit " \
"the alpha compiler\n"
// use to set flags for arg types
#define SET_FLAG 1 #define SET_FLAG 1
#include <string.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../tmp/flex.h" #include <string.h>
#include "typedefs.h"
#include <stdbool.h>
#include "symbol_table.h"
#include <sys/stat.h> #include <sys/stat.h>
#include "../tmp/flex.h"
#include "symbol_table.h"
#include "typedefs.h"
extern int line_number, column_number; extern int line_number, column_number;
extern char *yytext; extern char *yytext;
extern FILE *yyin; extern FILE *yyin;
int arg; int arg;
SymbolTable * top; SymbolTable *top;
SymbolTable * curr; SymbolTable *curr;
// int main(int argc, char* argv[]); // int main(int argc, char* argv[]);
char *is_tok(int argc, char* argv[]); char *is_tok(int argc, char *argv[]);
// int is_alpha_file(char *file, int file_len); // int is_alpha_file(char *file, int file_len);
void enter_scope(int, int); void enter_scope(int, int);
void exit_scope(void); void exit_scope(void);
@ -33,9 +39,9 @@ FILE *tok_flag = NULL;
FILE *st_flag = NULL; FILE *st_flag = NULL;
int no_flag = 0; int no_flag = 0;
int main(int argc, char* argv[]); int main(int argc, char *argv[]);
int new_file(char *arg, char *alpha); int new_file(char *arg, char *alpha);
int is_alpha_file(char *alpha, int file_len); int is_alpha_file(char *alpha, int file_len);
bool is_help(char * input); bool is_help(char *input);
int run(FILE*alpha); int run(FILE *alpha);
int check_flag(char * arg, char* alpha); int check_flag(char *arg, char *alpha);