-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadGrammar.c
67 lines (55 loc) · 1.65 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
/* Group 3
Ashrya Agrawal 2018A7PS0210P
Kalit Naresh Inani 2018A7PS0207P
Prajwal Gupta 2018A7PS0231P */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ARROW "=>"
#include "readGrammar.h"
// This function populates the llist by reading the grammer text file
void readGrammar(char *fileName, Node *llist)
{
FILE *fp = fopen(fileName, "r");
// Exit if file could not be opened
if (fp == NULL)
{
printf("Could not read %s\n", fileName);
exit(0);
}
int line = 0;
char eachLine[1000];
// Read the file line by line
while (fscanf(fp, " %[^\n]", eachLine) != EOF)
{
char *delim = " ";
char *token;
// Extract the leftmost non-terminal of the rule
token = strtok(eachLine, delim);
llist[line].element = (char *)malloc(sizeof(char) * (strlen(token) + 1));
strcpy(llist[line].element, token);
llist[line].nxt = NULL;
Node *curr = &llist[line];
token = strtok(NULL, delim);
int ruleSize = 0;
while (token != NULL)
{
if (strcmp(ARROW, token) != 0)
{
Node *next = (Node *)malloc(sizeof(Node));
next->element = (char *)malloc(sizeof(char) * (strlen(token) + 1));
strcpy(next->element, token);
next->ruleSize = 0;
next->nxt = NULL;
curr->nxt = next;
curr = next;
ruleSize++;
}
token = strtok(NULL, delim);
}
curr = &llist[line];
curr->ruleSize = ruleSize;
line++;
}
fclose(fp);
}