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
b495a394
Commit
b495a394
authored
Jan 05, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of kompiler.org:trs
parents
4e7b7bb0
d9bedbd8
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
37 deletions
+54
-37
src/node.py
src/node.py
+1
-1
src/parser.py
src/parser.py
+5
-2
tests/parser.py
tests/parser.py
+22
-0
tests/test_b1_ch08.py
tests/test_b1_ch08.py
+14
-13
tests/test_b1_ch10.py
tests/test_b1_ch10.py
+5
-14
tests/test_calc.py
tests/test_calc.py
+7
-7
No files found.
src/node.py
View file @
b495a394
...
@@ -129,7 +129,7 @@ class ExpressionBase(object):
...
@@ -129,7 +129,7 @@ class ExpressionBase(object):
return
ExpressionNode
(
'*'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
'*'
,
self
,
to_expression
(
other
))
def
__div__
(
self
,
other
):
def
__div__
(
self
,
other
):
return
ExpressionNode
(
'
-
'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
'
/
'
,
self
,
to_expression
(
other
))
def
__pow__
(
self
,
other
):
def
__pow__
(
self
,
other
):
return
ExpressionNode
(
'^'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
'^'
,
self
,
to_expression
(
other
))
...
...
src/parser.py
View file @
b495a394
...
@@ -274,15 +274,18 @@ class Parser(BisonParser):
...
@@ -274,15 +274,18 @@ class Parser(BisonParser):
def
on_binary
(
self
,
target
,
option
,
names
,
values
):
def
on_binary
(
self
,
target
,
option
,
names
,
values
):
"""
"""
binary : exp PLUS exp
binary : exp PLUS exp
| exp MINUS exp
| exp TIMES exp
| exp TIMES exp
| exp DIVIDE exp
| exp DIVIDE exp
| exp POW exp
| exp POW exp
| exp MINUS exp
"""
"""
if
0
<=
option
<
5
:
# rule: exp PLUS
exp
if
0
<=
option
<
4
:
# rule: exp {PLUS,TIMES,DIVIDES,POW}
exp
return
Node
(
values
[
1
],
values
[
0
],
values
[
2
])
return
Node
(
values
[
1
],
values
[
0
],
values
[
2
])
if
option
==
4
:
# rule: exp MINUS exp
return
Node
(
'+'
,
values
[
0
],
Node
(
'-'
,
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
...
...
tests/parser.py
View file @
b495a394
...
@@ -98,6 +98,28 @@ def run_expressions(base_class, expressions, fail=True, silent=False,
...
@@ -98,6 +98,28 @@ def run_expressions(base_class, expressions, fail=True, silent=False,
raise
raise
def
apply_expressions
(
base_class
,
expressions
,
fail
=
True
,
silent
=
False
,
**
kwargs
):
parser
=
ParserWrapper
(
base_class
,
**
kwargs
)
for
exp
,
times
,
out
in
expressions
:
res
=
None
try
:
res
=
parser
.
run
([
exp
]
+
list
(
'@'
*
times
))
assert
res
==
out
except
:
# pragma: nocover
if
not
silent
:
print
>>
sys
.
stderr
,
'error: %s gives %s, but expected: %s'
\
%
(
exp
,
str
(
res
),
str
(
out
))
if
not
silent
and
hasattr
(
res
,
'nodes'
):
print
>>
sys
.
stderr
,
'result graph:'
print
>>
sys
.
stderr
,
generate_graph
(
res
)
print
>>
sys
.
stderr
,
'expected graph:'
print
>>
sys
.
stderr
,
generate_graph
(
out
)
if
fail
:
raise
def
graph
(
parser
,
*
exp
,
**
kwargs
):
def
graph
(
parser
,
*
exp
,
**
kwargs
):
return
generate_graph
(
ParserWrapper
(
parser
,
**
kwargs
).
run
(
exp
))
return
generate_graph
(
ParserWrapper
(
parser
,
**
kwargs
).
run
(
exp
))
...
...
tests/test_b1_ch08.py
View file @
b495a394
import
unittest
import
unittest
from
src.parser
import
Parser
from
src.parser
import
Parser
from
src.node
import
Expression
Node
as
N
,
Expression
Leaf
as
L
from
src.node
import
ExpressionLeaf
as
L
from
tests.parser
import
run_expressions
from
tests.parser
import
run_expressions
,
apply_expressions
class
TestB1Ch08
(
unittest
.
TestCase
):
class
TestB1Ch08
(
unittest
.
TestCase
):
def
test_diagnostic_test
(
self
):
def
test_diagnostic_test
_parser
(
self
):
run_expressions
(
Parser
,
[
run_expressions
(
Parser
,
[
(
'6*5^2'
,
N
(
'*'
,
L
(
6
),
N
(
'^'
,
L
(
5
),
L
(
2
)))),
(
'6*5^2'
,
L
(
6
)
*
L
(
5
)
**
2
),
(
'-5*(-3)^2'
,
N
(
'*'
,
N
(
'-'
,
L
(
5
)),
(
'-5*(-3)^2'
,
(
-
L
(
5
))
*
(
-
L
(
3
))
**
2
),
N
(
'^'
,
N
(
'-'
,
L
(
3
)),
L
(
2
)))),
(
'7p-3p'
,
L
(
7
)
*
'p'
+
-
(
L
(
3
)
*
'p'
)),
(
'-5*(-3)^2'
,
N
(
'*'
,
N
(
'-'
,
L
(
5
)),
(
'-5a*-6'
,
(
-
L
(
5
))
*
'a'
*
(
-
L
(
6
))),
N
(
'^'
,
N
(
'-'
,
L
(
3
)),
L
(
2
)))),
(
'3a-8--5-2a'
,
L
(
3
)
*
'a'
+
-
L
(
8
)
+
-
(
-
L
(
5
))
+
-
(
L
(
2
)
*
'a'
)),
(
'7p-3p'
,
N
(
'-'
,
N
(
'*'
,
L
(
7
),
L
(
'p'
)),
N
(
'*'
,
L
(
3
),
L
(
'p'
)))),
])
(
'-5a*-6'
,
N
(
'*'
,
N
(
'*'
,
N
(
'-'
,
L
(
5
)),
L
(
'a'
)),
N
(
'-'
,
L
(
6
)))),
def
test_diagnostic_test_application
(
self
):
(
'3a-8--5-2a'
,
N
(
'-'
,
N
(
'-'
,
N
(
'-'
,
N
(
'*'
,
L
(
3
),
L
(
'a'
)),
L
(
8
)),
apply_expressions
(
Parser
,
[
N
(
'-'
,
L
(
5
))),
N
(
'*'
,
L
(
2
),
L
(
'a'
)))),
(
'7p+2p'
,
1
,
(
L
(
7
)
+
2
)
*
'p'
),
#('7p-3p', 1, (L(7) - 3) * 'p'),
])
])
tests/test_b1_ch10.py
View file @
b495a394
...
@@ -9,21 +9,12 @@ class TestB1Ch10(unittest.TestCase):
...
@@ -9,21 +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)'
,
N
(
'*'
,
L
(
5
),
N
(
'-'
,
L
(
'a'
),
(
'5(a-2b)'
,
L
(
5
)
*
(
L
(
'a'
)
+
-
(
L
(
2
)
*
'b'
))),
N
(
'*'
,
L
(
2
),
L
(
'b'
))))),
(
'-(3a+6b)'
,
-
(
L
(
3
)
*
L
(
'a'
)
+
L
(
6
)
*
'b'
)),
(
'-(3a+6b)'
,
N
(
'-'
,
N
(
'+'
,
N
(
'*'
,
L
(
3
),
L
(
'a'
)),
(
'18-(a-12)'
,
L
(
18
)
+
-
(
L
(
'a'
)
+
-
L
(
12
))),
N
(
'*'
,
L
(
6
),
L
(
'b'
))))),
(
'18-(a-12)'
,
N
(
'-'
,
L
(
18
),
N
(
'-'
,
L
(
'a'
),
L
(
12
)))),
(
'-p-q+5(p-q)-3q-2(p-q)'
,
(
'-p-q+5(p-q)-3q-2(p-q)'
,
N
(
'-'
,
-
L
(
'p'
)
+
-
L
(
'q'
)
+
L
(
5
)
*
(
L
(
'p'
)
+
-
L
(
'q'
))
+
-
(
L
(
3
)
*
'q'
)
\
N
(
'-'
,
+
-
(
L
(
2
)
*
(
L
(
'p'
)
+
-
L
(
'q'
)))
N
(
'+'
,
N
(
'-'
,
N
(
'-'
,
L
(
'p'
)),
L
(
'q'
)),
N
(
'*'
,
L
(
5
),
N
(
'-'
,
L
(
'p'
),
L
(
'q'
)))),
N
(
'*'
,
L
(
3
),
L
(
'q'
))
),
N
(
'*'
,
L
(
2
),
N
(
'-'
,
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
))
...
...
tests/test_calc.py
View file @
b495a394
...
@@ -13,10 +13,10 @@ class TestCalc(unittest.TestCase):
...
@@ -13,10 +13,10 @@ class TestCalc(unittest.TestCase):
def
test_basic_on_exp
(
self
):
def
test_basic_on_exp
(
self
):
expressions
=
[(
'4'
,
L
(
4
)),
expressions
=
[(
'4'
,
L
(
4
)),
(
'3+4'
,
N
(
'+'
,
L
(
3
),
L
(
4
)
)),
(
'3+4'
,
L
(
3
)
+
L
(
4
)),
(
'3-4'
,
N
(
'-'
,
L
(
3
),
L
(
4
)
)),
(
'3-4'
,
L
(
3
)
+
-
L
(
4
)),
(
'3/4'
,
N
(
'/'
,
L
(
3
),
L
(
4
)
)),
(
'3/4'
,
L
(
3
)
/
L
(
4
)),
(
'-4'
,
N
(
'-'
,
L
(
4
)
)),
(
'-4'
,
-
L
(
4
)),
(
'3^4'
,
N
(
'^'
,
L
(
3
),
L
(
4
))),
(
'3^4'
,
N
(
'^'
,
L
(
3
),
L
(
4
))),
(
'(2)'
,
L
(
2
))]
(
'(2)'
,
L
(
2
))]
...
@@ -83,7 +83,7 @@ class TestCalc(unittest.TestCase):
...
@@ -83,7 +83,7 @@ class TestCalc(unittest.TestCase):
def
test_negation
(
self
):
def
test_negation
(
self
):
run_expressions
(
Parser
,
[
run_expressions
(
Parser
,
[
(
'-9'
,
N
(
'-'
,
L
(
9
)
)),
(
'-9'
,
-
L
(
9
)),
(
'--9'
,
N
(
'-'
,
N
(
'-'
,
L
(
9
))
)),
(
'--9'
,
--
L
(
9
)),
(
'a--9'
,
N
(
'-'
,
L
(
'a'
),
N
(
'-'
,
L
(
9
)
))),
(
'a--9'
,
L
(
'a'
)
+
-
(
-
L
(
9
))),
])
])
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