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
9e395dc2
Commit
9e395dc2
authored
Mar 08, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added new syntax for derivatives to the parser.
parent
30700670
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
5 deletions
+35
-5
src/parser.py
src/parser.py
+26
-3
tests/test_parser.py
tests/test_parser.py
+9
-2
No files found.
src/parser.py
View file @
9e395dc2
...
...
@@ -15,7 +15,8 @@ from pybison import BisonParser, BisonSyntaxError
from
graph_drawing.graph
import
generate_graph
from
node
import
ExpressionNode
as
Node
,
ExpressionLeaf
as
Leaf
,
OP_MAP
,
\
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
OP_DIV
,
Scope
,
PI
OP_DERIV
,
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
OP_DIV
,
\
Scope
,
PI
from
rules
import
RULES
from
strategy
import
pick_suggestion
from
possibilities
import
filter_duplicates
,
apply_suggestion
...
...
@@ -57,7 +58,8 @@ class Parser(BisonParser):
# TODO: add a runtime check to verify that this token list match the list
# of tokens of the lex script.
tokens
=
[
'NUMBER'
,
'IDENTIFIER'
,
'NEWLINE'
,
'QUIT'
,
'RAISE'
,
'GRAPH'
,
'LPAREN'
,
'RPAREN'
,
'FUNCTION'
]
\
'LPAREN'
,
'RPAREN'
,
'FUNCTION'
,
'LBRACKET'
,
'RBRACKET'
,
\
'APOSTROPH'
]
\
+
filter
(
lambda
t
:
t
!=
'FUNCTION'
,
TOKEN_MAP
.
values
())
# ------------------------------
...
...
@@ -331,6 +333,7 @@ class Parser(BisonParser):
| unary
| binary
| nary
| derivative
"""
# | concat
...
...
@@ -345,12 +348,29 @@ class Parser(BisonParser):
if
option
==
2
:
# rule: LPAREN exp RPAREN
return
values
[
1
]
if
option
in
[
3
,
4
,
5
]:
# rule: unary | binary | nary
if
option
in
[
3
,
4
,
5
,
6
]:
# rule: unary | binary | nary | derivative
return
values
[
0
]
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
def
on_derivative
(
self
,
target
,
option
,
names
,
values
):
"""
derivative : LBRACKET exp RBRACKET APOSTROPH
| derivative APOSTROPH
"""
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DERIV
][
0
]
if
option
==
0
:
# rule: LBRACKET exp RBRACKET APOSTROPH
return
Node
(
op
,
values
[
1
])
if
option
==
1
:
# rule: derivative APOSTROPH
return
Node
(
op
,
values
[
0
])
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
def
on_unary
(
self
,
target
,
option
,
names
,
values
):
"""
unary : MINUS exp %prec NEG
...
...
@@ -482,6 +502,9 @@ class Parser(BisonParser):
[ \t\v\f] { }
[\n] { yycolumn = 0; returntoken(NEWLINE); }
"[" { returntoken(LBRACKET); }
"]" { returntoken(RBRACKET); }
"'" { returntoken(APOSTROPH); }
. { printf("unknown char %c ignored.\n", yytext[0]); }
%%
...
...
tests/test_parser.py
View file @
9e395dc2
...
...
@@ -6,6 +6,7 @@ from src.node import ExpressionNode as Node, ExpressionLeaf as Leaf
from
tests.parser
import
ParserWrapper
,
run_expressions
,
line
,
graph
from
tests.rulestestcase
import
tree
from
src.rules.goniometry
import
sin
,
cos
from
src.rules.derivatives
import
der
class
TestParser
(
unittest
.
TestCase
):
...
...
@@ -47,12 +48,18 @@ class TestParser(unittest.TestCase):
self
.
assertEqual
(
tree
(
'-(a / b)'
),
(
-
a
)
/
b
)
def
test_functions
(
self
):
root
,
x
=
tree
(
'sin x,
x'
)
x
=
tree
(
'
x'
)
self
.
assertEqual
(
root
,
sin
(
x
))
self
.
assertEqual
(
tree
(
'sin x'
),
sin
(
x
))
self
.
assertEqual
(
tree
(
'sin(x)'
),
sin
(
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 cos x'
),
sin
(
cos
(
x
)))
self
.
assertEqual
(
tree
(
'sin cos x ^ 2'
),
sin
(
cos
(
x
))
**
2
)
def
test_derivative
(
self
):
x
=
tree
(
'x'
)
self
.
assertEqual
(
tree
(
'[x]
\
'
'
),
der
(
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