Python - Only printing 'PRINT'
<p>I am trying to make my own basic programming language. I have the following code in my <code>smrlang.py</code> file</p>

<pre><code>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()
</code></pre>

<p>And I have this in my <code>one.smr</code> file:</p>

<pre><code>PRINT "HELLO WORLD"
</code></pre>

<p>The output should be something like <code>PRINT STRING</code>, but when I use the command <code>python3 smrlang.py one.smr</code>, the output is just <code>PRINT</code>. I am using Python 3</p>

<p>Debugging it in the head, I found the problem:</p>

<pre><code>elif state == 1:
    string += tok
</code></pre>

<p>You don't reset the token here. It will be <code>aababcabcd</code> instead of <code>abcd</code> and recognizing <code>\</code> won't work (as it will be <code>aababcabcd\</code>).</p>

<p>This also causes the token to just be everything and it will never print.</p>

<p>Try changing it to:</p>

<pre><code>elif state == 1:
    string += tok
    tok = ""
</code></pre>

<p>Output after fix:</p>

<pre><code>&gt; py -3 temp.py temp.txt
STRING
['PRINT']
</code></pre>

python