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
53f649a7
Commit
53f649a7
authored
Feb 20, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parser will add negation to left-most child of scope.
parent
bf7d96e6
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
49 additions
and
17 deletions
+49
-17
external/graph_drawing
external/graph_drawing
+1
-1
src/node.py
src/node.py
+12
-0
src/parser.py
src/parser.py
+25
-10
src/possibilities.py
src/possibilities.py
+4
-0
src/rules/negation.py
src/rules/negation.py
+2
-1
tests/test_b1_ch08.py
tests/test_b1_ch08.py
+2
-2
tests/test_b1_ch10.py
tests/test_b1_ch10.py
+3
-3
No files found.
graph_drawing
@
fbfd0f2a
Subproject commit
821bdb8f8408fb36ee1d92ada162589794a8c5b3
Subproject commit
fbfd0f2a3af6157d4994270bcdfa660910a2119d
src/node.py
View file @
53f649a7
...
@@ -72,6 +72,18 @@ class ExpressionBase(object):
...
@@ -72,6 +72,18 @@ class ExpressionBase(object):
def
clone
(
self
):
def
clone
(
self
):
return
copy
.
deepcopy
(
self
)
return
copy
.
deepcopy
(
self
)
def
left
(
self
):
"""
Return the left-most child of this node. When this instance is a leaf,
the leaf will be returned.
"""
node
=
self
while
not
node
.
is_leaf
:
node
=
node
[
0
]
return
node
def
__lt__
(
self
,
other
):
def
__lt__
(
self
,
other
):
"""
"""
Comparison between this expression{node,leaf} and another
Comparison between this expression{node,leaf} and another
...
...
src/parser.py
View file @
53f649a7
...
@@ -16,7 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS)
...
@@ -16,7 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS)
from
pybison
import
BisonParser
,
BisonSyntaxError
from
pybison
import
BisonParser
,
BisonSyntaxError
from
graph_drawing.graph
import
generate_graph
from
graph_drawing.graph
import
generate_graph
from
node
import
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
from
node
import
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
Scope
from
rules
import
RULES
from
rules
import
RULES
from
possibilities
import
filter_duplicates
,
pick_suggestion
,
apply_suggestion
from
possibilities
import
filter_duplicates
,
pick_suggestion
,
apply_suggestion
...
@@ -193,11 +193,15 @@ class Parser(BisonParser):
...
@@ -193,11 +193,15 @@ class Parser(BisonParser):
if
not
retval
.
negated
and
retval
.
type
!=
TYPE_OPERATOR
:
if
not
retval
.
negated
and
retval
.
type
!=
TYPE_OPERATOR
:
return
retval
return
retval
if
self
.
subtree_map
and
retval
.
type
==
TYPE_OPERATOR
:
if
self
.
subtree_map
:
#
and retval.type == TYPE_OPERATOR:
# Update the subtree map to let the subtree point to its parent
# Update the subtree map to let the subtree point to its parent
# node.
# node.
parent_nodes
=
self
.
subtree_map
.
keys
()
parent_nodes
=
self
.
subtree_map
.
keys
()
if
retval
.
is_leaf
:
if
retval
in
parent_nodes
:
self
.
subtree_map
[
retval
]
=
None
else
:
for
child
in
retval
:
for
child
in
retval
:
if
child
in
parent_nodes
:
if
child
in
parent_nodes
:
self
.
subtree_map
[
child
]
=
retval
self
.
subtree_map
[
child
]
=
retval
...
@@ -357,9 +361,14 @@ class Parser(BisonParser):
...
@@ -357,9 +361,14 @@ class Parser(BisonParser):
"""
"""
if
option
==
0
:
# rule: NEG exp
if
option
==
0
:
# rule: NEG exp
node
=
values
[
1
]
# Add negation to the left-most child
node
.
negated
+=
1
if
values
[
1
].
is_leaf
or
values
[
1
].
op
!=
OP_MUL
:
return
node
values
[
1
].
negated
+=
1
else
:
child
=
Scope
(
values
[
1
])[
0
]
child
.
negated
+=
1
return
values
[
1
]
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
...
@@ -378,12 +387,18 @@ class Parser(BisonParser):
...
@@ -378,12 +387,18 @@ class Parser(BisonParser):
if
option
==
4
:
# rule: exp MINUS exp
if
option
==
4
:
# rule: exp MINUS exp
node
=
values
[
2
]
node
=
values
[
2
]
# Add negation to the left-most child
if
node
.
is_leaf
or
node
.
op
!=
OP_MUL
:
node
.
negated
+=
1
else
:
node
=
Scope
(
node
)[
0
]
node
.
negated
+=
1
node
.
negated
+=
1
# Explicit call the hook handler on the created unary negation.
# Explicit call the hook handler on the created unary negation.
node
=
self
.
hook_handler
(
'binary'
,
4
,
names
,
values
,
node
)
node
=
self
.
hook_handler
(
'binary'
,
4
,
names
,
values
,
node
)
return
Node
(
'+'
,
values
[
0
],
node
)
return
Node
(
'+'
,
values
[
0
],
values
[
2
]
)
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
...
...
src/possibilities.py
View file @
53f649a7
...
@@ -73,6 +73,9 @@ def apply_suggestion(root, subtree_map, suggestion):
...
@@ -73,6 +73,9 @@ def apply_suggestion(root, subtree_map, suggestion):
else
:
else
:
parent_node
=
None
parent_node
=
None
print
'suggestion:'
,
suggestion
.
root
,
parent_node
print
'subtree_map:'
,
subtree_map
# There is either a parent node or the subtree is the root node.
# There is either a parent node or the subtree is the root node.
# FIXME: FAIL: test_diagnostic_test_application in tests/test_b1_ch08.py
# FIXME: FAIL: test_diagnostic_test_application in tests/test_b1_ch08.py
#try:
#try:
...
@@ -85,4 +88,5 @@ def apply_suggestion(root, subtree_map, suggestion):
...
@@ -85,4 +88,5 @@ def apply_suggestion(root, subtree_map, suggestion):
if
parent_node
:
if
parent_node
:
parent_node
.
substitute
(
suggestion
.
root
,
subtree
)
parent_node
.
substitute
(
suggestion
.
root
,
subtree
)
return
root
return
root
return
subtree
return
subtree
src/rules/negation.py
View file @
53f649a7
from
..node
import
Scope
,
OP_ADD
,
OP_MUL
,
OP_DIV
,
TYPE_OPERATOR
from
..node
import
Scope
,
OP_ADD
,
OP_MUL
,
OP_DIV
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
from
..translate
import
_
...
@@ -46,6 +46,7 @@ def match_negate_polynome(node):
...
@@ -46,6 +46,7 @@ def match_negate_polynome(node):
--a -> a
--a -> a
-(a + b) -> -a - b
-(a + b) -> -a - b
"""
"""
print
'match_negate_polynome:'
,
node
,
node
.
negated
assert
node
.
negated
,
str
(
node
.
negated
)
+
'; '
+
str
(
node
)
assert
node
.
negated
,
str
(
node
.
negated
)
+
'; '
+
str
(
node
)
p
=
[]
p
=
[]
...
...
tests/test_b1_ch08.py
View file @
53f649a7
...
@@ -11,9 +11,9 @@ class TestB1Ch08(unittest.TestCase):
...
@@ -11,9 +11,9 @@ class TestB1Ch08(unittest.TestCase):
run_expressions
(
Parser
,
[
run_expressions
(
Parser
,
[
(
'6*5^2'
,
L
(
6
)
*
L
(
5
)
**
2
),
(
'6*5^2'
,
L
(
6
)
*
L
(
5
)
**
2
),
(
'-5*(-3)^2'
,
(
-
L
(
5
))
*
(
-
L
(
3
))
**
2
),
(
'-5*(-3)^2'
,
(
-
L
(
5
))
*
(
-
L
(
3
))
**
2
),
(
'7p-3p'
,
L
(
7
)
*
'p'
+
-
(
L
(
3
)
*
'p'
)),
(
'7p-3p'
,
L
(
7
)
*
'p'
+
(
-
L
(
3
)
*
'p'
)),
(
'-5a*-6'
,
(
-
L
(
5
))
*
'a'
*
(
-
L
(
6
))),
(
'-5a*-6'
,
(
-
L
(
5
))
*
'a'
*
(
-
L
(
6
))),
(
'3a-8--5-2a'
,
L
(
3
)
*
'a'
+
-
L
(
8
)
+
-
(
-
L
(
5
))
+
-
(
L
(
2
)
*
'a'
)),
(
'3a-8--5-2a'
,
L
(
3
)
*
'a'
+
-
L
(
8
)
+
(
--
L
(
5
))
+
(
-
L
(
2
)
*
'a'
)),
])
])
def
test_diagnostic_test_application
(
self
):
def
test_diagnostic_test_application
(
self
):
...
...
tests/test_b1_ch10.py
View file @
53f649a7
...
@@ -9,12 +9,12 @@ class TestB1Ch10(unittest.TestCase):
...
@@ -9,12 +9,12 @@ class TestB1Ch10(unittest.TestCase):
def
test_diagnostic_test
(
self
):
def
test_diagnostic_test
(
self
):
run_expressions
(
Parser
,
[
run_expressions
(
Parser
,
[
(
'5(a-2b)'
,
L
(
5
)
*
(
L
(
'a'
)
+
-
(
L
(
2
)
*
'b'
))),
(
'5(a-2b)'
,
L
(
5
)
*
(
L
(
'a'
)
+
(
-
L
(
2
)
*
'b'
))),
(
'-(3a+6b)'
,
-
(
L
(
3
)
*
L
(
'a'
)
+
L
(
6
)
*
'b'
)),
(
'-(3a+6b)'
,
-
(
L
(
3
)
*
L
(
'a'
)
+
L
(
6
)
*
'b'
)),
(
'18-(a-12)'
,
L
(
18
)
+
-
(
L
(
'a'
)
+
-
L
(
12
))),
(
'18-(a-12)'
,
L
(
18
)
+
-
(
L
(
'a'
)
+
-
L
(
12
))),
(
'-p-q+5(p-q)-3q-2(p-q)'
,
(
'-p-q+5(p-q)-3q-2(p-q)'
,
-
L
(
'p'
)
+
-
L
(
'q'
)
+
L
(
5
)
*
(
L
(
'p'
)
+
-
L
(
'q'
))
+
-
(
L
(
3
)
*
'q'
)
\
-
L
(
'p'
)
+
-
L
(
'q'
)
+
L
(
5
)
*
(
L
(
'p'
)
+
-
L
(
'q'
))
+
(
-
L
(
3
)
*
'q'
)
\
+
-
(
L
(
2
)
*
(
L
(
'p'
)
+
-
L
(
'q'
)))
+
(
-
L
(
2
)
*
(
L
(
'p'
)
+
-
L
(
'q'
)))
),
),
(
'(2+3/7)^4'
,
(
'(2+3/7)^4'
,
N
(
'^'
,
N
(
'+'
,
L
(
2
),
N
(
'/'
,
L
(
3
),
L
(
7
))),
L
(
4
))
N
(
'^'
,
N
(
'+'
,
L
(
2
),
N
(
'/'
,
L
(
3
),
L
(
7
))),
L
(
4
))
...
...
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