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
1c4e9200
Commit
1c4e9200
authored
Dec 08, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code cleanup and updated pybison version.
parent
bf906321
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
7 deletions
+13
-7
external/pybison
external/pybison
+1
-1
src/parser.py
src/parser.py
+8
-5
tests/parser.py
tests/parser.py
+1
-1
tests/test_parser.py
tests/test_parser.py
+3
-0
No files found.
pybison
@
b200b18e
Subproject commit
260359e31ceeaa46d26a26303e08cda37c7578f5
Subproject commit
b200b18e408400386c30c59572a6c72805d2ca87
src/parser.py
View file @
1c4e9200
...
@@ -63,6 +63,9 @@ class Parser(BisonParser):
...
@@ -63,6 +63,9 @@ class Parser(BisonParser):
# override default read method with a version that prompts for input
# override default read method with a version that prompts for input
# ------------------------------------------------------------------
# ------------------------------------------------------------------
def
read
(
self
,
nbytes
):
def
read
(
self
,
nbytes
):
if
self
.
file
==
sys
.
stdin
and
self
.
file
.
closed
:
return
''
try
:
try
:
return
raw_input
(
'>>> '
)
+
'
\
n
'
return
raw_input
(
'>>> '
)
+
'
\
n
'
except
EOFError
:
except
EOFError
:
...
@@ -187,24 +190,24 @@ class Parser(BisonParser):
...
@@ -187,24 +190,24 @@ class Parser(BisonParser):
"""
"""
if
option
in
[
0
,
1
]:
# rule: exp IDENTIFIER | exp NUMBER
if
option
in
[
0
,
1
]:
# rule: exp IDENTIFIER | exp NUMBER
#
NOTE
: xy -> x*y
#
example
: xy -> x*y
#
NOTE
: (x)4 -> x*4
#
example
: (x)4 -> x*4
val
=
int
(
values
[
1
])
if
option
==
1
else
values
[
1
]
val
=
int
(
values
[
1
])
if
option
==
1
else
values
[
1
]
return
Node
(
'*'
,
*
(
combine
(
'*'
,
values
[
0
])
+
[
Leaf
(
val
)]))
return
Node
(
'*'
,
*
(
combine
(
'*'
,
values
[
0
])
+
[
Leaf
(
val
)]))
if
option
==
2
:
# rule: exp LPAREN exp RPAREN
if
option
==
2
:
# rule: exp LPAREN exp RPAREN
#
NOTE
: x(y) -> x*(y)
#
example
: x(y) -> x*(y)
return
Node
(
'*'
,
*
(
combine
(
'*'
,
values
[
0
])
return
Node
(
'*'
,
*
(
combine
(
'*'
,
values
[
0
])
+
combine
(
'*'
,
values
[
2
])))
+
combine
(
'*'
,
values
[
2
])))
if
option
==
3
:
if
option
==
3
:
#
NOTE: x4 -> x
^4
#
example: xy4 -> x*y
^4
identifier
,
exponent
=
list
(
values
[
1
])
identifier
,
exponent
=
list
(
values
[
1
])
node
=
Node
(
'^'
,
Leaf
(
identifier
),
Leaf
(
int
(
exponent
)))
node
=
Node
(
'^'
,
Leaf
(
identifier
),
Leaf
(
int
(
exponent
)))
return
Node
(
'*'
,
values
[
0
],
node
)
return
Node
(
'*'
,
values
[
0
],
node
)
if
option
==
4
:
if
option
==
4
:
#
NOTE
: x4 -> x^4
#
example
: x4 -> x^4
identifier
,
exponent
=
list
(
values
[
0
])
identifier
,
exponent
=
list
(
values
[
0
])
return
Node
(
'^'
,
Leaf
(
identifier
),
Leaf
(
int
(
exponent
)))
return
Node
(
'^'
,
Leaf
(
identifier
),
Leaf
(
int
(
exponent
)))
...
...
tests/parser.py
View file @
1c4e9200
...
@@ -83,7 +83,7 @@ def run_expressions(base_class, expressions, keepfiles=1, fail=True,
...
@@ -83,7 +83,7 @@ def run_expressions(base_class, expressions, keepfiles=1, fail=True,
try
:
try
:
res
=
parser
.
run
([
exp
])
res
=
parser
.
run
([
exp
])
assert
res
==
out
assert
res
==
out
except
:
except
:
# pragma: nocover
if
not
silent
:
if
not
silent
:
print
>>
sys
.
stderr
,
'error: %s = %s, but expected: %s'
\
print
>>
sys
.
stderr
,
'error: %s = %s, but expected: %s'
\
%
(
exp
,
str
(
res
),
str
(
out
))
%
(
exp
,
str
(
res
),
str
(
out
))
...
...
tests/test_parser.py
View file @
1c4e9200
...
@@ -23,3 +23,6 @@ class TestParser(unittest.TestCase):
...
@@ -23,3 +23,6 @@ class TestParser(unittest.TestCase):
def
test_line
(
self
):
def
test_line
(
self
):
self
.
assertEqual
(
line
(
Parser
,
'4a'
),
'4 * a'
)
self
.
assertEqual
(
line
(
Parser
,
'4a'
),
'4 * a'
)
#def test_raise(self):
# ParserWrapper(Parser).run(['raise'])
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