简体   繁体   English

Python - 只打印“打印”

[英]Python - Only printing 'PRINT'

I am trying to make my own basic programming language.我正在尝试制作自己的基本编程语言。 I have the following code in my smrlang.py file我的smrlang.py文件中有以下代码

from sys import *

tokens = []

def open_file(filename):
    data = open(filename, "r").read()
    return data

def smr(filecontents):
    tok = ""
    state = 0
    string = ""
    filecontents = list(filecontents)
    for char in filecontents:
        tok += char
        if tok == " ":
            if state == 0:
                tok = ""
            else:
                tok = " "
        elif tok == "PRINT":
            tokens.append("PRINT")
            tok = ""
        elif tok == "\"":
            if state == 0:
                state = 1
            elif state == 1:
                print("STRING")
                string = ""
                state = 0
        elif state == 1:
            string += tok
    print(tokens)
def run():
    data = open_file(argv[1])
    smr(data)
run()

And I have this in my one.smr file:我的one.smr文件中有这个:

PRINT "HELLO WORLD"

The output should be something like PRINT STRING , but when I use the command python3 smrlang.py one.smr , the output is just PRINT .输出应该类似于PRINT STRING ,但是当我使用命令python3 smrlang.py one.smr ,输出只是PRINT I am using Python 3我正在使用 Python 3

Debugging it in the head, I found the problem:头部调试,发现问题:

elif state == 1:
    string += tok

You don't reset the token here.您不要在此处重置令牌。 It will be aababcabcd instead of abcd and recognizing \\ won't work (as it will be aababcabcd\\ ).它将是aababcabcd而不是abcd并且识别\\将不起作用(因为它将是aababcabcd\\ )。

This also causes the token to just be everything and it will never print.这也导致令牌只是一切,永远不会打印。

Try changing it to:尝试将其更改为:

elif state == 1:
    string += tok
    tok = ""

Output after fix:修复后输出:

> py -3 temp.py temp.txt
STRING
['PRINT']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试仅打印“状态失败”,但Python正在打印所有内容 - Trying to print out only “state failed” but Python is printing everything Python正在打印所有内容,我只希望它打印最后一个数字 - Python is printing everything, I only want it to print the last number Python 打印函数中的函数打印 - Function printing in Print Function in Python Python 在不应该打印的时候打印,而打印不应该打印的内容 - Python is printing when it should not print, and printing what should not print Visual Studio 2019 Python 2.7 调试只打印第一个打印语句 - Visual Studio 2019 Python 2.7 debugging only printing first print statement 我想打印列表中添加的所有元素,但它只打印最后一个元素。(Python) - I want to print all the element added in list but it is only printing the last element.(Python) Python - 如何显示窗口+打印文本? 它的唯一印刷但不显示窗口 - Python - How to show the window + print the text? Where its only printing but not showing the window Python:打印合并排序函数的结果时,打印选项给出了所有步骤。 如何只打印最终排序的数组? - Python: While printing the result of merge sort function, print option gives all steps. How to print only final sorted array? 多种打印输出,仅打印其中一种 - Multiple print output, printing only one of them 在Python中仅打印有效数字 - Printing only significant digits in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM