Thursday, 13 July 2017

Lexical analyzer for sample language using LEX.

CL1_A3.l:

letter[a-zA-Z]
digit[0-9]
whitespace[\n\t]
underscore[_]

%{
#include<stdio.h>
%}

%%
{whitespace}+ ;

int|float|double|if|else|for|do|while|switch|char|continue|default|enum|void|unsigned|signed|short|long|extrn|goto|case|break|auto|register|return|const|static|struct|typedef|sizeof|union|include|main|stdio|printf|scanf printf("\n%s-->is a Keyword ",yytext);

{digit}+ printf("\n%s--> is an Integer",yytext);

({digit}*\.{digit}+) printf("\n%s--> is a Float Value",yytext);

"+"|"-"|"*"|"/"|"%" printf("\n%s--> is an Arithmetic Operator",yytext);

"&"|"|"|"!"|"^" printf("\n%s--> is a Bitwise Operator",yytext);

"&&"|"||" printf("\n%s--> is a Logical Operator",yytext);
 
"<"|">"|"<="|">="|"==" printf("\n%s--> is a Relational Operator",yytext);

"="|"=+"|"=-"|"=*"|"=/"|"=%" printf("\n%s--> is a ASSIGNMENT OPERATOR",yytext);

"%d"|"%s"|"%c"|"%f"|"%e" printf("\n%s--> is a Format Specifier",yytext); 

"{"|"}"|"["|"]"|"("|")"|"#"|"'"|"."|"\""|"\\"|";"|"," printf("\n%s--> is a Special Character",yytext); 

{letter}({letter}|{digit}|{underscore})* printf("\n%s--> is an Identifier",yytext);

\#({letter}*\<{letter}*.{letter}*>) printf("\n%s--> is a Header File",yytext);
%%

main()
{
    yyin=fopen("CL1_A3.c","r");
    yylex();
    fclose(yyin);
    printf("\n");
}

int yywrap()
{
    return 1;
}


CL1_A3.c:

#include<stdio.h>

main()
{
    int i=0;
    int a[10],b[3][3];
    float f;
    printf("HELLO");
    if(i==0)
    {
        f=10.5;
    }
    return 0;
}

OUTPUT:


No comments:

Post a Comment