-
Notifications
You must be signed in to change notification settings - Fork 0
/
readGrammar.c
123 lines (107 loc) · 2.29 KB
/
readGrammar.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
/*
* Group No. - 48
* Members:
* 2018A7PS0181P, Bikash Jena
* 2018A7PS0276P, Jatin Jain
* 2018A7PS0131P, Utkarsh Dwivedi
*/
/*
* provides readGrammar("grammar.txt",grammar* G)
* that reads grammar from grammar.txt,
* populates array of linked-list grammar G
*/
#include "structures.h"
bool isterminal(char[]);
bool isnonterminal(char[]);
bool iskeyword(char[]);
void insertGrammarToken(char grammar_token[], Grammar_Node *node);
int num_of_keywords=0;
Grammar readGrammar(char *filename, Grammar grammar)
{
FILE *file = fopen(filename, "r");
int counter = 0;
while (!feof(file))
{
grammar.rules[counter].num_of_nodes = 0;
char buffer[MAX_RULE_LEN], *grammar_token;
fscanf(file, "%[^\n]\n", buffer);
if (strcmp(buffer, "\n")==0)
{
continue;
}
grammar_token = strtok(buffer, " ");
insertGrammarToken(grammar_token, &grammar.rules[counter]);
grammar_token = strtok(NULL, " ");
Grammar_Node *temp = &grammar.rules[counter];
while (grammar_token != NULL)
{
grammar_token = strtok(NULL, " ");
if (grammar_token == NULL)
{
break;
}
temp->next_node = (Grammar_Node *)malloc(sizeof(Grammar_Node));
insertGrammarToken(grammar_token, temp->next_node);
grammar.rules[counter].num_of_nodes++;
temp->next_node->prev_node = temp;
temp = temp->next_node;
}
counter++;
}
grammar.num_of_rules = counter;
fclose(file);
return grammar;
}
void insertGrammarToken(char *grammar_token, Grammar_Node *node)
{
if (isterminal(grammar_token))
{
grammar_token[strlen(grammar_token) - 1] = '\0';
grammar_token++;
strcpy(node->name, grammar_token);
}
else if (isnonterminal(grammar_token))
{
grammar_token[strlen(grammar_token) - 1] = '\0';
grammar_token++;
strcpy(node->name, grammar_token);
}
else if (iskeyword(grammar_token))
{
strcpy(node->name, "keyword");
}
strcpy(node->value, grammar_token);
}
bool isterminal(char a[])
{
if (a[0] == '<')
{
return !(!(isupper(a[1])));
}
return false;
}
bool isnonterminal(char a[])
{
if (a[0] == '<')
{
return !(isupper((int)a[1]));
}
return false;
}
bool iskeyword(char a[])
{
if (a[0] != '<')
{
for (size_t i = 0; i < num_of_keywords; i++)
{
if (strcmp(keyword[i], a) == 0)
{
return true;
}
}
strcpy(keyword[num_of_keywords++], a);
return true;
}
else
return false;
}