Python parser.parse combined with if statement

-3

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)```

python
python-3.x
parsing
if-statement
asked on Stack Overflow Aug 28, 2020 by Letsmoe • edited Aug 28, 2020 by thisisjaymehta

1 Answer

0

I now found a solution to the issue, it's definitely not perfect but fits my needs. Here's the code:

def p_statement_expr(t):
    """statement : expression"""
    if line is not None:
        MwGCalc.Calc2(float(t[1]))
    if five is not None:
        MwGCalc.Calc(float(t[1]))

In my MwGCalc script I check for inputs of those two values: five and line

They are both non-important variables, you can name them however you prefer, but if you do so you will have to change them here aswell:

if s is not None:
    for line in s.splitlines ():
        parser.parse ( line )
        line = None
if d is not None:
    for five in d.splitlines ():
        parser.parse ( five )
        five = None

Be sure you don't forget to set both values to None after parsing because it might lead to the first one to appear both times because it's still defined and in use. By changing those values the MwGCalc script can now check by using this code:

def Calc2(t):
    print ( 'Whatup', t )


def Calc(t):
    print ( 'Hi', t )

It defines those variables which I use in my first script, which then refers to them. Then it can be used for further processing by assigning the float value t to another variable which is then used by me for calculating some stuff.

Be sure to don't use 't[1]' because it can't be subscripted after sending to the other script, it will just be 't'.

Here's my script output when using '4 by 4' for s and '4 by 5' for d:

Whatup 16.0
Hi 20.0

Hope it might help someone, helped me a lot.

Have a nice day.

answered on Stack Overflow Aug 29, 2020 by Letsmoe

User contributions licensed under CC BY-SA 3.0