0% found this document useful (0 votes)
251 views5 pages

YACC

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

YACC(Yet Another Compiler Compiler) is a tool used to generate

parser that is used to parse certain rules that are given to the tool. It
is used to translate Context Free Grammar (CFG) (whose
specifications are given in .y file) into a C implementation(.c file) of a
corresponding push down automation. After compiling the C
program, a parser is generated which can accept grammar format
that we have given.

The yacc (yet another compiler compiler) utility provides a general


tool for imposing structure on the input to a computer program.
Before using yacc, we prepare a specification that includes:

 A set of rules to describe the elements of the input.

 Code to be invoked when a rule is recognized.

 Either a definition or declaration of a low-level scanner to


examine the input.

yacc then turns the specification into a C-language function that


examines the input stream. This function, called a parser, works by
calling the low-level scanner.
The scanner, called a lexical analyzer, picks up items from the input
stream. The selected items are known as tokens. Tokens are
compared to the input construct rules, called grammar rules.

When one of the rules is recognized, the code you have supplied for
the rule is invoked. This code is called an action. Actions are
fragments of C-language code. They can return values and use values
returned by other actions. The most important part while creating a
compiler using YACC is the specification of grammar rules. The
compilers execution depends mostly on the grammar rules.

YACC program consists of three parts:

 Declarations

 Rules

 Auxiliary Functions

Declarations
The Declarations part is further divided into two parts: YACC
declarations and C declarations. The C declarations are delimited by
%{ and %}. The content of this section is copied by the YACC into
.c file which is generated when we execute the .y file without any
change. The YACC declarations consists of the tokens which are
returned by the lexical analyzer. The function yylex() is used to read
the tokens.
Example:
/* Beginning of Declarations part */

%{
/*Beginning of C declarations*/

/*End of C declarations*/
%}
/*Beginning of YACC declarations */

/*End of YACC declarations */

/* End of Declarations Part */

%%

Rules
A rule is the heart of the compiler on the basis of which the compiler
parses the code. In YACC program, the rule consists of two parts: the
action part and the production part. A rule is specified of the form:
production_head : production_body {action in
C } ;

Productions
The production consists of two parts: production body and
production head. For example:
Expression : Expression ‘*’ Expression
The Expression is the non-terminal symbol and ‘*’ is the terminal
symbol. The LHS of : is called the Head part and the RHS of : is called
the body of the production. We can use ‘multiply’ token also in place
of ‘*’ token. In such case, we have to change the name in the
declaration section. The head of a production is always a non-
terminal. Every non-terminal in the grammar must appear in the
head part of at least one production. Note that a non-terminal in the
head part of a production may have one or more production bodies
separated by a “|”. The non-terminal has four production bodies
Expression '+' Expression , Expression '*' Expression , '(' Expression ')'
and DIGIT. The first production body has an associated print action
op_printf("+") and the second production body has an associated
action op_print("*"). yyparse() executes the action only when the
body Expression '+' Expression has been matched with the input. The
action part of a single production may have several statements of C
code.
Actions
The action part is enclosed within a ‘{‘ and ‘}’ which consists of C
statements. These statements are executed when the input matches
with the production rule. Example:
expr: DIGIT {printf("NUM%d ",pos);}

In this rule, when the input matches with the body of the production
DIGIT, it is reduced to expr and the action {printf("NUM%d ",pos);} is
executed.

Auxiliary Functions
The Auxiliary function consists of three mandatory functions:
yyerror(), yylex() and main(). One can add their own functions if they
want depending on the requirements. The main() function invokes
the yyparse() to parse the input. If yylex() is not defined in the
auxiliary routines sections, then it should as #include”lex.yy.c”.

Output Files:

 The output of YACC is a file named y.tab.c


 If it contains the main() definition, it must be compiled to be
executable.
 Otherwise, the code can be an external function definition for
the function int yyparse()
 If called with the –d option in the command line, Yacc produces
as output a header file y.tab.h with all its specific definition
(particularly important are token definitions to be included, for
example, in a Lex input file).
 If called with the –v option, Yacc produces as output a file
y.output containing a textual description of the LALR(1) parsing
table used by the parser. This is useful for tracking down how
the parser solves conflicts.

For Compiling YACC Program:

1. Write lex program in a file file.l and yacc in a file file.y


2. Open Terminal and Navigate to the Directory where you have
saved the files.
3. type lex file.l
4. type yacc file.y
5. type cc lex.yy.c y.tab.h -ll
6. type ./a.out

You might also like