-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeParse.y
105 lines (91 loc) · 1.98 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
%{
#include <stdio.h>
#include <ctype.h>
#include "SeParse.h"
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();"
"Scripts(); Can(be); Multi(Line, \"Can't they?\");"
"Commas(are, no, longer, optional, inside, arglists);"
"Scripts(); Can(); contain(\"tabs \\t and backspaces \\b\");"
"As(\"Well\\ as Quoted Strings\", and, '\"Quoted Strings inside"
"quoted strings\"');"
"esc(can, appear, outside, strings, ^z, \\012\\015\\n);"
"But(parenthesis, should, match);"
"We(\"have a funny way of specifying \\012 chars and even)\");"
"backslashes( \" \\\\ \");"
"new(\"in this version are ^m and ^A ctr-escapes, as in ^S^Q\");"
"The(next, line, will, give, a, syntax, error, because, it, has, two, adj, functions,"
"without, a, separating, semicolon);"
"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