Experiment No 10 Compiler
Experiment No 10 Compiler
10
AIM : To study the YACC tools.
Theory:
Yacc Yet Another Compiler-Compiler
Yacc is a computer program that serves as the standard parser generator on
Unix systems. The name is an acronym for "Yet Another Compiler Compiler." It
generates a parser (the part of a compiler that tries to make sense of the input)
based on an analytic grammar written in BNF notation. Yacc generates the code
for the parser in the C programming language.
Yacc provides a general tool for imposing structure on the input to a computer
program. The Yacc user prepares a specification of the input process; this includes
rules describing the input structure, code to be invoked when these rules are
recognized, and a low-level routine to do the basic input. Yacc then generates a
function to control the input process. This function, called a parser, calls the user-
supplied low-level input routine (the lexical analyzer) to pick up the basic items
(called tokens) from the input stream. These tokens are organized according to
the input structure rules, called grammar rules; when one of these rules has been
recognized, then user code supplied for this rule, an action, is invoked; actions
have the ability to return values and make use of the values of other actions. Yacc
provides a general tool for imposing structure on the input to a computer
program.
The Yacc user prepares a specification of the input process; this includes
rules describing the input structure, code to be invoked when these rules are
recognized, and a low-level routine to do the basic input. Yacc then generates a
function to control the input process. This function, called a parser, calls the user-
supplied low-level input routine (the lexical analyzer) to pick up the basic items
(called tokens) from the input stream. These tokens are organized according to
the input structure rules, called grammar rules; when one of these rules has been
recognized, then user code supplied for this rule, an action, is invoked; actions
have the ability to return values and make use of the values of other actions.
Yacc is written in a portable dialect of C[1] and the actions, and output
subroutine,are in C as well. Moreover, many of the syntactic conventions of Yacc
follow C.
The heart of the input specification is a collection of grammar rules. Each rule
describes an allowable structure and gives it a name. For example, one grammar
rule might be
date : month_name day ',' year ;
Here, date, month_name, day, and year represent structures of interest in the
input process; presumably, month_name, day, and year are defined elsewhere.
The comma ``,'' is enclosed in single quotes; this implies that the comma is to
appear literally in the input. The colon and semicolon merely serve as punctuation
in the rule, and have no significance in controlling the input. Thus, with proper
definitions, the input July 4, 1776 might be matched by the above rule. Yacc
cannot handle all possible specifications, its power compares favorably with
similar systems; moreover, the constructions which are difficult for Yacc to
handle are also frequently difficult for human beings to handle. Some users have
reported that the discipline of formulating valid Yacc specifications for their input
revealed errors of conception or design early in the program development.