Created
April 25, 2019 19:30
-
-
Save kodo-pp/45900c182a9cdecafd46df0c0ca80109 to your computer and use it in GitHub Desktop.
Possible bug in the standard lexer
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import lark" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[Token(ASSIGNMENT_OP, '+=')]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"grammar = r'''\n", | |
"start: BINOP | ASSIGNMENT_OP\n", | |
"\n", | |
"ASSIGNMENT_OP: \"=\"\n", | |
" | \"+=\"\n", | |
"\n", | |
"BINOP: \"*\"\n", | |
" | \"/\"\n", | |
" | \"%\" \n", | |
" | \"+\"\n", | |
" | \"-\"\n", | |
"\n", | |
"%import common.WS\n", | |
"%ignore WS\n", | |
"'''\n", | |
"\n", | |
"parser = lark.Lark(grammar, parser='lalr')\n", | |
"code = '+='\n", | |
"list(parser.lex(code))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[Token(BINOP, '+'), Token(ASSIGNMENT_OP, '=')]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"grammar = r'''\n", | |
"start: BINOP | ASSIGNMENT_OP\n", | |
"\n", | |
"ASSIGNMENT_OP: \"=\"\n", | |
" | \"+=\"\n", | |
"\n", | |
"BINOP: \"*\"\n", | |
" | \"/\"\n", | |
" | \"%\" \n", | |
" | \"+\"\n", | |
" | \"-\"\n", | |
" | \"long_operator\"\n", | |
"\n", | |
"%import common.WS\n", | |
"%ignore WS\n", | |
"'''\n", | |
"\n", | |
"parser = lark.Lark(grammar, parser='lalr')\n", | |
"code = '+='\n", | |
"list(parser.lex(code))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[Token(ASSIGNMENT_OP, '+=')]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"grammar = r'''\n", | |
"start: BINOP | ASSIGNMENT_OP\n", | |
"\n", | |
"ASSIGNMENT_OP: \"=\"\n", | |
" | \"+=\"\n", | |
" | \"even_longer_operator\"\n", | |
"\n", | |
"BINOP: \"*\"\n", | |
" | \"/\"\n", | |
" | \"%\" \n", | |
" | \"+\"\n", | |
" | \"-\"\n", | |
" | \"long_operator\"\n", | |
"\n", | |
"%import common.WS\n", | |
"%ignore WS\n", | |
"'''\n", | |
"\n", | |
"parser = lark.Lark(grammar, parser='lalr')\n", | |
"code = '+='\n", | |
"list(parser.lex(code))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Expected results**: `[Token(ASSIGNMENT_OP, '+=')]` for all of these code snippets\n", | |
"\n", | |
"**Actual results**: `[Token(ASSIGNMENT_OP, '+=')]`, `[Token(BINOP, '+'), Token(ASSIGNMENT_OP, '=')]`, `[Token(ASSIGNMENT_OP, '+=')]`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment