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
974545c7
Commit
974545c7
authored
12 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Removed bug in preprocessor that caused 'pi*a' to occur as 'pi a'.
parent
47ed0625
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
TODO
+0
-2
0 additions, 2 deletions
TODO
src/parser.py
+17
-23
17 additions, 23 deletions
src/parser.py
tests/test_parser.py
+3
-0
3 additions, 0 deletions
tests/test_parser.py
with
20 additions
and
25 deletions
TODO
+
0
−
2
View file @
974545c7
...
...
@@ -58,8 +58,6 @@ Division of 0 by 1 reduces to 0.
- Line printer: 1 / (n + n)x -> 1 / (n + n) * x
- Parser: 'apia' -> 'aa'
- Unit tests for strategy.
- Parser: add unit tests for operator associativity (derivatives/integrals).
...
...
This diff is collapsed.
Click to expand it.
src/parser.py
+
17
−
23
View file @
974545c7
...
...
@@ -179,32 +179,24 @@ class Parser(BisonParser):
# Replace known keywords with escape sequences.
words
=
list
(
self
.
__class__
.
words
)
words
.
insert
(
10
,
'
\n
'
)
words
.
insert
(
13
,
'
\r
'
)
for
i
,
keyword
in
enumerate
(
words
):
# FIXME: Why case-insensitivity?
data
=
re
.
sub
(
keyword
,
chr
(
i
),
data
,
flags
=
re
.
I
)
# TODO: remove this quick preprocessing hack. This hack enables
# concatenated expressions, since the grammar currently does not
# support those. This workaround will replace:
# - ")(" with ")*(".
# - "a(" with "a*(".
# - ")a" with ")*a".
# - "ab" with "a*b".
# - "4a" with "4*a".
# - "a4" with "a^4".
pattern
=
(
'
(?:(\))\s*([([])
'
# )( -> ) * (
# )[ -> ) * [
+
'
|([
\x00
-
\x09\x0b
-
\x19
a-z0-9])\s*([([])
'
# a( -> a * (
# a[ -> a * [
+
'
|(\))\s*([
\x00
-
\x09\x0b
-
\x19
a-z0-9])
'
# )a -> ) * a
+
'
|([
\x00
-
\x09\x0b
-
\x19
a-z])\s*
'
+
'
([
\x00
-
\x09\x0b
-
\x19
a-z0-9])
'
# ab -> a * b
+
'
|(\|)(\|)
'
# || -> | * |
+
'
|([0-9])\s*([
\x00
-
\x09\x0b
-
\x19
a-z])
'
# 4a -> 4 * a
+
'
|([
\x00
-
\x09\x0b
-
\x19
a-z])([0-9])
'
# a4 -> a ^ 4
+
'
|([
\x00
-
\x09\x0b
-
\x19
0-9])(\s+[0-9]))
'
# 4 4 -> 4 * 4
rsv
=
'
\x00
-
\x09\x0b
-
\x0c\x0e
-
\x19
'
pattern
=
(
'
(?:(\))\s*([([])
'
# )( -> ) * (
# )[ -> ) * [
+
'
|([
'
+
rsv
+
'
a-z0-9])\s*([([])
'
# a( -> a * (
# a[ -> a * [
+
'
|(\))\s*([
'
+
rsv
+
'
a-z0-9])
'
# )a -> ) * a
+
'
|([
'
+
rsv
+
'
a-z])\s*
'
+
'
([
'
+
rsv
+
'
a-z0-9])
'
# ab -> a * b
+
'
|(\|)(\|)
'
# || -> | * |
+
'
|([0-9])\s*([
'
+
rsv
+
'
a-z])
'
# 4a -> 4 * a
+
'
|([
'
+
rsv
+
'
a-z])([0-9])
'
# a4 -> a ^ 4
+
'
|([
'
+
rsv
+
'
0-9])(\s+[0-9]))
'
# 4 4 -> 4 * 4
)
def
preprocess_data
(
match
):
...
...
@@ -213,7 +205,9 @@ class Parser(BisonParser):
# Make sure there are no multiplication and exponentiation signs
# inserted between a function and its argument(s): "sin x" should
# not be written as "sin*x", because that is bogus.
if
ord
(
left
)
<=
0x9
or
0x0b
<=
ord
(
left
)
<=
0x19
:
# Bugfix: omit 0x0e (pi) to prevent "pi a" (should be "pi*a")
if
ord
(
left
)
<=
0x9
or
0x0b
<=
ord
(
left
)
<=
0x0d
\
or
0x0f
<=
ord
(
left
)
<=
0x19
:
return
left
+
'
'
+
right
# If all characters on the right are numbers. e.g. "a4", the
...
...
@@ -223,7 +217,7 @@ class Parser(BisonParser):
# return '%s^%s' % (left, right)
# match: ab | abc | abcd (where left = "a")
return
'
*
'
.
join
([
left
]
+
list
(
r
e
.
sub
(
r
'
^ +
'
,
''
,
right
)))
return
'
*
'
.
join
([
left
]
+
list
(
r
ight
.
lstrip
(
)))
if
self
.
verbose
:
# pragma: nocover
data_before
=
data
...
...
This diff is collapsed.
Click to expand it.
tests/test_parser.py
+
3
−
0
View file @
974545c7
...
...
@@ -187,3 +187,6 @@ class TestParser(RulesTestCase):
def
test_precedence
(
self
):
self
.
assertEqual
(
tree
(
'
ab / cd
'
),
tree
(
'
a * (b / c) * d
'
))
def
test_pi_multiplication_sign
(
self
):
self
.
assertEqual
(
tree
(
'
apia
'
),
tree
(
'
a * pi * a
'
))
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