cdex 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cdex/__init__.py +55 -0
- cdex/programs/ex1/input.c +9 -0
- cdex/programs/ex1/tokens.c +96 -0
- cdex/programs/ex2/input.c +8 -0
- cdex/programs/ex2/lexer.l +23 -0
- cdex/programs/ex3/calc.l +18 -0
- cdex/programs/ex3/calc.y +45 -0
- cdex/programs/ex4/tac.l +16 -0
- cdex/programs/ex4/tac.y +131 -0
- cdex/programs/ex5/type.l +17 -0
- cdex/programs/ex5/type.y +116 -0
- cdex/programs/ex6/optimize.c +119 -0
- cdex/programs/ex7/backend.c +72 -0
- cdex-0.1.0.dist-info/METADATA +41 -0
- cdex-0.1.0.dist-info/RECORD +17 -0
- cdex-0.1.0.dist-info/WHEEL +5 -0
- cdex-0.1.0.dist-info/top_level.txt +1 -0
cdex/__init__.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Write the CS3501 lab source files into a directory."""
|
|
2
|
+
from importlib.resources import files
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
__all__ = ["ex1", "ex2", "ex3", "ex4", "ex5", "ex6", "ex7"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _write(experiment, directory):
|
|
9
|
+
destination = Path(experiment if directory is None else directory)
|
|
10
|
+
destination.mkdir(parents=True, exist_ok=True)
|
|
11
|
+
folder = files(__package__).joinpath("programs").joinpath(experiment)
|
|
12
|
+
sources = sorted(folder.iterdir(), key=lambda item: item.name)
|
|
13
|
+
targets = [destination / item.name for item in sources]
|
|
14
|
+
for target in targets:
|
|
15
|
+
if target.exists() or target.is_symlink():
|
|
16
|
+
raise FileExistsError(f"{target} already exists. Choose a fresh directory.")
|
|
17
|
+
for source, target in zip(sources, targets):
|
|
18
|
+
with target.open("xb") as output:
|
|
19
|
+
output.write(source.read_bytes())
|
|
20
|
+
return targets
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def ex1(directory=None):
|
|
24
|
+
"""Write the C lexical analyzer source files and return their paths."""
|
|
25
|
+
return _write("ex1", directory)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def ex2(directory=None):
|
|
29
|
+
"""Write the Lex lexical analyzer source files and return their paths."""
|
|
30
|
+
return _write("ex2", directory)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def ex3(directory=None):
|
|
34
|
+
"""Write the Lex/Yacc calculator (3d) source files and return their paths."""
|
|
35
|
+
return _write("ex3", directory)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def ex4(directory=None):
|
|
39
|
+
"""Write the Three-address code source files and return their paths."""
|
|
40
|
+
return _write("ex4", directory)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def ex5(directory=None):
|
|
44
|
+
"""Write the Type checking source files and return their paths."""
|
|
45
|
+
return _write("ex5", directory)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def ex6(directory=None):
|
|
49
|
+
"""Write the Code optimization source files and return their paths."""
|
|
50
|
+
return _write("ex6", directory)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def ex7(directory=None):
|
|
54
|
+
"""Write the 8086 code generator source files and return their paths."""
|
|
55
|
+
return _write("ex7", directory)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <ctype.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
|
|
5
|
+
int main()
|
|
6
|
+
{
|
|
7
|
+
FILE *fp;
|
|
8
|
+
int ch, i;
|
|
9
|
+
char word[100];
|
|
10
|
+
|
|
11
|
+
char keywords[][10] = {
|
|
12
|
+
"int", "float", "char", "if", "else",
|
|
13
|
+
"for", "while", "return", "void"
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
fp = fopen("input.c", "r");
|
|
17
|
+
|
|
18
|
+
if (fp == NULL)
|
|
19
|
+
{
|
|
20
|
+
printf("File not found");
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
printf("TOKENS\n");
|
|
25
|
+
|
|
26
|
+
while ((ch = fgetc(fp)) != EOF)
|
|
27
|
+
{
|
|
28
|
+
if (ch == '#')
|
|
29
|
+
{
|
|
30
|
+
while ((ch = fgetc(fp)) != '\n' && ch != EOF);
|
|
31
|
+
printf("Header directive found\n");
|
|
32
|
+
}
|
|
33
|
+
else if (isalpha(ch) || ch == '_')
|
|
34
|
+
{
|
|
35
|
+
i = 0;
|
|
36
|
+
word[i++] = ch;
|
|
37
|
+
|
|
38
|
+
while (isalnum(ch = fgetc(fp)) || ch == '_')
|
|
39
|
+
word[i++] = ch;
|
|
40
|
+
|
|
41
|
+
word[i] = '\0';
|
|
42
|
+
|
|
43
|
+
if (ch != EOF)
|
|
44
|
+
ungetc(ch, fp);
|
|
45
|
+
|
|
46
|
+
int flag = 0;
|
|
47
|
+
|
|
48
|
+
for (i = 0; i < 9; i++)
|
|
49
|
+
if (strcmp(word, keywords[i]) == 0)
|
|
50
|
+
flag = 1;
|
|
51
|
+
|
|
52
|
+
if (flag)
|
|
53
|
+
printf("Keyword: %s\n", word);
|
|
54
|
+
else
|
|
55
|
+
printf("Identifier: %s\n", word);
|
|
56
|
+
}
|
|
57
|
+
else if (isdigit(ch))
|
|
58
|
+
{
|
|
59
|
+
printf("Number: %c", ch);
|
|
60
|
+
|
|
61
|
+
while (isdigit(ch = fgetc(fp)))
|
|
62
|
+
printf("%c", ch);
|
|
63
|
+
|
|
64
|
+
printf("\n");
|
|
65
|
+
|
|
66
|
+
if (ch != EOF)
|
|
67
|
+
ungetc(ch, fp);
|
|
68
|
+
}
|
|
69
|
+
else if (ch == '"')
|
|
70
|
+
{
|
|
71
|
+
printf("String literal: \"");
|
|
72
|
+
while ((ch = fgetc(fp)) != '"' && ch != EOF)
|
|
73
|
+
printf("%c", ch);
|
|
74
|
+
|
|
75
|
+
printf("\"\n");
|
|
76
|
+
}
|
|
77
|
+
else if (strchr("+-*/%=<>!", ch))
|
|
78
|
+
{
|
|
79
|
+
int next = fgetc(fp);
|
|
80
|
+
|
|
81
|
+
if (next == '=' || (ch == '+' && next == '+') ||
|
|
82
|
+
(ch == '-' && next == '-'))
|
|
83
|
+
printf("Operator: %c%c\n", ch, next);
|
|
84
|
+
else
|
|
85
|
+
{
|
|
86
|
+
printf("Operator: %c\n", ch);
|
|
87
|
+
|
|
88
|
+
if (next != EOF)
|
|
89
|
+
ungetc(next, fp);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fclose(fp);
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
%}
|
|
4
|
+
|
|
5
|
+
%%
|
|
6
|
+
"int"|"float"|"char"|"if"|"else"|"for"|"while"|"return"|"void" { printf("Keyword: %s\n", yytext); }
|
|
7
|
+
[0-9]+ { printf("Number: %s\n", yytext); }
|
|
8
|
+
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
|
|
9
|
+
\"[^\"]*\" { printf("String: %s\n", yytext); }
|
|
10
|
+
"=="|"!="|"<="|">="|"++"|"--"|"+"|"-"|"*"|"/"|"="|">"|"<" { printf("Operator: %s\n", yytext); }
|
|
11
|
+
[ \t\n]+ { }
|
|
12
|
+
. { }
|
|
13
|
+
%%
|
|
14
|
+
|
|
15
|
+
int main()
|
|
16
|
+
{
|
|
17
|
+
yylex();
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
int yywrap(void)
|
|
21
|
+
{
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
cdex/programs/ex3/calc.l
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include "y.tab.h"
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
%}
|
|
5
|
+
|
|
6
|
+
%%
|
|
7
|
+
[0-9]+ { yylval = atoi(yytext); return NUM; }
|
|
8
|
+
[a-z] { yylval = yytext[0] - 'a'; return VAR; }
|
|
9
|
+
[+\-*/%()] { return yytext[0]; }
|
|
10
|
+
"=" { return '='; }
|
|
11
|
+
\n { return '\n'; }
|
|
12
|
+
[ \t] ;
|
|
13
|
+
. ;
|
|
14
|
+
%%
|
|
15
|
+
int yywrap(void)
|
|
16
|
+
{
|
|
17
|
+
return 1;
|
|
18
|
+
}
|
cdex/programs/ex3/calc.y
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
|
|
4
|
+
int yylex(void);
|
|
5
|
+
int yyerror(char *s);
|
|
6
|
+
int var[26];
|
|
7
|
+
%}
|
|
8
|
+
|
|
9
|
+
%token NUM VAR
|
|
10
|
+
%left '+' '-'
|
|
11
|
+
%left '*' '/' '%'
|
|
12
|
+
%%
|
|
13
|
+
input:
|
|
14
|
+
| input line
|
|
15
|
+
;
|
|
16
|
+
|
|
17
|
+
line:
|
|
18
|
+
expr '\n' { printf("Result = %d\n", $1); }
|
|
19
|
+
| VAR '=' expr '\n' { var[$1] = $3; printf("Stored\n"); }
|
|
20
|
+
;
|
|
21
|
+
|
|
22
|
+
expr:
|
|
23
|
+
NUM { $$ = $1; }
|
|
24
|
+
| VAR { $$ = var[$1]; }
|
|
25
|
+
| expr '+' expr { $$ = $1 + $3; }
|
|
26
|
+
| expr '-' expr { $$ = $1 - $3; }
|
|
27
|
+
| expr '*' expr { $$ = $1 * $3; }
|
|
28
|
+
| expr '/' expr { $$ = $1 / $3; }
|
|
29
|
+
| expr '%' expr { $$ = $1 % $3; }
|
|
30
|
+
| '(' expr ')' { $$ = $2; }
|
|
31
|
+
;
|
|
32
|
+
%%
|
|
33
|
+
|
|
34
|
+
int main()
|
|
35
|
+
{
|
|
36
|
+
printf("Enter expressions (Ctrl+D to exit):\n");
|
|
37
|
+
yyparse();
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
int yyerror(char *s)
|
|
42
|
+
{
|
|
43
|
+
printf("Invalid expression\n");
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
cdex/programs/ex4/tac.l
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include "y.tab.h"
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
%}
|
|
6
|
+
|
|
7
|
+
%option noyywrap
|
|
8
|
+
|
|
9
|
+
%%
|
|
10
|
+
[0-9]+ { strcpy(yylval.str, yytext); return NUM; }
|
|
11
|
+
[a-zA-Z] { strcpy(yylval.str, yytext); return ID; }
|
|
12
|
+
[+\-*/=()] { return yytext[0]; }
|
|
13
|
+
[ \t] ;
|
|
14
|
+
\n { return '\n'; }
|
|
15
|
+
. ;
|
|
16
|
+
%%
|
cdex/programs/ex4/tac.y
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
|
|
6
|
+
int yylex(void);
|
|
7
|
+
int yyerror(char *s);
|
|
8
|
+
|
|
9
|
+
struct Quad
|
|
10
|
+
{
|
|
11
|
+
char op[10];
|
|
12
|
+
char arg1[10];
|
|
13
|
+
char arg2[10];
|
|
14
|
+
char result[10];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
struct Quad q[20];
|
|
18
|
+
int count = 0;
|
|
19
|
+
%}
|
|
20
|
+
|
|
21
|
+
%union
|
|
22
|
+
{
|
|
23
|
+
char str[10];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
%token <str> ID NUM
|
|
27
|
+
%type <str> expr
|
|
28
|
+
|
|
29
|
+
%left '+' '-'
|
|
30
|
+
%left '*' '/'
|
|
31
|
+
|
|
32
|
+
%%
|
|
33
|
+
input:
|
|
34
|
+
ID '=' expr '\n'
|
|
35
|
+
{
|
|
36
|
+
strcpy(q[count].op, "=");
|
|
37
|
+
strcpy(q[count].arg1, $3);
|
|
38
|
+
strcpy(q[count].arg2, "-");
|
|
39
|
+
strcpy(q[count].result, $1);
|
|
40
|
+
count++;
|
|
41
|
+
printf("\nTHREE ADDRESS CODE\n");
|
|
42
|
+
|
|
43
|
+
for(int i = 0; i < count; i++)
|
|
44
|
+
{
|
|
45
|
+
if(strcmp(q[i].op, "=") == 0)
|
|
46
|
+
printf("%s = %s\n", q[i].result, q[i].arg1);
|
|
47
|
+
else
|
|
48
|
+
printf("%s = %s %s %s\n",
|
|
49
|
+
q[i].result, q[i].arg1, q[i].op, q[i].arg2);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
printf("\nQUADRUPLES\n");
|
|
53
|
+
printf("Op\tArg1\tArg2\tResult\n");
|
|
54
|
+
|
|
55
|
+
for(int i = 0; i < count; i++)
|
|
56
|
+
printf("%s\t%s\t%s\t%s\n",
|
|
57
|
+
q[i].op, q[i].arg1, q[i].arg2, q[i].result);
|
|
58
|
+
|
|
59
|
+
printf("\nTRIPLES\n");
|
|
60
|
+
printf("Index\tOp\tArg1\tArg2\n");
|
|
61
|
+
|
|
62
|
+
for(int i = 0; i < count; i++)
|
|
63
|
+
printf("%d\t%s\t%s\t%s\n",
|
|
64
|
+
i, q[i].op, q[i].arg1, q[i].arg2);
|
|
65
|
+
}
|
|
66
|
+
;
|
|
67
|
+
|
|
68
|
+
expr:
|
|
69
|
+
expr '+' expr
|
|
70
|
+
{
|
|
71
|
+
strcpy(q[count].op, "+");
|
|
72
|
+
strcpy(q[count].arg1, $1);
|
|
73
|
+
strcpy(q[count].arg2, $3);
|
|
74
|
+
sprintf(q[count].result, "t%d", count);
|
|
75
|
+
strcpy($$, q[count].result);
|
|
76
|
+
count++;
|
|
77
|
+
}
|
|
78
|
+
| expr '-' expr
|
|
79
|
+
{
|
|
80
|
+
strcpy(q[count].op, "-");
|
|
81
|
+
strcpy(q[count].arg1, $1);
|
|
82
|
+
strcpy(q[count].arg2, $3);
|
|
83
|
+
sprintf(q[count].result, "t%d", count);
|
|
84
|
+
strcpy($$, q[count].result);
|
|
85
|
+
count++;
|
|
86
|
+
}
|
|
87
|
+
| expr '*' expr
|
|
88
|
+
{
|
|
89
|
+
strcpy(q[count].op, "*");
|
|
90
|
+
strcpy(q[count].arg1, $1);
|
|
91
|
+
strcpy(q[count].arg2, $3);
|
|
92
|
+
sprintf(q[count].result, "t%d", count);
|
|
93
|
+
strcpy($$, q[count].result);
|
|
94
|
+
count++;
|
|
95
|
+
}
|
|
96
|
+
| expr '/' expr
|
|
97
|
+
{
|
|
98
|
+
strcpy(q[count].op, "/");
|
|
99
|
+
strcpy(q[count].arg1, $1);
|
|
100
|
+
strcpy(q[count].arg2, $3);
|
|
101
|
+
sprintf(q[count].result, "t%d", count);
|
|
102
|
+
strcpy($$, q[count].result);
|
|
103
|
+
count++;
|
|
104
|
+
}
|
|
105
|
+
| '(' expr ')'
|
|
106
|
+
{
|
|
107
|
+
strcpy($$, $2);
|
|
108
|
+
}
|
|
109
|
+
| ID
|
|
110
|
+
{
|
|
111
|
+
strcpy($$, $1);
|
|
112
|
+
}
|
|
113
|
+
| NUM
|
|
114
|
+
{
|
|
115
|
+
strcpy($$, $1);
|
|
116
|
+
}
|
|
117
|
+
;
|
|
118
|
+
%%
|
|
119
|
+
|
|
120
|
+
int main()
|
|
121
|
+
{
|
|
122
|
+
printf("Enter an assignment expression:\n");
|
|
123
|
+
yyparse();
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
int yyerror(char *s)
|
|
128
|
+
{
|
|
129
|
+
printf("Invalid expression\n");
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
cdex/programs/ex5/type.l
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include "y.tab.h"
|
|
3
|
+
%}
|
|
4
|
+
|
|
5
|
+
%option noyywrap
|
|
6
|
+
|
|
7
|
+
%%
|
|
8
|
+
"int" { return INT; }
|
|
9
|
+
"float" { return FLOAT; }
|
|
10
|
+
[0-9]+\.[0-9]+ { return FLOATNUM; }
|
|
11
|
+
[0-9]+ { return NUM; }
|
|
12
|
+
[a-zA-Z] { yylval.val = yytext[0] - 'a'; return ID; }
|
|
13
|
+
[+\-*/=()] { return yytext[0]; }
|
|
14
|
+
[ \t] ;
|
|
15
|
+
\n { return '\n'; }
|
|
16
|
+
. ;
|
|
17
|
+
%%
|
cdex/programs/ex5/type.y
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
%{
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
int yylex(void);
|
|
4
|
+
int yyerror(char *s);
|
|
5
|
+
|
|
6
|
+
int type[26];
|
|
7
|
+
%}
|
|
8
|
+
|
|
9
|
+
%union
|
|
10
|
+
{
|
|
11
|
+
int val;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
%token INT FLOAT
|
|
15
|
+
%token <val> ID NUM FLOATNUM
|
|
16
|
+
%type <val> expr
|
|
17
|
+
|
|
18
|
+
%left '+' '-'
|
|
19
|
+
%left '*' '/'
|
|
20
|
+
|
|
21
|
+
%%
|
|
22
|
+
input:
|
|
23
|
+
| input line
|
|
24
|
+
;
|
|
25
|
+
|
|
26
|
+
line:
|
|
27
|
+
INT ID '\n'
|
|
28
|
+
{
|
|
29
|
+
type[$2] = 1;
|
|
30
|
+
printf("Integer variable declared\n");
|
|
31
|
+
}
|
|
32
|
+
| FLOAT ID '\n'
|
|
33
|
+
{
|
|
34
|
+
type[$2] = 2;
|
|
35
|
+
printf("Float variable declared\n");
|
|
36
|
+
}
|
|
37
|
+
| ID '=' expr '\n'
|
|
38
|
+
{
|
|
39
|
+
if(type[$1] == 0)
|
|
40
|
+
printf("Error: Undeclared variable\n");
|
|
41
|
+
else if($3 == -1)
|
|
42
|
+
printf("Error: Type mismatch in expression\n");
|
|
43
|
+
else if(type[$1] != $3)
|
|
44
|
+
printf("Error: Type mismatch in assignment\n");
|
|
45
|
+
else
|
|
46
|
+
printf("Type checking successful\n");
|
|
47
|
+
}
|
|
48
|
+
;
|
|
49
|
+
|
|
50
|
+
expr:
|
|
51
|
+
ID
|
|
52
|
+
{
|
|
53
|
+
if(type[$1] == 0)
|
|
54
|
+
{
|
|
55
|
+
printf("Error: Undeclared variable\n");
|
|
56
|
+
$$ = -1;
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
$$ = type[$1];
|
|
60
|
+
}
|
|
61
|
+
| NUM
|
|
62
|
+
{
|
|
63
|
+
$$ = 1;
|
|
64
|
+
}
|
|
65
|
+
| FLOATNUM
|
|
66
|
+
{
|
|
67
|
+
$$ = 2;
|
|
68
|
+
}
|
|
69
|
+
| expr '+' expr
|
|
70
|
+
{
|
|
71
|
+
if($1 == -1 || $3 == -1 || $1 != $3)
|
|
72
|
+
$$ = -1;
|
|
73
|
+
else
|
|
74
|
+
$$ = $1;
|
|
75
|
+
}
|
|
76
|
+
| expr '-' expr
|
|
77
|
+
{
|
|
78
|
+
if($1 == -1 || $3 == -1 || $1 != $3)
|
|
79
|
+
$$ = -1;
|
|
80
|
+
else
|
|
81
|
+
$$ = $1;
|
|
82
|
+
}
|
|
83
|
+
| expr '*' expr
|
|
84
|
+
{
|
|
85
|
+
if($1 == -1 || $3 == -1 || $1 != $3)
|
|
86
|
+
$$ = -1;
|
|
87
|
+
else
|
|
88
|
+
$$ = $1;
|
|
89
|
+
}
|
|
90
|
+
| expr '/' expr
|
|
91
|
+
{
|
|
92
|
+
if($1 == -1 || $3 == -1 || $1 != $3)
|
|
93
|
+
$$ = -1;
|
|
94
|
+
else
|
|
95
|
+
$$ = $1;
|
|
96
|
+
}
|
|
97
|
+
| '(' expr ')'
|
|
98
|
+
{
|
|
99
|
+
$$ = $2;
|
|
100
|
+
}
|
|
101
|
+
;
|
|
102
|
+
%%
|
|
103
|
+
|
|
104
|
+
int main()
|
|
105
|
+
{
|
|
106
|
+
printf("Type Checking\n");
|
|
107
|
+
printf("Enter statements (Ctrl+D to exit):\n");
|
|
108
|
+
yyparse();
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
int yyerror(char *s)
|
|
113
|
+
{
|
|
114
|
+
printf("Invalid statement\n");
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
int isnum(char *s)
|
|
7
|
+
{
|
|
8
|
+
char *end;
|
|
9
|
+
strtol(s, &end, 10);
|
|
10
|
+
return *s != '\0' && *end == '\0';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
int main()
|
|
15
|
+
{
|
|
16
|
+
char line[100], lhs[20], a[20], b[20], op;
|
|
17
|
+
int x, y, result;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
printf("Enter three-address code (END to stop):\n");
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
while (1)
|
|
24
|
+
{
|
|
25
|
+
fgets(line, sizeof(line), stdin);
|
|
26
|
+
if (strncmp(line, "END", 3) == 0)
|
|
27
|
+
break;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if (sscanf(line, "%19s = %19s %c %19s",
|
|
31
|
+
lhs, a, &op, b) == 4)
|
|
32
|
+
{
|
|
33
|
+
// Constant Folding
|
|
34
|
+
if (isnum(a) && isnum(b))
|
|
35
|
+
{
|
|
36
|
+
x = atoi(a);
|
|
37
|
+
y = atoi(b);
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
switch (op)
|
|
41
|
+
{
|
|
42
|
+
case '+': result = x + y; break;
|
|
43
|
+
case '-': result = x - y; break;
|
|
44
|
+
case '*': result = x * y; break;
|
|
45
|
+
case '/':
|
|
46
|
+
if (y == 0)
|
|
47
|
+
{
|
|
48
|
+
printf("%s = %s / %s\n", lhs, a, b);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
result = x / y;
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
printf("%s", line);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
printf("%s = %d\n", lhs, result);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// Strength Reduction
|
|
64
|
+
else if (op == '*' && strcmp(b, "2") == 0)
|
|
65
|
+
printf("%s = %s + %s\n", lhs, a, a);
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
else if (op == '*' && strcmp(a, "2") == 0)
|
|
69
|
+
printf("%s = %s + %s\n", lhs, b, b);
|
|
70
|
+
// Algebraic Transformations
|
|
71
|
+
else if (op == '+' && strcmp(b, "0") == 0)
|
|
72
|
+
printf("%s = %s\n", lhs, a);
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
else if (op == '+' && strcmp(a, "0") == 0)
|
|
76
|
+
printf("%s = %s\n", lhs, b);
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
else if (op == '-' && strcmp(b, "0") == 0)
|
|
80
|
+
printf("%s = %s\n", lhs, a);
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
else if (op == '-' && strcmp(a, b) == 0)
|
|
84
|
+
printf("%s = 0\n", lhs);
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
else if (op == '*' && strcmp(b, "1") == 0)
|
|
88
|
+
printf("%s = %s\n", lhs, a);
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
else if (op == '*' && strcmp(a, "1") == 0)
|
|
92
|
+
printf("%s = %s\n", lhs, b);
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
else if (op == '*' &&
|
|
96
|
+
(strcmp(a, "0") == 0 || strcmp(b, "0") == 0))
|
|
97
|
+
printf("%s = 0\n", lhs);
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
else if (op == '/' && strcmp(b, "1") == 0)
|
|
101
|
+
printf("%s = %s\n", lhs, a);
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
else
|
|
105
|
+
printf("%s = %s %c %s\n", lhs, a, op, b);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
else if (sscanf(line, "%19s = %19s", lhs, a) == 2)
|
|
110
|
+
printf("%s = %s\n", lhs, a);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
else
|
|
114
|
+
printf("Invalid statement: %s", line);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
int main()
|
|
6
|
+
{
|
|
7
|
+
char line[100], x[20], y[20], z[20], op[5];
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
printf("Enter TAC statements (0 to stop):\n");
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
while (fgets(line, sizeof(line), stdin))
|
|
14
|
+
{
|
|
15
|
+
if (line[0] == '0')
|
|
16
|
+
break;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if (sscanf(line, "%19s = %19s %4s %19s",
|
|
20
|
+
x, y, op, z) == 4)
|
|
21
|
+
{
|
|
22
|
+
printf("\n8086 Code:\n");
|
|
23
|
+
printf("MOV AX, [%s]\n", y);
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if (!strcmp(op, "+"))
|
|
27
|
+
printf("ADD AX, [%s]\n", z);
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
else if (!strcmp(op, "-"))
|
|
31
|
+
printf("SUB AX, [%s]\n", z);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
else
|
|
35
|
+
{
|
|
36
|
+
printf("Unsupported operator\n");
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
printf("MOV [%s], AX\n", x);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
else if (sscanf(line, "%19s = %19s", x, y) == 2)
|
|
46
|
+
{
|
|
47
|
+
printf("\n8086 Code:\n");
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
if (y[0] >= '0' && y[0] <= '9')
|
|
51
|
+
printf("MOV AX, %s\n", y);
|
|
52
|
+
else
|
|
53
|
+
printf("MOV AX, [%s]\n", y);
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
printf("MOV [%s], AX\n", x);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
else
|
|
61
|
+
{
|
|
62
|
+
printf("Invalid TAC statement\n");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
printf("\nMOV AX, 4C00H\n");
|
|
68
|
+
printf("INT 21H\n");
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cdex
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Export seven CS3501 compiler design lab experiments as C, Lex and Yacc source files.
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
# CS3501 Compiler Design Lab
|
|
9
|
+
|
|
10
|
+
Install with `python -m pip install cdex` after publication. To install from the extracted project before publication, run `python -m pip install .`.
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
import cdex
|
|
14
|
+
|
|
15
|
+
print(cdex.ex2()) # creates ex2/lexer.l and ex2/input.c
|
|
16
|
+
print(cdex.ex3("my_calculator")) # writes calc.l and calc.y to my_calculator/
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Each function writes the bundled text files and returns a list of paths. Use a fresh destination when a filename already exists. Python 3.9 or later is required. To compile the exported sources on Ubuntu, install GCC, Flex and Bison. Run each command from the corresponding exported directory.
|
|
20
|
+
|
|
21
|
+
| Function | Experiment | Ubuntu command |
|
|
22
|
+
| --- | --- | --- |
|
|
23
|
+
| `ex1()` | C lexical analyzer | `gcc tokens.c -o tokens && ./tokens` |
|
|
24
|
+
| `ex2()` | Lex lexical analyzer | `flex lexer.l && gcc lex.yy.c -o lexer && ./lexer < input.c` |
|
|
25
|
+
| `ex3()` | Lex/Yacc calculator (3d) | `yacc -d calc.y && flex calc.l && gcc y.tab.c lex.yy.c -o calc && ./calc` |
|
|
26
|
+
| `ex4()` | Three-address code | `yacc -d tac.y && flex tac.l && gcc y.tab.c lex.yy.c -o tac && ./tac` |
|
|
27
|
+
| `ex5()` | Type checking | `yacc -d type.y && flex type.l && gcc y.tab.c lex.yy.c -o type && ./type` |
|
|
28
|
+
| `ex6()` | Code optimization | `gcc optimize.c -o optimize && ./optimize` |
|
|
29
|
+
| `ex7()` | 8086 code generator | `gcc backend.c -o backend && ./backend` |
|
|
30
|
+
|
|
31
|
+
## Publish directly to PyPI
|
|
32
|
+
|
|
33
|
+
From the extracted folder containing pyproject.toml, run:
|
|
34
|
+
|
|
35
|
+
```powershell
|
|
36
|
+
python -m pip install --upgrade twine
|
|
37
|
+
python -m twine check dist/*
|
|
38
|
+
python -m twine upload --repository-url https://upload.pypi.org/legacy/ --username __token__ dist/*
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Enter your PyPI API token at the hidden password prompt. The included distributions are already built. If you edit the source, rebuild in a clean dist directory with `python -m pip install build` and `python -m build` before uploading.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
cdex/__init__.py,sha256=cKj7V1T-29gLvoYRJx_FJD1VAPVtVw-JoDOiPr9JHWk,1841
|
|
2
|
+
cdex/programs/ex1/input.c,sha256=kH_06q0vHGPbIWJLkfKxUTePqbuQlfORWBN_7WDaVFg,153
|
|
3
|
+
cdex/programs/ex1/tokens.c,sha256=I8kzDegSYsQNhJfOzWyQiaPhi413p5Qa-RFrC0mL8fk,2058
|
|
4
|
+
cdex/programs/ex2/input.c,sha256=8A_IgHu2N0ARSbfI5Y0HfjBmYRRiDnIpUCQiNvT1okw,102
|
|
5
|
+
cdex/programs/ex2/lexer.l,sha256=-DCxOxyUtEBczwaM6TeoAl0zYRmeRlqR8muvIrRFM2E,499
|
|
6
|
+
cdex/programs/ex3/calc.l,sha256=kBoLGqjSI3CJvfm7GrltwlWjW-4vpN9147nySlTwEJE,321
|
|
7
|
+
cdex/programs/ex3/calc.y,sha256=NStbdH5RDkpVVNAIGn7HWQZZA4GtA0lPceKDmk3ois4,857
|
|
8
|
+
cdex/programs/ex4/tac.l,sha256=3deED2e3UhEMCY1YEUW8Mxzb1x3XXYT7GgREKE3_lVQ,338
|
|
9
|
+
cdex/programs/ex4/tac.y,sha256=p0NiGKk-jjljTc84sfg14TnqEjXOGtGkiIc2MDeHYzg,2578
|
|
10
|
+
cdex/programs/ex5/type.l,sha256=vtFKHKj6Qq3XVI2DLDF94cUKYB0Bsu_LxqDEbZsV7Us,350
|
|
11
|
+
cdex/programs/ex5/type.y,sha256=55S9FPI6d0u4HplwL_r8SaxoYEvSfl0FeUPqqwcbO3o,1854
|
|
12
|
+
cdex/programs/ex6/optimize.c,sha256=cW7Q_Sp-uAxHFZd-m676XW7xYT6faVjyIAuCOxECiRI,2460
|
|
13
|
+
cdex/programs/ex7/backend.c,sha256=dOZt-VE0HYWosmLLiTe5uNmq3eZq4Y3Q4-S0UwwC07s,1179
|
|
14
|
+
cdex-0.1.0.dist-info/METADATA,sha256=sMfQeNFI38rBRLqsn8CtPiX61zaFnY1o-mKPSWA0bSc,2084
|
|
15
|
+
cdex-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
16
|
+
cdex-0.1.0.dist-info/top_level.txt,sha256=saEONKs68Kzl79qLKaFTLeH4hZnmFmA_3GSjfChLX7c,5
|
|
17
|
+
cdex-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cdex
|