[FEM] add missing parser handling of '^' character (#6966)

* [FEM] add missing parser handling of '^' character

- it is common to use equations like "x^2" but one currently get only an error
- this PR fixes this by adding parsing support for the '^' character as power function

This way is it now also possible to calculate a root by e.g. "x^(0.5).
This commit is contained in:
Uwe
2022-06-05 13:13:49 +02:00
committed by GitHub
parent b0878c6225
commit d285a8e397

View File

@@ -37,6 +37,7 @@ tokens = (
'LPAREN',
'RPAREN',
'COMMENT',
'POWER'
)
# Tokens
@@ -48,6 +49,7 @@ t_DIVIDE = r'/'
t_EQUALS = r'='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_POWER = r'\^'
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
@@ -91,7 +93,8 @@ lex.lex()
precedence = (
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE'),
('right', 'UMINUS'),
('left', 'POWER'),
('right', 'UMINUS')
)
# dictionary of names (for storing variables)
@@ -112,11 +115,13 @@ def p_expression_binop(p):
'''expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression'''
| expression DIVIDE expression
| expression POWER expression'''
if p[2] == '+' : p[0] = p[1] + p[3]
elif p[2] == '-': p[0] = p[1] - p[3]
elif p[2] == '*': p[0] = p[1] * p[3]
elif p[2] == '/': p[0] = p[1] / p[3]
elif p[2] == '^': p[0] = p[1] ** p[3]
def p_expression_uminus(p):