Introduction to Lex and Yacc: Compiler Design Essentials

Slide Note
Embed
Share

Lex and Yacc are essential tools in compiler design. Lex serves as a lexical analyzer, converting source code to tokens, while Yacc is a parser generator that implements parsing based on BNF grammars. Through these tools, strings are processed, and code is generated for efficient compilation. This introduction covers the basic structure of Lex programs, including declarations, rules, and auxiliary functions, as well as how Lex and Yacc work together to transform specifications into executable code.


Uploaded on Sep 19, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


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

  2. What is LEX ? 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. 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 - info@isquareit.edu.in

  3. Lex Source 1. Give lex Program as a input to lex compiler. 2. Lex compiler generate the lex.yy.c file 3. Lex.yy.c is c extension file which can compile easily by using ccompiler . 4. Lex.yy.c as a input to ccompiler , 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 - info@isquareit.edu.in

  4. The structure of LEX programs DECLARATIONS %% RULES %% AUXILIARY FUNCTIONS 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. 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 - info@isquareit.edu.in

  5. 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 - info@isquareit.edu.in

  6. 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 - info@isquareit.edu.in

  7. 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 - info@isquareit.edu.in

  8. 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 - info@isquareit.edu.in

  9. 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 - info@isquareit.edu.in

  10. References 1. Compilers : Principles,Techniques and Tools by Alfred V.Aho, Monica S. Lam, Ravi Sethi and Jeffrey D.Ulman . 2. Modern Compiler Implementation in C by Andrew W.Appel 3. Flex & Bison by John Levine. 4. Lex & Yacc John R. Levine, Tony Mason, Doug Brown Paperback - 366 pages 2nd/updated edition (October 1992) O'Reilly & Associates ISBN: 1565920007 5. http://dinosaur.compilertools.net/ 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 - info@isquareit.edu.in

Related