Version 1.1 of BtYacc (Backtracking Yacc) is available

Vadim Maslov <vadik@siber.com>
8 Oct 1997 00:48:58 -0400

          From comp.compilers

Related articles
Version 1.1 of BtYacc (Backtracking Yacc) is available vadik@siber.com (Vadim Maslov) (1997-10-08)
| List of all articles for this month |

From: Vadim Maslov <vadik@siber.com>
Newsgroups: comp.compilers
Date: 8 Oct 1997 00:48:58 -0400
Organization: Compilers Central
Keywords: parse, yacc, tools

We announce that Version 1.1 of Backtarcking Yacc (BtYacc) is
available for free downloads from http://www.siber.com/btyacc/


README file with more explanation follows:




BTYACC -- backtracking yacc
===========================


BTYACC was created by Chris Dodd using ideas from many places and lots
of code from the Berkeley Yacc distribution, which is a public domain
yacc clone put together by the good folks at Berkeley. This code is
distributed with NO WARRANTEE and is public domain. It is certain to
contain bugs, which you should report to:
chrisd@collins.com


Vadim Maslov of Siber Systems <vadik@siber.com> modified BTYACC to
make it suitable for production environment (and to fix bugs, too).
My changes are described at the end of this document.




See the README.BYACC, NEW_FEATURES, and ACKNOWLEDGEMENTS files for
more about Berkeley Yacc and other sources of info.


BTYACC is a modified version of yacc that supports automatic
backtracking and semantic disambiguation to parse ambiguous grammars,
as well as syntactic sugar for inherited attributes (which tend to
introduce conflicts). Whenever a btyacc generated parser runs into a
shift-reduce or reduce-reduce error in the parse table, it remembers
the current parse point (yacc stack and input stream state), and goes
into trial parse mode. It then continues parsing, ignoring most rule
actions. If it runs into an error (either through the parse table or
through an action calling YYERROR), it backtracks to the most recent
conflict point and tries a different alternative. If it finds a
successful parse (reaches the end of the input or an action calls
YYVALID), it backtracks to the point where it first entered trial
parse mode, and continues with a full parse (executing all actions),
following the path of the successful trial.


Actions in btyacc come in two flavors -- {}-actions, which are only
executed when not in trial mode, and []-actions which are executed
regardless of mode. There are also inherited attributes, which look
like arguments (they are enclosed in "()") and act like []-actions.


What this buys you:


* No more lexer feedback hack. In yacc grammars for C, a standard
hack, know as the "lexer feedback hack" is used to find typedef names.
The lexer uses semantic information to decide if any given identifier
is a typedef-name or not and returns a special token. With btyacc,
you no longer need to do this; the lexer should just always return an
identifier. The btyacc grammar then needs a rule of the form:


typename: ID [ if (!IsTypeName(LookupId($1))) YYERROR; ]


While the hack works adequately well for parsing C, it becomes a
nightmare when you try to parse something like C++, where treating an
ID as a typedef becomes heavily dependent on context.


* Easy disambiguation via simple ordering. Btyacc runs its trials via
the rule "try shifting first, then try reducing by the order that the
conflicting rules appear in the input file". This means you can deal
with semantic a disambiguation rule like:
        [1] If it looks like a declaration it is, otherwise
        [2] If it looks like an expression it is, otherwise
        [3] it is a syntax error
[Ellis & Stroustrup, Annotated C++ Reference Manual, p93]


To deal with this, you need only put all the rules for declarations
before the rules for expressions in the grammar file.


* No extra cost if you do not use it. Backtracking is only triggered
when the parse hits a shift/reduce or reduce/reduce conflict in the
table. If you have no conflicts in your grammar, there is no extra
cost, other than some extra code which will never be invoked.


* C++ and ANSI C compatible parsers. The parsers produced by btyacc
can be compiled with C++ correctly. If you "#define" YYSTYPE to be
some C++ type with constructor and destructor, everything will work
fine. My favorite is "#define YYSTYPE SmartPointer", where
SmartPointer is a smart pointer type that does garbage collection on
the pointed to objects.


BTYACC was originally written to make it easy to write a C++ parser
(my goal was to be able to use the grammar out of the back of the ARM
with as few modifications as possible). Anyone who has ever looked at
Jim Roskind public domain C++ yacc grammar, or the yacc-based grammar
used in g++ knows how difficult this is. BTYACC is very useful for
parsing any ambiguous grammar, particularly ones that come from trying
to merge two (or more) complete grammars.


Limitations of the backtracking: Currently, the generated parser does
NO pruning of alternate parsing paths. To avoid an exponential
explosion of possible paths (and parsing time), you need to manually
tell the parser when it can throw away saved paths using YYVALID. In
pratice, this turns out to be fairly easy to do. A C++ parser (for
example) can juts put a [YYVALID;] after every complete declaration
and statement rule, corresponding to pruning the backtracking state
after seeing a ';' or '}' -- there will never be a situation in which
it is useful to backtrack past either of these.


