YACC
YACC
YACC
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.
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.
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 */
%%
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: