Showing posts with label LEX. Show all posts
Showing posts with label LEX. Show all posts

Floating Point & Integers Lex Program Compiler Design

Program:


// frn.l


%{

%}

DIGIT    [0-9]

%%

{DIGIT}*        {ECHO;printf(" Integer");}
{DIGIT}*?\.{DIGIT}*    {ECHO;printf(" Float ");}

%%
main()
{
    yylex();
}


Output:


nn@linuxmint ~ $ lex frn.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<test1.txt
1 Integer
500 Integer
10.1 Float
.1 Float
1.0 Float

nn@linuxmint ~ $

// input : test1.txt

1
500
10.1
.1
1.0

Convert Lowercase to Uppercase & Reverse - Lex Progam - Compiler Design

CHANGE CASE - LEX - PROGRAM


Program:

// Lex file: cap.l


lower [a-z]
CAPS  [A-Z]
space    [ \t\n]

%%
{lower}         {printf("%c",yytext[0]- 32);}
{CAPS}        {printf("%c",yytext[0]+ 32);}
{space}        ECHO;
.                    ECHO;
%%

main()
{
    yylex();
   
}

Output:
nn@linuxmint ~ $ lex cap.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<tst.txt
WWW.2K8618.BLOGSPOT.COM
sanjana    jamsheena chaithanya neethu
GOVINDAPRASAD VIPIN ADARSH SHIVIN
baby brinda kavya helen
SALMAN TINU RICHARD  SIBIN
SHIVIN laji NABEEL
www.2k8cse.cu.cc
nn@linuxmint ~ $

// tst.txt

www.2k8618.blogspot.com
SANJANA    JAMSHEENA CHAITHANYA NEETHU
govindaprasad vipin adarsh shivin
BABY BRINDA KAVYA HELEN
salman tinu richard  sibin
shivin LAJI nabeel
WWW.2K8CSE.CU.CC

Count of Words Starting with a - Lex Program - Compiler Design

Program:

// Lex file: aa.l

%{
        int count=0;
%}
alpha    [a-zA-Z]
digit      [0-9]
space    [ \t\n]
start      ^a
%%

{start}                                           {count++;}
{space}(a|A)({alpha}|{digit})*    {count++;}
.                                                      ;

%%

main()
{
    yylex();
    printf("count= %d\n",count);
}
Output:

nn@linuxmint ~ $ lex aa.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<tst.txt

count= 6
nn@linuxmint ~ $



// tst.txt

afsal ARIFA aaa www.2k8618.blogspot.com
aiswarya saranya    sooraj
arun reshmi
a www.2k8cse.cu.cc

Select Lines Ending with 'com' - Lex Program - Compiler Design

Program:

// Lex file: com.l

%{
int count=0;
%}

%%
.*com\n {count++;ECHO;}
. ;

%%

main()
{
    yylex();
    printf("\nCount= %d\n",count);
    return 0;
   
}

Output:
nn@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt

www.google.com
www.yahoo.com
www.2k8618.blogspot.com




Count= 3
nn@linuxmint ~ $

// com.txt

www.2k8cs.tk
www.google.com
www.yahoo.com
www.2k8618.blogspot.com
www.2k8cse.cu.cc

Convert Decimal Number to Hexadecimal Number in a File - Lex Program - Compiler Design

Program:




// lex file: conv.l

%{
    #include<stdio.h>
    int num,r,digit=0,count,pcount=0,i;
    char a[20];
%}
DIGIT [0-9]

%%

{DIGIT}+ {    num=atoi(yytext);
        while(num!=0)
        {
            r=num%16;
            digit='0'+r;
            if(digit>'9')
            digit+=7;
            a[count++]=digit;
            num=num/16;
        }
        for(i=count-1;i>=pcount;--i)
                printf("%c",a[i]);
                pcount=count;
        }
.|\n    ECHO;
       
%%
main()
{
    yylex();
    return 0;
}      

Output:
nn@linuxmint ~ $ lex conv.l
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out<c.txt
DEEPAK A
F HUNAIF
NAVITHA 10
15 RAJINA
ABID 18
1A SANITHA
nn@linuxmint ~ $


// c.txt

DEEPAK 10
15 HUNAIF
NAVITHA 16
21 RAJINA
ABID 24
26 SANITHA

Count The Positive numbers, Negative numbers & Fractions - Lex Program - Compiler Design

Aim:
Write a lex program to count the number of Positive numbers, Negative numbers & Fractions.

Program:

// lex file: a.l

%{
    int postiveno=0;
    int negtiveno=0;
    int positivefractions=0;
    int negativefractions=0;
%}

DIGIT [0-9]
%%

\+?{DIGIT}+                              postiveno++;
-{DIGIT}+                                  negtiveno++;

\+?{DIGIT}*\.{DIGIT}+            positivefractions++;
-{DIGIT}*\.{DIGIT}+                negativefractions++;
. ;   
%%

main()
{
    yylex();
    printf("\nNo. of positive numbers: %d",postiveno);
    printf("\nNo. of Negative numbers: %d",negtiveno);
    printf("\nNo. of Positive fractions: %d",positivefractions);
    printf("\nNo. of Negative fractions: %d\n",negativefractions);
}

Output:

nn@linuxmint ~ $ lex a.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<a.txt


No. of positive numbers: 2
No. of Negative numbers: 3
No. of Positive fractions: 4
No. of Negative fractions: 5
nn@linuxmint ~ $

// Input file: a.txt
+12,-123,1.1,-1.1,12,-2,-3,2.1,3.2,5.1,-5.5,-6.1,-7.7,-8.8

Count the Vowels - Lex Program

Program:

// lex file: vw.l

%{
int count=0;
%}

%%

[aeiouAEIOU] {count++;ECHO;}

%%
main()
{
    yylex();
    printf("\nNumber of vowels= %d\n",count);
    return 0;
}

Output:

nn@linuxmint ~ $ lex vw.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<vw.txt
www.2k8618.blogspot.com
www.2k8cs.tk
www.google.com
www.gmail.com


Number of vowels= 10
nn@linuxmint ~ $

Count The Number of lines ending with "com" - Lex Program - Compiler Design

Program:

//Lex file: com.l

%{
int count=0;
%}
DIGIT [0-9]
ALPHA [a-zA-Z]
%%
({ALPHA}|{DIGIT})*com {count++;}
%%

main()
{
    yylex();
    printf("Count= %d\n",count);
    return 0;
   
}

Output :

n@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt
www.2k8618.blogspot.
www.2k8cs.tk
www.google.
www.gmail.

Count= 3
nn@linuxmint ~ $

Parser for SWITCH Statements - YACC Program - Compiler Design



Program:
(sw.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
switch    return SWITCH;
case return CASE;
break return BREAK;
default return DEFAULT;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(sw.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE OR AND ELSE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%

S       : ST {printf("Input accepted.\n");exit(0);}
         ;
ST     :    SWITCH '(' ID ')' '{' B '}'
         ;
   
B       :    C
         |    C    D
        ;
   
C      :    C    C
        |    CASE NUM ':' E ';'
        | BREAK ';'
        ;

D      :    DEFAULT    ':' E ';' BREAK ';'
        ;
    
E    : ID'='E
    | E'+'E
    | E'-'E
    | E'*'E
    | E'/'E
    | E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;

%%

#include "lex.yy.c"

main()
{
printf("Enter the exp: ");
yyparse();
}

Output:
students@cselab-desktop:~$ lex sw.l
students@cselab-desktop:~$ yacc sw.y
conflicts: 2 shift/reduce
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: switch(a)
{
case 1: a=1;
    break;
case 2: a=2;
    break;
default:a=0;
break;
}
Input accepted.
students@cselab-desktop:~$

Parser for DO WHILE Statements - YACC Program - Compiler Design





Program:

(dowh.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
do    return DO;
while    return WHILE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(dowh.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM DO WHILE LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'

%%

S : ST1 {printf("Input accepted.\n");exit(0);};
ST1    :    DO '{' ST '}' WHILE'(' E2 ')'';';
ST      :     ST ST
          | E';'
          ;
E       : ID'='E
         | E'+'E
         | E'-'E
         | E'*'E
         | E'/'E
         | E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         | ID
         | NUM
         ;
E2     : E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         | ID
         | NUM
         ;

%%

#include "lex.yy.c"

main()
{
   printf("Enter the exp: ");
   yyparse();
}

Output:

students@ccflab-desktop:~$ lex dowh.l
students@ccflab-desktop:~$ yacc dowh.y
conflicts: 2 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp: do{a=b+1;}while(a<10);
Input accepted.
students@ccflab-desktop:~$

Parser for WHILE Statements - YACC Program - Compiler Design



Program:

(wh.l)

alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
while    return WHILE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(wh.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM WHILE LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S        : ST1 {printf("Input accepted.\n");exit(0);};
ST1    :    WHILE'(' E2 ')' '{' ST '}'
ST      :     ST ST
          | E';'
          ;
E       : ID'='E
          | E'+'E
          | E'-'E
          | E'*'E
          | E'/'E
          | E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;
E2     : E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;

%%

#include "lex.yy.c"

main()
{
   printf("Enter the exp: ");
   yyparse();
}


Output:

students@ccflab-desktop:~$ lex wh.l
students@ccflab-desktop:~$ yacc wh.y
conflicts: 2 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp: while(a>1){ b=1;}
Input accepted.

Parser for IF-THEN-ELSE Statements - YACC Program - Compiler Design





Program:

(Lex Program: ift.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]   
if    return IF;
then    return THEN;
else    return ELSE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(Yacc Program: ift.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM IF THEN LE GE EQ NE OR AND ELSE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%

S      : ST {printf("Input accepted.\n");exit(0);};
ST    : IF '(' E2 ')' THEN ST1';' ELSE ST1';'
        | IF '(' E2 ')' THEN ST1';'
        ;
ST1  : ST
        | E
        ;
E    : ID'='E
      | E'+'E
      | E'-'E
      | E'*'E
      | E'/'E
      | E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;
E2  : E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;

%%

#include "lex.yy.c"

main()
{
  printf("Enter the exp: ");
  yyparse();
}
       
Output:

students@cselab-desktop:~$ lex ift.lex
students@cselab-desktop:~$ yacc ift.y
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: if(a==1)  then b=1; else b=2;
Input accepted.
students@cselab-desktop:~$

Expression Evaluation - Yacc Program - Compiler Design

Program:

// Yacc Program: expr.y

%{
#include"stdio.h"
%}
%token DIGIT
%%
S       : E '\n' {printf("%d\n",$1);return 1;}
        ;
E       : E '+' T { $$ = $1 + $3 ;}
        | T
        ;
T       :T '-' FF { $$ = $1 - $3 ;}
        | FF
        ;
FF      :FF '/' FFF { $$ = $1 / $3 ;}
        |FFF;
FFF     :FFF '*' F { $$ =$1*$3 ;}
        |F
        ;
F       : '(' E ')' { $$ = $2 ;}
        |DIGIT
        ;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
        yylval=c-'0';
        return DIGIT;
}
return c;
}

main()
{
printf("Enter the exp:");
yyparse();
}

Output:
nn@linuxmint ~ $ lex io.l
nn@linuxmint ~ $ yacc expr.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the exp:5*(5+1)-9/2
26
nn@linuxmint ~ $

Parser for IF-THEN Statements - YACC - LEX - Compiler Design

Program:

(if.l)

ALPHA [A-Za-z]
DIGIT [0-9]
%%
[ \t\n]
if                return IF;
then                return THEN;
{DIGIT}+            return NUM;
{ALPHA}({ALPHA}|{DIGIT})*         return ID;
"<="                return LE;
">="                return GE;
"=="                return EQ;
"!="                return NE;
"||"                return OR;
"&&"                return AND;
.                return yytext[0];
%%          

(if.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM IF THEN LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S   : ST {printf("Input accepted.\n");exit(0);};
ST  :    IF '(' E2 ')' THEN ST1';'
    ;
ST1 : ST
    | E
    ;      
E   : ID'='E
    | E'+'E
    | E'-'E
    | E'*'E
    | E'/'E
    | E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;
E2  : E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;
%%

#include "lex.yy.c"

main()
{
printf("Enter the statement: ");
yyparse();
}  
Output:
nn@linuxmint ~ $ lex if.l
nn@linuxmint ~ $ yacc if.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the statement: if(i>) then i=1;
syntax error
nn@linuxmint ~ $ ./a.out
Enter the statement: if(i>8) then i=1;
Input accepted.
nn@linuxmint ~ $

Postfix Evaluaion YACC LEX S6

YACC Program:(p1.y)

%{
#include<stdio.h>
#include<assert.h>
 int Pop();
 int Top();
 void Push(int val);
%}

%token T_Int

%%
S : S E    {printf(" %d\n", Top());}
  |
  ;

E : E E '+' {Push(Pop()+Pop());}
  | E E '-' {int op2=Pop();Push(Pop()-op2);}
  | E E '*' {Push(Pop() * Pop());}
  | E E '/' {int op2=Pop();Push(Pop()/op2);}
  | T_Int   {Push(yylval);}
  ;
%%
#include "lex.yy.c"
 int stack[100],count=0;

 int Pop()
 {
  assert(count>0);
  return stack[--count];
 }

 int Top()
 {
  assert(count>0);
  return stack[count-1];
 }

 void Push(int val)
 {
  assert(count<sizeof(stack)/sizeof(int));
  stack[count++]=val;
 }

int main()
 {
  return yyparse();
 }

Lex progam:(p1.l)

DIGIT    [0-9]
%%
{DIGIT}+ {yylval=atoi(yytext);return T_Int;}
[-+*/] {return yytext[0];}
\n    yyterminate();
.    ;

Output:

students@ccflab-desktop:~$ lex p1.l
students@ccflab-desktop:~$ yacc p1.y
conflicts: 1 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
2 3 +
5
students@ccflab-desktop:~$ ./a.out
2 3 *
6
students@ccflab-desktop:~$ ./a.out
3 2 -
1
students@ccflab-desktop:~$ ./a.out
6 3 /
2
students@ccflab-desktop:~$


Implement Finite Automata in lex-Odd no of 'a's

                                                 




program:

%{

%}

reg (b|ab*a)*ab* 

%%

{reg}      printf("%s Accepted",yytext);
.*     printf("%s Not Accepted",yytext);


%%

main()
{
yylex();
return 0;
}

Input:x.c
a
ab
aab
aba
abba

Output:
students@ccflab-desktop:~$ lex b.l
students@ccflab-desktop:~$ gcc lex.yy.c -lfl
students@ccflab-desktop:~$ ./a.out<x.c
a Accepted
ab Accepted
aab Not Accepted
aba Not Accepted
abba Not Accepted
students@ccflab-desktop:~$ 

Select Lines Starting with 'm' - Lex Program - Compiler Design

Program:

%{
%}
reg1 ^m.*\n

%%
{reg1} {ECHO;}
. ;
%%
Output:

( input file : c1.c

#include<stdio.h>
main ()
{
    int i,n,fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
            }
    printf("Factorial=%d\n",fact);
}

)


nn@linuxmint ~ $ lex l7.lex
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c

main ()











nn@linuxmint ~ $

Count The Number of Lines,Words and Characters - Lex Program - Compiler Design

Program:
%{
   
    int lnno=0,wordno=0,charno=0;
%}

word [.* .*\t]
eol [\n]

%%
{word}    {wordno++; charno+=yyleng;}
{eol}     {charno++;lnno++;wordno++;}
.    {charno++;}
%%
main()
{
    yylex();
    printf("Line number= %d\n",lnno);
    printf("Word number= %d\n",wordno);
    printf("Character number= %d\n",charno);
    return 0;
}

Output:

nn@linuxmint ~ $ lex l6.l
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c
Line number= 13
Word number= 43
Character number= 232
nn@linuxmint ~ $
( input file : c1.c

#include<stdio.h>
main ()
{
    int i,n,fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
    }
    printf("Factorial=%d\n",fact);

}
)

Remove All The Occurrences of a Word or Letter - Lex Program - Compiler Design

Program:

%{
  

%}

%%
"printf"    ;
(.|\n)        ECHO;
%%
main()
{
yylex();
return 0;
}

Output:
( input file: c1.c
#include<stdio.h>
main ()
{
    int i,n,fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
    }
    printf("Factorial=%d\n",fact);

}
)


nn@linuxmint ~ $ lex l5.lex
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c
#include<stdio.h>
main ()
{
    int i,n,fact=1;
    ("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
    }
    ("Factorial=%d\n",fact);

}


nn@linuxmint ~ $

Count The Number of Occurrences of a Particular Word or Letter - Lex Program - Compiler Design

Program:

%{
    int icount=0,factcount=0;
%}

%%

fact    factcount++;
i         icount++;
(.|\n)    ;

%%
main()
{
    yylex();
    printf("Count of \"fact\"= %d \nCount of letter 'i' = %d\n",factcount,icount);  
    return 0;
}

Output:


nn@linuxmint ~ $ lex l4.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<c1.c
Count of "fact"= 5
Count of letter 'i' = 17
nn@linuxmint ~ $


// Input file: c1.c
#include<stdio.h>
main ()
{
    int i,n,fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
    }
    printf("Factorial=%d\n",fact);

}
Related Posts Plugin for WordPress, Blogger...