Inherited attributes in btyacc:


Inherited attributes look a lot like function arguments to
non-terminals, which is what they end up being in a recursive descent
parser, but NOT how they are implemented in btyacc. Basically they
are just syntactic sugar for embedded semantic actions and $0, $-1,
... in normal yacc. btyacc gives you two big advantages besides just
the syntax:
        1. it does type checking on the inherited attributes,
              so you do not have to specify $<type>0 and makes sure
              you give the correct number of arguments (inherited
              attributes) to every use of a non-terminal.
        2. It "collapses" identical actions from that are produced
              from inherited attributes. This eliminates many
              potential reduce-reduce conflicts arising from
              the inherited attributes.


You use inherited attributes by declaring the types of the attributes
in the preamble with a type declaration and declaring names of the
attributes on the lhs of the yacc rule. You can of course have more
than one rule with the same lhs, and you can even give them different
names in each, but the type and number must be the same.


Here is a small example:
%type <t1> lhs(<t1>, <t2>) /* lhs takes two inherited attributes */
stuff(<t1>, <t2>)
%%
lhs($i1, $i2) : { $$ = $i1 }
| lhs($i1, $i2) stuff($1,$i2) { $$ = $2; }


This is roughly equivalent to the following yacc code:
lhs : { $$ = $<t1>-1; }
        | lhs [ $<t1>$ = $-1; ] [ $<t2>$ = $<t2>0; ] stuff { $$ = $4; }
        ;


See the file "test/t2.y" for a longer and more complete example. At
the current time, the start symbol cannot have any arguments.


Variant parsers:


Btyacc supports the -S flag to use a different parser skeleton,
changing the way that the parser is called and used. The skeleton
"push.skel" is included to produce a "passive" parser that you feed
tokens to (rather than having the parser call a seperate yylex
routine). With push.skel, yyparse is defined as follows:


int yyparse(int token, YYSTYPE yylval)


You should call yyparse repeatedly with successive tokens of input.
It returns 0 if more input is needed, 1 for a successful parse, and -1
for an unrecoverable parse error.


Chris Dodd
chrisd@collins.com




Vadim Maslov additions
======================


** Added


%include file_name


command.


It acts just like #include in C.


Helps a lot when the list of tokens used by this grammar
is generated by another program.




** Changed 16-bit short numbers to 32-bit int numbers in grammar
tables, so that huge grammar tables (tables that are larger than 32768
elements) resulting from huge grammars (Cobol grammar, for instance)
can work correctly. You need to have 32-bit integer to index table
bigger than 32768 elements, 16-bit integer is not enough.


The original BtYacc just generated non-working tables larger than
32768 elements without even notifying about the table overflow.




** Make error recovery work correctly when error happens while
processing nested conflicts. Original BtYacc could infinitely cycle in
certain situations that involved error recovery while in nested
conflict.


More detailed explanation: when we have nested conflicts (conflict
that happens while trial-processing another conflict), it leads btyacc
into NP-complete searching of conflict tree. The ultimate goal is
YYVALID operator that selects a particular branch of that tree as a
valid one.


If no YYVALID is found on the tree, then error recovery takes over.
The problem with this is that error recovery is started in the same
state context that exists on the last surveyed branch of the conflict
tree. Sometimes this last branch may be of zero length and it results
in recovering to exactly the same state as existed before entering the
conflict. BtYacc cycles then.


We solved this problem by memorizing the longest path in the conflict
tree while browsing it. If we ever get into error recovery, we restore
state that existed on the longest path. Effectively we say: if we
have an error, let us move forward as far as we possibly could while
we were browsing the conflict tree.




** Introduce YYVALID_NESTED operation in addition to simply YYVALID.
When we have a nested conflict (conflict while processing in trial
mode for another conflict), we want to relate YYVALID to a particular
level of conflict being in trial.


Since we mostly anticipate only 2-level nested conflicts
YYVALID_NESTED tells the parser to satisfy only the internal conflict.
Therefore, in 1-level conflict situation YYVALID_NESTED acts like a
regular YYVALID, but in 2-level conflict it is a no-op and the other
YYVALID for outer conflict will be searched for.




** Improved grammar error diagnostics.




** Improved handling of situation where /tmp directory is missing.
Original btyacc just died quietly when /tmp directory was missing. We
added code that states the problem explicitly. While on UNIX /tmp
directory is always present, it may be missing on WIN32 systems,
therefore diagnosing this situation is important.


Vadim Maslov
vadik@siber.com
--


Post a followup to this message

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