-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
93 lines (77 loc) · 2.23 KB
/
parse.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Maximum number of characters user can input */
#define BUFFER_SIZE 80
// Implement a lexer parser in this file that splits text into individual tokens.
// You may reuse any functions you write for your main shell.
// The point is that you get something small working first!
//
//
// Implement your function parse here
// Parse should accept 1 string as an argument.
// Think carefully about the return type
// --what would be a useful type to store a
// --collection of tokens?
/*
* The vect_t struct from Assignment 2 is used to help with storing data about the
* given commands. The main reason for using this kind of struct is to help with
* looping through the commands, as the size field serves as an indication of
* where the data array ends.
*/
typedef struct string_vect {
//string array storing user input
char **data;
//size field indicating number of strings currently in array
unsigned int size;
} vect_t;
/*
* Creates a new vector. The data array will be large enough to hold BUFFER_SIZE
* number of characters.
*/
vect_t *vect_new() {
vect_t *v = (vect_t *) malloc(sizeof(vect_t));
v->data = (char **) calloc(80, sizeof(char *));
v->size = 0;
return v;
}
/*
* Frees all data allocated to vector.
*/
void delete_vect(vect_t *v) {
free(v);
free(v->data);
}
/*
* Lexes a single line of input and prints out the individual token, one token
* per line. Stores token in vect_t struct.
*/
vect_t *parse(char* str) {
char **token_list;
char *char_list = strtok(str, " ");
int index = 0;
vect_t *v = vect_new();
// char_list will be null when there are no more token to process
while (char_list != NULL) {
v->data[index] = char_list;
index++;
v->size++;
char_list = strtok(NULL, " ");
}
// print strings in vector
index = 0;
while(v->size > index) {
printf("%s\n", v->data[index]);
index++;
}
return v;
}
/*
* Main method for program. Calls parse and frees vector used to store
* tokens while printing.
*/
int main(int argc, char** argv){
vect_t *tokens = parse(argv[1]);
delete_vect(tokens);
return 0;
}