[Python] Forever Loop Implementing Custom Language

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    Hi so I'm trying to make my own language using python and PLY I have done conditional statements but the forever loop it does the command once and then freezes its very weird Code:

    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)


    And Heres The Content Of File: user 2 MOD 0 = if say "Even" end else say "Odd" end yell "Hi\n" forever 3

    -Thank You Im using python and ply I believe it's the parsing - I was forced to write this by stack overflow

    Continue reading...

Compartilhe esta Página