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

@ -3,7 +3,9 @@
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
@ -15,16 +17,21 @@ int main(int argc, char *argv[]) {
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");
@ -56,7 +63,6 @@ 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
@ -67,7 +73,8 @@ 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++) {
@ -80,7 +87,10 @@ 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(
"On line number %d and column number %d we have an invalid "
"character:%s\n",
line_number, column_number, yytext);
// return -1; // return -1;
} }
column_number += yyleng; column_number += yyleng;
@ -102,8 +112,6 @@ 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);
@ -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,7 +164,6 @@ 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

View File

@ -1,19 +1,25 @@
#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 \
"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 // 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;