Created
October 23, 2014 11:24
-
-
Save evidex/6a3cff98b1dfe5b9903e to your computer and use it in GitHub Desktop.
Flex Generator
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
""" Automatically write Jflex rules for a dictionary of tokens and their regex patterns | |
""" | |
tokens = { | |
"IF": "if", | |
"THEN": "then", | |
"ELSE": "else", | |
"RETURN": "return", | |
"VOID": "void", | |
"WHILE": "while", | |
"ADD": "+", | |
"SUB": "-", | |
"MUL": "*", | |
"DIV": "/", | |
"LT": "<", | |
"LTE": "<=", | |
"GT": ">", | |
"GTE": ">=", | |
"NOT_EQUAL": "!=", | |
"EQUAL": "==", | |
"ASSIGN": "=", | |
"SEMICOL": ";", | |
"COMMA": ",", | |
"LPAREN": "(", | |
"RPAREN": ")", | |
"LSQUARE": "[", | |
"RSQUARE": "]", | |
"LCURL": "{", | |
"RCURL": "}", | |
"COMMENT": "\/\*(.*)\*\/" | |
} | |
with open("token_flex.flex", "w") as f: | |
for token, regx in tokens.iteritems(): | |
line = '"'+ regx +'" { return new CminusToken( CminusToken.TokenKind.'+ token +' );}\n' | |
f.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment