-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.y
More file actions
153 lines (135 loc) · 3.26 KB
/
parser.y
File metadata and controls
153 lines (135 loc) · 3.26 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/ast.h"
void yyerror(const char *s);
int yylex(void);
Workflow *workflow = NULL;
Task *current_task = NULL;
char **depends_array = NULL;
int depends_count = 0;
void cleanup_parser() {
if (depends_array) {
for (int i = 0; i < depends_count; i++) {
free(depends_array[i]);
}
free(depends_array);
depends_array = NULL;
depends_count = 0;
}
}
%}
%union {
int num;
char* str;
struct {
char **array;
int count;
} str_list;
}
%token <str> STRING ID
%token <num> NUMBER
%token WORKFLOW NAME TASKS COMMAND DEPENDS_ON
%type <str> name_field command_field
%type <str_list> depends_on_field string_list
%%
program:
workflow_block
{ cleanup_parser(); }
;
workflow_block:
WORKFLOW ':' STRING
{
if (workflow) free_workflow(workflow);
workflow = create_workflow($3);
printf("Created workflow: %s\n", $3);
}
task_section
;
task_section:
TASKS ':' '[' task_list ']'
{
if (!workflow) {
fprintf(stderr, "Error: Workflow not initialized before task_section\n");
YYABORT;
}
}
;
task_list:
task_item
{ printf("Parsed one task item\n"); }
| task_list ',' task_item
{ printf("Parsed another task item\n"); }
;
task_item:
'{' task_body '}'
{ printf("Completed task item\n"); }
;
task_body:
name_field ',' command_field
{
if (!workflow) {
fprintf(stderr, "Error: Workflow not initialized in task_body\n");
free($1);
free($3);
YYABORT;
}
printf("Creating task: %s\n", $1);
current_task = create_task($1, $3);
add_task(workflow, current_task);
printf("Task added to workflow: %s\n", workflow->name);
}
| name_field ',' command_field ',' depends_on_field
{
if (!workflow) {
fprintf(stderr, "Error: Workflow not initialized in task_body\n");
free($1);
free($3);
for (int i = 0; i < $5.count; i++) {
free($5.array[i]);
}
free($5.array);
YYABORT;
}
printf("Creating task with dependencies: %s\n", $1);
current_task = create_task($1, $3);
current_task->depends_on = $5.array;
current_task->depends_count = $5.count;
add_task(workflow, current_task);
printf("Task added to workflow: %s\n", workflow->name);
}
;
name_field:
NAME ':' STRING { $$ = $3; }
;
command_field:
COMMAND ':' STRING { $$ = $3; }
;
depends_on_field:
DEPENDS_ON ':' '[' string_list ']'
{
$$ = $4;
}
;
string_list:
STRING
{
$$.array = malloc(sizeof(char *));
$$.array[0] = $1;
$$.count = 1;
}
| string_list ',' STRING
{
$1.array = realloc($1.array, sizeof(char *) * ($1.count + 1));
$1.array[$1.count] = $3;
$1.count++;
$$ = $1;
}
;
%%
void yyerror(const char *s) {
extern int yylineno;
fprintf(stderr, "Error at line %d: %s\n", yylineno, s);
cleanup_parser();
}