Using Visual Yacc at GMU/SITE*
Visual yacc is a tool that takes a lex and a yacc specification pair
and generates a parser with the the same behavior as the original parser
plus it will draw a representation of the stack maintained by the
parser as well as a corresponding parse tree.
Usage
Creating the visualizing parser is done with
/home/cs540/Vyacc/visual_yacc [-v] file_name
where file_name corresponds to a lex file file_name.l and a yacc file file_name.y.
The optional -v (verbose) flag can be used to see what is happening during
creation.
The result is a file file_name.out
Typing this command with redirected input will cause a window to
appear on your screen. This window has several buttons:
- start - start the visualization
- step - take a single step
- multi-step - take a user-defined number of steps
- go - parse the entire input string
- quit - end the visualization
On the left side of the screen, we show the current parse stack. Shifts
cause the stack to grow and reductions pop multiple elements from the
stack (and push a single element). If you are using the displayable
attributes, these values are shown on the stack as well.
On the right side of the screen, we show the parse tree. At the moment
no attributes are displayed, but you can see what rules are chosen by
YACC during the parse.
Examples to get you started
We have included an Examples (/home/cs540/Vyacc/Examples)
directory that should help you figure out
how to visualize your own grammers.
Each of the ch* directories correspond to examples from the O'Reilly &
Associates book "lex & yacc" by John R. Levine, Tony Mason & Doug Brown.
Directory gram corresponds to a pascal-oid language.
Each directory contains:
- lex and yacc
specification pairs designed to work with visual_yacc
- A ReadMe file that describes how to use the executable
in the directory and that shows you what the input files look like.
- An executable
- input files (some directories)
Writing your own visualizable grammars
There are several rules you must follow in order to write your own visualizable
grammars.
- The files must be lex-able and yacc-able on their own.
They should be integrted by the linker - in other words, your lex specification
will #include "y.tab.h" and the yacc specification will not
include the lexor code.
The Visual YACC will reject any code that
would not be buildable this way.
- In the version I have installed, a single integer attribute can be visualized.
The syntax for declaring visual attribute is shown below.
Example
In this example, we need 2 attributes, a string and an integer (visualizable).
This is defined in the union below. If you don't want the integer value
displayed, VYint can just be int
%union {
VYint ival;
char *sval;
}
%token <sval> NAME
%token <ival> NUMBER
%type <ival> expression
These attributes are referenced in the obvious way:
.
.
.
| NAME '=' expression {printf("%s = %d\n",$1,$3); }
.
.
.
| expression '-' NUMBER { $$ = $1 - $3; }
.
.
.
In this example, NUMBER is set in the lexor just as it would be in normal YACC:
{. . . yylval.ival = . . . return NUMBER; }
Interested in how the tool was built?
* This will be installed on osf1 soon as well.