Forever Loop implementieren benutzerdefinierte SprachePython

Python-Programme
Anonymous
 Forever Loop implementieren benutzerdefinierte Sprache

Post by Anonymous »

Hallo, also versuche ich, meine eigene Sprache mit Python zu erstellen, und ich habe bedingte Aussagen gemacht, aber die für immer Schleife erlegt den Befehl einmal und erfriert dann seinen seltsamen
Code: < /p>
import ply.lex as lex
import ply.yacc as yacc
# --------------------
# 1. Lexer Part
# --------------------
tokens = (
'NUMBER',
'PLUS',
'TOP',
'SUBTRACT',
'MODULUS',
'COMPARE',
'MULTIPLY',
'IF',
'SAY',
'STRING',
'ELSE',
'END',
'USER',
'YELL',
'FOREVER'
)

t_PLUS = r'\+'
t_TOP = r'top'
t_SUBTRACT = r'-'
t_MODULUS = r'MOD'
t_COMPARE = r'='
t_SAY = r'say'
t_IF = r'if'
t_ELSE = r'else'
t_END = r'end'
t_USER = r'user'
t_YELL = r'yell'
t_FOREVER = r'forever'
t_MULTIPLY = r'\*'
t_ignore = ' \t'
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t

def t_STRING(t):
r'\".*?\"'
t.value = t.value[1:-1]
return t

def t_error(t):
print("Illegal character:", t.value[0])
t.lexer.skip(1)

lexer = lex.lex()
stack = []
boolean = 2
# --------------------
# 2. Parser Part
# --------------------

def p_expression_seq(p):
'expression : expression expression'
pass

def p_expression_plus(p):
'expression : PLUS'
global stack
a = stack.pop()
b = stack.pop()
stack.append(int(a+b))

def p_expression_multiply(p):
'expression : MULTIPLY'
global stack
a = stack.pop()
b = stack.pop()
stack.append(int(a*b))

def p_expression_say(p):
'expression : SAY STRING'
p[0] = p[2]

def p_expression_yell(p):
'expression : YELL STRING'
if "\\n" in p[2]:
print(p[2].replace("\\n",""))
else:
print(p[2],end="")

def p_expression_forever(p):
'expression : FOREVER expression'
while True:
print(p[2])

def p_expression_sub(p):
'expression : SUBTRACT'
global stack
a = stack.pop()
b = stack.pop()
stack.append(int(b-a))

def p_expression_mod(p):
'expression : MODULUS'
global stack
a = stack.pop()
b = stack.pop()
stack.append(int(b%a))

def p_expression_user(p):
'expression : USER'
global stack
user = int(input())
stack.append(user)

def p_expression_compare(p):
'expression : COMPARE'
global stack
global boolean
a = stack.pop()
b = stack.pop()
if a==b:
stack.append(1)
boolean = 1
elif a!=b:
stack.append(0)
boolean = 0

def p_expression_if(p):
'expression : IF expression END'
if boolean == 1:
print(p[2])

def p_expression_else(p):
'expression : ELSE expression END'
if boolean == 0:
print(p[2])

def p_expression_top(p):
'expression : TOP'
print(stack.pop())

def p_expression_term(p):
'expression : term'
p[0] = p[1]

def p_term_number(p):
'term : NUMBER'
global stack
p[0] = p[1]
stack.append(int(p[0]))

def p_error(p):
print("Syntax error!")

parser = yacc.yacc()

# --------------------
# 3. Running It
# --------------------
filename = 'Test.txt' # Replace with your file name
try:
with open(filename, 'r') as file:
s = file.read() # Read the entire content of the file
result = parser.parse(s)
print(result)
except FileNotFoundError:
print(f"File {filename} not found!")
print(stack)
< /code>
Und ist der Inhalt der Datei:
Benutzer 2 mod 0 = Wenn "sogar" Ende sonst "enden" örtes "Ende yell" hi \ n "Forever 3 < /p>
yell yell
im Stapel -Überfluss und Ply, das ist, war es an der Parsing - ich war für das Schreiben von Stack übermut < /p < /p < /p < /p < /p < /p < /p < /p.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post