26f8093c28d77467743bc9254a8b4eeaa080ef6a
[external/ragel.git] / doc / ragel.1.in
1 .\"
2 .\"   Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3 .\"
4
5 .\"   This file is part of Ragel.
6 .\"
7 .\"   Ragel is free software; you can redistribute it and/or modify
8 .\"   it under the terms of the GNU General Public License as published by
9 .\"   the Free Software Foundation; either version 2 of the License, or
10 .\"   (at your option) any later version.
11 .\"
12 .\"   Ragel is distributed in the hope that it will be useful,
13 .\"   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 .\"   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 .\"   GNU General Public License for more details.
16 .\"
17 .\"   You should have received a copy of the GNU General Public License
18 .\"   along with Ragel; if not, write to the Free Software
19 .\"   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20
21 .\"   Process this file with
22 .\"   groff -man -Tascii ragel.1
23 .\"
24 .TH RAGEL 1 "@PUBDATE@" "Ragel @VERSION@" "Ragel State Machine Compiler"
25 .SH NAME
26 ragel \- compile regular languages into executable state machines 
27 .SH SYNOPSIS
28 .B ragel 
29 .RI [ options ]
30 .I file
31 .SH DESCRIPTION
32 .B Note:
33 this is the frontend component of Ragel, which generates an intermediate
34 file format that must be processed by one of rlgen-cd(1), rlgen-java(1), 
35 rlgen-ruby(1) or rlgen-dot(1).
36
37 Ragel compiles finite state machines from regular languages into executable
38 code.  Ragel can generate C, C++, Objective-C, D, or Java code. Ragel state
39 machines can not only recognize byte
40 sequences as regular expression machines do, but can also execute code at
41 arbitrary points in the recognition of a regular language.  User code is
42 embedded using inline operators that do not disrupt the regular language
43 syntax.
44
45 The core language consists of standard regular expression operators, such as
46 union, concatenation and kleene star, accompanied by action embedding
47 operators. Ragel also provides operators that let you control any
48 non-determinism that you create, construct scanners using the longest match
49 paradigm, and build state machines using the statechart model. It is also
50 possible to influence the execution of a state machine from inside an embedded
51 action by jumping or calling to other parts of the machine and reprocessing
52 input.
53
54 Ragel provides a very flexibile interface to the host language that attempts to
55 place minimal restrictions on how the generated code is used and integrated
56 into the application. The generated code has no dependencies.
57
58 .SH OPTIONS
59 .TP
60 .BR \-h ", " \-H ", " \-? ", " \-\-help
61 Display help and exit.
62 .TP
63 .B \-o " file"
64 Write output to file. If -o is not given, a default file name is chosen by
65 replacing the suffix of the input. For source files ending in .rh the suffix .h
66 is used. For all other source files a suffix based on the output language
67 is used (.c, .cpp, .m, .dot)
68 .TP
69 .B \-n
70 Do not perform state minimization.
71 .TP
72 .B \-m
73 Perform minimization once, at the end of the state machine compilation. 
74 .TP
75 .B \-l
76 Minimize after nearly every operation. Lists of like operations such as unions
77 are minimized once at the end. This is the default minimization option.
78 .TP
79 .B \-e
80 Minimize after every operation.
81 .TP
82 .B \-S <spec>
83 FSM specification to output (for rlgen-dot)
84 .TP
85 .B \-M <machine>
86 Machine definition/instantiation to output (for rlgen-dot)
87 .TP
88 .B \-C
89 The host language is C, C++, Obj-C or Obj-C++. This is the default host language option.
90 .TP
91 .B \-D
92 The host language is D.
93 .TP
94 .B \-J
95 The host language is Java.
96 .SH RAGEL INPUT
97 NOTE: This is a very brief description of Ragel input. Ragel is described in
98 more detail in the user guide available from the homepage (see below).
99
100 Ragel normally passes input files straight to the output. When it sees an FSM
101 specification that contains machine instantiations it stops to generate the
102 state machine. If there are write statements (such as "write exec") then ragel emits the
103 corresponding code. There can be any number of FSM specifications in an input
104 file. A multi-line FSM specification starts with '%%{' and ends with '}%%'. A
105 single line FSM specification starts with %% and ends at the first newline.
106 .SH FSM STATEMENTS
107 .TP
108 .I Machine Name:
109 Set the the name of the machine. If given, it must be the first statement.
110 .TP
111 .I Alphabet Type:
112 Set the data type of the alphabet.
113 .TP
114 .I GetKey:
115 Specify how to retrieve the alphabet character from the element type.
116 .TP
117 .I Include:
118 Include a machine of same name as the current or of a different name in either
119 the current file or some other file.
120 .TP
121 .I Action Definition:
122 Define an action that can be invoked by the FSM.
123 .TP
124 .I Fsm Definition, Instantiation and Longest Match Instantiation:
125 Used to build FSMs. Syntax description in next few sections.
126 .TP
127 .I Access:
128 Specify how to access the persistent state machine variables.
129 .TP
130 .I Write:
131 Write some component of the machine.
132 .TP
133 .I Variable:
134 Override the default variable names (p, pe, cs, act, etc).
135 .SH BASIC MACHINES
136 The basic machines are the base operands of the regular language expressions.
137 .TP
138 .I 'hello'
139 Concat literal. Produces a concatenation of the characters in the string.
140 Supports escape sequences with '\\'.  The result will have a start state and a
141 transition to a new state for each character in the string. The last state in
142 the sequence will be made final. To make the string case-insensitive, append
143 an 'i' to the string, as in 'cmd'i\fR.
144 .TP
145 .I \(dqhello\(dq
146 Identical to single quote version.
147 .TP
148 .I [hello]
149 Or literal. Produces a union of characters.  Supports character ranges 
150 with '\-', negating the sense of the union with an initial '^' and escape
151 sequences with '\\'. The result will have two states with a transition between
152 them for each character or range. 
153 .LP
154 NOTE: '', "", and [] produce null FSMs. Null machines have one state that is
155 both a start state and a final state and match the zero length string. A null machine
156 may be created with the null builtin machine.
157 .TP
158 .I integer
159 Makes a two state machine with one transition on the given integer number.
160 .TP
161 .I hex
162 Makes a two state machine with one transition on the given hexidecimal number.
163 .TP
164 .I "/simple_regex/"
165 A simple regular expression. Supports the notation '.', '*' and '[]', character
166 ranges with '\-', negating the sense of an OR expression with and initial '^'
167 and escape sequences with '\\'. Also supports one trailing flag: i. Use it to
168 produce a case-insensitive regular expression, as in /GET/i.
169 .TP
170 .I lit .. lit
171 Specifies a range. The allowable upper and lower bounds are concat literals of
172 length one and number machines. 
173 For example, 0x10..0x20,  0..63, and 'a'..'z' are valid ranges.
174 .TP 
175 .I "variable_name"
176 References the machine definition assigned to the variable name given.
177 .TP
178 .I "builtin_machine"
179 There are several builtin machines available. They are all two state machines
180 for the purpose of matching common classes of characters. They are:
181 .RS
182 .TP
183 .B any
184 Any character in the alphabet.
185 .TP
186 .B ascii
187 Ascii characters 0..127.
188 .TP
189 .B extend
190 Ascii extended characters. This is the range -128..127 for signed alphabets
191 and the range 0..255 for unsigned alphabets.
192 .TP
193 .B alpha
194 Alphabetic characters /[A-Za-z]/.
195 .TP
196 .B digit
197 Digits /[0-9]/.
198 .TP
199 .B alnum
200 Alpha numerics /[0-9A-Za-z]/.
201 .TP
202 .B lower
203 Lowercase characters /[a-z]/.
204 .TP
205 .B upper
206 Uppercase characters /[A-Z]/.
207 .TP
208 .B xdigit
209 Hexidecimal digits /[0-9A-Fa-f]/.
210 .TP
211 .B cntrl
212 Control characters 0..31.
213 .TP
214 .B graph
215 Graphical characters /[!-~]/.
216 .TP
217 .B print
218 Printable characters /[ -~]/.
219 .TP
220 .B punct
221 Punctuation. Graphical characters that are not alpha-numerics
222 /[!-/:-@\\[-`{-~]/. 
223 .TP
224 .B space
225 Whitespace /[\\t\\v\\f\\n\\r ]/.
226 .TP
227 .B null
228 Zero length string. Equivalent to '', "" and [].
229 .TP
230 .B empty
231 Empty set. Matches nothing.
232 .RE
233 .SH BRIEF OPERATOR REFERENCE
234 Operators are grouped by precedence, group 1 being the lowest and group 6 the
235 highest.
236 .LP
237 .B GROUP 1:
238 .TP
239 .I expr , expr
240 Join machines together without drawing any transitions, setting up a start
241 state or any final states. Start state must be explicitly specified with the
242 "start" label. Final states may be specified with the an epsilon transitions to
243 the implicitly created "final" state.
244 .LP
245 .B GROUP 2:
246 .TP
247 .I expr | expr
248 Produces a machine that matches any string in machine one or machine two.
249 .TP
250 .I expr & expr
251 Produces a machine that matches any string that is in both machine one and
252 machine two.
253 .TP
254 .I expr - expr
255 Produces a machine that matches string that is in machine one but not in
256 machine two.
257 .LP
258 .B GROUP 3:
259 .TP
260 .I expr . expr
261 Produces a machine that matches all the strings in machine one followed
262 by all the strings in machine two.
263 .LP
264 NOTE: Concatenation is the default operator. Two machines next to each other
265 with no operator between them results in the concatenation operation.
266 .LP
267 .B GROUP 4:
268 .TP
269 .I label: expr
270 Attaches a label to an expression. Labels can be used by epsilon transitions
271 and fgoto and fcall statements in actions. Also note that the referencing of a
272 machine definition causes the implicit creation of label by the same name.
273 .LP
274 .B GROUP 5:
275 .TP
276 .I expr -> label
277 Draws an epsilon transition to the state defined by label. Label must
278 be a name in the current scope. Epsilon transitions are resolved when
279 comma operators are evaluated and at the root of the expression tree of
280 machine assignment/instantiation.
281 .LP
282 .B GROUP 6: Actions
283 .LP
284 An action may be a name predefined with an action statement or may
285 be specified directly with '{' and '}' in the expression.
286 .TP
287 .I expr > action
288 Embeds action into starting transitions.
289 .TP
290 .I expr @ action
291 Embeds action into transitions that go into a final state.
292 .TP
293 .I expr $ action
294 Embeds action into all transitions. Does not include pending out transitions.
295 .TP
296 .I expr % action
297 Embeds action into pending out transitions from final states.
298 .LP
299 .B GROUP 6: EOF Actions
300 .LP
301 When a machine's finish routine is called the current state's EOF actions are
302 executed. 
303 .TP
304 .I expr >/ action
305 Embed an EOF action into the start state.
306 .TP
307 .I expr </ action
308 Embed an EOF action into all states except the start state.
309 .TP
310 .I expr $/ action
311 Embed an EOF action into all states.
312 .TP
313 .I expr %/ action
314 Embed an EOF action into final states.
315 .TP
316 .I expr @/ action
317 Embed an EOF action into all states that are not final.
318 .TP
319 .I expr <>/ action
320 Embed an EOF action into all states that are not the start
321 state and that are not final (middle states).
322 .LP
323 .B GROUP 6: Global Error Actions
324 .LP
325 Global error actions are stored in states until the final state machine has
326 been fully constructed. They are then transferred to error transitions, giving
327 the effect of a default action.
328 .TP
329 .I expr >! action
330 Embed a global error action into the start state.
331 .TP
332 .I expr <! action
333 Embed a global error action into all states except the start state.
334 .TP
335 .I expr $! action
336 Embed a global error action into all states.
337 .TP
338 .I expr %! action
339 Embed a global error action into the final states.
340 .TP
341 .I expr @! action
342 Embed a global error action into all states which are not final.
343 .TP
344 .I expr <>! action
345 Embed a global error action into all states which are not the start state and
346 are not final (middle states).
347 .LP
348 .B GROUP 6: Local Error Actions 
349 .LP
350 Local error actions are stored in states until the named machine is fully
351 constructed. They are then transferred to error transitions, giving the effect
352 of a default action for a section of the total machine. Note that the name may
353 be omitted, in which case the action will be transferred to error actions upon
354 construction of the current machine.
355 .TP
356 .I expr >^ action
357 Embed a local error action into the start state.
358 .TP
359 .I expr <^ action
360 Embed a local error action into all states except the start state.
361 .TP
362 .I expr $^ action
363 Embed a local error action into all states.
364 .TP
365 .I expr %^ action
366 Embed a local error action into the final states.
367 .TP
368 .I expr @^ action
369 Embed a local error action into all states which are not final.
370 .TP
371 .I expr <>^ action
372 Embed a local error action into all states which are not the start state and
373 are not final (middle states).
374 .LP
375 .B GROUP 6: To-State Actions
376 .LP
377 To state actions are stored in states and executed any time the machine moves
378 into a state. This includes regular transitions, and transfers of control such
379 as fgoto. Note that setting the current state from outside the machine (for
380 example during initialization) does not count as a transition into a state.
381 .TP
382 .I expr >~ action
383 Embed a to-state action action into the start state.
384 .TP
385 .I expr <~ action
386 Embed a to-state action into all states except the start state.
387 .TP
388 .I expr $~ action
389 Embed a to-state action into all states.
390 .TP
391 .I expr %~ action
392 Embed a to-state action into the final states.
393 .TP
394 .I expr @~ action
395 Embed a to-state action into all states which are not final.
396 .TP
397 .I expr <>~ action
398 Embed a to-state action into all states which are not the start state and
399 are not final (middle states).
400 .LP
401 .B GROUP 6: From-State Actions
402 .LP
403 From state actions are executed whenever a state takes a transition on a character.
404 This includes the error transition and a transition to self.
405 .TP
406 .I expr >* action
407 Embed a from-state action into the start state.
408 .TP
409 .I expr <* action
410 Embed a from-state action into every state except the start state.
411 .TP
412 .I expr $* action
413 Embed a from-state action into all states.
414 .TP
415 .I expr %* action
416 Embed a from-state action into the final states.
417 .TP
418 .I expr @* action
419 Embed a from-state action into all states which are not final.
420 .TP
421 .I expr <>* action
422 Embed a from-state action into all states which are not the start state and
423 are not final (middle states).
424 .LP
425 .B GROUP 6: Priority Assignment
426 .LP
427 Priorities are assigned to names within transitions. Only priorities on the
428 same name are allowed to interact. In the first form of priorities the name
429 defaults to the name of the machine definition the priority is assigned in.
430 Transitions do not have default priorities.
431 .TP
432 .I expr > int
433 Assigns the priority int in all transitions leaving the start state.
434 .TP
435 .I expr @ int
436 Assigns the priority int in all transitions that go into a final state.
437 .TP
438 .I expr $ int
439 Assigns the priority int in all existing transitions.
440 .TP
441 .I expr % int
442 Assigns the priority int in all pending out transitions.
443 .LP
444 A second form of priority assignment allows the programmer to specify the name
445 to which the priority is assigned, allowing interactions to cross machine
446 definition boundaries.
447 .TP
448 .I expr > (name,int)
449 Assigns the priority int to name in all transitions leaving the start state.
450 .TP
451 .I expr @ (name, int)
452 Assigns the priority int to name in all transitions that go into a final state.
453 .TP
454 .I expr $ (name, int)
455 Assigns the priority int to name in all existing transitions.
456 .TP
457 .I expr % (name, int)
458 Assigns the priority int to name in all pending out transitions.
459 .LP
460 .B GROUP 7:
461 .TP
462 .I expr *
463 Produces the kleene star of a machine. Matches zero or more repetitions of the
464 machine.
465 .TP
466 .I expr **
467 Longest Matching Kleene Star. This version of kleene star puts a higher
468 priority on staying in the machine over wrapping around and starting over. This
469 operator is equivalent to ( ( expr ) $0 %1 )*.
470 .TP
471 .I expr ?
472 Produces a machine that accepts the machine given or the null string. This operator
473 is equivalent to  ( expr | '' ).
474 .TP
475 .I expr +
476 Produces the machine concatenated with the kleen star of itself. Matches one or
477 more repetitions of the machine.  This operator is equivalent to ( expr . expr* ).
478 .TP
479 .I expr {n}
480 Produces a machine that matches exactly n repetitions of expr.
481 .TP
482 .I expr {,n}
483 Produces a machine that matches anywhere from zero to n repetitions of expr.
484 .TP
485 .I expr {n,}
486 Produces a machine that matches n or more repetitions of expr.
487 .TP
488 .I expr {n,m}
489 Produces a machine that matches n to m repetitions of expr.
490 .LP
491 .B GROUP 8:
492 .TP
493 .I ! expr
494 Produces a machine that matches any string not matched by the given machine.
495 This operator is equivalent to ( *extend - expr ).
496 .LP
497 .B GROUP 9:
498 .TP
499 .I ( expr )
500 Forces precedence on operators.
501 .SH VALUES AVAILABLE IN CODE BLOCKS
502 .TP
503 .I fc
504 The current character. Equivalent to *p.
505 .TP
506 .I fpc
507 A pointer to the current character. Equivalent to p.
508 .TP
509 .I fcurs
510 An integer value representing the current state.
511 .TP
512 .I ftargs
513 An integer value representing the target state.
514 .TP
515 .I fentry(<label>)
516 An integer value representing the entry point <label>.
517 .SH STATEMENTS AVAILABLE IN CODE BLOCKS
518 .TP
519 .I fhold;
520 Do not advance over the current character. Equivalent to --p;.
521 .TP
522 .I fexec <expr>;
523 Sets the current character to something else. Equivalent to p = (<expr>)-1;
524 .TP
525 .I fgoto <label>;
526 Jump to the machine defined by <label>. 
527 .TP
528 .I fgoto *<expr>;
529 Jump to the entry point given by <expr>. The expression must
530 evaluate to an integer value representing a state.
531 .TP
532 .I fnext <label>;
533 Set the next state to be the entry point defined by <label>.  The fnext
534 statement does not immediately jump to the specified state. Any action code
535 following the statement is executed.
536 .TP
537 .I fnext *<expr>;
538 Set the next state to be the entry point given by <expr>. The expression must
539 evaluate to an integer value representing a state.
540 .TP
541 .I fcall <label>;
542 Call the machine defined by <label>. The next fret will jump to the
543 target of the transition on which the action is invoked.
544 .TP
545 .I fcall *<expr>;
546 Call the entry point given by <expr>. The next fret will jump to the target of
547 the transition on which the action is invoked.
548 .TP
549 .I fret;
550 Return to the target state of the transition on which the last fcall was made.
551 .TP
552 .I fbreak;
553 Save the current state and immediately break out of the machine.
554 .SH BUGS
555 Ragel is still under development and has not yet matured. There are probably
556 many bugs.
557 .SH CREDITS
558 Ragel was written by Adrian Thurston <thurston@cs.queensu.ca>.  Objective-C
559 output contributed by Erich Ocean. D output contributed by Alan West. 
560 .SH "SEE ALSO"
561 .BR rlgen-cd (1),
562 .BR rlgen-dot (1),
563 .BR rlgen-java (1),
564 .BR re2c (1),
565 .BR flex (1)
566
567 Homepage: http://www.cs.queensu.ca/~thurston/ragel/