//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Jancy features automaton functions as a built-in support for creating // protocol analyzers, programming language lexers and other recognizers. // If you ever used tools like Lex, Flex, Ragel etc then you are already // familiar with the idea. If not, then it is pretty simple, actually. First, // you define a list of recognized lexemes in form of regular expressions. Then // you specify which actions to execute when these lexemes are found in the // input stream. Jancy compiler will then automatically build a DFA to // recognize your language. //.............................................................................. jnc.AutomatonResult automaton fooBar (jnc.Recognizer* recognizer) { %% "foo" printf ("lexeme found: foo\n"); %% "bar" printf ("lexeme found: bar\n"); %% \d+ // you can access matched lexeme via recognizer.m_lexeme field printf ("lexeme found: dec '%s'\n", recognizer.m_lexeme); %% \s+ // ignore whitespace } //.............................................................................. // entry point int main () { printf ("main ()\n"); // automaton functions cannot be directly called -- you need a recognizer // object to store the state of DFA jnc.Recognizer recognizer (fooBar); // recognize () method is the simplest way to do recognition in one go bool result = try recognizer.recognize ("foo bar 100 baz"); if (!result) printf ("recognition error: %s\n", std.getLastError ().m_description); // It's also OK to perform recognition incrementally, chunk by chunk. // This is cructial when analyzing protocols operating over stream // transports like TCP or Serial, where it is not guaranteed that a message // will be delivered as a whole and not as multiple segments. recognizer.reset (); try { recognizer.write (" ba"); recognizer.write ("r f"); recognizer.write ("oo "); recognizer.write ("100"); recognizer.write ("000"); // notify recognizer about eof (this can trigger actions or errors) recognizer.eof (); catch: printf ("recognition error: %s\n", std.getLastError ().m_description); } return 0; } //..............................................................................