vendredi 28 août 2020

Python parser.parse combined with if statement

I am working on a calculator project, I am using yacc as a parser and as I have 4 different values to parse I wanted to check what was just parsed. I have a strong feeling it won't work but I don't know what to use else. I Only get this error message: Process finished with exit code -1073741571 (0xC00000FD)

If anyone can help me, I would very much appreciate it.

This is my code, the piece of code I'm talking about would be:

if parser.parse(C1):

t[0] = float(C1)

MwGCalc.MwGCalc(C1)

MwGCalc is this right here, it currently only works for 4*4 but it's just for testing purposes and not very well thought out:

print("Content-type: text/html\n\n")
import numpy as np
import cgitb
import cgi



cgitb.enable()
form = cgi.FieldStorage()
C1 = form.getvalue('C1')
# Defining Variables


#Actual Calculating
if C1 == 256:
    p = C1
    print(p)

And this right here is the Code for the calculator:

import numpy as np
import MwGCalc
#!C:\Users\Letsmoe\Anaconda3\python.exe
print("Content-type: text/html\n\n")
tokens = (
    'NAME', 'NUMBER',
    'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'EQUALS',
    'LPAREN', 'RPAREN', 'POWER', 'FUNC',
)

# Tokens

t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUALS = r'='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
t_POWER = r'\^'
t_FUNC = r'(sin)|(cos)|(tan)|(ln)|(log)|(sqrt)'


def t_NUMBER(t):
    r'\d+'  # [0-9]+
    try:
        t.value = int(t.value)
    except ValueError:
        print("Integer value too large %d", t.value)
        t.value = 0
    return t


# Ignored characters
t_ignore = r" \t\r"


def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")


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


# Build the lexer

lexer = lex.lex()

# Parsing rules

precedence = (
    ('left', 'PLUS', 'MINUS'),
    ('left', 'TIMES', 'DIVIDE'),
    ('left', 'POWER'),
    ('right', 'UMINUS'),
)

# dictionary of names
names = {}


def p_statement_assign(t):
    'statement : NAME EQUALS expression'
    names[t[1]] = t[3]


def p_statement_expr(t):
    'statement : expression'
    if parser.parse(C1):
        t[0] = float(C1)
        MwGCalc.MwgCalc(C1)


def p_expression_binop(t):
    '''expression : expression PLUS expression
                  | expression MINUS expression
                  | expression TIMES expression
                  | expression DIVIDE expression
                  | expression POWER expression'''
    if t[2] == '*':
        t[0] = t[3]**t[1]


def p_expression_uminus(t):
    'expression : MINUS expression %prec UMINUS'
    t[0] = -t[2]


def p_expression_func(t):
    'expression : FUNC LPAREN expression RPAREN'
    if t[1] == 'sin':
        t[0] = np.sin(t[3])
    elif t[1] == 'cos':
        t[0] = np.cos(t[3])
    elif t[1] == 'log':
        t[0] = (np.log(t[3])) / (np.log(10))
    elif t[1] == 'sqrt':
        t[0] = np.sqrt(t[3])
    elif t[1] == 'ln':
        t[0] = (np.log(t[3]))


def p_expression_group(t):
    'expression : LPAREN expression RPAREN'
    t[0] = t[2]


def p_expression_number(t):
    'expression : NUMBER'
    t[0] = t[1]


def p_expression_name(t):
    'expression : NAME'
    try:
        t[0] = names[t[1]]
    except LookupError:
        print("Undefined name '%s'" % t[1])
        t[0] = 0


def p_error(t):
    print("Syntax error at '%s'" % t.value)


if __name__ == "__main__":  # HTML is following
    print()  # blank line, end of headers
    print("<TITLE>CGI script output</TITLE>")

    import ply.yacc as yacc

    parser = yacc.yacc()
    # while True:
    #    try:
    #        s = input('calc > ')   # Use raw_input on Python 2
    #    except EOFError:
    #          break
    #    parser.parse(s)

    import cgi
    import cgitb

    cgitb.enable()
    form = cgi.FieldStorage()
    C1 = form.getvalue('C1')
    if C1 is not None:
        C1 = str(C1)
    C2 = form.getvalue('C2')
    if C2 is not None:
        C2 = str(C2)
    C3 = form.getvalue('C3')
    if C3 is not None:
        C3 = str(C3)
    C4 = form.getvalue('C4')
    if C4 is not None:
        C4 = str(C4)

    for C1 in C1.splitlines():
        parser.parse(C1)
    for C2 in C2.splitlines():
        parser.parse(C2)
    for C3 in C3.splitlines():
        parser.parse(C3)
    for C4 in C4.splitlines():
        parser.parse(C4)```

Aucun commentaire:

Enregistrer un commentaire