From 45c598cdafdb4e21de52a924c806b58c2348a467 Mon Sep 17 00:00:00 2001 From: MaksimShagov <43129418+MaksimShagov@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:26:25 +0300 Subject: [PATCH] flex --- benchmarks/flex/build.sh | 2 + benchmarks/flex/lexer.l | 119 +++++++++++++++++++++++++++++++++++++++ benchmarks/flex/test.py | 48 ++++++++++++++++ benchmarks/flex/test2.py | 18 ++++++ 4 files changed, 187 insertions(+) create mode 100644 benchmarks/flex/build.sh create mode 100644 benchmarks/flex/lexer.l create mode 100644 benchmarks/flex/test.py create mode 100644 benchmarks/flex/test2.py diff --git a/benchmarks/flex/build.sh b/benchmarks/flex/build.sh new file mode 100644 index 00000000..4e881c44 --- /dev/null +++ b/benchmarks/flex/build.sh @@ -0,0 +1,2 @@ +flex lexer.l +gcc lex.yy.c -o lexer -lfl \ No newline at end of file diff --git a/benchmarks/flex/lexer.l b/benchmarks/flex/lexer.l new file mode 100644 index 00000000..d4834402 --- /dev/null +++ b/benchmarks/flex/lexer.l @@ -0,0 +1,119 @@ +%{ +#include +#include "token_types.hpp" +#include + +#define WRITE_TO_FILE +#define PRINT + +FILE *fp; +%} + +DIGIT [0-9] +IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* +KEYWORD "bool"|"int"|"str"|"else"|"range"|"for"|"import"|"def"|"or"|"not"|"True"|"False"|"float"|"if"|"elif"|"while"|"break"|"continue"|"return"|"and"|"in"|"None" +OPERATOR "%"|","|"-"|"=="|">"|"("|"."|"="|"*"|"!="|"<="|")"|"]"|"+"|"/"|"<"|">="|"[" +SPECIAL ":"|"->"|" "++|[\n] +QUOTE1 "\'"[^'\\]*"\'" +QUOTE2 "\""[^"\\]*"\"" +%% + +{DIGIT}+ { + #ifdef PRINT + printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : IntegerLiteral\n", fp); + #endif +} + +{DIGIT}+"."{DIGIT}* { + #ifdef PRINT + printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : FloatingPointLiteral\n", fp); + #endif +} + +{KEYWORD} { + #ifdef PRINT + printf( "A keyword: %s\n", yytext); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : Keyword\n", fp); + #endif +} + +{IDENTIFIER} { + #ifdef PRINT + printf( "An identifier: %s\n", yytext ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : Identifier\n", fp); + #endif +} + +{OPERATOR} { + #ifdef PRINT + printf( "An operator: %s\n", yytext ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : Operator\n", fp); + #endif +} + +{QUOTE1}|{QUOTE2} { + #ifdef PRINT + printf( "An string: %s\n", yytext ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : StringLiteral\n", fp); + #endif +} + +{SPECIAL} { + #ifdef PRINT + printf( "An Special: %s\n", yytext ); + #endif + + #ifdef WRITE_TO_FILE + fputs(yytext, fp); + fputs(" : Special\n", fp); + #endif +} + +[ ] printf( "Space: %s\n", yytext ); +"{"[^}\n]*"}" printf( "Unrecognized character: %s\n", yytext );/* eat up one-line comments */ + + + +. printf( "Unrecognized character: %s\n", yytext ); + +%% + +main(int argc, char **argv) +{ + ++argv, --argc; /* skip over program name */ + if ( argc > 0 ) + yyin = fopen( argv[0], "r" ); + else + yyin = stdin; + + fp = fopen("output.log", "w"); + + yylex(); + fclose(fp); +} \ No newline at end of file diff --git a/benchmarks/flex/test.py b/benchmarks/flex/test.py new file mode 100644 index 00000000..1574f03e --- /dev/null +++ b/benchmarks/flex/test.py @@ -0,0 +1,48 @@ +int x 213 +323 +4324 +23423.321312 ++ +bool +int +str +else +range +for +import +def +or +not +True +False +float +if +elif +while +break +continue +return +and +in +None +% +. +] +, += ++ +- +* +/ +== +!= +< +> +<= +>= +( +) +[ +"string" +'string' +[ \t]+ /* eat up whitespace */ diff --git a/benchmarks/flex/test2.py b/benchmarks/flex/test2.py new file mode 100644 index 00000000..0f7d556c --- /dev/null +++ b/benchmarks/flex/test2.py @@ -0,0 +1,18 @@ +import inspect + +def whoami(): + return inspect.stack()[1][3] + +def whosdaddy(): + return inspect.stack()[2][3] + +def foo(): + print "I'm '%s', Dad is '%s'" % (whoami(), whosdaddy()) + return "Foo" + +def bar(x=foo()): + print x + +bar() + bar() + bar(). \ No newline at end of file