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