Programming with LEX & YACC

ISquareIT 262 views 11 slides Jun 24, 2020
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Lex is called as lexical analyzer, it is a first phase of compiler design.
YACC is a parser generator that takes an input file with an attribute-enriched BNF grammar specification.


Slide Content

Programming with LEX & YACC Prof. Prashant Gadakh Department of Computer Engineering International Institute of Information Technology, I²IT www.isquareit.edu.in

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] Lex is called as lexical analyzer, it is a first phase of compiler design. During the primary stage of the compiler peruses the information and changes over strings in the source to tokens. With customary articulations we can indicate examples to lex so it can create code that will permit it to output and match strings in the information. Each example indicated in the contribution to lex has a related activity. What is LEX ?

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] Lex Source Give lex Program as a input to lex compiler. Lex compiler generate the lex.yy.c file Lex.yy.c is c extension file which can compile easily by using “c compiler”. Lex.yy.c as a input to “c compiler”, it will generate output file

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] The structure of LEX programs The declarations section consists of two parts,  auxiliary declarations  and  regular definitions . The auxiliary declarations are copied as such by LEX to the output  lex.yy.c  file. This C code consists of instructions to the C compiler and are not processed by the LEX tool. The auxiliary declarations (which are optional) are written in C language and are enclosed within ' %{ ' and ' %} ' . It is generally used to declare functions, include header files, or define global variables and constants. DECLARATIONS %% RULES %% AUXILIARY FUNCTIONS

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] Simple LEX programs %{ #include < stdio.h > %} %% [0123456789]+ printf ("NUMBER\n"); [a- zA -Z][a-zA-Z0-9]* printf ("WORD\n"); %% Running the Program $ lex example_lex.l gcc lex.yy.c – ll ./ a.out Lex translates the Lex specification into C source file called lex.yy.c which we compile and link with lex library –ll. Then we can execute the resulting program to check that it works as we expected

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] What is YACC ? YACC is a parser generator that takes an input file with an attribute-enriched BNF grammar specification. It generates the output C file y.tab.c containing the function int yyparse (void) that implements its parser. This function automatically invokes yylex () everytime it needs a token to continue parsing.

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] The structure of YACC programs YACC is a parser generator that takes an input file with an attribute-enriched BNF grammar specification. It generates the output C file y.tab.c containing the function int yyparse (void) that implements its parser. This function automatically invokes yylex () everytime it needs a token to continue parsing. %{ /* C includes */ }% /* Other Declarations */ %% /* Rules */ %% /* user subroutines */

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] Simple YACC programs %{ #include< stdio.h > #include " y.tab.h " extern int yylval ; %} %% [0-9]+ {           yylval = atoi ( yytext );           return NUMBER;        } [\t] ; [\n] return 0; . return yytext [0]; %% int yywrap () { return 1; } How To Run: $ yacc -d arithmatic.y $ lex arithmatic.l $ gcc lex.yy.c y.tab.c $./ a.out

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] Compiling YACC programs For Compiling YACC Program: Write lex program in a file file.l and yacc in a file file.y Open Terminal and Navigate to the Directory where you have saved the files. type lex file.l type yacc file.y type cc lex.yy.c y.tab.h - ll type ./ a.out

International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] References Compilers : Principles,Techniques and Tools by Alfred V.Aho , Monica S. Lam, Ravi Sethi and Jeffrey D.Ulman . Modern Compiler Implementation in C by Andrew W.Appel Flex & Bison by John Levine. Lex & Yacc John R. Levine, Tony Mason, Doug Brown Paperback - 366 pages 2nd/updated edition (October 1992) O'Reilly & Associates ISBN: 1565920007 http://dinosaur.compilertools.net/

Thank-You International Institute of Information Technology (I²IT) P-14, Rajiv Gandhi Infotech Park, MIDC Phase – 1, Hinjawadi , Pune – 411057, India http://www.isquareit.edu.in/ [email protected]