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
2b325ca8
Commit
2b325ca8
authored
Mar 12, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added 'delta' syntax for derivatives.
parent
41f07554
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
17 deletions
+46
-17
TODO
TODO
+2
-0
src/parser.py
src/parser.py
+37
-16
tests/test_parser.py
tests/test_parser.py
+7
-1
No files found.
TODO
View file @
2b325ca8
...
@@ -101,3 +101,5 @@ Division of 0 by 1 reduces to 0.
...
@@ -101,3 +101,5 @@ Division of 0 by 1 reduces to 0.
ERROR: 41.14-41.15: "syntax error, unexpected RPAREN" near ")".
ERROR: 41.14-41.15: "syntax error, unexpected RPAREN" near ")".
- No matches for sin(pi), sin(2pi), sin(4pi), etc...
- No matches for sin(pi), sin(2pi), sin(4pi), etc...
- Line generator: 'der(f(x), x)' -> 'd/dx f(x)'
src/parser.py
View file @
2b325ca8
...
@@ -59,7 +59,7 @@ class Parser(BisonParser):
...
@@ -59,7 +59,7 @@ class Parser(BisonParser):
# of tokens of the lex script.
# of tokens of the lex script.
tokens
=
[
'NUMBER'
,
'IDENTIFIER'
,
'NEWLINE'
,
'QUIT'
,
'RAISE'
,
'GRAPH'
,
tokens
=
[
'NUMBER'
,
'IDENTIFIER'
,
'NEWLINE'
,
'QUIT'
,
'RAISE'
,
'GRAPH'
,
'LPAREN'
,
'RPAREN'
,
'FUNCTION'
,
'LBRACKET'
,
'RBRACKET'
,
\
'LPAREN'
,
'RPAREN'
,
'FUNCTION'
,
'LBRACKET'
,
'RBRACKET'
,
\
'APOSTROPH'
]
\
'APOSTROPH'
,
'DELTA'
]
\
+
filter
(
lambda
t
:
t
!=
'FUNCTION'
,
TOKEN_MAP
.
values
())
+
filter
(
lambda
t
:
t
!=
'FUNCTION'
,
TOKEN_MAP
.
values
())
# ------------------------------
# ------------------------------
...
@@ -154,11 +154,11 @@ class Parser(BisonParser):
...
@@ -154,11 +154,11 @@ class Parser(BisonParser):
# - "a4" with "a^4".
# - "a4" with "a^4".
pattern
=
(
'(?:(
\
))
\
s*(
\
()
'
# match: )( result: ) * (
pattern
=
(
'(?:(
\
))
\
s*(
\
()
'
# match: )( result: ) * (
+ '
|
([
a
-
z0
-
9
])
\
s
*
(
\
()
'
# match: a( result: a * (
+ '
|
([
a
-
ce
-
z0
-
9
])
\
s
*
(
\
()
'
# match: a( result: a * (
+ '
|
(
\
))
\
s
*
([
a
-
z0
-
9
])
'
# match: )a result: ) * a
+ '
|
(
\
))
\
s
*
([
a
-
ce
-
z0
-
9
])
'
# match: )a result: ) * a
+ '
|
([
a
-
z
])
\
s
*
([
a
-
z
]
+
)
'
# match: ab result: a * b
+ '
|
([
a
-
ce
-
z
])
\
s
*
([
a
-
ce
-
z
]
+
)
'
# match: ab result: a * b
+ '
|
([
0
-
9
])
\
s
*
([
a
-
z
])
'
# match: 4a result: 4 * a
+ '
|
([
0
-
9
])
\
s
*
([
a
-
ce
-
z
])
'
# match: 4a result: 4 * a
+ '
|
([
a
-
z
])
\
s
*
([
0
-
9
])
'
# match: a4 result: a ^ 4
+ '
|
([
a
-
ce
-
z
])
\
s
*
([
0
-
9
])
'
# match: a4 result: a ^ 4
+ '
|
([
0
-
9
])
\
s
+
([
0
-
9
]))
') # match: 4 4 result: 4 * 4
+ '
|
([
0
-
9
])
\
s
+
([
0
-
9
]))
') # match: 4 4 result: 4 * 4
def preprocess_data(match):
def preprocess_data(match):
...
@@ -333,7 +333,8 @@ class Parser(BisonParser):
...
@@ -333,7 +333,8 @@ class Parser(BisonParser):
| unary
| unary
| binary
| binary
| nary
| nary
| derivative
| bracket_derivative
| delta_derivative
"""
"""
# | concat
# | concat
...
@@ -348,16 +349,17 @@ class Parser(BisonParser):
...
@@ -348,16 +349,17 @@ class Parser(BisonParser):
if
option
==
2
:
# rule: LPAREN exp RPAREN
if
option
==
2
:
# rule: LPAREN exp RPAREN
return
values
[
1
]
return
values
[
1
]
if
option
in
[
3
,
4
,
5
,
6
]:
# rule: unary | binary | nary | derivative
if
3
<=
option
<=
7
:
# rule: unary | binary | nary
# | bracket_derivative | delta_derivative
return
values
[
0
]
return
values
[
0
]
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_derivative
(
self
,
target
,
option
,
names
,
values
):
def
on_
bracket_
derivative
(
self
,
target
,
option
,
names
,
values
):
"""
"""
derivative : LBRACKET exp RBRACKET APOSTROPH
bracket_
derivative : LBRACKET exp RBRACKET APOSTROPH
|
derivative APOSTROPH
| bracket_
derivative APOSTROPH
"""
"""
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DERIV
][
0
]
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DERIV
][
0
]
...
@@ -365,12 +367,29 @@ class Parser(BisonParser):
...
@@ -365,12 +367,29 @@ class Parser(BisonParser):
if
option
==
0
:
# rule: LBRACKET exp RBRACKET APOSTROPH
if
option
==
0
:
# rule: LBRACKET exp RBRACKET APOSTROPH
return
Node
(
op
,
values
[
1
])
return
Node
(
op
,
values
[
1
])
if
option
==
1
:
# rule: derivative APOSTROPH
if
option
==
1
:
# rule:
bracket_
derivative APOSTROPH
return
Node
(
op
,
values
[
0
])
return
Node
(
op
,
values
[
0
])
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_delta_derivative
(
self
,
target
,
option
,
names
,
values
):
"""
delta_derivative : DELTA DIVIDE DELTA IDENTIFIER TIMES exp
| DELTA LPAREN exp RPAREN DIVIDE DELTA IDENTIFIER
"""
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DERIV
][
0
]
if
option
==
0
:
# rule: DELTA DIVIDE DELTA IDENTIFIER TIMES exp
return
Node
(
op
,
values
[
5
],
Leaf
(
values
[
3
]))
if
option
==
1
:
# rule: DELTA LPAREN exp RPAREN DIVIDE DELTA IDENTIFIER
return
Node
(
op
,
values
[
2
],
Leaf
(
values
[
6
]))
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
def
on_unary
(
self
,
target
,
option
,
names
,
values
):
def
on_unary
(
self
,
target
,
option
,
names
,
values
):
"""
"""
unary : MINUS exp %prec NEG
unary : MINUS exp %prec NEG
...
@@ -440,8 +459,9 @@ class Parser(BisonParser):
...
@@ -440,8 +459,9 @@ class Parser(BisonParser):
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
# -----------------------------------------
# -----------------------------------------
#
PI
and operator tokens
#
Special characters
and operator tokens
# -----------------------------------------
# -----------------------------------------
#delta = '"%s"%s' % (DELTA, ' ' * (8 - len(DELTA)))
operators
=
'"%s"%s{ returntoken(IDENTIFIER); }
\
n
'
\
operators
=
'"%s"%s{ returntoken(IDENTIFIER); }
\
n
'
\
%
(
PI
,
' '
*
(
8
-
len
(
PI
)))
%
(
PI
,
' '
*
(
8
-
len
(
PI
)))
functions
=
[]
functions
=
[]
...
@@ -491,8 +511,9 @@ class Parser(BisonParser):
...
@@ -491,8 +511,9 @@ class Parser(BisonParser):
%%
%%
"d" { returntoken(DELTA); }
[0-9]+"."?[0-9]* { returntoken(NUMBER); }
[0-9]+"."?[0-9]* { returntoken(NUMBER); }
[a-zA-Z] { returntoken(IDENTIFIER); }
[a-
ce-
zA-Z] { returntoken(IDENTIFIER); }
"(" { returntoken(LPAREN); }
"(" { returntoken(LPAREN); }
")" { returntoken(RPAREN); }
")" { returntoken(RPAREN); }
"""
+
operators
+
r"""
"""
+
operators
+
r"""
...
...
tests/test_parser.py
View file @
2b325ca8
...
@@ -59,8 +59,14 @@ class TestParser(unittest.TestCase):
...
@@ -59,8 +59,14 @@ class TestParser(unittest.TestCase):
self
.
assertEqual
(
tree
(
'sin cos x'
),
sin
(
cos
(
x
)))
self
.
assertEqual
(
tree
(
'sin cos x'
),
sin
(
cos
(
x
)))
self
.
assertEqual
(
tree
(
'sin cos x ^ 2'
),
sin
(
cos
(
x
))
**
2
)
self
.
assertEqual
(
tree
(
'sin cos x ^ 2'
),
sin
(
cos
(
x
))
**
2
)
def
test_derivative
(
self
):
def
test_
bracket_
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
(
der
(
x
)))
self
.
assertEqual
(
tree
(
'[x]
\
'
\
'
'
),
der
(
der
(
x
)))
def
test_bracket_derivative
(
self
):
exp
,
x
=
tree
(
'x ^ 2, x'
)
self
.
assertEqual
(
tree
(
'd/dx x ^ 2'
),
der
(
exp
,
x
))
self
.
assertEqual
(
tree
(
'd(x ^ 2)/dx'
),
der
(
exp
,
x
))
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