0% found this document useful (0 votes)
16 views21 pages

Saviour Lex

Compiler desing

Uploaded by

pathocentre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

Saviour Lex

Compiler desing

Uploaded by

pathocentre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

FOR CREATING A LEX FILE: gedit filename.

FOR COMPILING A LEX FILE: lex filename.l

gcc lex.yy.c -ll

FOR SEEING OUTPUT: ./a.out

FOR CREATING A YACC FILE: gedit filename.y

NOTE: TO COMPILE YACC CODES:

yacc -d assign20.y

lex assign20.l

gcc lex.yy.c y.tab.c

FOR SEEING OUTPUT: ./a.out

1.Program in lex whether user input is integer/alphabet/decimal.

CODE:

%{

#include <stdio.h>

%}

%%

[0-9]+"."[0-9]+ {printf("\nDecimal Numbers");}

[0-9]+ {printf("\nINteger");}

[a-zA-Z]+ {printf("\nAlphabets");}

.; {printf("\nOther than alphabets");}

%%

void main()

printf("\n Enter input");

yylex();

int yywrap()

{
return 1;

2. lex code to print factors of a number.

%{

#include <stdio.h>

int n,i,sum=0;

%}

%%

[0-9]+ {n=atoi(yytext);

for(i=1;i<=n;i++)

if(n%i==0)

printf("\n Factors are %d",i);

%%

int main()

printf("Enter input");

yylex();

3. Program in lex to check perfect number

%{

#include<stdio.h>
int n,i,sum=0;

%}

%%

[0-9]+ {n=atoi(yytext);

for(i=1;i<n;i++)

if(n%i==0)

printf("\n Factors are :::%d",i);

sum=sum+i;

if(sum==n)

printf("\n Perfect No.");

else

printf("\n Not perfect no");

%%

void main()

printf("\n Enter the input:");

yylex();

int yywrap()

return 1;

4. Lex code for decimal to binary

%{
#include<stdio.h>

int i,b=0,r,p=1,num;

%}

%%

[0-9]+ {num=atoi(yytext);

while(num>0)

r=num%2;

b+=r*p;

p*=10;

num/=2;

printf("\n Binary no. is:::%d",b);

[0-9]+"."[0-9]+ {printf("\n Decimal");}

[a-zA-z]+ {printf("\n Alphabet");}

[a-zA-z0-9]+ {printf("\n Alphanumeric");}

. {printf("\n Other Character");}

%%

void main()

printf("\n Enter the number \n");

yylex();

int yywrap()

return 1;

5.lex code to calculate sum of digits


%{

#include<stdio.h>

int n,r,s=0;

%}

%%

[0-9]+ {n=atoi(yytext);

while(n!=0)

r=n%10;

n=n/10;

s=s+r;

printf("\n The sum of digits is :::%d",s);

[0-9]+"."[0-9]+ {printf("\n Decimal");}

[a-zA-z]+ {printf("\n Alphabet! Sum not possible");}

[a-zA-z0-9]+ {printf("\n Alphanumeric ! Sum not possible");}

. {printf("\n Other Character ! Sum not possible");}

%%

void main()

printf("\n Enter the number \n");

yylex();

int yywrap()

return 1;

6. lex code to count space words characters and new lines.

%{
#include<stdio.h>

int lc=0,sc=0,ch=0,wc=0;

%}

%%

[\n] { lc++; ch+=yyleng;}

[ \t] { sc++; ch+=yyleng;}

[^\t\n ]+ { wc++; ch+=yyleng;}

%%

int main()

printf("Enter the Sentence : ");

yylex();

printf("Number of lines : %d\n",lc);

printf("Number of spaces : %d\n",sc);

printf("Number of words, charc : %d , %d\n",wc,ch);

return 0;

int yywrap()

return 1;

NOTE: TO CHECK OUTPUT PRESS (Ctrl+D once or twice) AFTER ENTERING THE SENTENCE OR
SENTENCES.

7. Program in LEX to identify type of operator.

%{
#include <stdio.h>

%}

%%

"+" {printf("\n Arithmetic Operators");}

"-" {printf("\n Arithmetic Operators");}

"*" {printf("\n Arithmetic Operators");}

"/" {printf("\n Arithmetic Operators");}

"%" {printf("\n Modulus Operators");}

">" {printf("\n Relational Operators");}

"<" {printf("\n Relational Operators");}

">=" {printf("\n Relational Operators");}

"<=" {printf("\n Relational Operators");}

"==" {printf("\n Equal Operators");}

"++" {printf("\n Increment Operators");}

"--" {printf("\n Decrement Operators");}

"&&" {printf("\n And Operators");}

"||" {printf("\n Or Operators");}

"=" {printf("\n Assignment Operators");}

. {printf("\n Other Operators");}

%%

void main()

printf("Enter operator :");

yylex();

int yywrap()

return 1;

8.Program in lex to check whether input is valid keyword or not.


%{

#include<stdio.h>

%}

%%

if|else|do|while|switch|case|for|break|case|continue {printf("\nValid keyword");}

[0-9]+ {printf("\n INteger");}

. {printf("\nOther than character");}

%%

void main()

printf("\nEnter the input");

yylex();

int yywrap()

return 1;

9. lex code to count number of keywords.

%{

#include<stdio.h>

int count=0;

%}

%%

if|else|do|while|break|switch|continue|for|void|int|main|return|case {count++;}

"\n" {printf("\nNo of keyword %d",count);}

%%

void main()

printf("\nEnter keyword:");

yylex();
}

int yywrap()

return 1;

10. lex code to check prime number.

%{

#include<stdio.h>

int c,j,flag=0;

%}

%%

[0-9]+ {c=atoi(yytext);

if(c==2)

printf("\n prime");

else if(c==0 || c==1)

printf("\n not prime");

else

for(j=2; j<c; j++)

if(c% j==0)

flag=1;

if(flag==1)

printf("\n not prime");


else

printf("\n prime");

%%

void main()

printf("\n enter the input");

yylex();

int yywrap()

return 1;

11. lex code to check arithmetic expression or not.

%{

#include <stdio.h>

#include<string.h>

int i=0,o=0,k,flag=0;

char id[10][10],op[10][10];

%}

%%

[a-zA-Z0-9]+ {flag++; strcpy(id[i],yytext);i++;}

"+"|"-"|"*"|"/" {flag-- ; strcpy(op[o],yytext);o++;}

.|\n {return 0;}

%%

void main()

printf("Enter the expression\n");


yylex();

if(flag!=1)

printf("\n Invalid expression\n");

else

printf("\n operators are \n");

for(k=0;k<o;k++)

printf("%s\t",op[k]);

printf("\n identifiers are \n");

for(k=0;k<i;k++)

printf("%s\t",id[k]);

int yywrap()

return 1;

12. lex code to check whether a given string starts with vowel or not.

%{

#include <stdio.h>

%}

%%

^[aeiouAEIOU] { printf("The first letter is a vowel.\n"); }


^[^aeiouAEIOU] { printf("The first letter is not a vowel.\n"); }

.|\n ;

%%

int main() {

printf("Enter a string: ");

yylex();

return 0;

int yywrap(){

return 1;

13.lex code to take number as input in txt file and check number of odd and even numbers.

%{

#include<stdio.h>

int c1=0,c2=0;

%}

%%

[0-9]*[0|2|4|6|8] {c1++;}

[0-9]*[1|3|5|7|9] {c2++;}

%%

void main()

extern FILE *yyin;

yyin = fopen("file15.txt","r");

yylex();

printf("\n Total no of even number : %d",c1);

printf("\n Total no of odd number : %d",c2);

}
int yywrap()

return 1;

14. lex code to take words as input in txt file and check frequency of words if present.

%{

#include<stdio.h>

#include<string.h>

char word[]="love";

int c=0;

%}

%%

[a-zA-Z]+ {if(strcmp(yytext,word)==0) c++;}

%%

void main(){

extern FILE *yyin;

yyin = fopen("input16.txt","r");

yylex();

if(c==0)

printf("\nNot Found");

else

printf("\nFound %d times.",c);

int yywrap(){

return 1;

15. yacc code to check whether the input is identifier or not.

LEX CODE:
%{

#include "y.tab.h"

%}

%%

[a-zA-Z_][a-zA-Z_0-9]* return letter;

[0-9] return digit;

. return yytext [0];

\n return 0;

%%

int yywrap()

return 1;

YACC CODE:

%{

#include <stdio.h>

int valid=1;

%}

%token digit letter

%%

Start: letter S

S: letter S

| digit S

%%

int yyerror() {
printf("in It is not identifier");

valid = 0;

return 0;

void main()

printf("In Enter input: ");

yyparse();

if(valid)

printf("In Identitiar");

return;

NOTE: TO COMPILE YACC CODES:

yacc -d assign20.y

lex assign20.l

gcc lex.yy.c y.tab.c

16. lex code to accept strings with 0 only.

%{

#include <stdio.h>

%}

%start A DEAD

%%

<INITIAL>0 BEGIN A;

<INITIAL>[^\n0] BEGIN DEAD;

<INITIAL>\n BEGIN INITIAL; { printf("Not Accepted\n"); }


<A>0 BEGIN A;

<A>[^\n0] BEGIN DEAD;

<A>\n BEGIN INITIAL; { printf("Accepted\n"); }

<DEAD>[^\n] BEGIN DEAD;

<DEAD>\n BEGIN INITIAL; { printf("Invalid\n"); }

%%

int main() {

printf("Enter String: ");

yylex();

return 0;

int yywrap()

return 1;

17. lex code to check whether ‘bb’is present in string or not.

%{

#include <stdio.h>

#include <string.h>

void check_bb(const char *str) {

if (strstr(str, "bb") != NULL) {

printf("Substring \"bb\" is present.\n");

} else {

printf("Substring \"bb\" is not present.\n");


}

%}

%%

.* { check_bb(yytext); }

%%

int main(int argc, char **argv) {

yylex();

return 0;

int yywrap(){

return 1;

18. lex code to replace ‘A’with ‘Best’ and store in another file.

%{

#include<stdio.h>

#include<string.h>

char replace_with[]="Best";

char replace[]="A";

%}

%%

[a-zA-Z]+ {if(strcmp(yytext,replace) == 0)

fprintf(yyout,"%s",replace_with);

else

fprintf(yyout,"%s",yytext);}

. {fprintf(yyout,"%s",yytext);}

%%
int yywrap(){

return 1;

void main(){

extern FILE *yyin, *yyout;

yyin =fopen("input.txt","r");

yyout = fopen("output.txt","w");

yylex();

NOTE: CREATE SEPARATE txt FILES FOR INPUT AND OUTPUT

19. lex code to search a word from file.

%{

#include<stdio.h>

#include<string.h>

void check(char*);

%}

%%

[a-zA-Z]+ check(yytext);

%%

void main(){

printf("\nEnter the Word : ");

yylex();

void check(char *str){

FILE *fp;

char temp[100];

fp = fopen("input1.txt","r");

while((fscanf(fp, "%s", temp)) != EOF){

if(!strcmp(temp,str)){

printf("\nWord is Found ");


return;

printf("\nWord not Found ");

return;

int yywrap(){

return 1;

20. yacc code to recognize string with grammar {a^nb^n/n>=0}

LEX CODE:

%{

/* Definition section */

#include "y.tab.h"

%}

/* Rule Section */

%%

[aA] {return A;}

[bB] {return B;}

\n {return NL;}

. {return yytext[0];}

%%

int yywrap()

return 1;

YACC CODE:

%{
/* Definition section */

#include<stdio.h>

#include<stdlib.h>

%}

%token A B NL

/* Rule Section */

%%

stmt: S NL { printf("valid string\n");

exit(0); }

S: A S B |

%%

int yyerror(char *msg)

printf("invalid string\n");

exit(0);

//driver code

void main()

printf("enter the string\n");

yyparse();

21.lex code to check whether a word is palindrome or not

%{
#include<stdio.h>

int i,j,flag=0;

%}

%%

[a-zA-Z0-9]+ {for(i=0,j=yyleng-1;i<=j;i++,j--)

if(yytext[i]==yytext[j])

flag=1;

else

flag=0;

break;

if(flag==1)

printf("\nIt is Palindrome");

else

printf("\nIt is not Palindrome");

%%

void main()

printf("\nEnter the Expression:");

yylex();

int yywrap()

return 1;

You might also like