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
65cc88c7
Commit
65cc88c7
authored
Apr 18, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added IMPLICIT_RULES list for rules that are not printed in verbose rewrite_all mode.
parent
51e123ec
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
17 deletions
+47
-17
src/parser.py
src/parser.py
+35
-14
src/rules/precedences.py
src/rules/precedences.py
+8
-1
tests/rulestestcase.py
tests/rulestestcase.py
+4
-1
tests/test_leiden_oefenopgave.py
tests/test_leiden_oefenopgave.py
+0
-1
No files found.
src/parser.py
View file @
65cc88c7
...
...
@@ -20,6 +20,7 @@ from node import ExpressionNode as Node, \
DEFAULT_LOGARITHM_BASE
,
OP_VALUE_MAP
,
SPECIAL_TOKENS
,
OP_INT
,
\
OP_INT_INDEF
,
negation_to_node
from
rules.utils
import
find_variable
from
rules.precedences
import
IMPLICIT_RULES
from
strategy
import
find_possibilities
from
possibilities
import
apply_suggestion
...
...
@@ -239,9 +240,10 @@ class Parser(BisonParser):
if not self.root_node:
raise RuntimeError('
No
expression
')
if self.possibilities
!=
None:
if self.possibilities
is not
None:
if self.verbose:
print '
Expression
has
not
changed
,
not
updating
possibilities
'
return
self.possibilities = find_possibilities(self.root_node)
...
...
@@ -261,29 +263,49 @@ class Parser(BisonParser):
for i, p in enumerate(self.possibilities):
print '
%
d
%
s
' % (i, p)
def rewrite(self, index=0, verbose=False):
def rewrite(self, index=0, verbose=False
, check_implicit=True
):
self.find_possibilities()
if not self.possibilities:
return
False
return
suggestion = self.possibilities[index]
if self.verbose:
print '
Applying
suggestion
:
', suggestion
print '
EXPLICIT
:
', suggestion
elif verbose:
print suggestion
expression = apply_suggestion(self.root_node, suggestion
)
self.set_root_node(apply_suggestion(self.root_node, suggestion)
)
if self.verbose:
print '
After
application
:
', expression
elif verbose:
print expression
print '
', self.root_node
# Only apply any remaining implicit hints if the suggestion itself is
# not implicit
if check_implicit and suggestion.handler not in IMPLICIT_RULES:
self.find_possibilities()
self.set_root_node(expression)
while self.possibilities:
sugg = self.possibilities[0]
if sugg.handler not in IMPLICIT_RULES:
break
if self.verbose:
print '
IMPLICIT
:
', sugg
return True
self.set_root_node(apply_suggestion(self.root_node, sugg))
if self.verbose:
print '
', self.root_node
self.find_possibilities()
if verbose and not self.verbose:
print self.root_node
return self.root_node
def rewrite_all(self, verbose=False):
i = 0
...
...
@@ -292,10 +314,10 @@ class Parser(BisonParser):
i += 1
if i > 100:
print '
Too
many
rewrite
steps
,
aborting
...
'
lines.append('
Too
many
rewrite
steps
,
aborting
...
')
break
if not verbose:
if not verbose
or not i
:
return self.root_node
#def hook_run(self, filename, retval):
...
...
@@ -354,8 +376,7 @@ class Parser(BisonParser):
return
if
option
==
5
:
# rule: REWRITE NEWLINE
self
.
rewrite
()
return
self
.
root_node
return
self
.
rewrite
()
if
option
==
6
:
# rule: REWRITE NUMBER NEWLINE
self
.
rewrite
(
int
(
values
[
1
]))
...
...
src/rules/precedences.py
View file @
65cc88c7
...
...
@@ -4,7 +4,7 @@ from .numerics import reduce_fraction_constants
from
.logarithmic
import
factor_in_exponent_multiplicant
,
\
factor_out_exponent
,
raised_base
from
.derivatives
import
chain_rule
from
.negation
import
double_negation
# Functions to move to the beginning of the possibilities list. Pairs of within
# the list itself are compared by their position in the list: lower in the list
...
...
@@ -40,3 +40,10 @@ RELATIVE = [
# Convert to dictionaries for efficient lookup
HIGH
=
dict
([(
h
,
i
)
for
i
,
h
in
enumerate
(
HIGH
)])
LOW
=
dict
([(
h
,
i
)
for
i
,
h
in
enumerate
(
LOW
)])
# List of implicit rules. Implicit rules are condidererd trivial and are
# therefore not printed in verbose rewrite_all mode
IMPLICIT_RULES
=
[
double_negation
,
]
tests/rulestestcase.py
View file @
65cc88c7
...
...
@@ -12,7 +12,10 @@ def tree(exp, **kwargs):
def
rewrite
(
exp
,
**
kwargs
):
return
ParserWrapper
(
Parser
,
**
kwargs
).
run
([
exp
,
'@'
])
wrapper
=
ParserWrapper
(
Parser
,
**
kwargs
)
wrapper
.
run
([
exp
])
return
wrapper
.
parser
.
rewrite
(
check_implicit
=
False
)
class
RulesTestCase
(
unittest
.
TestCase
):
...
...
tests/test_leiden_oefenopgave.py
View file @
65cc88c7
...
...
@@ -82,7 +82,6 @@ class TestLeidenOefenopgave(TestCase):
'x ^ 2 + (1 + 1)(-x) + 1'
,
'x ^ 2 + 2(-x) + 1'
,
'x ^ 2 - 2x + 1'
,
'x ^ 2 - 2x + 1'
,
])
def
test_1_4_1
(
self
):
...
...
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