-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e02e882
commit 45c598c
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
flex lexer.l | ||
gcc lex.yy.c -o lexer -lfl |
This file contains 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
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); | ||
} |
This file contains 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
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 */ |
This file contains 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
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(). |