Monday, 25 April 2016

Program in YACC to recognize the language (a^n b , n>=10). (output to say input is valid or not)


File Name : pr12.y
/* program to check whether the given string is  a part of the language a^n b or not */
/* Shows nothing if string is a part of language */
/* shows a message if string is not a part of language */
%{
 #include<stdio.h>
 int yylex(void);
 int yyerror(char *);
 
%}

%token A B   //tokens : the alphabets of language 'a' and 'b' 

%%
//production rules for grammar
expr: s B         
  ;
s : s A 
| A   

;

%%

int main()
{
 
 printf("Enter the string \n");
 yyparse(); 
 return 0;
}

int yyerror(char *s)
{
 printf("Invalid: Not a part of the language - a^n b \n");
}

File Name : pr12.l
%{
 #include "y.tab.h"
 extern int yylval;
%}
A [a]
B [b]
%%

{A} {yylval=yytext[0];return A;}
{B} {yylval=yytext[1];return B;}
\n {return 0;}
. {return yytext[0];}

%%

// Output of the Above Program.
Program in YACC to recognize the language (a^n b , n>=10). (output to say input is valid or not)
Program in YACC to recognize the language (a^n b , n>=10). (output to say input is valid or not)

Program in YACC to recognize the string „abbb^n, „ab^n „a^n of the langauge (a^n b^n , n>=1)

//Program in YACC to recognize the string „abbb^n, „ab^n „a^n of the langauge (a^n b^n , n>=1)

File Name : pr11.y
%{
 #include<stdio.h>
 int yylex(void);
 int yyerror(char *); 
%}
%token A B

%%

statement : expr 
;
expr :A expr B
| A B   
;

%%

int main()
{
 printf("Enter the string :");
 yyparse();
 return 0;
}
int yyerror(char *s)
{
 printf("Invalid");
}


File Name : pr11.l
%{
 #include "y.tab.h"
 extern int yylval;
%}
A [a]
B [b]
%%

{A} {yylval=yytext[0];return A;}
{B} {yylval=yytext[1];return B;}
\n {return 0;}
. {return yytext[0];}

%%

// Output of the Above Program.

Program in YACC to recognize the string „abbb^n, „ab^n „a^n of the langauge (a^n b^n , n>=1)
Program in YACC to recognize the string „abbb^n, „ab^n „a^n of the langauge (a^n b^n , n>=1)

Program in YACC to evaluate an expression (simple calculator program for addition and subtraction, multiplication, division)

/*Program in YACC to evaluate an expression (simple calculator program for addition and
subtraction, multiplication, division).*/
File Name : calc10.y
%{
 #include<stdio.h>
int yylex(void);
int yyerror(char *);
%}

%token NAME NUMBER

%%
statement : NAME '=' expression
 | expression { printf("=%d\n",$1);}
 ;
expression:expression'+' NUMBER{$$ = $1+$3;}
 |expression '-' NUMBER {$$ = $1-$3;}
 |expression '*' NUMBER {$$ = $1*$3;}
 |expression '/' NUMBER { if ($3!=0){ $$ = $1/$3; }else { printf("Error: divide by Zero"); } }
 |NUMBER {$$=$1;}
 ;

%%
int main()
{
yyparse();
return 0;
}
int yyerror(char *s)
{
 printf("%s",s);
}

File Name : calc10.l
%{
 #include "y.tab.h"
 extern int yylval;
%}

%%
[0-9]+ { yylval=atoi(yytext);return NUMBER;}
\n  {return 0;}
.   {return yytext[0];}
%%

// Output of the Above Program.
Program in YACC to evaluate an expression (simple calculator program for addition and subtraction, multiplication, division)
Program in YACC to evaluate an expression (simple calculator program for addition and
subtraction, multiplication, division)

Sunday, 24 April 2016

Lex specification program that generates a C program which takes a string “abcd” and prints the pattern

/*Write a Lex specification program that generates a C program which takes a string
“abcd” and prints the following output.
        abcd
        abc
        ab
        a
*/

%{ #include<stdio.h>
char ch;
char i,j;
%}

%%
[a-z]* { for(i='d';i>=1;--i)
      {
           for(j='a';j<=i;++j)
           {
              printf("%c ",j);
           }
          printf("\n");
      }
}
%%

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

//Output of the above program
Lex specification program that generates a C program which takes a string “abcd” and prints the pattern
Lex specification program that generates a C program which takes a string “abcd” and prints the pattern

Lex program to recognize a valid arithmetic expression

//Lex program to recognize a valid arithmetic expression.
%{#include<stdio.h>
int a=0,b=0,c=0,d=0,ob=0,cb=0;
int flaga=0,flagb=0,flagc=0,flagd=0;
%}

%%
[a-zA-z]+ printf("\n %s is an identifier\n",yytext);
[+] {a++;flaga=1;}
[-] {b++;flagb=1;}
[*] {c++;flagc=1;}
[/] {d++;flagd=1;}
[(] ob++;
[)] cb++;
%%

