pcddd 1.0.1__tar.gz
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.
- pcddd-1.0.1/LICENSE +21 -0
- pcddd-1.0.1/PKG-INFO +41 -0
- pcddd-1.0.1/README.md +27 -0
- pcddd-1.0.1/pcd_codes/__init__.py +2 -0
- pcddd-1.0.1/pcd_codes/__main__.py +4 -0
- pcddd-1.0.1/pcd_codes/cli.py +55 -0
- pcddd-1.0.1/pcd_codes/programs/01_string_patterns.c +31 -0
- pcddd-1.0.1/pcd_codes/programs/02_nfa_to_dfa.c +90 -0
- pcddd-1.0.1/pcd_codes/programs/03_regex_to_nfa.c +80 -0
- pcddd-1.0.1/pcd_codes/programs/04_lexical_analyser_spaces_comments.c +24 -0
- pcddd-1.0.1/pcd_codes/programs/05_predictive_parser.c +115 -0
- pcddd-1.0.1/pcd_codes/programs/06_operator_lexical_analyser.c +78 -0
- pcddd-1.0.1/pcd_codes/programs/07_shift_reduce_parser.c +106 -0
- pcddd-1.0.1/pcd_codes/programs/08_lalr_bottom_up_parser.c +43 -0
- pcddd-1.0.1/pcd_codes/programs/09_slr1_parser.c +67 -0
- pcddd-1.0.1/pcd_codes/programs/10_three_address_code.c +61 -0
- pcddd-1.0.1/pcddd.egg-info/PKG-INFO +41 -0
- pcddd-1.0.1/pcddd.egg-info/SOURCES.txt +21 -0
- pcddd-1.0.1/pcddd.egg-info/dependency_links.txt +1 -0
- pcddd-1.0.1/pcddd.egg-info/entry_points.txt +2 -0
- pcddd-1.0.1/pcddd.egg-info/top_level.txt +1 -0
- pcddd-1.0.1/pyproject.toml +29 -0
- pcddd-1.0.1/setup.cfg +4 -0
pcddd-1.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 R Sam Gladson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pcddd-1.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pcddd
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: PCD C programs stored as text and printable from the command line
|
|
5
|
+
Author: R Sam Gladson
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# PCD Code Library
|
|
16
|
+
|
|
17
|
+
A small Python package containing 10 C programs from the PCD experiments, stored as text and printed from the command line.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install pcddd
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
pcd list
|
|
29
|
+
pcd 1
|
|
30
|
+
pcd 2
|
|
31
|
+
...
|
|
32
|
+
pcd 10
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The package prints the stored C source code; it does not execute the C programs.
|
|
36
|
+
|
|
37
|
+
You can save a program from CMD:
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
pcd 1 > program1.c
|
|
41
|
+
```
|
pcddd-1.0.1/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# PCD Code Library
|
|
2
|
+
|
|
3
|
+
A small Python package containing 10 C programs from the PCD experiments, stored as text and printed from the command line.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install pcddd
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
pcd list
|
|
15
|
+
pcd 1
|
|
16
|
+
pcd 2
|
|
17
|
+
...
|
|
18
|
+
pcd 10
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The package prints the stored C source code; it does not execute the C programs.
|
|
22
|
+
|
|
23
|
+
You can save a program from CMD:
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
pcd 1 > program1.c
|
|
27
|
+
```
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from importlib.resources import files
|
|
2
|
+
|
|
3
|
+
PROGRAMS = {
|
|
4
|
+
"1": "01_string_patterns.c",
|
|
5
|
+
"2": "02_nfa_to_dfa.c",
|
|
6
|
+
"3": "03_regex_to_nfa.c",
|
|
7
|
+
"4": "04_lexical_analyser_spaces_comments.c",
|
|
8
|
+
"5": "05_predictive_parser.c",
|
|
9
|
+
"6": "06_operator_lexical_analyser.c",
|
|
10
|
+
"7": "07_shift_reduce_parser.c",
|
|
11
|
+
"8": "08_lalr_bottom_up_parser.c",
|
|
12
|
+
"9": "09_slr1_parser.c",
|
|
13
|
+
"10": "10_three_address_code.c",
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
TITLES = {
|
|
17
|
+
"1": "Recognize string under 'a', 'a*b+', 'abb'",
|
|
18
|
+
"2": "Convert NFA to DFA",
|
|
19
|
+
"3": "Convert regular expression into NFA",
|
|
20
|
+
"4": "Lexical analyser to ignore redundant spaces, tabs, newlines and comments",
|
|
21
|
+
"5": "Construct a predictive parser",
|
|
22
|
+
"6": "Lexical analyser for operators",
|
|
23
|
+
"7": "Shift-reduce parsing",
|
|
24
|
+
"8": "Construct LALR bottom-up parser",
|
|
25
|
+
"9": "Implement SLR(1) Parsing algorithm",
|
|
26
|
+
"10": "Generate three address code using C",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def print_program(number: str) -> None:
|
|
31
|
+
if number not in PROGRAMS:
|
|
32
|
+
raise SystemExit(f"Program {number} not found. Use 'pcd list' to see available programs.")
|
|
33
|
+
path = files("pcd_codes").joinpath("programs", PROGRAMS[number])
|
|
34
|
+
print(path.read_text(encoding="utf-8"), end="")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def main() -> None:
|
|
38
|
+
import argparse
|
|
39
|
+
|
|
40
|
+
parser = argparse.ArgumentParser(
|
|
41
|
+
prog="pcd",
|
|
42
|
+
description="Print PCD C programs stored as source text."
|
|
43
|
+
)
|
|
44
|
+
parser.add_argument("command", nargs="?", help="program number (1-10) or 'list'")
|
|
45
|
+
args = parser.parse_args()
|
|
46
|
+
|
|
47
|
+
if args.command is None or args.command.lower() == "list":
|
|
48
|
+
print("Available PCD programs:\n")
|
|
49
|
+
for number, title in TITLES.items():
|
|
50
|
+
print(f"{number:>2}. {title}")
|
|
51
|
+
print("\nUsage: pcd <number>")
|
|
52
|
+
print("Example: pcd 1")
|
|
53
|
+
return
|
|
54
|
+
|
|
55
|
+
print_program(args.command)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#include<stdio.h>
|
|
2
|
+
#include<string.h>
|
|
3
|
+
int main(){
|
|
4
|
+
char str[100];
|
|
5
|
+
int i=0;
|
|
6
|
+
printf("Enter a String:");
|
|
7
|
+
scanf("%s",str);
|
|
8
|
+
|
|
9
|
+
if(strcmp(str,"a")==0){
|
|
10
|
+
printf("String matches pattern'a'");
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
if(strcmp(str,"abb")==0){
|
|
14
|
+
printf("String matches pattern 'abb'\n");
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
i=0;
|
|
18
|
+
while(str[i]=='a'){
|
|
19
|
+
i++;
|
|
20
|
+
}
|
|
21
|
+
int bcount = 0;
|
|
22
|
+
while(str[i]=='b'){
|
|
23
|
+
bcount++;
|
|
24
|
+
i++;
|
|
25
|
+
}
|
|
26
|
+
if(bcount > 0 && str[i]=='\0')
|
|
27
|
+
printf("String matches pattern 'a*b+'\n");
|
|
28
|
+
else
|
|
29
|
+
printf("String does not match any pattern \n");
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include<string.h>
|
|
3
|
+
#define MAX 10
|
|
4
|
+
int n;
|
|
5
|
+
int i , j ,sym;
|
|
6
|
+
int t[MAX][MAX][MAX];
|
|
7
|
+
int dfastates[MAX][MAX];
|
|
8
|
+
int dfacount=0;
|
|
9
|
+
int visited[MAX];
|
|
10
|
+
void adddfastate(int set[]){
|
|
11
|
+
for( i=0; i<dfacount;i++){
|
|
12
|
+
int same=1;
|
|
13
|
+
for(j=0;j<n;j++){
|
|
14
|
+
if(dfastates[i][j]!=set[j]){
|
|
15
|
+
same=0;
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if(same) return;
|
|
20
|
+
}
|
|
21
|
+
for (j=0;j<n;j++){
|
|
22
|
+
dfastates[dfacount][j]=set[j];
|
|
23
|
+
}
|
|
24
|
+
dfacount++;
|
|
25
|
+
}
|
|
26
|
+
void printset(int set[]){
|
|
27
|
+
printf("{");
|
|
28
|
+
for(i=0;i<n;i++){
|
|
29
|
+
if(set[i]) printf("q%d",i);
|
|
30
|
+
}
|
|
31
|
+
printf("}");
|
|
32
|
+
}
|
|
33
|
+
void nfatodfa(int start){
|
|
34
|
+
int q[MAX][MAX];
|
|
35
|
+
int front=0,rear=0;
|
|
36
|
+
int startset[MAX]={0};
|
|
37
|
+
startset[start]=1;
|
|
38
|
+
memcpy(q[rear++],startset,sizeof(startset));
|
|
39
|
+
adddfastate(startset);
|
|
40
|
+
printf("\nDFA states:\n");
|
|
41
|
+
while(front<rear){
|
|
42
|
+
int current[MAX];
|
|
43
|
+
memcpy(current,q[front++],sizeof(current));
|
|
44
|
+
printf("\n State");
|
|
45
|
+
printset(current);
|
|
46
|
+
printf("->");
|
|
47
|
+
for( sym=0;sym<2;sym++){
|
|
48
|
+
int newset[MAX]={0};
|
|
49
|
+
for( i=0;i<n;i++){
|
|
50
|
+
if(current[i]){
|
|
51
|
+
for( j=0; j<n;j++){
|
|
52
|
+
if(t[i][sym][j]){
|
|
53
|
+
newset[j]=1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
printf("\n on input %c->", sym==0?'a':'b');
|
|
59
|
+
printset(newset);
|
|
60
|
+
adddfastate(newset);
|
|
61
|
+
int exists=0;
|
|
62
|
+
for( i=0;i<rear;i++){
|
|
63
|
+
if(memcmp(q[i],newset,sizeof(newset))==0){
|
|
64
|
+
exists=1;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if(!exists){
|
|
69
|
+
memcpy(q[rear++],newset,sizeof(newset));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
int main(){
|
|
75
|
+
int start;
|
|
76
|
+
printf("Enter number of nfa states:");
|
|
77
|
+
scanf("%d",&n);
|
|
78
|
+
printf("\nEnter nfa transition table(0/1) for a and b");
|
|
79
|
+
for( i=0; i<n; i++){
|
|
80
|
+
for( sym=0; sym<2;sym++){
|
|
81
|
+
for(j=0;j<n;j++){
|
|
82
|
+
scanf("%d",&t[i][sym][j]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
printf("\nEnter start state:");
|
|
87
|
+
scanf("%d",&start);
|
|
88
|
+
nfatodfa(start);
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
include<stdio.h>
|
|
2
|
+
#include<string.h>
|
|
3
|
+
#define MAX 100
|
|
4
|
+
int state=0;
|
|
5
|
+
typedef struct{
|
|
6
|
+
int start;
|
|
7
|
+
int end;
|
|
8
|
+
}
|
|
9
|
+
NFA;
|
|
10
|
+
void printTrans(int s,char c,int d){
|
|
11
|
+
if(c=='e')
|
|
12
|
+
printf("q%d--e-->q%d\n",s,d);
|
|
13
|
+
else
|
|
14
|
+
printf("q%d--%c-->q%d\n",s,c,d);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
NFA createBasic(char c){
|
|
18
|
+
NFA n;
|
|
19
|
+
n.start=state++;
|
|
20
|
+
n.end=state++;
|
|
21
|
+
printTrans(n.start,c,n.end);
|
|
22
|
+
return n;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
NFA concatenate(NFA a,NFA b){
|
|
26
|
+
printTrans(a.end,'e',b.start);
|
|
27
|
+
NFA n;
|
|
28
|
+
n.start=a.start;
|
|
29
|
+
n.end=b.end;
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
NFA unionNFA(NFA a,NFA b){
|
|
34
|
+
NFA n;
|
|
35
|
+
n.start=state++;
|
|
36
|
+
n.end=state++;
|
|
37
|
+
printTrans(n.start,'e',a.start);
|
|
38
|
+
printTrans(n.start,'e',b.start);
|
|
39
|
+
printTrans(a.end,'e',n.end);
|
|
40
|
+
printTrans(b.end,'e',n.end);
|
|
41
|
+
return n;
|
|
42
|
+
}NFA kleene(NFA a){
|
|
43
|
+
NFA n;
|
|
44
|
+
n.start=state++;
|
|
45
|
+
n.end=state++;
|
|
46
|
+
printTrans(n.start,'e',a.start);
|
|
47
|
+
printTrans(n.start,'e',n.end);
|
|
48
|
+
printTrans(a.end,'e',a.start);
|
|
49
|
+
printTrans(a.end,'e',n.end);
|
|
50
|
+
return n;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
int main(){
|
|
54
|
+
char regex[MAX];
|
|
55
|
+
NFA stack[MAX];
|
|
56
|
+
int top=-1;
|
|
57
|
+
printf("Enter simple regex(example:a/b*):");
|
|
58
|
+
scanf("%s",regex);
|
|
59
|
+
int i;
|
|
60
|
+
for(i=0;i<strlen(regex);i++){
|
|
61
|
+
char c=regex[i];
|
|
62
|
+
if(c=='a'||c=='b'){
|
|
63
|
+
stack[++top]=createBasic(c);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
else if(c=='*'){
|
|
67
|
+
stack[top]=kleene(stack[top]);
|
|
68
|
+
}
|
|
69
|
+
else if(c=='|'){
|
|
70
|
+
NFA b=stack[top--];
|
|
71
|
+
NFA a=stack[top--];
|
|
72
|
+
stack[++top]=unionNFA(a,b);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
printf("\n Start state:q%d\n",stack[top].start);
|
|
78
|
+
printf("Final state:q%d\n",stack[top].end);
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
int main()
|
|
3
|
+
{
|
|
4
|
+
FILE *fp;
|
|
5
|
+
char ch, next;
|
|
6
|
+
fp = fopen("input.txt", "r");
|
|
7
|
+
if (fp == NULL){
|
|
8
|
+
printf("Cannot open file\n");
|
|
9
|
+
return 1;
|
|
10
|
+
}
|
|
11
|
+
printf("Source code after removing spaces, tabs, newlines & comments:\n");
|
|
12
|
+
while ((ch = fgetc(fp)) != EOF) {
|
|
13
|
+
if (ch == ' ' || ch == '\t' || ch == '\n')
|
|
14
|
+
continue;
|
|
15
|
+
if (ch == '\\'){
|
|
16
|
+
next = fgetc(fp);
|
|
17
|
+
if (next == '\n')
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
putchar(ch);
|
|
21
|
+
}
|
|
22
|
+
fclose(fp);
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
|
|
5
|
+
char stack[100];
|
|
6
|
+
int top = -1;
|
|
7
|
+
|
|
8
|
+
void push(char c) {
|
|
9
|
+
stack[++top] = c;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
char pop() {
|
|
13
|
+
if (top == -1) return '\0';
|
|
14
|
+
return stack[top--];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
char peek() {
|
|
18
|
+
if (top == -1) return '\0';
|
|
19
|
+
return stack[top];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
void printStack() {
|
|
23
|
+
for (int i = 0; i <= top; i++) {
|
|
24
|
+
printf("%c", stack[i]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
int getRowIndex(char c) {
|
|
29
|
+
if (c == 'S') return 0;
|
|
30
|
+
if (c == 'B') return 1;
|
|
31
|
+
return -1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
int getColIndex(char c) {
|
|
35
|
+
if (c == 'a') return 0;
|
|
36
|
+
if (c == 'b') return 1;
|
|
37
|
+
if (c == '$') return 2;
|
|
38
|
+
return -1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
int main() {
|
|
42
|
+
|
|
43
|
+
char table[2][3][10] = {
|
|
44
|
+
{ "aBa", "", "" },
|
|
45
|
+
{ "", "bB", "#" }
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
char input[100];
|
|
49
|
+
printf("Enter the input string (end with $): ");
|
|
50
|
+
scanf("%s", input);
|
|
51
|
+
|
|
52
|
+
push('$');
|
|
53
|
+
push('S');
|
|
54
|
+
|
|
55
|
+
int ip = 0;
|
|
56
|
+
printf("\n%-20s %-20s %-20s\n", "Stack", "Input Buffer", "Action");
|
|
57
|
+
printf("------------------------------------------------------------\n");
|
|
58
|
+
|
|
59
|
+
while (peek() != '$') {
|
|
60
|
+
|
|
61
|
+
printStack();
|
|
62
|
+
printf("\t\t\t");
|
|
63
|
+
printf("%s", input + ip);
|
|
64
|
+
printf("\t\t\t");
|
|
65
|
+
|
|
66
|
+
char X = peek();
|
|
67
|
+
char a = input[ip];
|
|
68
|
+
|
|
69
|
+
if (X == a) {
|
|
70
|
+
printf("Match %c\n", a);
|
|
71
|
+
pop();
|
|
72
|
+
ip++;
|
|
73
|
+
}
|
|
74
|
+
else if (X >= 'a' && X <= 'z') {
|
|
75
|
+
|
|
76
|
+
printf("\nSyntax Error: Terminal mismatch! Expected %c but found %c\n", X, a);
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
|
|
81
|
+
int r = getRowIndex(X);
|
|
82
|
+
int c = getColIndex(a);
|
|
83
|
+
|
|
84
|
+
if (r == -1 || c == -1 || strlen(table[r][c]) == 0) {
|
|
85
|
+
printf("\nSyntax Error: No parsing table entry for [%c, %c]\n", X, a);
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
char *prod = table[r][c];
|
|
90
|
+
printf("%c -> %s\n", X, prod);
|
|
91
|
+
pop();
|
|
92
|
+
|
|
93
|
+
if (strcmp(prod, "#") != 0) {
|
|
94
|
+
for (int i = strlen(prod) - 1; i >= 0; i--) {
|
|
95
|
+
push(prod[i]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
printStack();
|
|
102
|
+
printf("\t\t\t");
|
|
103
|
+
printf("%s", input + ip);
|
|
104
|
+
printf("\t\t\t");
|
|
105
|
+
|
|
106
|
+
if (peek() == '$' && input[ip] == '$') {
|
|
107
|
+
printf("Success\n");
|
|
108
|
+
printf("\nResult: String successfully Parsed & Accepted!\n");
|
|
109
|
+
} else {
|
|
110
|
+
printf("Error\n");
|
|
111
|
+
printf("\nResult: Syntax Error. Input rejected.\n");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdbool.h>
|
|
4
|
+
// Function to validate and classify the operator
|
|
5
|
+
void validateOperator(const char *op) {
|
|
6
|
+
// 1-character operators
|
|
7
|
+
if (strlen(op) == 1) {
|
|
8
|
+
switch (op[0]) {
|
|
9
|
+
case '+': case '-': case '*': case '/': case '%':
|
|
10
|
+
printf("'%s' is a Valid Arithmetic Operator.\n", op);
|
|
11
|
+
return;
|
|
12
|
+
case '<': case '>':
|
|
13
|
+
printf("'%s' is a Valid Relational Operator.\n", op);
|
|
14
|
+
return;
|
|
15
|
+
case '=':
|
|
16
|
+
printf("'%s' is a Valid Assignment Operator.\n", op);
|
|
17
|
+
return;
|
|
18
|
+
case '!':
|
|
19
|
+
printf("'%s' is a Valid Logical NOT Operator.\n", op);
|
|
20
|
+
return;
|
|
21
|
+
case '&': case '|': case '^': case '~':
|
|
22
|
+
printf("'%s' is a Valid Bitwise Operator.\n", op);
|
|
23
|
+
return;
|
|
24
|
+
case '?': case ':':
|
|
25
|
+
printf("'%s' is a Valid Conditional/Ternary Operator.\n", op);
|
|
26
|
+
return;
|
|
27
|
+
default:
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// 2-character operators
|
|
32
|
+
if (strlen(op) == 2) {
|
|
33
|
+
if (strcmp(op, "++") == 0 || strcmp(op, "--") == 0) {
|
|
34
|
+
printf("'%s' is a Valid Increment/Decrement Operator.\n", op);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (strcmp(op, "==") == 0 || strcmp(op, "!=") == 0 ||
|
|
38
|
+
strcmp(op, "<=") == 0 || strcmp(op, ">=") == 0) {
|
|
39
|
+
printf("'%s' is a Valid Relational Operator.\n", op);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (strcmp(op, "&&") == 0 || strcmp(op, "||") == 0) {
|
|
43
|
+
printf("'%s' is a Valid Logical Operator.\n", op);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (strcmp(op, "+=") == 0 || strcmp(op, "-=") == 0 ||
|
|
47
|
+
strcmp(op, "*=") == 0 || strcmp(op, "/=") == 0 || strcmp(op, "%=") == 0) {
|
|
48
|
+
printf("'%s' is a Valid Compound Assignment Operator.\n", op);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (strcmp(op, "<<") == 0 || strcmp(op, ">>") == 0) {
|
|
52
|
+
printf("'%s' is a Valid Bitwise Shift Operator.\n", op);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// 3-character operators
|
|
57
|
+
if (strlen(op) == 3) {
|
|
58
|
+
if (strcmp(op, "<<=") == 0 || strcmp(op, ">>=") == 0) {
|
|
59
|
+
printf("'%s' is a Valid Compound Bitwise Shift Operator.\n", op);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// If no match is found
|
|
64
|
+
printf("'%s' is an INVALID Operator.\n", op);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
int main() {
|
|
68
|
+
char input[10];
|
|
69
|
+
|
|
70
|
+
printf("--- Lexical Analyzer: Operator Validator ---\n");
|
|
71
|
+
printf("Enter a symbol to validate: ");
|
|
72
|
+
|
|
73
|
+
// Read string token, ignoring trailing newline characters
|
|
74
|
+
if (scanf("%9s", input) == 1) {
|
|
75
|
+
validateOperator(input);
|
|
76
|
+
}
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
char stack[100], input[100];
|
|
4
|
+
int top = -1, ip = 0;
|
|
5
|
+
void displayStack()
|
|
6
|
+
{
|
|
7
|
+
int i;
|
|
8
|
+
for (i = 0; i <= top; i++)
|
|
9
|
+
printf("%c", stack[i]);
|
|
10
|
+
}
|
|
11
|
+
void reduce()
|
|
12
|
+
{
|
|
13
|
+
/* E -> id */
|
|
14
|
+
if (top >= 1 &&
|
|
15
|
+
stack[top - 1] == 'i' &&
|
|
16
|
+
stack[top] == 'd')
|
|
17
|
+
{
|
|
18
|
+
stack[top - 1] = 'E';
|
|
19
|
+
top--;
|
|
20
|
+
printf("Reduce: E -> id\n");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* E -> (E) */
|
|
25
|
+
if (top >= 2 &&
|
|
26
|
+
stack[top - 2] == '(' &&
|
|
27
|
+
stack[top - 1] == 'E' &&
|
|
28
|
+
stack[top] == ')')
|
|
29
|
+
{
|
|
30
|
+
top -= 2;
|
|
31
|
+
stack[top] = 'E';
|
|
32
|
+
printf("Reduce: E -> (E)\n");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
/* E -> E+E */
|
|
36
|
+
if (top >= 2 &&
|
|
37
|
+
stack[top - 2] == 'E' &&
|
|
38
|
+
stack[top - 1] == '+' &&
|
|
39
|
+
stack[top] == 'E')
|
|
40
|
+
{
|
|
41
|
+
top -= 2;
|
|
42
|
+
stack[top] = 'E';
|
|
43
|
+
printf("Reduce: E -> E+E\n");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
/* E -> E*E */
|
|
47
|
+
if (top >= 2 &&
|
|
48
|
+
stack[top - 2] == 'E' &&
|
|
49
|
+
stack[top - 1] == '*' &&
|
|
50
|
+
stack[top] == 'E')
|
|
51
|
+
{
|
|
52
|
+
top -= 2;
|
|
53
|
+
stack[top] = 'E';
|
|
54
|
+
printf("Reduce: E -> E*E\n");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
int main()
|
|
59
|
+
{
|
|
60
|
+
printf("SHIFT-REDUCE PARSER\n");
|
|
61
|
+
printf("--------------------\n");
|
|
62
|
+
|
|
63
|
+
printf("Grammar:\n");
|
|
64
|
+
printf("E -> E+E\n");
|
|
65
|
+
printf("E -> E*E\n");
|
|
66
|
+
printf("E -> (E)\n");
|
|
67
|
+
printf("E -> id\n\n");
|
|
68
|
+
|
|
69
|
+
printf("Enter input string (use id for identifier): ");
|
|
70
|
+
scanf("%s", input);
|
|
71
|
+
|
|
72
|
+
printf("\n%-15s %-15s %-20s\n",
|
|
73
|
+
"Stack", "Input", "Action");
|
|
74
|
+
printf("-----------------------------------------------\n");
|
|
75
|
+
|
|
76
|
+
while (input[ip] != '\0')
|
|
77
|
+
{
|
|
78
|
+
/* Shift */
|
|
79
|
+
stack[++top] = input[ip++];
|
|
80
|
+
|
|
81
|
+
printf("%-15s %-15s ",
|
|
82
|
+
stack, input + ip);
|
|
83
|
+
|
|
84
|
+
printf("Shift %c\n", stack[top]);
|
|
85
|
+
|
|
86
|
+
/* Try reduction */
|
|
87
|
+
reduce();
|
|
88
|
+
}
|
|
89
|
+
/* Continue reducing until no reduction is possible */
|
|
90
|
+
while (1)
|
|
91
|
+
{
|
|
92
|
+
int oldTop = top;
|
|
93
|
+
reduce();
|
|
94
|
+
if (oldTop == top)
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
printf("\nFinal Stack: ");
|
|
98
|
+
displayStack();
|
|
99
|
+
|
|
100
|
+
if (top == 0 && stack[0] == 'E')
|
|
101
|
+
printf("\n\nInput string ACCEPTED.\n");
|
|
102
|
+
else
|
|
103
|
+
printf("\n\nInput string REJECTED.\n");
|
|
104
|
+
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
char stack[50];
|
|
4
|
+
int top = -1;
|
|
5
|
+
void push(char c) {
|
|
6
|
+
stack[++top] = c;
|
|
7
|
+
}
|
|
8
|
+
void pop(int n) {
|
|
9
|
+
top -= n;
|
|
10
|
+
}
|
|
11
|
+
int main() {
|
|
12
|
+
char input[50];
|
|
13
|
+
int i = 0;
|
|
14
|
+
printf("Enter string (e.g., i+i*i): ");
|
|
15
|
+
scanf("%s", input);
|
|
16
|
+
push('$');
|
|
17
|
+
while (input[i] != '\0') {
|
|
18
|
+
if (input[i] == 'i') {
|
|
19
|
+
push('F');
|
|
20
|
+
printf("Shift i, Reduce F->i\n");
|
|
21
|
+
stack[top] = 'T';
|
|
22
|
+
printf("Reduce T->F\n");
|
|
23
|
+
stack[top] = 'E';
|
|
24
|
+
printf("Reduce E->T\n");
|
|
25
|
+
}
|
|
26
|
+
else if (input[i] == '+' || input[i] == '*') {
|
|
27
|
+
push(input[i]);
|
|
28
|
+
printf("Shift %c\n", input[i]);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
printf("Rejected\n");
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (top >= 2 && stack[top] == 'E') {
|
|
38
|
+
printf("Accepted by LALR Bottom-Up Parser\n");
|
|
39
|
+
} else {
|
|
40
|
+
printf("Rejected\n");
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
char input[20];
|
|
5
|
+
int ip = 0;
|
|
6
|
+
int main() {
|
|
7
|
+
int stateStack[20];
|
|
8
|
+
int top = 0;
|
|
9
|
+
stateStack[top] = 0;
|
|
10
|
+
printf("Enter input string: ");
|
|
11
|
+
scanf("%s", input);
|
|
12
|
+
strcat(input, "$");
|
|
13
|
+
while (1) {
|
|
14
|
+
int state = stateStack[top];
|
|
15
|
+
char symbol = input[ip];
|
|
16
|
+
switch (state) {
|
|
17
|
+
case 0:
|
|
18
|
+
if (symbol == 'c') {
|
|
19
|
+
stateStack[++top] = 3;
|
|
20
|
+
ip++;
|
|
21
|
+
}
|
|
22
|
+
else if (symbol == 'd') {
|
|
23
|
+
stateStack[++top] = 4;
|
|
24
|
+
ip++;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
printf("Rejected\n");
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
case 1:
|
|
32
|
+
if (symbol == '$') {
|
|
33
|
+
printf("Accepted\n");
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
case 2:
|
|
38
|
+
if (symbol == 'c') {
|
|
39
|
+
stateStack[++top] = 3;
|
|
40
|
+
ip++;
|
|
41
|
+
}
|
|
42
|
+
else if (symbol == 'd') {
|
|
43
|
+
stateStack[++top] = 4;
|
|
44
|
+
ip++;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
printf("Rejected\n");
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
case 4: /* Reduce C → d */
|
|
52
|
+
top--;
|
|
53
|
+
state = stateStack[top];
|
|
54
|
+
if (state == 0)
|
|
55
|
+
stateStack[++top] = 2;
|
|
56
|
+
else if (state == 2)
|
|
57
|
+
stateStack[++top] = 5;
|
|
58
|
+
break;
|
|
59
|
+
case 5: /* Reduce S → CC */
|
|
60
|
+
printf("Accepted\n");
|
|
61
|
+
return 0;
|
|
62
|
+
default:
|
|
63
|
+
printf("Rejected\n");
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <ctype.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
char stack[50];
|
|
5
|
+
char input[50];
|
|
6
|
+
int tempCount = 1;
|
|
7
|
+
void printTAC(char op, char a, char b) {
|
|
8
|
+
printf("t%d = %c %c %c\n", tempCount, a, op, b);
|
|
9
|
+
tempCount++;
|
|
10
|
+
}
|
|
11
|
+
int precedence(char c) {
|
|
12
|
+
if (c == '+' || c == '-') return 1;
|
|
13
|
+
if (c == '*' || c == '/') return 2;
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
int main() {
|
|
17
|
+
char opStack[50];
|
|
18
|
+
char valStack[50];
|
|
19
|
+
int topOp = -1, topVal = -1;
|
|
20
|
+
printf("Enter expression: ");
|
|
21
|
+
scanf("%s", input);
|
|
22
|
+
for (int i = 0; input[i] != '\0'; i++) {
|
|
23
|
+
char ch = input[i];
|
|
24
|
+
if (isalnum(ch)) {
|
|
25
|
+
valStack[++topVal] = ch;
|
|
26
|
+
}
|
|
27
|
+
else if (ch == '(') {
|
|
28
|
+
opStack[++topOp] = ch;
|
|
29
|
+
}
|
|
30
|
+
else if (ch == ')') {
|
|
31
|
+
while (topOp != -1 && opStack[topOp] != '(') {
|
|
32
|
+
char op = opStack[topOp--];
|
|
33
|
+
char b = valStack[topVal--];
|
|
34
|
+
char a = valStack[topVal--];
|
|
35
|
+
printTAC(op, a, b);
|
|
36
|
+
valStack[++topVal] = 't' + tempCount - 1;
|
|
37
|
+
}
|
|
38
|
+
topOp--; // remove '('
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
while (topOp != -1 &&
|
|
42
|
+
precedence(opStack[topOp]) >= precedence(ch)) {
|
|
43
|
+
char op = opStack[topOp--];
|
|
44
|
+
char b = valStack[topVal--];
|
|
45
|
+
char a = valStack[topVal--];
|
|
46
|
+
printTAC(op, a, b);
|
|
47
|
+
valStack[++topVal] = 't' + tempCount - 1;
|
|
48
|
+
}
|
|
49
|
+
opStack[++topOp] = ch;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
while (topOp != -1) {
|
|
53
|
+
char op = opStack[topOp--];
|
|
54
|
+
char b = valStack[topVal--];
|
|
55
|
+
char a = valStack[topVal--];
|
|
56
|
+
printTAC(op, a, b);
|
|
57
|
+
valStack[++topVal] = 't' + tempCount - 1;
|
|
58
|
+
}
|
|
59
|
+
printf("Final result stored in t%d\n", tempCount - 1);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pcddd
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: PCD C programs stored as text and printable from the command line
|
|
5
|
+
Author: R Sam Gladson
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# PCD Code Library
|
|
16
|
+
|
|
17
|
+
A small Python package containing 10 C programs from the PCD experiments, stored as text and printed from the command line.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install pcddd
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
pcd list
|
|
29
|
+
pcd 1
|
|
30
|
+
pcd 2
|
|
31
|
+
...
|
|
32
|
+
pcd 10
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The package prints the stored C source code; it does not execute the C programs.
|
|
36
|
+
|
|
37
|
+
You can save a program from CMD:
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
pcd 1 > program1.c
|
|
41
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
pcd_codes/__init__.py
|
|
5
|
+
pcd_codes/__main__.py
|
|
6
|
+
pcd_codes/cli.py
|
|
7
|
+
pcd_codes/programs/01_string_patterns.c
|
|
8
|
+
pcd_codes/programs/02_nfa_to_dfa.c
|
|
9
|
+
pcd_codes/programs/03_regex_to_nfa.c
|
|
10
|
+
pcd_codes/programs/04_lexical_analyser_spaces_comments.c
|
|
11
|
+
pcd_codes/programs/05_predictive_parser.c
|
|
12
|
+
pcd_codes/programs/06_operator_lexical_analyser.c
|
|
13
|
+
pcd_codes/programs/07_shift_reduce_parser.c
|
|
14
|
+
pcd_codes/programs/08_lalr_bottom_up_parser.c
|
|
15
|
+
pcd_codes/programs/09_slr1_parser.c
|
|
16
|
+
pcd_codes/programs/10_three_address_code.c
|
|
17
|
+
pcddd.egg-info/PKG-INFO
|
|
18
|
+
pcddd.egg-info/SOURCES.txt
|
|
19
|
+
pcddd.egg-info/dependency_links.txt
|
|
20
|
+
pcddd.egg-info/entry_points.txt
|
|
21
|
+
pcddd.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pcd_codes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pcddd"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
description = "PCD C programs stored as text and printable from the command line"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "R Sam Gladson" }
|
|
13
|
+
]
|
|
14
|
+
license = "MIT"
|
|
15
|
+
license-files = ["LICENSE"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
pcd = "pcd_codes.cli:main"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
include = ["pcd_codes*"]
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.package-data]
|
|
29
|
+
pcd_codes = ["programs/*.c"]
|
pcddd-1.0.1/setup.cfg
ADDED