cleaned up lex and makefile

This commit is contained in:
RustyRaptor committed 2021-02-04 20:28:51 -07:00
1 parent ce94eed32b
commit a82131c0c5
2 files changed
+30

No files matched your search

+10
View File
@@ -1,26 +1,36 @@
# FLAGS = -O0 -Wall -Wextra -pedantic
# the gcc options so I don't have to copy paste them everywhere
# any time I make changes to them.
FLAGS = -O0 -Wextra -pedantic
# Entry point target
all: lab1
# executable target
lab1: lex.yy.o
gcc $(FLAGS) -o lab1 lex.yy.o
# compile the object code
lex.yy.o: lex.yy.c
gcc $(FLAGS) -c lex.yy.c
# generate the c code from lex
lex.yy.c: lab1.l
lex lab1.l
# reset the code base to just the source files
reset: clean
rm lab1 lex.yy.c
# remove the object file
clean:
rm -rf *.o
# runs valgrind to look for any memory mistakes
memcheck:
valgrind ./lab1 < /etc/passwd -s
# executes our code to test it's functionality
test:
./lab1 < /etc/passwd
+20
View File
@@ -9,8 +9,23 @@
Shaun Cooper
January 2015
*/
/* Stores an array where the index corresponds to the
length of the word and the value is the number of
instances of words of that length
*/
int lgths[100];
/* Stores the count of number groupings we find */
int cnt = 0;
/* The first rule looks for groupings of only letters then
increments lgths based on yyleng which stores the length
of the regex found
The second rule looks for groupings of 1 or more digits
then increment cnt by one
The remaining lines will just skip over anything else found or newlines
*/
%%
[a-zA-Z]+ lgths[yyleng]++;
[0-9]+ cnt++;
@@ -18,20 +33,25 @@
\n ;
%%
// our yywrap will print our values when we finish reading all inputs
int yywrap()
{
int i;
// print a table showing number of letters and instances
// skips over entries where we found no words of that length
printf("Length No. words\n");
for (i=1; i<100; i++) {
if (lgths[i] > 0) {
printf("%5d%10d\n",i,lgths[i]);
}
}
// print a pretty separator and then just print the count of numbers we found
printf("\n\n\n-------------\n");
printf("There are %d numbers\n", cnt);
return(1);
}
// This executes the entry point for lex
int main()
{ yylex();