main()
{
printf("Enter expression:");
yylex();
if(ob==cb)
printf("\nvalid expression\n");
else
printf("invalid expression\n");
printf("Addition=%d\tSubtract=%d\nMultiply=%d\tDivide=%d\n",a,b,c,d);
printf("\n Operators used:\n");
if(flaga==1)
printf("+\t");
if (flagb==1)
printf("-\t");
if(flagc==1)
printf("*\t");
if(flagd==1)
printf("/\n");
}

// Output of the above program  
Lex program to recognize a valid arithmetic expression.
Lex program to recognize a valid arithmetic expression.

Lex program to count the number of words,small and capital letters, digits and special characters in a C file

/*Lex program to count the number of words, characters, blank spaces and lines in a C file.*/
 
 
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%} 
 
%%
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%

main(void)
{
yyin= fopen("abc.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
 
int yywrap()
{
return(1);
} 

// file abc.txt
x program to count the number of words,small and capital letters, digits and special characters in a C file
x program to count the number of words,small and capital letters, digits and special characters in a C file



// Output of the above Program
x program to count the number of words,small and capital letters, digits and special characters in a C file
Lex program to count the number of words,small and capital letters, digits and special characters in a C file

 

Lex program to count the number of identifiers


//Lex program to count the number of identifiers
%{#include<iostream.h>
int count=0;
char ch=0;
%}
digit[0-9]
letter[a-zA-Z_]

%%
{letter}({letter}|{digit})* {
 count++;
}

%%
int main()
{
 yylex();
 printf("count: %d",count);
 return 0;
} 
 
// Output of the above Program
Lex program to count the number of identifiers
Lex program to count the number of identifiers

Lex program that distinguishes keywords, integers, floats, identifiers, operators, and comments

/*Lex program that distinguishes keywords, integers, floats, identifiers, operators, and
comments in any simple programming language.*/ 
 
%{
enum{INTEGER,FLOAT,IDENTIFIER,OPERATOR,COMMENT};
%}
digit [0-9]
letter[A-Za-z_]

%%
" "|"\t" ;
{digit}+   { return INTEGER; }
{digit}+\.{digit}+ { return FLOAT; }
'+' |
'-' |
'*' |
'/' { return OPERATOR; }

{letter}({letter}|{digit})* { return IDENTIFIER; }
"/*" { return COMMENT;}
%%
int main(void)
{
  int result;
  int running = 1;
 while(running)
 {
  result = yylex();
  switch(result)
                {
  case INTEGER: printf("integer"); break;
  case FLOAT: printf("float"); break;
  case OPERATOR: printf("operator"); break;
  case IDENTIFIER:printf("identifier"); break;
  case COMMENT: printf("comment"); break;
  }
 }
return 0;
}
 
// Output of the above Program
Lex program that distinguishes keywords, integers, floats, identifiers, operators, and comments
Lex program that distinguishes keywords, integers, floats, identifiers, operators, and comments
  
 

Lex program to find the length of the longest word

 /*Write a Lex program that finds the length of the longest word (defined as a contiguous
 string of upper and lower case letters) in the input.*/
 
%{ #include<stdio.h>
int k=0;
%}

%%
[a-zA-Z]+ {
if(yyleng>k)
{  k= yyleng;
}
}
%%

int main(int argc[],char **argv[])
{
 yyin=fopen("abc.txt","r");
 yylex(); 
 printf("largest: %d",k);
 printf("\n");
 return 0;
}
 
 //file abc.txt
 
Lex program to find the length of the longest word
Lex program to find the length of the longest word

//Output of the above program

Lex program to find the length of the longest word

Lex program to find the length of the longest word

Checkout this video on Lex Program to Find the Length of the longest word.


Lex program that implements the Caesar cipher

/* Write a Lex program that implements the Caesar cipher: it replaces every letter 
with the one three letters after in in alphabetical order, wrapping around at Z.
e.g. a is replaced by d, b by e, and so on z by c.  */ 
 
%%
[a-z] {char ch = yytext[0];
ch += 3;
if (ch> 'z') ch -= ('z'+1- 'a');
printf ("%c" ,ch );
}
[A-Z] { char ch = yytext[0] ;
ch += 3;
if (ch> 'Z') ch -= ('Z'+1- 'A');
printf("%c",ch);
}
%%
 
// Output of the above program

Caesar cipher
Caesar cipher
 

Lex Program to Count the Number of Lines and Characters in the Input File

//Lex Program to Count the Number of Lines and Characters in the Input File
 
%{ #include<studio.h>
int n_char=0;
int n_lines=0;
%}

%%
\n {++n_lines, ++n_char;}
. ++n_char;

%%

int main(int argc[],char *argv[])
{
 yyin=fopen("abc.txt", "r"); 
 yylex();
 printf("n# of characters: %d",n_char);
 printf("\n");
 printf("n# of lines: %d",n_lines);
 printf("\n"); 
 return 0;
}
 
// file abc.txt 
Lex Program to Count the Number of Lines and Characters in the Input File
Lex Program to Count the Number of Lines and Characters in the Input File
 
// Output of the above program  
Lex Program to Count the Number of Lines and Characters in the Input File
Lex Program to Count the Number of Lines and Characters in the Input File
Checkout this video on Lex Program to Count the Number of Lines and Characters.