From ce94eed32b070f1e729aef278de983d137c330a0 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Sat, 16 Jan 2021 23:31:57 -0700 Subject: [PATCH] LAB1 Start --- LAB1/Makefile | 26 ++++++++++++++++++++++++++ LAB1/lab1.l | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 LAB1/Makefile create mode 100644 LAB1/lab1.l diff --git a/LAB1/Makefile b/LAB1/Makefile new file mode 100644 index 0000000..8fd9efe --- /dev/null +++ b/LAB1/Makefile @@ -0,0 +1,26 @@ +# FLAGS = -O0 -Wall -Wextra -pedantic + +FLAGS = -O0 -Wextra -pedantic + +all: lab1 + +lab1: lex.yy.o + gcc $(FLAGS) -o lab1 lex.yy.o + +lex.yy.o: lex.yy.c + gcc $(FLAGS) -c lex.yy.c + +lex.yy.c: lab1.l + lex lab1.l + +reset: clean + rm lab1 lex.yy.c + +clean: + rm -rf *.o + +memcheck: + valgrind ./lab1 < /etc/passwd -s + +test: + ./lab1 < /etc/passwd diff --git a/LAB1/lab1.l b/LAB1/lab1.l new file mode 100644 index 0000000..9a38dc9 --- /dev/null +++ b/LAB1/lab1.l @@ -0,0 +1,38 @@ + /* This lex routine uses a counting array to match alphabeticstrings + and make a frequency count. + + The real item to notice is that yywrap() is called at EOF and then is run + to do what we need to do. yywrap() returns true when we have a successful + end to the program. We may want to return false (0) if we want to lexing process + to fail + + Shaun Cooper + January 2015 + */ + int lgths[100]; + int cnt = 0; +%% +[a-zA-Z]+ lgths[yyleng]++; +[0-9]+ cnt++; +. | +\n ; +%% + +int yywrap() +{ + int i; + printf("Length No. words\n"); + for (i=1; i<100; i++) { + if (lgths[i] > 0) { + printf("%5d%10d\n",i,lgths[i]); + } + } + printf("\n\n\n-------------\n"); + printf("There are %d numbers\n", cnt); + return(1); +} + +int main() +{ yylex(); + +}