aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--4/input4
-rw-r--r--4/main.l7
-rw-r--r--5/input5
-rw-r--r--5/main.l24
-rw-r--r--flake.nix59
5 files changed, 66 insertions, 33 deletions
diff --git a/4/input b/4/input
index 7f0cc0b..d1dff11 100644
--- a/4/input
+++ b/4/input
@@ -2,4 +2,8 @@ int main() {
2 int n; 2 int n;
3 printf("hello world"); 3 printf("hello world");
4 scanf("%d", &n); 4 scanf("%d", &n);
5 printf("hello world");
6 printf("hello world");
7 printf("hello world");
8 printf("hello world");
5} 9}
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 {
13 prints++; 13 prints++;
14 fprintf(yyout, "writef"); 14 fprintf(yyout, "writef");
15} 15}
16; fprintf(yyout, "%s", yytext); 16. fprintf(yyout, "%s", yytext);
17\n fprintf(yyout, "\n"); 17\n fprintf(yyout, "\n");
18%% 18%%
19 19
@@ -21,13 +21,12 @@ int main(void) {
21 yyin=fopen("4/input", "r+"); 21 yyin=fopen("4/input", "r+");
22 yyout=fopen("4/output", "w"); 22 yyout=fopen("4/output", "w");
23 yylex(); 23 yylex();
24 printf("%d printf occurrences\n", prints); 24 printf("%d printf occurrence(s)\n", prints);
25 printf("%d scanf occurrences\n", scans); 25 printf("%d scanf occurrence(s)\n", scans);
26} 26}
27 27
28int yywrap() 28int yywrap()
29{ 29{
30exit(0);
31 return(1); 30 return(1);
32} 31}
33 32
diff --git a/5/input b/5/input
new file mode 100644
index 0000000..8be510e
--- /dev/null
+++ b/5/input
@@ -0,0 +1,5 @@
1int main() {
2 int one, two, three;
3 three = two + one;
4 printf(three);
5}
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 @@
1%{
2 int k = 0;
3 int o = 0;
4 int i = 0;
5%}
6
7%%
8int|main|printf|scanf|char|if|else|for|switch|case k++;
9[+*/%&=] o++;
10[a-zA-Z_][a-zA-Z0-9_]* i++;
11. ;
12\n ;
13%%
14
15int main(void) {
16 yylex();
17 printf("%d keywords\n", k);
18 printf("%d operators\n", o);
19 printf("%d identifiers\n", i);
20}
21
22int yywrap() {
23 return 1;
24}
diff --git a/flake.nix b/flake.nix
index b8892fa..c85db6e 100644
--- a/flake.nix
+++ b/flake.nix
@@ -4,37 +4,38 @@
4 }; 4 };
5 5
6 outputs = { self, nixpkgs, utils, ... }: 6 outputs = { self, nixpkgs, utils, ... }:
7 utils.lib.eachDefaultSystem (system: 7 utils.lib.eachDefaultSystem (system:
8 let 8 let
9 pkgs = nixpkgs.legacyPackages."${system}"; 9 pkgs = nixpkgs.legacyPackages."${system}";
10 nativeBuildInputs = with pkgs; [ 10 nativeBuildInputs = with pkgs; [
11 flex 11 flex
12 bison 12 bison
13 ]; 13 ];
14 14
15 buildScript = pkgs.writeScriptBin "build-script" 15 buildScript = pkgs.writeScriptBin "build-script"
16 '' 16 ''
17 set -e 17 set -e
18 prog_name="$1" 18 prog_name="$1"
19 outdir="outputs/$prog_name" 19 outdir="outputs/$prog_name"
20 mkdir -p "$outdir" 20 mkdir -p "$outdir"
21 21
22 ${pkgs.flex}/bin/flex -o "$outdir"/lex.yy.cc "$prog_name"/main.l 22 ${pkgs.flex}/bin/flex -o "$outdir"/lex.yy.cc "$prog_name"/main.l
23 ${pkgs.gcc}/bin/gcc "$outdir"/lex.yy.cc -o "$outdir"/exec 23 ${pkgs.gcc}/bin/gcc "$outdir"/lex.yy.cc -o "$outdir"/exec
24 24
25 ./"$outdir"/exec < "$prog_name"/input 25 ./"$outdir"/exec < "$prog_name"/input
26 rm -r "outputs"/* 26 rm -r outputs
27 ''; 27 '';
28 28
29 in rec { 29 in
30 devShell = pkgs.mkShell { 30 rec {
31 nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [ 31 devShell = pkgs.mkShell {
32 gcc 32 nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [
33 ]); 33 gcc
34 }; 34 ]);
35 defaultApp = { 35 };
36 type = "app"; 36 defaultApp = {
37 program = "${buildScript}/bin/build-script"; 37 type = "app";
38 }; 38 program = "${buildScript}/bin/build-script";
39 }); 39 };
40 });
40} 41}