0% found this document useful (0 votes)
55 views14 pages

Overview of Yacc Program

The document discusses the yacc tool, which is a parser generator that generates tables and driver routines in C from a grammar specification. It cooperates with the lex tool to generate tokens. A yacc input file consists of three sections - definitions, rules, and auxiliary functions. The rules section defines the grammar rules and associated semantic actions. Yacc invokes yylex() to retrieve tokens and executes actions associated with parsing rules. It returns 0 if the input is accepted by the grammar.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views14 pages

Overview of Yacc Program

The document discusses the yacc tool, which is a parser generator that generates tables and driver routines in C from a grammar specification. It cooperates with the lex tool to generate tokens. A yacc input file consists of three sections - definitions, rules, and auxiliary functions. The rules section defines the grammar rules and associated semantic actions. Yacc invokes yylex() to retrieve tokens and executes actions associated with parsing rules. It returns 0 if the input is accepted by the grammar.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to yacc tool

(bison- Linux terminology)


• yacc – yet another compiler compiler.
• A LALR(1) parser generator.
• Yacc generates
– Tables – according to the grammar rules.
– Driver routines – in C programming language.
– y.output – a report file.
Review of Parser
• Parser invokes scanner for tokens.
• Parser analyze the syntactic structure
according to grammars.
• Finally, parser executes the semantic routines.
Cooperate with lex (flex- Linux terminology)
• Invokes yylex() automatically.
• Generate y.tab.h file through the -d option.
• The lex input file must contains y.tab.h
• For each token that lex recognized, a number
is returned (from yylex() function.)
Writing Yacc Input File
• A Yacc input file consists of three sections:
– Definition
– Rules
– Auxiliary functions
• Separated by %%
• Similar to lex (actually, lex imitates yacc)
Structure of YACC program

• Definitions
• Rules
• Auxiliary functions
Syntax-YACC Program
•Definitions | Declarations
%token

%%
•Rules
Head:body1 {action1} | body2 {action2}
%%

•Auxiliary functions
Definitions Section

• C source code, include files, etc.


• %token
• Yacc invokes yylex() when a token is required.
• All terminal symbols should be declared
through %token.
• Yacc produces y.tab.h by %token definitions.
The file contains the following sections:
•Declarations section. This section contains entries that:
– Include standard I/O header file
– Define global variables
– Define the list rule as the place to start processing
– Define the tokens used by the parser
– Define the operators and their precedence
• Rules section. The rules section defines the rules that parse the input stream.
– %start - Specifies that the whole input should match stat.
– %union - By default, the values returned by actions and the lexical analyzer are
integers. yacc can also support values of other types, including structures. In
addition, yacc keeps track of the types, and inserts appropriate union member names so that
the resulting parser will be strictly type checked. The yacc value stack is declared to be a union
of the various types of values desired. The user declares the union, and associates union
member names to each token and nonterminal symbol having a value. When the value is
referenced through a $$ or $n construction, yacc will automatically insert the appropriate union
name, so that no unwanted conversions will take place.
– %type - Makes use of the members of the %union declaration and gives an individual type for
the values associated with each part of the grammar.
– %toksn - Lists the tokens which come from lex tool with their type.
Semantic Routines
• The action in semantic routines are executed for the production rule.
• The action is actually C source code.
• LHS: $$ RHS: $1 $2 ……
• Default action: { $$ = $1; }
• Action between a rule is allowed. For ex:
expression : simple_expression
| simple_expression {somefunc($1);} relop simple_expression;
Example- To find Boolean value

Rules section with semantic actions


%%
start:
expr ‘;’ {printf(value is %d\n”, $1);}
expr :
BOOLEAN {$$ = $1;}
| expr AND expr {$$ = $1 && $3;}
| expr OR expr {$$ = $1 && $3;}
| NOT expr {$$ = $2;}
%%
Variables and functions used in YACC program
1) yytext-pointer to the input character stream or matched
input string.

2) yylval: values associated with the tokens are returned by Lex


in the variable yylval.
Ex: yylval= a to i (yytext)
converts string to int and stores in variable yylval.

3) yywrap(): called by lex or yacc tool when input is exhausted


or finished and returns 1 when input is finished.

4) yyparse(): responsible for parsing. It reads tokens and


executes actions-which are given in YACC program. Returns
0 if the string is accepted.
Example: Check whether the string aaabbb is accepted by the given grammar

below using YACC or not


S->aSb| ε YACC Program
Lex program %{ #include<stdio.h>
#include<conio.h>
%}
% { #include “y.tab.h” %token A B // tokens defined in YACC
%}
(Rules section)
%%
%% start: S ‘\n’ {return \n;}
[a] {return A; } S: A S B
| ;
[b] {return B; } %%
[\n] {return 0; }
(Auxiliary function section)
int main()
%% {
printf(enter the string”);
//”y.tab.h” has all the tokens used in YACC if (yyparse==0)
printf(“valid string”);
program and matches with the token }
declaration in YACC program which is yyerror()
accessible by the Lex program {
printf(string is not accepted”);
exit(0); }
yywrap()
{ return 1;
}
Linking lex & yacc program

Commands for execution

1)lex bas.l
2)yacc -d bas.y
3)gcc lex.yy.c y.tab.c -ll
4)./a.out

After compiling yacc program, it generates two files namely y.tab.h


and .c files

You might also like