Last active
May 14, 2020 15:38
-
-
Save IonianIronist/1bbeb300ca5da4266d50a5a5b9f6406e to your computer and use it in GitHub Desktop.
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
""" | |
input : | |
123 8 0x7ff 45. | |
23.67 .89000 | |
0x5A 6.2 | |
output: | |
(venv) ionianironist@debian:~/PycharmProjects/untitled$ python lab.py | |
INT_TOKEN 123 | |
INT_TOKEN 8 | |
HEX_TOKEN 0x7ff | |
FLOAT_TOKEN 45. | |
FLOAT_TOKEN 23.67 | |
FLOAT_TOKEN .89000 | |
HEX_TOKEN 0x5A | |
FLOAT_TOKEN 6.2 | |
""" | |
import plex | |
# define patterns | |
# letter = plex.Range("AZaz") | |
digit = plex.Range("09") | |
dot = plex.Str(".") | |
integer = plex.Rep1(digit) | |
floating = (integer + dot + plex.Opt(integer)) | (dot + integer) | |
hexPrefix = plex.Str("0x") | |
hexDigit = plex.Range("09AFaf") | |
hexadecimal = hexPrefix + plex.Rep1(hexDigit) | |
# the scanner lexicon - constructor argument is a list of (pattern,action ) tuples | |
lexicon = plex.Lexicon([ | |
(integer, "INT_TOKEN"), | |
(floating, "FLOAT_TOKEN"), | |
(hexadecimal, "HEX_TOKEN"), | |
(plex.Rep1(plex.Any(" \t\n")), plex.IGNORE) | |
]) | |
with open("identifiers.txr", "r") as fp: | |
scanner = plex.Scanner(lexicon, fp) | |
while True: | |
try: | |
token, matched = scanner.read() | |
except plex.errors.PlexError: | |
_, lineno, charno = scanner.position() # lineno is 1-based, charno is 0-based | |
print("Scanner Error at line {} char {}".format(lineno, charno + 1)) | |
break # lexical analysis ends after error | |
if not token: | |
break # reached end-of-text (EOT) | |
print(token, matched) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment