This example shows how to override re2c input mechanism: instead of reading input characters from a buffer in memory, read them directly from file using STL std::ifstream class. Note that we use tellg / seekg and rely on the ability to move backward and forward in the input stream: this might not be possible, for example with stdin stream. The program below converts Windows-style line endings CR LF to Unix-style line endings LF.

This program uses a non-standard way of checking for the end of input: it disables the usual cheching mechnism with re2c:yyfill:enable = 0; (this suppresses the generation of YYLESSTHAN and YYFILL) and puts the responsibility for checking on YYSKIP. This results in more frequent checks: YYSKIP is happens on each input character, while YYLESSTHAN happens only once per each strongly connected component of automaton. However, this method allows to avoid padding, which would require buffering input and nullify all advantages of direct-file input.


// re2c $INPUT -o $OUTPUT
#include <cassert>
#include <cstdio>
#include <fstream>
#include <sstream>

static void convert_newlines(std::ifstream &in, std::ostringstream &out) {
    std::streampos mar;
    for (;;) {
        re2c:api= custom;
        re2c:api:style = free-form;
        re2c:define:YYCTYPE   = char;
        re2c:define:YYPEEK    = "in.peek()";
        re2c:define:YYSKIP    = "{ in.ignore(); if (in.eof()) return; }";
        re2c:define:YYBACKUP  = "mar = in.tellg();";
        re2c:define:YYRESTORE = "in.seekg(mar);";
        re2c:yyfill:enable = 0;

        *      { out.put(yych); continue; }
        "\r\n" { out.put('\n'); continue; }

int main() {
    const char *fname = "input";
    const char s1[] = "Text\r\nwith\r\nnewlines.\r\n\r\n";
    const char s2[] = "Text\nwith\nnewlines.\n\n";

    std::ofstream f(fname, std::ios::binary);
    f.write(s1, sizeof(s1) - 1);

    std::ifstream in(fname, std::ios::binary);
    std::ostringstream out;
    convert_newlines(in, out);
    assert(out.str() == s2);

    return 0;

Compile as re2c -o