Quantcast
Channel: Tokenize a Stack-Based language - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by Zach Gates for Tokenize a Stack-Based language

$
0
0

Python 3, 228 bytes

import re;L=listprint(L(map(lambda i:i+'"'if i[0]=='"'and not i[-1]=='"'else i,map(lambda i:'"%s"'%i[1]if i[0]=="'"else i,filter(None,sum([L(i)for i in re.findall('(\'.)|(".*")|(\d+)|([^\w\"\'\s\\\])|(".*"?)',input())],[]))))))

Here's a nice, long, two-liner.


Test it out in Python 3. Here's some examples:

$ python3 test.py"PPCG"23+['"PPCG"', '23', '+']$ python3 test.py'a "bc"+['"a"', '"bc"', '+']$ python3 test.py12 34+-"abc"de'fg\"['12', '34', '+', '-', '"abc"de\'fg\\"']$ python3 test.py"foo['"foo"']$ python3 test.py[]$ python3 test.py'""['""', '""']

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>