-
Notifications
You must be signed in to change notification settings - Fork 3
/
README
314 lines (249 loc) · 9.48 KB
/
README
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
NAME
MarpaX::Simple::Lexer - simplify lexing for Marpa parser
SYNOPSIS
use 5.010;
use strict;
use warnings;
use lib 'lib/';
use Marpa::XS;
use MarpaX::Simple::Lexer;
my $grammar = Marpa::XS::Grammar->new( {
actions => 'main',
default_action => 'do_what_I_mean',
start => 'query',
rules => [
{
lhs => 'query', rhs => [qw(condition)],
min => 1, separator => 'OP', proper => 1, keep => 1,
},
[ condition => [qw(word)] ],
[ condition => [qw(quoted)] ],
[ condition => [qw(OPEN-PAREN SPACE? query SPACE? CLOSE-PAREN)] ],
[ condition => [qw(NOT condition)] ],
[ 'SPACE?' => [] ],
[ 'SPACE?' => [qw(SPACE)] ],
],
lhs_terminals => 0,
});
$grammar->precompute;
my $recognizer = Marpa::XS::Recognizer->new( { grammar => $grammar } );
use Regexp::Common qw /delimited/;
my $lexer = MyLexer->new(
recognizer => $recognizer,
tokens => {
word => qr{\b\w+\b},
'quoted' => qr[$RE{delimited}{-delim=>qq{\'\"}}],
OP => qr{\s+(OR)\s+|\s+},
NOT => '!',
'OPEN-PAREN' => '(',
'CLOSE-PAREN' => ')',
'SPACE' => qr{\s+()},
},
debug => 1,
);
$lexer->recognize(\*DATA);
use Data::Dumper;
print Dumper $recognizer->value;
sub do_what_I_mean {
shift;
my @children = grep defined && length, @_;
return scalar @children > 1 ? \@children : shift @children;
}
package MyLexer;
use base 'MarpaX::Simple::Lexer';
sub grow_buffer {
my $self = shift;
my $rv = $self->SUPER::grow_buffer( @_ );
${ $self->buffer } =~ s/[\r\n]+//g;
return $rv;
}
package main;
__DATA__
hello !world OR "he hehe hee" ( foo OR !boo )
WARNING
This is experimental module in alpha stage I cooked during weekend to
simplify and speed up writing marpa grammar and lexer for vCards.
I'm publishing it to collect feedback and because I believe it can be
very useful to people experimenting with Marpa.
DESCRIPTION
This module helps you start with Marpa::XS parser and simplifies lexing.
TUTORIAL
Where to start
Here is template you can start a new parser from:
use strict; use warnings;
use Marpa::XS;
use MarpaX::Simple::Lexer;
my $grammar = Marpa::XS::Grammar->new( {
start => 'query',
rules => [
[ query => [qw(something)] ],
],
lhs_terminals => 0,
});
$grammar->precompute;
my $recognizer = Marpa::XS::Recognizer->new( { grammar => $grammar } );
my $lexer = MarpaX::Simple::Lexer->new(
recognizer => $recognizer,
tokens => {},
debug => 1,
);
$lexer->recognize(\*DATA);
__DATA__
hello !world "he hehe hee" ( foo OR boo )
It's a working program that prints the following output:
Expect token(s): 'something'
Buffer start: hello !world "he heh...
Unknown token: 'something'
Unexpected message, type "parse exhausted" at ...
First line says that at this moment parser expects 'something'. It's
going to look for it in the following text (second line). Third line
says that lexer doesn't know anything about 'something'. It's not a
surprise that parsing fails.
What can we do with 'something'? We either put it into grammar or lexer.
In above example it's pretty obvious that it's gonna be in the grammar.
Put some grammar
rules => [
# over query is a sequence of conditions separated with OPs
{
lhs => 'query', rhs => [qw(condition)],
min => 1, separator => 'OP', proper => 1, keep => 1,
},
# each condition can be one of the following
[ condition => [qw(word)] ],
[ condition => [qw(quoted)] ],
[ condition => [qw(OPEN-PAREN SPACE? query SPACE? CLOSE-PAREN)] ],
[ condition => [qw(NOT condition)] ],
],
Our program works and gives us helpful results:
Expect token(s): 'word', 'quoted', 'OPEN-PAREN', 'NOT'
Buffer start: hello !world OR "he ...
Unknown token: 'word'
...
First token
tokens => {
word => qr{\w+},
},
Ouput:
Expect token(s): 'word', 'quoted', 'OPEN-PAREN', 'NOT'
Buffer start: hello !world OR "he ...
Token 'word' matched hello
Unknown token: 'quoted'
Unknown token: 'OPEN-PAREN'
Unknown token: 'NOT'
Expect token(s): 'OP'
Congrats! First token matched. More tokens:
use Regexp::Common qw /delimited/;
my $lexer = MarpaX::Simple::Lexer->new(
recognizer => $recognizer,
tokens => {
word => qr{\b\w+\b},
OP => qr{\s+|\s+OR\s+},
NOT => '!',
'OPEN-PAREN' => '(',
'CLOSE-PAREN' => ')',
'quoted' => qr[$RE{delimited}{-delim=>qq{\'\"}}],
},
debug => 1,
);
Tokens matching empty string
You can not have such. In our example grammar we have 'SPACE?' that is
optional. You could try to use "qr{\s*}", but lexer would die with an
error. Instead use the following:
rules => [
...
[ 'SPACE?' => [] ],
[ 'SPACE?' => [qw(SPACE)] ],
],
...
tokens => {
...
'SPACE' => qr{\s+},
},
Lexer's ambiguity
This module uses marpa's alternative input model what allows you to
describe ambiguous lexer, e.g. several tokens starts at the same point.
This is not always give you multiple results, but allows to start faster
and keep improving tokens and grammar to avoid unnecessary ambiguity
cases.
Longest token match
Let's look at string "x OR y". It should match "word OP word", but it
matches "word OP word OP word". This happens because of how we defined
OP token. If we change it to "qr{\s+OR\s+|\s+}" then results are better.
Input buffer
By default lexer reads data from the input stream in chunks into a
buffer and grow the buffer only when it's shorter than "min_buffer"
bytes. By default it's 4kb. This is good for memory consuption, but it
can result in troubles when a terminal may be larger than a buffer. For
example consider a document with embedded base64 encoded binary files.
You can use several solutions to workaround this problem.
Read everything into memory. Simplest way out. It's not default value to
avoid encouragement:
my $lexer = MarpaX::Simple::Lexer->new(
min_buffer => 0,
...
);
Use larger buffer:
my $lexer = MarpaX::Simple::Lexer->new(
min_buffer => 10*1024*1024, # 10MB
...
);
Use built in protection from such cases. When a regular expression token
matches whole buffer and buffer still can grow then lexer grows buffer
and retries. This allows you to write a regular expression that matches
till end of token or end of buffer ("$"). Note that this may result in
token incomplete match if input ends right in the middle of it.
tokens => {
...
'text-paragraph' => qr{\w[\w\s]+?(?:\n\n|$)},
},
Adjust grammar. In most cases you can split long terminal into multiple
terminals with limitted length. For example:
rules => [
...
{ lhs => 'text', rhs => 'text-chunk', min => 1 },
],
Filtering input
Input can be filtered with subclassing grow_buffer method:
package MyLexer;
use base 'MarpaX::Simple::Lexer';
sub grow_buffer {
my $self = shift;
my $rv = $self->SUPER::grow_buffer( @_ );
${ $self->buffer } =~ s/[\r\n]+//g;
return $rv;
}
Actions
The simplest possible action that can produce some results:
my $grammar = Marpa::XS::Grammar->new( {
actions => 'main',
default_action => 'do_what_I_mean',
...
);
sub do_what_I_mean {
shift;
my @children = grep defined && length, @_;
return @children > 1 ? \@children : shift @children;
}
...
$lexer->recognize(\*DATA);
use Data::Dumper;
print Dumper $recognizer->value;
Token's values
Values of tokens are set to whatever token matches in the input, however
for regexp tokens you can use $1 to set value. Here is part of data from
our example:
'(',
' ',
[ ...
Paren is followed by optional space. We can change SPACE token:
'SPACE' => qr{\s+()},
New token captures empty string into $1 and it skipped by default
action.
Similar trick can be used with OP, but to cature 'OR' without spaces:
OP => qr{\s+(OR)\s+|\s+},
What's next
Add more actions. Experiment. Enjoy.
AUTHOR
Ruslan Zakirov <[email protected]>
LICENSE
Under the same terms as perl itself.