Posts

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

Image
  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...

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

Image
  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)

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

Image
  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-...

8. Lex program to recognize a valid arithmetic expression

Image
  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( " \n valid expression \n " ); else printf ( "invalid expression \n " ); printf( "Addition=%d \t Subtract=%d \n Multiply=%d \t Divide=%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 ...

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

Image
  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

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

Image
  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() { ...

5. Lex program to count the number of identifiers

Image
  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