Showing posts with label System programming and compiler designing. Show all posts
Showing posts with label System programming and compiler designing. Show all posts

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

/* program to check whether the given string is  a part of the language a^n b or not */

File Name : pr12.y

%{
 #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:

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 this code

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:

Lex program to recognize a valid arithmetic expression.

//Lex program to recognize a valid arithmetic expression.

%{

#include<stdio.h>

int p=0,q=0,r=0,s=0,ob=0,cb=0;
int flagp=0,flagq=0,flagr=0,flags=0;
%}

%%
[a-zA-z]+ printf("\n %s is an identifier\n",yytext);
[+] {p++;flagp=1;}
[-] {q++;flagq=1;}
[*] {r++;flagr=1;}
[/] {s++;flags=1;}
[(] ob++;
[)] cb++;
%%

void 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",p,q,r,s);
printf("\n Operators used:\n");
if(flagp==1)
printf("+\t");
if (flagq==1)
printf("-\t");
if(flagr==1)
printf("*\t");
if(flags==1)
printf("/\n");
}

//output of this code


Lex specification program that generates a C program which takes a string “abcd" and generates pattern

Lex specification program that generates a C program which takes a string “abcd" and generates 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 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 this code


Lex program to count the number of words, characters, blank spaces and lines in a file

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

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

//abc.txt file

output :

Lex program to count the number of identifiers

//Lex program to count the number of identifiers


%{
#include<iostream.h>
int count_identifiers=0;
%}
digit[0-9]
letter[a-zA-Z_]

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

%%
int main()
{
 yylex();
 printf("count: %d",count_identifiers);
 return 0;

output:


Lex program that distinguishes keywords, integers, floats, identifiers, operators, and comments in any simple programming language

/*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()
{
  int output1;
  int main_run = 1;
 while(main_run)
 {
  result = yylex();
  switch(output1)
 {
  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:

Lex program that finds the lenght of 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 longest_word=0;
%}

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

int main()
{
 yyin=fopen("abc.txt","r");
 yylex(); 
 printf("largest: %d",longest_word);
 printf("\n");
 return 0;
}

// abc.txt file
output:

Lex Program that implements 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 cipher_char = yytext[0];
cipher_char += 3;
if (cipher_char> 'z') cipher_char -= ('z'+1- 'a');
printf ("%c" ,cipher_char );
}
[A-Z] { char cipher_char = yytext[0] ;
cipher_char += 3;
if (cipher_char> 'Z') cipher_char -= ('Z'+1- 'A');
printf("%c",cipher_char);
}
%%

/*Output of this 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
 
%{ 
  #include<stdio.h>
  int no_of_char=0;
  int no_of_lines=0;
%}

%%
\n {++no_of_lines, ++no_of_char;}
. ++no_of_char;

%%

int main()
{
 yyin=fopen("abc.txt", "r"); 
 yylex();
 printf("n0 of characters: %d",no_of_char);
 printf("\n");
 printf("n0 of lines: %d",no_of_lines);
 printf("\n"); 
 return 0;
}

//abc.txt file
output:


Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...