Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
bc8b6611
Commit
bc8b6611
authored
12 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
The use of brackets is now equal to that of parentheses.
parent
8fbf2e15
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/parser.py
+7
-20
7 additions, 20 deletions
src/parser.py
tests/test_parser.py
+14
-3
14 additions, 3 deletions
tests/test_parser.py
with
21 additions
and
23 deletions
src/parser.py
+
7
−
20
View file @
bc8b6611
...
...
@@ -110,6 +110,7 @@ class Parser(BisonParser):
(
'
left
'
,
(
'
MINUS
'
,
'
PLUS
'
,
'
NEG
'
)),
(
'
left
'
,
(
'
INTEGRAL
'
,
'
DERIVATIVE
'
)),
(
'
left
'
,
(
'
TIMES
'
,
)),
(
'
left
'
,
(
'
PRIME
'
,
)),
(
'
left
'
,
(
'
DIVIDE
'
,
)),
(
'
right
'
,
(
'
FUNCTION
'
,
)),
(
'
right
'
,
(
'
POW
'
,
)),
...
...
@@ -466,6 +467,7 @@ class Parser(BisonParser):
exp : NUMBER
| IDENTIFIER
| LPAREN exp RPAREN
| LBRACKET exp RBRACKET
| unary
| binary
| nary
...
...
@@ -480,10 +482,10 @@ class Parser(BisonParser):
if
option
==
1
:
# rule: IDENTIFIER
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
]
if
3
<=
option
<=
5
:
# rule: unary | binary | nary
if
4
<=
option
<=
6
:
# rule: unary | binary | nary
return
values
[
0
]
raise
BisonSyntaxError
(
'
Unsupported option %d in target
"
%s
"
.
'
...
...
@@ -495,7 +497,7 @@ class Parser(BisonParser):
| FUNCTION_LPAREN exp RPAREN
| FUNCTION exp
| DERIVATIVE exp
|
bracket_derivative
|
exp PRIME
| INTEGRAL exp
| integral_bounds TIMES exp %prec INTEGRAL
| LBRACKET exp RBRACKET lbnd ubnd
...
...
@@ -535,8 +537,8 @@ class Parser(BisonParser):
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
return
Node
(
OP_DER
,
values
[
1
],
Leaf
(
values
[
0
][
-
2
]))
if
option
==
4
:
# rule:
bracket_derivative
return
values
[
0
]
if
option
==
4
:
# rule:
exp PRIME
return
Node
(
OP_DER
,
values
[
0
]
)
if
option
==
5
:
# rule: INTEGRAL exp
fx
,
x
=
find_integration_variable
(
values
[
1
])
...
...
@@ -599,21 +601,6 @@ class Parser(BisonParser):
raise
BisonSyntaxError
(
'
Unsupported option %d in target
"
%s
"
.
'
%
(
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
):
"""
binary : exp PLUS exp
...
...
This diff is collapsed.
Click to expand it.
tests/test_parser.py
+
14
−
3
View file @
bc8b6611
...
...
@@ -72,8 +72,10 @@ class TestParser(RulesTestCase):
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)
'
),
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)'))
...
...
@@ -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
))
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
'
)
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
)))
def
test_delta_derivative
(
self
):
exp
,
x
,
d
=
tree
(
'
x ^ 2, x, d
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment