sureshmoharana2013
378 views
14 slides
Mar 26, 2019
Slide 1 of 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
About This Presentation
Compiler Design Lab
Size: 58.2 KB
Language: en
Added: Mar 26, 2019
Slides: 14 pages
Slide Content
LEX PROGRAMMING
What is Lex ? Lex /Flex allows to specify a LA by specifying regular definitions to describe pattern for tokens. The input notation for the lex tool is reffered as the Lex language and tool itself is the Lex tool. Lex compiler transforms the input patterns into a TD and generates a code, in a file called lex.yy.c that simulates the TD.
How Lex Works? lex.l lex.yy.c lex.yy.c a.out Input Stream Sequence of Tokens Lex Compiler C Compiler a.out
Lex in Detail User Tokens Lex ( regex+action ) yylex () { } Optional Driver Code GCC
Lex Specification A lex program has following form, declarations %% translation rules %% driver functions Note: %% is the smallest possible lex program
Declarations A series of rules of the form name definitions E.g. DIGIT [0-9] COMMENTSTART “\*” ID [a- zA -Z][a-zA-Z0-9]*
Contd... The parts required to be copied to lex.yy.c should be written inside %{... %}. E.g. %{ //comment #include< stdio.h > %}
Translation Rules Rules portion of the lex program contains a sequence of rules of the form, pattern action E.g. {letter}({letter}|{digit})* return id; {digit}+ return num; Note: action must begin in same line
Sample Program %{ #include< stdio.h > %} dgt [0-9] %% { dgt }+ return atoi ( yytext ); %% v oid main() { int val,sum =0,n=0; while(( val = yylex ())>0){ sum= sum+val ; n++; } If(n>0) printf (“ avg =%d\ n”,sum /n); }
Compiling Lex Installation: sudo apt-get install flex Step-1: lex demo.l Step-2: gcc lex.yy.c – ll Step-3: ./ a.out Note: - ll is used to link the default yywarp ().
Lex Functions y ylex (): each invocation scans the input where left off, returns 0 on EOF y ytext : buffer holds the characters that match the pattern, char * yytext y yleng : length of lexeme matched, return an integer y yin : the input stream pointer, FILE * y yout : the output stream pointer, FILE *
Conflict Resolution in Lex Conflict possible when more than one pattern matches the input or lexeme. Rules to avoid the conflict, The longest match is chosen If multiple rules match, rule enlisted top most is chosen
Contd... E.g. Input: CS335 (CS) { printf (“Department”);} (CS)[0-9]3 { printf (“Course”);} [a- zA -Z]+[0-9]+ { printf (“ AnythingElse ”);} Output: Course E.g. Input: CS3351 Output: AnythingElse
Some More Translation Rules [ \t\n] ; (represents no action) [a-z]+ ECHO; (display the matched lexeme) . (class of characters except \n) [^a- zA -Z] (any character is not a letter) ab?c (ac or abc )