From 30047205e440e9ad8984e1830b8b819b5fe1f9f7 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 16 Apr 2021 10:02:33 +0530 Subject: add prog 5 --- 4/input | 4 ++++ 4/main.l | 7 +++---- 5/input | 5 +++++ 5/main.l | 24 ++++++++++++++++++++++++ flake.nix | 59 ++++++++++++++++++++++++++++++----------------------------- 5 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 5/input create mode 100644 5/main.l diff --git a/4/input b/4/input index 7f0cc0b..d1dff11 100644 --- a/4/input +++ b/4/input @@ -2,4 +2,8 @@ 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 index f768382..28593f6 100644 --- a/4/main.l +++ b/4/main.l @@ -13,7 +13,7 @@ printf { prints++; fprintf(yyout, "writef"); } -; fprintf(yyout, "%s", yytext); +. fprintf(yyout, "%s", yytext); \n fprintf(yyout, "\n"); %% @@ -21,13 +21,12 @@ int main(void) { yyin=fopen("4/input", "r+"); yyout=fopen("4/output", "w"); yylex(); - printf("%d printf occurrences\n", prints); - printf("%d scanf occurrences\n", scans); + printf("%d printf occurrence(s)\n", prints); + printf("%d scanf occurrence(s)\n", scans); } int yywrap() { -exit(0); return(1); } diff --git a/5/input b/5/input new file mode 100644 index 0000000..8be510e --- /dev/null +++ b/5/input @@ -0,0 +1,5 @@ +int main() { + int one, two, three; + three = two + one; + printf(three); +} diff --git a/5/main.l b/5/main.l new file mode 100644 index 0000000..ee13053 --- /dev/null +++ b/5/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/flake.nix b/flake.nix index b8892fa..c85db6e 100644 --- a/flake.nix +++ b/flake.nix @@ -4,37 +4,38 @@ }; outputs = { self, nixpkgs, utils, ... }: - utils.lib.eachDefaultSystem (system: - let - pkgs = nixpkgs.legacyPackages."${system}"; - nativeBuildInputs = with pkgs; [ - flex - bison - ]; + utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages."${system}"; + nativeBuildInputs = with pkgs; [ + flex + bison + ]; - buildScript = pkgs.writeScriptBin "build-script" - '' - set -e - prog_name="$1" - outdir="outputs/$prog_name" - mkdir -p "$outdir" + buildScript = pkgs.writeScriptBin "build-script" + '' + set -e + prog_name="$1" + outdir="outputs/$prog_name" + mkdir -p "$outdir" - ${pkgs.flex}/bin/flex -o "$outdir"/lex.yy.cc "$prog_name"/main.l - ${pkgs.gcc}/bin/gcc "$outdir"/lex.yy.cc -o "$outdir"/exec + ${pkgs.flex}/bin/flex -o "$outdir"/lex.yy.cc "$prog_name"/main.l + ${pkgs.gcc}/bin/gcc "$outdir"/lex.yy.cc -o "$outdir"/exec - ./"$outdir"/exec < "$prog_name"/input - rm -r "outputs"/* - ''; + ./"$outdir"/exec < "$prog_name"/input + rm -r outputs + ''; - in rec { - devShell = pkgs.mkShell { - nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [ - gcc - ]); - }; - defaultApp = { - type = "app"; - program = "${buildScript}/bin/build-script"; - }; - }); + in + rec { + devShell = pkgs.mkShell { + nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [ + gcc + ]); + }; + defaultApp = { + type = "app"; + program = "${buildScript}/bin/build-script"; + }; + }); } -- cgit v1.2.3