summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Shetye2018-06-13 10:51:38 +0530
committerSunil Shetye2018-06-13 10:51:38 +0530
commit53be10ce9fee3e15452ac3ba275b2c4cd4bb94a6 (patch)
tree0e74bad27cbe6a5a9fade5691654dceaca79de44
parent360c657b53a7b53018a4576a806f6bef92e1e14d (diff)
downloadsci2js-53be10ce9fee3e15452ac3ba275b2c4cd4bb94a6.tar.gz
sci2js-53be10ce9fee3e15452ac3ba275b2c4cd4bb94a6.tar.bz2
sci2js-53be10ce9fee3e15452ac3ba275b2c4cd4bb94a6.zip
split addition and multiplication operators
-rwxr-xr-xsci2jslex.py18
-rwxr-xr-xsci2jsyacc.py22
2 files changed, 31 insertions, 9 deletions
diff --git a/sci2jslex.py b/sci2jslex.py
index 931a7e12..83eadeab 100755
--- a/sci2jslex.py
+++ b/sci2jslex.py
@@ -64,7 +64,8 @@ tokens = [
'NUMBER',
'OPENBRACKET',
'OPENSQBRACKET',
- 'OPERATOR',
+ 'MULTIPLICATION',
+ 'ADDITION',
'PREVAR',
'QSTRING',
'SEMICOLON',
@@ -86,7 +87,7 @@ def t_COMMENT(t):
pass
def t_NUMBER(t):
- r'-?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
+ r'(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
global afterarray
afterarray = False
t.state = 'NUMBER'
@@ -135,11 +136,18 @@ def t_DOT(t):
t.state = 'DOT'
return t
-def t_OPERATOR(t):
- r'[+\-*/^\\]'
+def t_MULTIPLICATION(t):
+ r'[*/^\\]'
global afterarray
afterarray = False
- t.state = 'OPERATOR'
+ t.state = 'MULTIPLICATION'
+ return t
+
+def t_ADDITION(t):
+ r'[+\-]'
+ global afterarray
+ afterarray = False
+ t.state = 'ADDITION'
return t
def t_COMMA(t):
diff --git a/sci2jsyacc.py b/sci2jsyacc.py
index 77b7e050..e174aa20 100755
--- a/sci2jsyacc.py
+++ b/sci2jsyacc.py
@@ -5,6 +5,12 @@ import ply.yacc as yacc
from sci2jslex import tokens
+precedence = (
+ ('left', 'ADDITION'),
+ ('left', 'MULTIPLICATION'),
+ ('right', 'UNARYADDITION'),
+)
+
start = 'statementblock'
# define statementblock
@@ -66,8 +72,8 @@ def p_termarraylist_termarraylist_semicolon_expression(p):
p[0] = str(p[1]) + ',' + str(p[3])
def p_termarraylist_termarraylist_expression(p):
- '''termarraylist : termarraylist expression
- | expression expression'''
+ '''termarraylist : termarraylist term
+ | term term'''
p[0] = str(p[1]) + ',' + str(p[2])
# end define termarraylist
@@ -87,10 +93,18 @@ def p_expression_term_transpose(p):
'expression : term TRANSPOSE'
p[0] = 'transpose(' + str(p[1]) + ')'
-def p_expression_operator_term(p):
- 'expression : expression OPERATOR term'
+def p_expression_expression_multiplication_expression(p):
+ 'expression : expression MULTIPLICATION expression'
+ p[0] = str(p[1]) + str(p[2]) + str(p[3])
+
+def p_expression_expression_addition_expression(p):
+ 'expression : expression ADDITION expression'
p[0] = str(p[1]) + str(p[2]) + str(p[3])
+def p_expression_addition_term(p):
+ 'expression : ADDITION term %prec UNARYADDITION'
+ p[0] = str(p[1]) + str(p[2])
+
def p_expression_term(p):
'expression : term'
p[0] = str(p[1])