%{ /* flex -o m.cpp m.f && gcc -o m m.cpp -lstdc++ -lfl */ #include #include #include using namespace std; map footnote_ids; /* old id -> new id */ vector footnotes; /* number -> footnote text */ int translate_fn_id(int old_id) { map::const_iterator it = footnote_ids.find(old_id); if(it != footnote_ids.end()) return it->second; footnote_ids.insert(make_pair(old_id, footnote_ids.size()+1)); return footnote_ids.size(); } int current_fn=0; void define_footnote(const char* txt) { int id = translate_fn_id(current_fn); if(footnotes.size() <= id) footnotes.resize(id+1); footnotes[id] = txt; } %} %x FN FN2 %% \[[0-9]+\] printf("[%d]", translate_fn_id(atoi(yytext+1))); @footnote[^\n]*\n ECHO; BEGIN FN; \[[[:digit:]]+\] current_fn = atoi(yytext+1); BEGIN FN2; [^\n]* define_footnote(yytext); \n BEGIN FN; %% int main(char argc, char** argv) { yyin = argc > 0 ? fopen(argv[1], "r") : stdin; yylex(); for(int i=1; i