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.


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:

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:

Writing your own visualizable grammars

There are several rules you must follow in order to write your own visualizable grammars.
  1. 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 "" and the yacc specification will not include the lexor code. The Visual YACC will reject any code that would not be buildable this way.
  2. In the version I have installed, a single integer attribute can be visualized. The syntax for declaring visual attribute is shown below.


    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.