Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
graph_drawing
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
graph_drawing
Commits
ec0a7e59
Commit
ec0a7e59
authored
Dec 08, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed issue that caused parentheses not to be added.
parent
45bee0e4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
2 deletions
+6
-2
line.py
line.py
+6
-2
No files found.
line.py
View file @
ec0a7e59
from
node
import
Leaf
def
generate_line
(
root
):
def
generate_line
(
root
):
"""
"""
Print an expression tree in a single text line. Where needed, add
Print an expression tree in a single text line. Where needed, add
...
@@ -48,13 +50,15 @@ def generate_line(root):
...
@@ -48,13 +50,15 @@ def generate_line(root):
"""
"""
Get the associativity of an operator node.
Get the associativity of an operator node.
"""
"""
if
not
node
.
nodes
and
len
(
node
)
>
1
:
# Check binary and n-ary operators
if
not
isinstance
(
node
,
Leaf
)
and
len
(
node
)
>
1
:
op
=
node
.
title
()
op
=
node
.
title
()
for
i
,
group
in
enumerate
(
operators
):
for
i
,
group
in
enumerate
(
operators
):
if
op
in
group
:
if
op
in
group
:
return
i
return
i
# Unary operator and leaves have highest precedence
return
max_assoc
return
max_assoc
def
traverse
(
node
):
def
traverse
(
node
):
...
@@ -85,7 +89,7 @@ def generate_line(root):
...
@@ -85,7 +89,7 @@ def generate_line(root):
for
child
in
node
:
for
child
in
node
:
exp
=
traverse
(
child
)
exp
=
traverse
(
child
)
# Check if there is an
assiociativity
conflict.
# Check if there is an
precedence
conflict.
# If so, add parentheses
# If so, add parentheses
if
assoc
(
child
)
<
node_assoc
:
if
assoc
(
child
)
<
node_assoc
:
exp
=
'('
+
exp
+
')'
exp
=
'('
+
exp
+
')'
...
...
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