Python Lexer

Asked 2 years ago, Updated 2 years ago, 75 views

I'm making the string Lexer, but I don't know how to make it, and the letters after the last space don't come out, but I'm curious and don't know how to fix it.

The goal is to categorize the string in the list based on the space, but leave the space in the "characters in" or in the "space". So when there's a code like this,

"Hello, world!" Output 'Hello, world!' Say 'Hello, world!'

to ensure that
["str:Hello, world!", "Output", "str:Hello, world!", "Speaking"]

How do I write Python code? For another example,

"Hello" output

["str: Hello", "Output"]

It's to switch to.

python programming-language

2022-09-20 17:26

1 Answers

If this is the case, first of all, you should not divide the given full string input into spaces, but divide it by quotation criteria.

import re

regex = r"(['\"][^'\"]+['\"]) ?([^'\" ]*)"
subst = "\\g<2> \\g<1>"
test_str = "\"Hello, world!\" output Say 'Hello, world!'"

In fact, quotation marks are not important, but the task of making command-arguments pairs with a unique object-descript structure in Korean is probably more essential. If that's the real problem with this... I'd like to introduce some of the abbreviations.

# I haven't turned the code. Just look at the concept!
inputstring = input
commands = []
For keyword in ('Output', 'Speaking') :
    pattern = r"(['\"][^'\"]+['\"]) ?" + keyword
    If inputstring matched pattern:
        commands.push
        Remove just matched from inputstring

PS. By the way... What you're trying to do is...?


2022-09-20 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.