-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.y
executable file
·88 lines (69 loc) · 1.56 KB
/
grammar.y
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
%{
#include "y.tab.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SPELLTOKEN 258
%}
%union {
char* str;
}
%token PRIORITOKEN PATRONUMTOKEN SPELLTOKEN GRIMOIRETOKEN FINITETOKEN
%token <str> STRING
%type <str> expr
%%
command: PRIORITOKEN { handlePrioriCommand(); } |
PATRONUMTOKEN { handlePatronumCommand(); } |
SPELLTOKEN expr { handleSpellCommand($2); } |
GRIMOIRETOKEN { handleGrimoireCommand(); } |
FINITETOKEN { handleFiniteCommand(); }
;
expr: STRING { $$ = $1; }
;
%%
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern int yylex(void);
extern int yyparse(void);
void yyerror(const char* msg);
int yywrap(void);
void handlePrioriCommand() {
printf("Priori spell call.\n");
}
void handlePatronumCommand() {
printf("Expecto Patronum!\n");
}
void handleSpellCommand(const char* spell) {
printf("%s\n", spell);
}
void handleGrimoireCommand() {
printf("v0.0.1\n");
}
void handleFiniteCommand() {
printf("[Voldemort] : Try another spell or lose.\n");
exit(0);
}
int main() {
char input[100];
printf("Accio v0.0.1 (main, beta version) \n");
while (1) {
printf(">>> ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
if (strncmp(input, ">>", 2) == 0) {
yy_scan_string(input + 2);
} else {
yy_scan_string(input);
}
yyparse();
}
return 0;
}
void yyerror(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(1);
}
int yywrap() {
return 1;
}