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