
                               U S I N G  Y A C C

   The work is actually fairly simple, once you get the big picture. The main
   thing is to augment, modify or replace the productions defined in micro.y.
   While doing this you'll find the need to add some TreeNode types that are
   present in ast_.h.  You will also have to add corresponding cases in a
   couple of routines (e.g.  new_TreeNode and print_TreeNode)

   Firstly, LEX and YACC each generate .c files ("lex.yy.c" and "y.tab.c"
   respectively), that  are  separately compiled  then linked   together.
   Like other C files, the only way that these  two files can communicate
   is by common  #include'd .h files and by  global variables.  Normally,
   YACC will generate  a  .h file,  "y.tab.h", that  can be included   by
   LEX. This  include file has #defines for  all the teerminals or tokens
   that YACC expects from  LEX, together with  a global variable, yylval,
   that is used to pass values of tokens from LEX to YACC.

   In general, it is not necessary to look at the .c  files that YACC and
   LEX generate, but it  is instructive to do so  if you get errors  when
   these files are compiled  and linked together.  Besides, that  way you
   will understand exactly  what LEX and  YACC are generating (ignore the
   tables though). Finally, you should NEVER edit the files that yacc and
   lex generate. Otherwise, the  makefile will not  work when  you modify
   the the input to LEX or YACC.

   In YACC all tokens and non-terminals  have a type associated with them
   which is defaulted to int. If you need more than  that you need to use
   the %union construct to  tell YACC what  kind of stuff  will end up in
   its stack and what values to expect from the lexer.

   When you say:

   %union {
           int  as_int;
           Tree tree_node;
   }

   you're saying  to YACC that the  values  it will deal with  are either
   integers or Trees.

   It is necessary to tell YACC the type  of all tokens and non-terminals
   that we will  reference  through the  $n construct  and that  are  not
   integers. This   is  done with   the %token declaration   for terminal
   symbols and with the %type declaration for the non-terminal symbols.
   Saying:

   %type <tree_node> Statement

   tells YACC that the  type of Statement is tree_node  (this must be one
   of the types declared in the %union {...}). Suppose somewhere you have
   a rule with a Statement in it at position n. YACC transforms the $n in
   the user defined actions in something like:

           yyss[top-length+n].tree_node
   (it is much more optimized than this, but you get the idea)

   The  %type stuff is used by  YACC alone, while the %token declarations
   define the communication  between  the YACC-generated parser  and  the
   LEX-generated scanner.

   You  need to declare  every token you're  going  to use and, for those
   whose associated value  is not integer, the type.   This is  a feature
   that YACC provides and that allows you to pass information in addition
   to  the  token type (and  you   may find  this  expecially  useful for
   constant and identifiers)
   The declaration of a integer constant might be:

   %token <as_int> INT_CONST

   This means that when  the parser will see a  INT_CONST it can assume
   that an integer value has been  placed somewhere by the  scanner. Right?
   Right, but where?

   A global variable of type YYLTYPE is declared for you by YACC. YYLTYPE
   is the union you've declared in the grammar source, so  you can use it
   to pass any type you  may need to  the parser. This variable is called
   yylval.

   Somewhere in the .l file you've something like:
   (note that  the  .l file  I'm  referring to is the   one you wrote for
   assignment  1,  and not  the  sample micro.l you  find  in the assign3
   directory)

   [0-9]+

   The action associated  with this rule could be:

   {
           yylval.as_int (atoi (yytext));
           return INT_CONST;
   }

   As you see,  you're placing the information  you want to convey in the
   yylval global variable and  that's all you need to  do. Thanks to  the
   type declarations  the YACC-generated parser  will correctly read that
   value everytime it will need the value associated with an INT_CONST.
   In particolar you can have something like:

   Expression:
           .
           .
           | INT_CONST
           { $$ = new_TreeNode (IntConst);
           $$->Node.value = $1;
           }
           ;

   (note that this is not the only possible solution, you could have
   built the TreeNode in the scanner and returned that)



