Collecting look ahead set

Craig <>
21 Feb 89 17:15:44 GMT

          From comp.compilers

Related articles
Collecting look ahead set (Craig) (1989-02-21)
Re: Collecting look ahead set megatest! (1989-02-23)
Re: Collecting look ahead set (1989-03-07)
Re: Collecting look ahead set (1989-03-09)
Re: Collecting look ahead set megatest! (1989-03-10)
| List of all articles for this month |

From: Craig <>
Newsgroups: comp.compilers
Date: 21 Feb 89 17:15:44 GMT
References: <>
Organization: Department of Computing at Lancaster University, UK.

The following two programs are usefull in the production of yacc based
grammars. The first will collect a follow set of acceptable tokens into
an array (yylkahead) which should be declared extern where ever you intend
to use it. At the moment the set is only collected when ever the grammar
reduces the 'error' token. This can then be used for debugging the grammar,
use in error messages etc. There is no manual page, it is however well

The second program will take the contents of the file produced by yacc
and produce a C function that will print the token name, given the number.
This is useful when used in conjunction with the first program as you can print
the names of the tokens found in the follow set.


----------------- cut here ----------------- cut here -----------------------
# A program to add code to that will collect an array
# of tokens that the compiler would have accepted, it is only
# compiled on an error token being read
# It is accesible to yacc programmers via the extern int variables
# extern int yylkahead[YYLOOKAHEAD],
# yylkind;
# yylkahead contains the look ahead symbols, yylkind is the number
# of entries in yylkahead
# If you find that yylkahead is not large enough then by setting
# YYLOOKAHEAD to a suitable value the size of the array can be
# altered, by default it is set to 50.
# Craig W. Wylie June 12th 1987
# University of Lancaster
# Lancaster
# U.K.
# All rights reserved, this software can be reproduced freely so long
# as the authors name and this text is included in the distribution
ex $1 << \YED_EOF 2> /dev/null 1>/dev/null
# define YYLOOKAHEAD 50
# endif
int yylkahead[YYLOOKAHEAD], /* Used to collect the look ahead set */
        yylkind; /* Number of tokens in look ahead set */
/ switch( /
                                the following code which collects the
                                look ahead set is largely derived from
                                " Introduction to Compiler Construction
                                    with Unix" Prentice Hall
                                by Axel T. Schreiner and
                                H. George Friedman, Jr.
                        if ((yyn = yypact[yystate]) > YYFLAG && yyn < YYLAST)
                        { register int x;

                                yylkind = 0;
                                for (x = yyn>0? yyn : 0; x < YYLAST; ++x)
                                        if (yychk[yyact[x]] == x - yyn
                                              && x - yyn != YYERRCODE)
                                                yylkahead[yylkind++] = x - yyn;

# dtok
# dtok creates a c function -- by default called disp_token
# that when called returns a string representation of the token
# number passed to it. ie if INT is defined as 300 then
# disp_token (INT)
# will return the string (" INT ")
# The representations are retrieved from a file in format,
# the name of this file is the first argument to dtok.
# Arguments
# ---------
# -f source file
# -c name for display function
# the only necessary argument is the -f argument
# and if this is not present then default input is
# from
# Zero arguments selects the defaults of for input and
# disp_token for output.
# Craig W. Wylie June 12th 1987
usage=`basename $0`": [-f input file name] [-c function name] "
prog=`basename $0`
if test $USER
        me="Don't know"

while test $# -ne 0
        case $1 in
                "-f") inpf=$2
                "-c") cfunc=$2
                * ) echo $1 "Bad argument"
                        echo $usage

if test -r $inpf
        echo $prog" : Input file "$inpf" not found"
echo "/*"
echo " Built by : "$me
echo " On : "`date`
echo " Yacc tool kit -- "`basename $0` $version"."$release
echo "*/"
echo "char *"$cfunc"(token)"
echo "int token;"
echo "{"
echo " switch (token){"
awk '
        /^# define/ {
                        print " case",$4,":"
                        print " return (\""$3"\");"
                        print " break;"

        END {
                        print " default :"
                        print " return (\"Unknown token\");"
                        print " }"
                        print "}"
                }' $inpf
[From Craig <>]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.