Programming with LEX YACC Prof Prashant Gadakh Department

  • Slides: 10
Download presentation
Programming with LEX & YACC Prof. Prashant Gadakh Department of Computer Engineering International Institute

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

What is LEX ? • Lex is called as lexical analyzer, it is a

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

Lex Source 1. Give lex Program as a input to lex compiler. 2. Lex

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 “c compiler”. 4. 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 - info@isquareit. edu. in

The structure of LEX programs DECLARATIONS %% RULES %% AUXILIARY FUNCTIONS The declarations section

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

Simple LEX programs %{ #include <stdio. h> %} %% [0123456789]+ printf("NUMBERn"); [a-z. A-Z][a-z. A-Z

Simple LEX programs %{ #include <stdio. h> %} %% [0123456789]+ printf("NUMBERn"); [a-z. A-Z][a-z. A-Z 0 -9]* printf("WORDn"); %% 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

What is YACC ? • YACC is a parser generator that takes an input

What is YACC ? • YACC is a parser generator that takes an input file with an attributeenriched 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

The structure of YACC programs • YACC is a parser generator that takes an

The structure of YACC programs • YACC is a parser generator that takes an input file with an attributeenriched 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

Simple YACC programs %{ #include<stdio. h> #include "y. tab. h" extern int yylval; %}

Simple YACC programs %{ #include<stdio. h> #include "y. tab. h" extern int yylval; %} How To Run: %% [0 -9]+ { $yacc -d arithmatic. y yylval=atoi(yytext); return NUMBER; $lex arithmatic. l } [t] ; $gcc lex. yy. c y. tab. c [n] return 0; . return yytext[0]; $. /a. out %% int yywrap() { return 1; }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

Compiling YACC programs For Compiling YACC Program: • Write lex program in a file.

Compiling YACC programs For Compiling YACC Program: • Write lex program in a file. l and yacc in a 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

References 1. Compilers : Principles, Techniques and Tools by Alfred V. Aho, Monica S.

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 2 nd/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