A reentrant C++ flex scanner.
Last active
December 23, 2022 17:06
-
-
Save kaushiks/ad4d28673dadd5577e13 to your computer and use it in GitHub Desktop.
A reentrant C++ flex scanner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%option reentrant | |
%option noyywrap | |
%option extra-type="Scanner *" | |
%option header-file="flex-scanner.h" | |
%option outfile="flex-scanner.c" | |
%{ | |
#include <cstdio> | |
#include "scanner.hpp" | |
%} | |
%% | |
[ \t\n] { | |
} | |
[0-9]+\.[0-9]+ { | |
printf("FLOAT: %s\n", yytext); | |
} | |
[0-9]+ { | |
printf("INTEGER: %s\n", yytext); | |
} | |
[a-zA-Z_]+[a-zA-Z0-9_]* { | |
printf("IDENTIFIER: %s\n", yytext); | |
} | |
. { | |
fprintf(stderr, "Unexpected token: %s\n", yytext); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Scanner; | |
#include <cstdio> | |
#include "flex-scanner.h" | |
#include "scanner.hpp" | |
int main(int argc, const char *argv[]) | |
{ | |
do { | |
char input[80] = {0}; | |
printf(">>> "); | |
if (fgets(input, 80, stdin)) { | |
do { | |
Scanner *scanner = Scanner::create(input); | |
scanner->scan(); | |
} while (0); | |
} | |
} while (1); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
flex flex-scanner.l | |
g++ -o d flex-scanner.c scanner.cc main.cc | |
test: all | |
./d | |
clean: | |
rm ./d | |
rm ./flex-scanner.c | |
rm ./flex-scanner.h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Scanner; | |
#include "flex-scanner.h" | |
#include "scanner.hpp" | |
Scanner* | |
Scanner::create(const char* string) | |
{ | |
Scanner* self = NULL; | |
yyscan_t flex; | |
YY_BUFFER_STATE buffer; | |
if (string) { | |
yylex_init(&flex); | |
buffer = yy_scan_string(string, flex); | |
self = new Scanner(flex, buffer); | |
yyset_extra(self, flex); | |
} | |
return self; | |
} | |
Scanner::Scanner(yyscan_t flex, YY_BUFFER_STATE buffer) | |
: _flex(flex), | |
_buffer(buffer) | |
{ | |
} | |
Scanner::~Scanner() | |
{ | |
yy_delete_buffer(_buffer, _flex); | |
yylex_destroy(_flex); | |
} | |
void | |
Scanner::scan() | |
{ | |
yylex(_flex); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Scanner { | |
public: | |
static Scanner* create(const char* string); | |
void scan(); | |
~Scanner(); | |
private: | |
Scanner(yyscan_t flex, YY_BUFFER_STATE buffer); | |
yyscan_t _flex; | |
YY_BUFFER_STATE _buffer; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment