-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeParse.y
108 lines (93 loc) · 2.07 KB
/
SeParse.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "SeParse.h"
int yylex(void);
void (*callbackProc)();
%}
%token <sval> WORD
%union {
char *sval;
}
%start Script
%%
Script : Script FunCall
| Empty
;
FunCall : FunName '(' ArgList ')' ';'
{ (*callbackProc)(3, NULL); }
;
FunName : WORD { (*callbackProc)(1, $1); }
;
ArgList : Args
| Empty
;
Args : Args ',' Arg
| Arg
;
Arg : WORD { (*callbackProc)(2, $1); }
;
Empty :
;
%%
void
ParseThis(line, callback)
char *line;
void (*callback)();
{
callbackProc = callback;
scSetInputBuffer(line);
yyparse();
}
void yyerror(char *msg)
{
(*callbackProc)(4, msg);
}
#ifdef TEST
void SignalBeginFunction(char *name)
{
printf("** Function call: %s(", name);
}
void SignalArg(char *arg)
{
char *p = arg;
printf("\n++Arg: (");
while (*p) {
if (isprint(*p))
putchar(*p);
else
printf("\\0%o", (int)*p);
p++;
}
putchar(')');
}
void SignalEndFunction()
{
printf("\n)\n");
}
void
main(int argc, char *argv[])
{
char long_line[1000];
char input_str[] = "This(is, a, real, funky); script();\n\
Scripts(); Can(be); Multi(Line, \"Can't they?\");\n\
Commas(are, no, longer, optional, inside, arglists);\n\
Scripts(); Can(); contain(\"tabs \\t and backspaces \\b\");\n\
As(\"Well\\ as Quoted Strings\", and, '\"Quoted Strings inside\n\
quoted strings\"');\n\
esc(can, appear, outside, strings, ^z, \\012\\015\\n)\n\
But(parenthesis, should, match);\n\
We(\"have a funny way of specifying \\012 chars and even)\"); \n\
backslashes( \" \\\\ \");\n\
new(\"in this version are ^m and ^A ctr-escapes, as in ^S^Q\");\n\
The(next, line, will, give, a, syntax, error, because, it, has, two, adj, functions,\n\
without, a, separating, semicolon);\n\
End() script()";
printf("------ String to parse: \n%s\n\n---- Parsing begins:\n", input_str);
strcpy(long_line, input_str);
ParseThis(long_line);
strcpy(long_line, input_str);
ParseThis(long_line);
}
#endif TEST