The Visual Yacc Distribution
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
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 directory in the distribution that
should help you figure out
how to visualize your own grammers. These examples are located in
Examples
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
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,
except for the special keyword VYint.
The visualization software will check this for you and reject any code that
would not be buildable this way.
Example 1
This example comes from ch3-02.
%union {
VYint val;
}
%type <val> NUMBER expression
These integers are referenced in the obvious way:
| expression '-' NUMBER { $$ = $1 - $3; }
In this example, NUMBER is set in the lexor:
{. . . yylval.val = . . . return NUMBER; }
Changing VYint to int is also fine except that no value will be
shown on the stack.
-
You can declare other attributes associated
with the individual nodes and computations using these attributes can
be arbitrarily complex. However, the value of these attributes will not
be shown during the visualization. The declarations must
occur after the declaration for x.
Example 3
In this example, we assume that the lexor sets the dval for NUMBER.
(yylval.s.dval = )
%union {
double dval;
VYint val;
int vblno;
}
. . .