1 // Example Flex yyin, yyrestart, yy_scan_string, yy_scan_bytes, yy_scan_buffer
2 // Flex reentrant allows us to pass the lexer to these functions, otherwise a
3 // global YY_SCANNER is expected, which can be generated with option --bison
4 //
5 // Some notes on Flex reentrant use:
6 // - yyscanner can only be used in lexer methods and in rules, in fact
7 // yyscanner is the same as the 'this' pointer;
8 // - yyin can only be used in lexer methods and in rules, use yyget_in(&lexer)
9 // with a lexer object;
10 // - YY_CURRENT_BUFFER can only be used in lexer methods and in rules, use
11 // yyget_current_buffer(&lexer) with a lexer object.
12
13 %top{
14 #include <algorithm>
15 }
16
17 %class{
18 public:
19 void do_lex();
20 std::vector<std::string> words;
21 }
22
23 %option flex reentrant
24
25 %%
26 \w+ words.push_back(str());
27 .|\n // ignore
28 %%
29
30 int main()
31 {
32 yyFlexLexer lexer;
33
34 lexer.do_lex();
35
36 std::copy(lexer.words.begin(), lexer.words.end(), std::ostream_iterator<std::string>(std::cout, " "));
37 std::cout << std::endl;
38 }
39
do_lex()40 void yyFlexLexer::do_lex()
41 {
42 // scan a string using reentrant yyget_in() as no input is set yet
43 std::string string("Lorem ipsum dolor sit amet,");
44 yyin = string;
45 yylex();
46
47 // restart scanning with new string input using reentrant yyrestart()
48 // note: yyget_in() won't work here because it does not reset the scanner
49 yyrestart("consectetur adipiscing elit.", yyscanner);
50 yylex();
51
52 // scan character string using yy_scan_string()
53 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
54 yy_scan_string("Quisque at accumsan turpis,", yyscanner);
55 yylex();
56
57 // scan bytes up to specified length using yy_scan_bytes()
58 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
59 yy_scan_bytes("non feugiat magna.", 18, yyscanner);
60 yylex();
61
62 // scan bytes in place using yy_scan_buffer(), buffer content is changed!!
63 // note: string must be \0-terminated and the length includes final \0
64 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
65 char scan_in_place[29] = "Quisque sed fringilla felis.";
66 yy_scan_buffer(scan_in_place, 29, yyscanner);
67 yylex();
68
69 // scan wide std::wstring
70 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
71 std::wstring wstring(L"Ut at ullamcorper metus,");
72 yy_scan_wstring(wstring, yyscanner);
73 yylex();
74
75 // scan wide string
76 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
77 yy_scan_wstring(L"eu ornare lorem.", yyscanner);
78 yylex();
79 }
80