-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf.sno
executable file
·138 lines (113 loc) · 3.09 KB
/
ebnf.sno
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
#!/usr/local/bin/snobol4 -b
***
*** Definitions
***
define('optspace(pattern)')
define('string()')
define('serialize()')
strings = array(2)
str_no = 0
define('emit_groff(str)')
define('begin_rule()')
define('end_rule()')
define('emit_nonterm()')
define('emit_term()')
define('emit_empty()')
define('begin_exp()')
define('end_exp()')
define('begin_alt()')
define('end_alt()')
define('repeat()')
define('repeat_with()')
&anchor = 1
&fullscan = 1
***
*** EBNF grammar
***
letter = &lcase &ucase
digit = "0123456789"
space = span(" " char(9) char(10) char(13)) | ""
eol = break(char(10) char(13))
nonterm = ( "`" break("`") $ *string() "`"
+ | (any(letter) (span(letter digit "_") | "")) $ *string() )
+ (optspace("~") *nonterm | "")
term = ( "'" break("'") $ *string() "'"
+ | '"' break('"') $ *string() '"' )
+ (optspace("~") *term | "")
lhs = nonterm
exp = nonterm *emit_nonterm()
+ | term *emit_term()
+ | optspace("[") *emit_empty() *begin_alt() *rhs optspace("]") *end_alt()
+ | optspace("{") *begin_exp() *rhs optspace("}") *end_exp()
+ (optspace("~") term *repeat_with() | *repeat())
+ | optspace("(") *begin_exp() *rhs optspace(")") *end_exp()
+ | *emit_empty()
rhs = exp
+ ( optspace("|") *begin_alt() *rhs *end_alt()
+ | optspace(",") *rhs
+ | "")
* NOTE: have to reset str_no if there is no lhs, since
* strings may already contain a nonterminal
rule = ( lhs optspace("=" | ":=" | "::=")
+ | *?(str_no = 0) "" $ *string() )
+ *begin_rule() rhs optspace(";") *end_rule()
comment = optspace("(*" breakx("*") "*)" | "#" eol)
groff = optspace("." eol $ str) *emit_groff(str)
pic = optspace("%" eol $ output)
grammar = arbno(comment | groff | pic | rule) rpos(0)
***
*** MAIN
***
lineno = 1
loop line = input :f(end)
line ".lf " int . lineno :s(next)
line ".EBNF" :s(src.l)
lineno = lineno + 1
next output = line :(loop)
src.l line = input
lineno = lineno + 1
line ".EBNF" :s(compile)
src = src line char(10) :(src.l)
compile
output = ".PS"
output = 'copy "syntax.pic";'
(src ? grammar, terminal = "FAILURE")
output = 'reset;'
output = ".PE"
output = ".lf " (lineno + 1)
src = "" :(loop)
***
*** Procedures
***
optspace
optspace = space pattern space :(return)
string
string = .strings[str_no = str_no + 1] :(nreturn)
serialize
* NOTE: will leave str_no == 0
serialize = '"' strings[str_no] '" ' serialize
eq(str_no = str_no - 1, 0) :s(return)f(serialize)
emit_groff
output = 'command ".' str '";' :(return)
begin_rule
output = 'begin_rule(' serialize() ');' :(return)
end_rule
output = 'end_rule();' :(return)
emit_nonterm
output = 'nonterminal(' serialize() ');' :(return)
emit_term
output = 'terminal(' serialize() ');' :(return)
emit_empty
output = 'empty();' :(return)
begin_exp
output = 'begin_group();' :(return)
end_exp
output = 'end_group();' :(return)
begin_alt
output = 'begin_alt(last []);' :(return)
end_alt
output = 'end_alt(2nd last []);' :(return)
repeat output = 'repeat(last []);' :(return)
repeat_with
output = 'repeat_with(last [],' serialize() ');' :(return)
end