From da1764a9f839e1d29b2bee12d35534f71f05c6ff Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 30 Apr 2021 22:25:19 +0530 Subject: law & order --- 01/input | 1 + 01/main.l | 20 ++++++++++++++++++++ 02/input | 5 +++++ 02/main.l | 29 +++++++++++++++++++++++++++++ 03/input | 7 +++++++ 03/main.l | 38 ++++++++++++++++++++++++++++++++++++++ 04/input | 9 +++++++++ 04/main.l | 32 ++++++++++++++++++++++++++++++++ 05/input | 5 +++++ 05/main.l | 24 ++++++++++++++++++++++++ 06/main.l | 15 +++++++++++++++ 06/main.y | 28 ++++++++++++++++++++++++++++ 07/main.l | 21 +++++++++++++++++++++ 07/main.y | 34 ++++++++++++++++++++++++++++++++++ 08/input | 1 + 08/main.l | 14 ++++++++++++++ 08/main.y | 28 ++++++++++++++++++++++++++++ 09/input | 1 + 09/main.l | 15 +++++++++++++++ 09/main.y | 30 ++++++++++++++++++++++++++++++ 1/input | 1 - 1/main.l | 20 -------------------- 2/input | 5 ----- 2/main.l | 29 ----------------------------- 3/input | 7 ------- 3/main.l | 38 -------------------------------------- 4/input | 9 --------- 4/main.l | 32 -------------------------------- 5/input | 5 ----- 5/main.l | 24 ------------------------ 6/main.l | 15 --------------- 6/main.y | 28 ---------------------------- 7/main.l | 21 --------------------- 7/main.y | 34 ---------------------------------- 8/input | 1 - 8/main.l | 14 -------------- 8/main.y | 28 ---------------------------- 9/input | 1 - 9/main.l | 15 --------------- 9/main.y | 30 ------------------------------ flake.nix | 18 +++++++++--------- 41 files changed, 366 insertions(+), 366 deletions(-) create mode 100644 01/input create mode 100644 01/main.l create mode 100644 02/input create mode 100644 02/main.l create mode 100644 03/input create mode 100644 03/main.l create mode 100644 04/input create mode 100644 04/main.l create mode 100644 05/input create mode 100644 05/main.l create mode 100644 06/main.l create mode 100644 06/main.y create mode 100644 07/main.l create mode 100644 07/main.y create mode 100644 08/input create mode 100644 08/main.l create mode 100644 08/main.y create mode 100644 09/input create mode 100644 09/main.l create mode 100644 09/main.y delete mode 100644 1/input delete mode 100644 1/main.l delete mode 100644 2/input delete mode 100644 2/main.l delete mode 100644 3/input delete mode 100644 3/main.l delete mode 100644 4/input delete mode 100644 4/main.l delete mode 100644 5/input delete mode 100644 5/main.l delete mode 100644 6/main.l delete mode 100644 6/main.y delete mode 100644 7/main.l delete mode 100644 7/main.y delete mode 100644 8/input delete mode 100644 8/main.l delete mode 100644 8/main.y delete mode 100644 9/input delete mode 100644 9/main.l delete mode 100644 9/main.y diff --git a/01/input b/01/input new file mode 100644 index 0000000..2ec8b41 --- /dev/null +++ b/01/input @@ -0,0 +1 @@ +alskjdfa;dsfja;kdsjfa;klsdjfa;lkjsdfkaljdsf;lakjsdf diff --git a/01/main.l b/01/main.l new file mode 100644 index 0000000..24f78ca --- /dev/null +++ b/01/main.l @@ -0,0 +1,20 @@ +%{ +int v = 0; +int c = 0; +%} + +%% +[aeiouAEIOU] v++; +[a-zA-Z] c++; +. ; +%% + +int main(void) { + yylex(); + printf("%d vowels\n", v); + printf("%d consonants\n", c); +} + +int yywrap() { + return(1); +} diff --git a/02/input b/02/input new file mode 100644 index 0000000..5ad8acb --- /dev/null +++ b/02/input @@ -0,0 +1,5 @@ +this is the first para text. +another line in this para. + +this is the second para text. +another line in this para. diff --git a/02/main.l b/02/main.l new file mode 100644 index 0000000..872c7bc --- /dev/null +++ b/02/main.l @@ -0,0 +1,29 @@ +%{ +int word_count = 0; +int line_count = 0; +int char_count = 0; +int para_count = 0; +int spaces = 0; +%} + +%% +\n line_count++; +[ \t] spaces++; +[A-Za-z]+ { word_count++; char_count+=yyleng;}; +\n\n para_count++; +<> {para_count++; return 1;}; +. ; +%% + +int main(void) { + yylex(); + printf("%d lines\n", line_count); + printf("%d words\n", word_count); + printf("%d blanks\n", spaces); + printf("%d characters\n", char_count); + printf("%d paragraphs\n", para_count); +} + +int yywrap() { + return(1); +} diff --git a/03/input b/03/input new file mode 100644 index 0000000..23cb9b7 --- /dev/null +++ b/03/input @@ -0,0 +1,7 @@ +1 2 3 4 +-1 -2 -3 -4 +1.1 2.2 +-1.1 -2.2 +3.-7 3.+7 -3.-7 +3.+7 +1/2 1/3 1/4 1/5 +-1/2 -1/3 -1/0 -73849/5678 diff --git a/03/main.l b/03/main.l new file mode 100644 index 0000000..f423e7a --- /dev/null +++ b/03/main.l @@ -0,0 +1,38 @@ +%{ +int posint = 0; +int posfrac = 0; +int negint = 0; +int negfrac = 0; +int errors = 0; +%} + +d [0-9] + +%% +(\+?|-){d}*\.-{d}+ errors++; // invalid numbers +(\+?|-){d}*\.\+{d}+ errors++; // invalid numbers +\+?{d}+\/[1-9]{d}* posfrac++; +-{d}+\/[1-9]{d}* negfrac++; +(\+?|-){d}+\/{d}+ errors++; +\+?{d}+ posint++; +-{d}+ negint++; +\+?{d}*\.{d}+ posfrac++; +-{d}*\.{d}+ negfrac++; +\n ; +. ; +%% + +int main(void) { + yylex(); + printf("%d positive integers\n" , posint); + printf("%d positive fractions\n" , posfrac); + printf("%d negative integers\n" , negint); + printf("%d negative fractions\n" , negfrac); + printf("%d errors\n" , errors); +} + +int yywrap() +{ + return(1); +} + diff --git a/04/input b/04/input new file mode 100644 index 0000000..d1dff11 --- /dev/null +++ b/04/input @@ -0,0 +1,9 @@ +int main() { + int n; + printf("hello world"); + scanf("%d", &n); + printf("hello world"); + printf("hello world"); + printf("hello world"); + printf("hello world"); +} diff --git a/04/main.l b/04/main.l new file mode 100644 index 0000000..28593f6 --- /dev/null +++ b/04/main.l @@ -0,0 +1,32 @@ +%{ + +int scans = 0; +int prints = 0; +%} + +%% +scanf { + scans++; + fprintf(yyout, "readf"); +}; +printf { + prints++; + fprintf(yyout, "writef"); +} +. fprintf(yyout, "%s", yytext); +\n fprintf(yyout, "\n"); +%% + +int main(void) { + yyin=fopen("4/input", "r+"); + yyout=fopen("4/output", "w"); + yylex(); + printf("%d printf occurrence(s)\n", prints); + printf("%d scanf occurrence(s)\n", scans); +} + +int yywrap() +{ + return(1); +} + diff --git a/05/input b/05/input new file mode 100644 index 0000000..8be510e --- /dev/null +++ b/05/input @@ -0,0 +1,5 @@ +int main() { + int one, two, three; + three = two + one; + printf(three); +} diff --git a/05/main.l b/05/main.l new file mode 100644 index 0000000..ee13053 --- /dev/null +++ b/05/main.l @@ -0,0 +1,24 @@ +%{ + int k = 0; + int o = 0; + int i = 0; +%} + +%% +int|main|printf|scanf|char|if|else|for|switch|case k++; +[+*/%&=] o++; +[a-zA-Z_][a-zA-Z0-9_]* i++; +. ; +\n ; +%% + +int main(void) { + yylex(); + printf("%d keywords\n", k); + printf("%d operators\n", o); + printf("%d identifiers\n", i); +} + +int yywrap() { + return 1; +} diff --git a/06/main.l b/06/main.l new file mode 100644 index 0000000..d2eded0 --- /dev/null +++ b/06/main.l @@ -0,0 +1,15 @@ +%{ +#include +%} + +%% + +a return A; +b return B; +. return yytext[0]; +\n return 0; +%% + +int yywrap() { + return 1; +} diff --git a/06/main.y b/06/main.y new file mode 100644 index 0000000..25005df --- /dev/null +++ b/06/main.y @@ -0,0 +1,28 @@ +%{ +#include +#include +int yylex(); +int yyerror(char *); +%} + +%token A B ERR + +%% + +S: + A S B + | + ; + +%% + +int yyerror(char *s) { + printf("error: %s", s); + exit(0); +} + +int main() { + yyparse(); + printf("valid string\n"); + return 0; +} diff --git a/07/main.l b/07/main.l new file mode 100644 index 0000000..2f98161 --- /dev/null +++ b/07/main.l @@ -0,0 +1,21 @@ +%{ +extern int yylval; +%} + +%% +[0-9]+ { + yylval = atoi(yytext); + return NUM; +} +"+" return ADD;; +"-" return SUB; +"*" return MUL; +"/" return DIV; +"(" return LPAR; +")" return RPAR; +\n return 0; +%% + +int yywrap() { + return 1; +} diff --git a/07/main.y b/07/main.y new file mode 100644 index 0000000..26a2007 --- /dev/null +++ b/07/main.y @@ -0,0 +1,34 @@ +%{ +#include +#include +int yylex(); +int yyerror(char *); +%} + +%token NUM ADD SUB MUL DIV LPAR RPAR +%left ADD SUB MUL DIV +%right NEG + +%% + +S: + E { printf("\n"); }; +E: + E ADD E { printf("+ "); } + | E MUL E { printf("* "); } + | E SUB E { printf("- "); } + | E DIV E { printf("/ "); } + | LPAR E RPAR + | SUB E %prec NEG { printf("- "); } + | NUM { printf("%d ", yylval); } + ; +%% + +int main() { + yyparse(); +} + +int yyerror(char *s) { + printf("error: %s", s); + exit(0); +} diff --git a/08/input b/08/input new file mode 100644 index 0000000..5c11124 --- /dev/null +++ b/08/input @@ -0,0 +1 @@ +aaaab diff --git a/08/main.l b/08/main.l new file mode 100644 index 0000000..ad755d5 --- /dev/null +++ b/08/main.l @@ -0,0 +1,14 @@ +%{ +extern int yylval; +%} + +%% +a return A; +b return B; +. return yytext[0]; +\n return 0; +%% + +int yywrap() { + return 1; +} diff --git a/08/main.y b/08/main.y new file mode 100644 index 0000000..04e98a7 --- /dev/null +++ b/08/main.y @@ -0,0 +1,28 @@ +%{ +#include +#include +int yyerror(char *); +%} + +%token A B +%start S + +%% + +S: C B + | + ; +C: A C + | A A A A + ; +%% + +int main() { + yyparse(); + printf("valid string"); +} + +int yyerror(char *s) { + printf("error: %s", s); + exit(0); +} diff --git a/09/input b/09/input new file mode 100644 index 0000000..01efb5e --- /dev/null +++ b/09/input @@ -0,0 +1 @@ +aaabbbbc diff --git a/09/main.l b/09/main.l new file mode 100644 index 0000000..7e888e9 --- /dev/null +++ b/09/main.l @@ -0,0 +1,15 @@ +%{ +extern int yylval; +%} + +%% +a return A; +b return B; +c return C; +. return yytext[0]; +\n return 0; +%% + +int yywrap() { + return 1; +} diff --git a/09/main.y b/09/main.y new file mode 100644 index 0000000..0db29c1 --- /dev/null +++ b/09/main.y @@ -0,0 +1,30 @@ +%{ +#include +#include +int yylex(); +int yyerror(char *); +%} + +%token A B C + +%% +S: X Y + | + ; +X: A X B + | + ; +Y: B Y C + | + ; +%% + +int main() { + yyparse(); + printf("valid string"); +} + +int yyerror(char *s) { + printf("error: %s", s); + exit(0); +} diff --git a/1/input b/1/input deleted file mode 100644 index 2ec8b41..0000000 --- a/1/input +++ /dev/null @@ -1 +0,0 @@ -alskjdfa;dsfja;kdsjfa;klsdjfa;lkjsdfkaljdsf;lakjsdf diff --git a/1/main.l b/1/main.l deleted file mode 100644 index 24f78ca..0000000 --- a/1/main.l +++ /dev/null @@ -1,20 +0,0 @@ -%{ -int v = 0; -int c = 0; -%} - -%% -[aeiouAEIOU] v++; -[a-zA-Z] c++; -. ; -%% - -int main(void) { - yylex(); - printf("%d vowels\n", v); - printf("%d consonants\n", c); -} - -int yywrap() { - return(1); -} diff --git a/2/input b/2/input deleted file mode 100644 index 5ad8acb..0000000 --- a/2/input +++ /dev/null @@ -1,5 +0,0 @@ -this is the first para text. -another line in this para. - -this is the second para text. -another line in this para. diff --git a/2/main.l b/2/main.l deleted file mode 100644 index 872c7bc..0000000 --- a/2/main.l +++ /dev/null @@ -1,29 +0,0 @@ -%{ -int word_count = 0; -int line_count = 0; -int char_count = 0; -int para_count = 0; -int spaces = 0; -%} - -%% -\n line_count++; -[ \t] spaces++; -[A-Za-z]+ { word_count++; char_count+=yyleng;}; -\n\n para_count++; -<> {para_count++; return 1;}; -. ; -%% - -int main(void) { - yylex(); - printf("%d lines\n", line_count); - printf("%d words\n", word_count); - printf("%d blanks\n", spaces); - printf("%d characters\n", char_count); - printf("%d paragraphs\n", para_count); -} - -int yywrap() { - return(1); -} diff --git a/3/input b/3/input deleted file mode 100644 index 23cb9b7..0000000 --- a/3/input +++ /dev/null @@ -1,7 +0,0 @@ -1 2 3 4 --1 -2 -3 -4 -1.1 2.2 --1.1 -2.2 -3.-7 3.+7 -3.-7 +3.+7 -1/2 1/3 1/4 1/5 --1/2 -1/3 -1/0 -73849/5678 diff --git a/3/main.l b/3/main.l deleted file mode 100644 index f423e7a..0000000 --- a/3/main.l +++ /dev/null @@ -1,38 +0,0 @@ -%{ -int posint = 0; -int posfrac = 0; -int negint = 0; -int negfrac = 0; -int errors = 0; -%} - -d [0-9] - -%% -(\+?|-){d}*\.-{d}+ errors++; // invalid numbers -(\+?|-){d}*\.\+{d}+ errors++; // invalid numbers -\+?{d}+\/[1-9]{d}* posfrac++; --{d}+\/[1-9]{d}* negfrac++; -(\+?|-){d}+\/{d}+ errors++; -\+?{d}+ posint++; --{d}+ negint++; -\+?{d}*\.{d}+ posfrac++; --{d}*\.{d}+ negfrac++; -\n ; -. ; -%% - -int main(void) { - yylex(); - printf("%d positive integers\n" , posint); - printf("%d positive fractions\n" , posfrac); - printf("%d negative integers\n" , negint); - printf("%d negative fractions\n" , negfrac); - printf("%d errors\n" , errors); -} - -int yywrap() -{ - return(1); -} - diff --git a/4/input b/4/input deleted file mode 100644 index d1dff11..0000000 --- a/4/input +++ /dev/null @@ -1,9 +0,0 @@ -int main() { - int n; - printf("hello world"); - scanf("%d", &n); - printf("hello world"); - printf("hello world"); - printf("hello world"); - printf("hello world"); -} diff --git a/4/main.l b/4/main.l deleted file mode 100644 index 28593f6..0000000 --- a/4/main.l +++ /dev/null @@ -1,32 +0,0 @@ -%{ - -int scans = 0; -int prints = 0; -%} - -%% -scanf { - scans++; - fprintf(yyout, "readf"); -}; -printf { - prints++; - fprintf(yyout, "writef"); -} -. fprintf(yyout, "%s", yytext); -\n fprintf(yyout, "\n"); -%% - -int main(void) { - yyin=fopen("4/input", "r+"); - yyout=fopen("4/output", "w"); - yylex(); - printf("%d printf occurrence(s)\n", prints); - printf("%d scanf occurrence(s)\n", scans); -} - -int yywrap() -{ - return(1); -} - diff --git a/5/input b/5/input deleted file mode 100644 index 8be510e..0000000 --- a/5/input +++ /dev/null @@ -1,5 +0,0 @@ -int main() { - int one, two, three; - three = two + one; - printf(three); -} diff --git a/5/main.l b/5/main.l deleted file mode 100644 index ee13053..0000000 --- a/5/main.l +++ /dev/null @@ -1,24 +0,0 @@ -%{ - int k = 0; - int o = 0; - int i = 0; -%} - -%% -int|main|printf|scanf|char|if|else|for|switch|case k++; -[+*/%&=] o++; -[a-zA-Z_][a-zA-Z0-9_]* i++; -. ; -\n ; -%% - -int main(void) { - yylex(); - printf("%d keywords\n", k); - printf("%d operators\n", o); - printf("%d identifiers\n", i); -} - -int yywrap() { - return 1; -} diff --git a/6/main.l b/6/main.l deleted file mode 100644 index d2eded0..0000000 --- a/6/main.l +++ /dev/null @@ -1,15 +0,0 @@ -%{ -#include -%} - -%% - -a return A; -b return B; -. return yytext[0]; -\n return 0; -%% - -int yywrap() { - return 1; -} diff --git a/6/main.y b/6/main.y deleted file mode 100644 index 25005df..0000000 --- a/6/main.y +++ /dev/null @@ -1,28 +0,0 @@ -%{ -#include -#include -int yylex(); -int yyerror(char *); -%} - -%token A B ERR - -%% - -S: - A S B - | - ; - -%% - -int yyerror(char *s) { - printf("error: %s", s); - exit(0); -} - -int main() { - yyparse(); - printf("valid string\n"); - return 0; -} diff --git a/7/main.l b/7/main.l deleted file mode 100644 index 2f98161..0000000 --- a/7/main.l +++ /dev/null @@ -1,21 +0,0 @@ -%{ -extern int yylval; -%} - -%% -[0-9]+ { - yylval = atoi(yytext); - return NUM; -} -"+" return ADD;; -"-" return SUB; -"*" return MUL; -"/" return DIV; -"(" return LPAR; -")" return RPAR; -\n return 0; -%% - -int yywrap() { - return 1; -} diff --git a/7/main.y b/7/main.y deleted file mode 100644 index 26a2007..0000000 --- a/7/main.y +++ /dev/null @@ -1,34 +0,0 @@ -%{ -#include -#include -int yylex(); -int yyerror(char *); -%} - -%token NUM ADD SUB MUL DIV LPAR RPAR -%left ADD SUB MUL DIV -%right NEG - -%% - -S: - E { printf("\n"); }; -E: - E ADD E { printf("+ "); } - | E MUL E { printf("* "); } - | E SUB E { printf("- "); } - | E DIV E { printf("/ "); } - | LPAR E RPAR - | SUB E %prec NEG { printf("- "); } - | NUM { printf("%d ", yylval); } - ; -%% - -int main() { - yyparse(); -} - -int yyerror(char *s) { - printf("error: %s", s); - exit(0); -} diff --git a/8/input b/8/input deleted file mode 100644 index 5c11124..0000000 --- a/8/input +++ /dev/null @@ -1 +0,0 @@ -aaaab diff --git a/8/main.l b/8/main.l deleted file mode 100644 index ad755d5..0000000 --- a/8/main.l +++ /dev/null @@ -1,14 +0,0 @@ -%{ -extern int yylval; -%} - -%% -a return A; -b return B; -. return yytext[0]; -\n return 0; -%% - -int yywrap() { - return 1; -} diff --git a/8/main.y b/8/main.y deleted file mode 100644 index 04e98a7..0000000 --- a/8/main.y +++ /dev/null @@ -1,28 +0,0 @@ -%{ -#include -#include -int yyerror(char *); -%} - -%token A B -%start S - -%% - -S: C B - | - ; -C: A C - | A A A A - ; -%% - -int main() { - yyparse(); - printf("valid string"); -} - -int yyerror(char *s) { - printf("error: %s", s); - exit(0); -} diff --git a/9/input b/9/input deleted file mode 100644 index 01efb5e..0000000 --- a/9/input +++ /dev/null @@ -1 +0,0 @@ -aaabbbbc diff --git a/9/main.l b/9/main.l deleted file mode 100644 index 7e888e9..0000000 --- a/9/main.l +++ /dev/null @@ -1,15 +0,0 @@ -%{ -extern int yylval; -%} - -%% -a return A; -b return B; -c return C; -. return yytext[0]; -\n return 0; -%% - -int yywrap() { - return 1; -} diff --git a/9/main.y b/9/main.y deleted file mode 100644 index 0db29c1..0000000 --- a/9/main.y +++ /dev/null @@ -1,30 +0,0 @@ -%{ -#include -#include -int yylex(); -int yyerror(char *); -%} - -%token A B C - -%% -S: X Y - | - ; -X: A X B - | - ; -Y: B Y C - | - ; -%% - -int main() { - yyparse(); - printf("valid string"); -} - -int yyerror(char *s) { - printf("error: %s", s); - exit(0); -} diff --git a/flake.nix b/flake.nix index c94f08a..17cceb1 100644 --- a/flake.nix +++ b/flake.nix @@ -13,15 +13,15 @@ bison ]; execs = [ - { name = "1"; yacc = false; } - { name = "2"; yacc = false; } - { name = "3"; yacc = false; } - { name = "4"; yacc = false; } - { name = "5"; yacc = false; } - { name = "6"; yacc = true; } - { name = "7"; yacc = true; } - { name = "8"; yacc = true; } - { name = "9"; yacc = true; } + { name = "01"; yacc = false; } + { name = "02"; yacc = false; } + { name = "03"; yacc = false; } + { name = "04"; yacc = false; } + { name = "05"; yacc = false; } + { name = "06"; yacc = true; } + { name = "07"; yacc = true; } + { name = "08"; yacc = true; } + { name = "09"; yacc = true; } { name = "10"; yacc = true; } { name = "11"; yacc = true; } ]; -- cgit v1.2.3