Skip to content

Instantly share code, notes, and snippets.

@evidex
Created October 23, 2014 11:24
Show Gist options
  • Save evidex/6a3cff98b1dfe5b9903e to your computer and use it in GitHub Desktop.
Save evidex/6a3cff98b1dfe5b9903e to your computer and use it in GitHub Desktop.
Flex Generator
""" 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