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
6503a97a
Commit
6503a97a
authored
Mar 14, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Solved preprocessor inserting multiplication signs in keywords.
parent
3d713049
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
18 deletions
+29
-18
src/parser.py
src/parser.py
+27
-17
tests/test_parser.py
tests/test_parser.py
+2
-1
No files found.
src/parser.py
View file @
6503a97a
...
@@ -141,6 +141,10 @@ class Parser(BisonParser):
...
@@ -141,6 +141,10 @@ class Parser(BisonParser):
import
re
import
re
# Replace known keywords with escape sequences.
for
i
,
keyword
in
enumerate
(
Parser
.
words
):
data
=
re
.
sub
(
keyword
,
chr
(
i
),
data
,
flags
=
re
.
I
)
# TODO: remove this quick preprocessing hack. This hack enables
# TODO: remove this quick preprocessing hack. This hack enables
# concatenated expressions, since the grammar currently does not
# concatenated expressions, since the grammar currently does not
# support those. This workaround will replace:
# support those. This workaround will replace:
...
@@ -151,20 +155,24 @@ class Parser(BisonParser):
...
@@ -151,20 +155,24 @@ class Parser(BisonParser):
# - "4a" with "4*a".
# - "4a" with "4*a".
# - "a4" with "a^4".
# - "a4" with "a^4".
pattern
=
(
'(?:(
\
))
\
s*(
\
()
'
# match: )( result: ) * (
pattern
=
(
'(?:(
\
))
\
s*(
\
()
'
# )( -> ) * (
+ '
|
([
a
-
z0
-
9
])
\
s
*
(
\
()
' # match: a( result: a * (
+ '
|
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z0
-
9
])
\
s
*
(
\
()
' # a( -> a * (
+ '
|
(
\
))
\
s
*
([
a
-
z0
-
9
])
' # match: )a result: ) * a
+ '
|
(
\
))
\
s
*
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z0
-
9
])
' # )a -> ) * a
+ '
|
([
a
-
z
])
\
s
*
([
a
-
z
]
+
)
' # match: ab result: a * b
+ '
|
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z
])
\
s
*
'
+ '
|
([
0
-
9
])
\
s
*
([
a
-
z
])
' # match: 4a result: 4 * a
+'
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z
]
+
)
' # ab -> a * b
+ '
|
([
a
-
z
])
\
s
*
([
0
-
9
])
' # match: a4 result: a ^ 4
+ '
|
([
0
-
9
])
\
s
*
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z
])
' # 4a -> 4 * a
+ '
|
([
0
-
9
])
\
s
+
([
0
-
9
]))
') # match: 4 4 result: 4 * 4
+ '
|
([
\
x00
-
\
x09
\
x0b
-
\
x19a
-
z
])
\
s
*
([
0
-
9
])
' # a4 -> a ^ 4
+ '
|
([
0
-
9
])
\
s
+
([
0
-
9
]))
' # 4 4 -> 4 * 4
)
def preprocess_data(match):
def preprocess_data(match):
left, right = filter(None, match.groups())
left, right = filter(None, match.groups())
# Filter words (otherwise they will be preprocessed as well)
# Make sure there are no multiplication and exponentiation signs
if left + right in Parser.words:
# inserted between a function and its argument(s): "sin x" should
return left + right
# not be written as "sin*x", because that is bogus.
if ord(left) <= 0x9 or 0x0b <= ord(left) <= 0x19:
return left + '
' + right
# If all characters on the right are numbers. e.g. "a4", the
# If all characters on the right are numbers. e.g. "a4", the
# expression implies exponentiation. Make sure ")4" is not
# expression implies exponentiation. Make sure ")4" is not
...
@@ -180,18 +188,20 @@ class Parser(BisonParser):
...
@@ -180,18 +188,20 @@ class Parser(BisonParser):
data_before
=
data
data_before
=
data
# Iteratively replace all matches.
# Iteratively replace all matches.
while
True
:
i
=
0
data_after
=
re
.
sub
(
pattern
,
preprocess_data
,
data
)
if
data
==
data_after
:
while
i
<
len
(
data
):
break
data
=
data
[:
i
]
+
re
.
sub
(
pattern
,
preprocess_data
,
data
[
i
:])
i
+=
1
data
=
data_after
# Replace escape sequences with original keywords.
for
i
,
keyword
in
enumerate
(
Parser
.
words
):
data
=
data
.
replace
(
chr
(
i
),
keyword
)
if
self
.
verbose
and
data_before
!=
data
_after
:
# pragma: nocover
if
self
.
verbose
and
data_before
!=
data
:
# pragma: nocover
print
'hook_read_after() modified the input data:'
print
'hook_read_after() modified the input data:'
print
'before:'
,
repr
(
data_before
)
print
'before:'
,
repr
(
data_before
)
print
'after :'
,
repr
(
data
_after
)
print
'after :'
,
repr
(
data
)
return
data
return
data
...
...
tests/test_parser.py
View file @
6503a97a
...
@@ -50,7 +50,8 @@ class TestParser(unittest.TestCase):
...
@@ -50,7 +50,8 @@ class TestParser(unittest.TestCase):
root
,
x
=
tree
(
'sin x, x'
)
root
,
x
=
tree
(
'sin x, x'
)
self
.
assertEqual
(
root
,
sin
(
x
))
self
.
assertEqual
(
root
,
sin
(
x
))
self
.
assertEqual
(
tree
(
'sin x ^ 2'
),
sin
(
x
)
**
2
)
#FIXME: self.assertEqual(tree('sin x ^ 2'), sin(x ** 2))
self
.
assertEqual
(
tree
(
'sin 2 x'
),
sin
(
2
)
*
x
)
self
.
assertEqual
(
tree
(
'sin(x) ^ 2'
),
sin
(
x
)
**
2
)
self
.
assertEqual
(
tree
(
'sin(x) ^ 2'
),
sin
(
x
)
**
2
)
self
.
assertEqual
(
tree
(
'sin (x) ^ 2'
),
sin
(
x
)
**
2
)
self
.
assertEqual
(
tree
(
'sin (x) ^ 2'
),
sin
(
x
)
**
2
)
self
.
assertEqual
(
tree
(
'sin(x ^ 2)'
),
sin
(
x
**
2
))
self
.
assertEqual
(
tree
(
'sin(x ^ 2)'
),
sin
(
x
**
2
))
...
...
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