Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
bc8b6611
Commit
bc8b6611
authored
Jun 13, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
The use of brackets is now equal to that of parentheses.
parent
8fbf2e15
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
23 deletions
+21
-23
src/parser.py
src/parser.py
+7
-20
tests/test_parser.py
tests/test_parser.py
+14
-3
No files found.
src/parser.py
View file @
bc8b6611
...
@@ -110,6 +110,7 @@ class Parser(BisonParser):
...
@@ -110,6 +110,7 @@ class Parser(BisonParser):
(
'left'
,
(
'MINUS'
,
'PLUS'
,
'NEG'
)),
(
'left'
,
(
'MINUS'
,
'PLUS'
,
'NEG'
)),
(
'left'
,
(
'INTEGRAL'
,
'DERIVATIVE'
)),
(
'left'
,
(
'INTEGRAL'
,
'DERIVATIVE'
)),
(
'left'
,
(
'TIMES'
,
)),
(
'left'
,
(
'TIMES'
,
)),
(
'left'
,
(
'PRIME'
,
)),
(
'left'
,
(
'DIVIDE'
,
)),
(
'left'
,
(
'DIVIDE'
,
)),
(
'right'
,
(
'FUNCTION'
,
)),
(
'right'
,
(
'FUNCTION'
,
)),
(
'right'
,
(
'POW'
,
)),
(
'right'
,
(
'POW'
,
)),
...
@@ -466,6 +467,7 @@ class Parser(BisonParser):
...
@@ -466,6 +467,7 @@ class Parser(BisonParser):
exp : NUMBER
exp : NUMBER
| IDENTIFIER
| IDENTIFIER
| LPAREN exp RPAREN
| LPAREN exp RPAREN
| LBRACKET exp RBRACKET
| unary
| unary
| binary
| binary
| nary
| nary
...
@@ -480,10 +482,10 @@ class Parser(BisonParser):
...
@@ -480,10 +482,10 @@ class Parser(BisonParser):
if
option
==
1
:
# rule: IDENTIFIER
if
option
==
1
:
# rule: IDENTIFIER
return
Leaf
(
values
[
0
])
return
Leaf
(
values
[
0
])
if
option
==
2
:
# rule: LPAREN exp RPAREN
if
option
in
(
2
,
3
):
# rule: LPAREN exp RPAREN | LBRACKET exp RBRACKET
return
values
[
1
]
return
values
[
1
]
if
3
<=
option
<=
5
:
# rule: unary | binary | nary
if
4
<=
option
<=
6
:
# rule: unary | binary | nary
return
values
[
0
]
return
values
[
0
]
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
...
@@ -495,7 +497,7 @@ class Parser(BisonParser):
...
@@ -495,7 +497,7 @@ class Parser(BisonParser):
| FUNCTION_LPAREN exp RPAREN
| FUNCTION_LPAREN exp RPAREN
| FUNCTION exp
| FUNCTION exp
| DERIVATIVE exp
| DERIVATIVE exp
|
bracket_derivative
|
exp PRIME
| INTEGRAL exp
| INTEGRAL exp
| integral_bounds TIMES exp %prec INTEGRAL
| integral_bounds TIMES exp %prec INTEGRAL
| LBRACKET exp RBRACKET lbnd ubnd
| LBRACKET exp RBRACKET lbnd ubnd
...
@@ -535,8 +537,8 @@ class Parser(BisonParser):
...
@@ -535,8 +537,8 @@ class Parser(BisonParser):
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
return
Node
(
OP_DER
,
values
[
1
],
Leaf
(
values
[
0
][
-
2
]))
return
Node
(
OP_DER
,
values
[
1
],
Leaf
(
values
[
0
][
-
2
]))
if
option
==
4
:
# rule:
bracket_derivative
if
option
==
4
:
# rule:
exp PRIME
return
values
[
0
]
return
Node
(
OP_DER
,
values
[
0
])
if
option
==
5
:
# rule: INTEGRAL exp
if
option
==
5
:
# rule: INTEGRAL exp
fx
,
x
=
find_integration_variable
(
values
[
1
])
fx
,
x
=
find_integration_variable
(
values
[
1
])
...
@@ -599,21 +601,6 @@ class Parser(BisonParser):
...
@@ -599,21 +601,6 @@ class Parser(BisonParser):
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
def
on_bracket_derivative
(
self
,
target
,
option
,
names
,
values
):
"""
bracket_derivative : LBRACKET exp RBRACKET PRIME
| bracket_derivative PRIME
"""
if
option
==
0
:
# rule: LBRACKET exp RBRACKET PRIME
return
Node
(
OP_DER
,
values
[
1
])
if
option
==
1
:
# rule: bracket_derivative PRIME
return
Node
(
OP_DER
,
values
[
0
])
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
def
on_binary
(
self
,
target
,
option
,
names
,
values
):
def
on_binary
(
self
,
target
,
option
,
names
,
values
):
"""
"""
binary : exp PLUS exp
binary : exp PLUS exp
...
...
tests/test_parser.py
View file @
bc8b6611
...
@@ -72,8 +72,10 @@ class TestParser(RulesTestCase):
...
@@ -72,8 +72,10 @@ class TestParser(RulesTestCase):
self
.
assertEqual
(
tree
(
'2(a + b)'
),
tree
(
'2 * (a + b)'
))
self
.
assertEqual
(
tree
(
'2(a + b)'
),
tree
(
'2 * (a + b)'
))
self
.
assertEqual
(
tree
(
'(a + b)2'
),
tree
(
'(a + b) * 2'
))
self
.
assertEqual
(
tree
(
'(a + b)2'
),
tree
(
'(a + b) * 2'
))
self
.
assertEqual
(
tree
(
'(a)(b)'
),
tree
(
'(a) * (b)'
))
self
.
assertEqual
(
tree
(
'(a)(b)'
),
tree
(
'a * b'
))
self
.
assertEqual
(
tree
(
'(a)[b]
\
'
'
),
tree
(
'(a) * [b]
\
'
'
))
self
.
assertEqual
(
tree
(
'(a)[b]'
),
tree
(
'a * b'
))
self
.
assertEqual
(
tree
(
'[a](b)'
),
tree
(
'a * b'
))
self
.
assertEqual
(
tree
(
'[a][b]'
),
tree
(
'a * b'
))
# FIXME: self.assertEqual(tree('(a)|b|'), tree('(a) * |b|'))
# FIXME: self.assertEqual(tree('(a)|b|'), tree('(a) * |b|'))
# FIXME: self.assertEqual(tree('|a|(b)'), tree('|a| * (b)'))
# FIXME: self.assertEqual(tree('|a|(b)'), tree('|a| * (b)'))
...
@@ -94,11 +96,20 @@ class TestParser(RulesTestCase):
...
@@ -94,11 +96,20 @@ class TestParser(RulesTestCase):
self
.
assertEqual
(
tree
(
'sin cos x ^ 2'
),
sin
(
cos
(
x
**
2
)))
self
.
assertEqual
(
tree
(
'sin cos x ^ 2'
),
sin
(
cos
(
x
**
2
)))
self
.
assertEqual
(
tree
(
'sin cos(x) ^ 2'
),
sin
(
cos
(
x
)
**
2
))
self
.
assertEqual
(
tree
(
'sin cos(x) ^ 2'
),
sin
(
cos
(
x
)
**
2
))
def
test_bracket_derivative
(
self
):
def
test_brackets
(
self
):
self
.
assertEqual
(
*
tree
(
'[x], x'
))
self
.
assertEqual
(
*
tree
(
'[x], (x)'
))
self
.
assertEqual
(
*
tree
(
'[x ^ 2], x ^ 2'
))
self
.
assertEqual
(
*
tree
(
'[x ^ 2](x), x ^ 2 * x'
))
def
test_derivative
(
self
):
x
=
tree
(
'x'
)
x
=
tree
(
'x'
)
self
.
assertEqual
(
tree
(
'[x]
\
'
'
),
der
(
x
))
self
.
assertEqual
(
tree
(
'[x]
\
'
'
),
der
(
x
))
self
.
assertEqual
(
tree
(
'x
\
'
'
),
der
(
x
))
self
.
assertEqual
(
tree
(
'[x]
\
'
\
'
'
),
der
(
der
(
x
)))
self
.
assertEqual
(
tree
(
'[x]
\
'
\
'
'
),
der
(
der
(
x
)))
self
.
assertEqual
(
tree
(
'(x)
\
'
\
'
'
),
der
(
der
(
x
)))
self
.
assertEqual
(
tree
(
'x
\
'
\
'
'
),
der
(
der
(
x
)))
def
test_delta_derivative
(
self
):
def
test_delta_derivative
(
self
):
exp
,
x
,
d
=
tree
(
'x ^ 2, x, d'
)
exp
,
x
,
d
=
tree
(
'x ^ 2, x, d'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment