-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
127 lines (105 loc) · 3.18 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "ada2asa.h"
#include "pcode.h"
#include "rpn2pci.h"
#include "btree2rpn.h"
#include "pushdown.h"
#define STACK_SIZE 50
#define TREE_SIZE 64
typedef char * string;
void reverse (char *array, int start, int end);
void mirror(char* array);
/*
*Author: Gustavo B. Fragoso
*/
int main (int argc, char **argv){
// Vector with only integers, used as a datastore
int stack[STACK_SIZE] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
// Result of Pushdown Automata
int productions[50], i;
// Binary Tree
char btree[TREE_SIZE] = {""};
// Reverse Polish Notation
string rpn;
FILE *input;
for (i = 0;i<TREE_SIZE;i++){
btree[i] = '_';
productions[i] = -1;
}
// Initial symbol
btree[0] = 'E';
btree[TREE_SIZE] = '\0';
if (argc == 2){
// Expr
string expr = argv[1];
int size = strlen(expr);
if(strlen(expr)<50){
FILE *output = fopen("output.txt", "w");
if(output!=NULL){
fprintf(output,"Run: %s %s \n", argv[0], argv[1]);
printf("Step 1. Catch regular expression ... done!\n");
// Execution of Pushdown Automata results in set of productions
printf("Step 2. Running pushdown automata ... ");
pushDownAutomata(expr,productions,output);
printf("done!\n");
// Apply productions to build ASA
printf("Step 3. Apply productions to build an btree ... ");
ada_to_asa(btree,productions);
fprintf(output,"\nBtree: %s", btree);
printf("done!\n");
printf("Step 4. Mirroring the btree ... ");
mirror(btree);
fprintf(output,"\nMirrored btree: %s", btree);
printf("done!\n");
// B-tree -> Reverse Polish Notation
printf("Step 5. Running postorder trasversal on btree ... ");
rpn = (string) malloc(50*sizeof(string));
postOrder(btree,size, 0, rpn);
fprintf(output,"\nReverse polish notation: %s\n", rpn);
printf("done!\n");
// RPN -> PCI
printf("Step 6. Convert RPN into pcode instructions ... ");
Tinstruction * instructions = toInstruction(rpn, size);
printf("done!\n");
// Array of instructions
printf("Step 7. Running pcode machine ... ");
fprintf(output,"\nOutput:\n");
fprintf(output,"%-10s %-7s %-7s %-7s %-7s %-15s %s\n","Inst","Level","Arg","Top","Counter","Base","Stack");
pcode(instructions, stack, output);
printf("done!\n");
free(rpn);
// Closing File
fclose(output);
printf("Output file generated.\n");
}else{
printf("\nError when opening the output file.");
}
}else{
printf("\nToo long expression");
}
}else{
printf("Insufficient params, running like this %s \"expr\"\n", argv[0]);
}
return 0;
}
void reverse (char *array, int start, int end){
int i , j = 0;
char aux[50];
for(i = end-1; i >= start; i--){
aux[j++] = array[i];
}
j = 0;
for (i = start; i < end; i++){
array[i] = aux[j++];
}
}
void mirror(char* array){
int i = 1, c = 2;
while(c < TREE_SIZE/2){
reverse(array, i , i + c);
i += c;
c *= 2;
}
}