Overview-Lex and Yacc Program

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Review of Parser

• Parser invokes scanner for tokens.


• Parser analyze the syntactic structure
according to grammars.
• Finally, parser executes the semantic routines.
Introduction to yacc (bison- Linux
terminology)
• yacc – yet another compiler compiler.
• An LALR(1) parser generator.
• Yacc generates
– Tables – according to the grammar rules.
– Driver routines – in C programming language.
– y.output – a report file.
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
Definition Section
• C source code, include header 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.
Syntax-YACC Program

• Definitions | Declarations
%token

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

• Auxiliary functions
• 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.
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: yyval= a to i (yytext)
converts string to int and stores in variable yyval.

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)
program and matches with the token printf(“valid string”);
}
declaration in YACC program which is
Int yyerror()
accessible to the Lex program {
printf(string is not accepted”);
exit(0); }
yywrap()
{ return 1;
Linking lex & yacc

Commands for execution

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

Hathal & Ahmad 11

You might also like