Skip to content

Commit

Permalink
flex
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimShagov committed Mar 25, 2024
1 parent e02e882 commit 45c598c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions benchmarks/flex/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flex lexer.l
gcc lex.yy.c -o lexer -lfl
119 changes: 119 additions & 0 deletions benchmarks/flex/lexer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
%{
#include <math.h>
#include "token_types.hpp"
#include <stdio.h>

#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);
}
48 changes: 48 additions & 0 deletions benchmarks/flex/test.py
Original file line number Diff line number Diff line change
@@ -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 */
18 changes: 18 additions & 0 deletions benchmarks/flex/test2.py
Original file line number Diff line number Diff line change
@@ -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().

0 comments on commit 45c598c

Please sign in to comment.