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