Skip to content

Commit

Permalink
Use xdot (if available) as graph viewer
Browse files Browse the repository at this point in the history
Update install instructions accordingly.
  • Loading branch information
PatrikHagglund committed Dec 17, 2023
1 parent 8bb07ee commit be8b759
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ machine instructions.

* The [ply](https://www.dabeaz.com/ply/) Python library is used for lexical analysis and parsing.

Optional:
* It is recommended to use the [xdot](https://github.com/jrfonseca/xdot.py#readme) program to view the abstract syntax tree. If `xdot` isn't available, the PDF viewer `evince` is used as a fallback.

### Fedora

dnf install -y python-ply
dnf install -y python-ply python-xdot evince

### Debian/Ubuntu

Expand Down Expand Up @@ -72,7 +75,7 @@ Here is a full example using the interpreter:
10946
-- Stack Frame --
Constants: {'K': 20}
Variables: {'m': 17711, 'n': 28657, 'k': 17711, 'count': 21}
Variables: {'count': 21, 'k': 17711, 'm': 17711, 'n': 28657}
Procedures: {}

Example of using the compiler, assembler and virtual machine:
Expand Down Expand Up @@ -104,11 +107,14 @@ Example of using the compiler, assembler and virtual machine:
Stack: []
Offset: -1

If you want to see a abstract syntax tree of your program, use the pl0_graphviz.py command:
To see the result of lexical analysis and parsing:

python pl0_lexer.py < examples/fibonacci.pl0
python pl0_parser.py < examples/fibonacci.pl0

./pl0_graphviz.py < examples/fibonacci.pl0
To get a graphical view of the abstract syntax tree:

A sample graph is included in the `examples` directory.
python pl0_graphviz.py < examples/fibonacci.pl0

For more advanced usage, including documentation on individual components, please see the [online documentation](http://programming.dojo.net.nz/study/pl0-language-tools/index).

Expand Down
Binary file removed examples/fibonacci.pdf
Binary file not shown.
11 changes: 6 additions & 5 deletions pl0_graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ def accept_term(self, node):
visitor = GraphPrinter()
contents = visitor.generate_graph(program)

print("Generating Graph...")
with open('graph.dot', 'w') as f:
f.write(contents)

print("Generating Graph...")
os.system("dot -v -Tpdf -ograph.pdf graph.dot")

print("Opening Graph...")
os.system("open graph.pdf")
# To view, use 'xdot' if available, else the PDF viewer 'evince'.
if (os.system("xdot graph.dot") >> 8):
print("Generating PDF...")
os.system("dot -T pdf -o graph.pdf graph.dot")
os.system("evince graph.pdf")
5 changes: 2 additions & 3 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ rm -rf err.txt
./pl0_interpreter.py < examples/fibonacci.pl0 > expect/fibonacci.run 2>>err.txt

# the graphviz output is messy so just ignore it. we'll test the .dot file only.
./pl0_graphviz.py < examples/fibonacci.pl0 2>>/dev/null
mv graph.dot expect/fibonacci.dot
rm -f graph.pdf
./pl0_graphviz.py < examples/fibonacci.pl0
(sleep 1; mv graph.dot expect/fibonacci.dot; rm -f graph.pdf) &

## retro tests ( should be easy to update for other backends )
for f in tests/*.pl0 ; do
Expand Down

0 comments on commit be8b759

Please sign in to comment.