Monday, 2 December 2013

Program to Print itself

Language C.
Program name: printitself.c

CODE:

#include<stdio.h>
 int main()
 {
  FILE *file;
  char ch;
  file = fopen("printitself.c","r");
  while(( ch = fgetc(file) ) != EOF)
  {
  fputc(ch,stdout);
}
  fclose(file);
  return 0;
 }

Counting 1 up-to Counting of Number (ASKED IN GOOGLE RECRUITMENT)

// 13 -> 1 2 3 4 5 6 7 8 9 10 11 12 13 -> 6 occurences of 1 -> f(13)=6
// AIM: To find all n such that f(n)=n

#include<stdio.h>
int main()
{
int num,temp=0,i=0,count_of_ones=0;
while(1)
{ ++i;
num=i;
while(num>0)
{
temp=num%10;
if(temp==1)
count_of_ones++;
//printf("%d ",temp);
num=num/10;
}
if(i==count_of_ones)
printf("\ni = %d and count of ones = %d\n",i,count_of_ones);
}
return 0;
}


OUTPUT: (obtained during half-an-hour runtime on i3 processor)
i = 1 and count of ones = 1

i = 199981 and count of ones = 199981

i = 199982 and count of ones = 199982

i = 199983 and count of ones = 199983

i = 199984 and count of ones = 199984

i = 199985 and count of ones = 199985

i = 199986 and count of ones = 199986

i = 199987 and count of ones = 199987

i = 199988 and count of ones = 199988

i = 199989 and count of ones = 199989

i = 199990 and count of ones = 199990

i = 200000 and count of ones = 200000

i = 200001 and count of ones = 200001

i = 1599981 and count of ones = 1599981

i = 1599982 and count of ones = 1599982

i = 1599983 and count of ones = 1599983

i = 1599984 and count of ones = 1599984

i = 1599985 and count of ones = 1599985

i = 1599986 and count of ones = 1599986

i = 1599987 and count of ones = 1599987

i = 1599988 and count of ones = 1599988

i = 1599989 and count of ones = 1599989

i = 1599990 and count of ones = 1599990

i = 2600000 and count of ones = 2600000

i = 2600001 and count of ones = 2600001

i = 13199998 and count of ones = 13199998

i = 35000000 and count of ones = 35000000

i = 35000001 and count of ones = 35000001

i = 35199981 and count of ones = 35199981

i = 35199982 and count of ones = 35199982

i = 35199983 and count of ones = 35199983

i = 35199984 and count of ones = 35199984

i = 35199985 and count of ones = 35199985

i = 35199986 and count of ones = 35199986

i = 35199987 and count of ones = 35199987

i = 35199988 and count of ones = 35199988

i = 35199989 and count of ones = 35199989

i = 35199990 and count of ones = 35199990

i = 35200000 and count of ones = 35200000

i = 35200001 and count of ones = 35200001

i = 117463825 and count of ones = 117463825

i = 500000000 and count of ones = 500000000

i = 500000001 and count of ones = 500000001

i = 500199981 and count of ones = 500199981

i = 500199982 and count of ones = 500199982

i = 500199983 and count of ones = 500199983

i = 500199984 and count of ones = 500199984

i = 500199985 and count of ones = 500199985

i = 500199986 and count of ones = 500199986

i = 500199987 and count of ones = 500199987

i = 500199988 and count of ones = 500199988

i = 500199989 and count of ones = 500199989

i = 500199990 and count of ones = 500199990

i = 500200000 and count of ones = 500200000

i = 500200001 and count of ones = 500200001

i = 501599981 and count of ones = 501599981

i = 501599982 and count of ones = 501599982

i = 501599983 and count of ones = 501599983

i = 501599984 and count of ones = 501599984

i = 501599985 and count of ones = 501599985

i = 501599986 and count of ones = 501599986

i = 501599987 and count of ones = 501599987

i = 501599988 and count of ones = 501599988

i = 501599989 and count of ones = 501599989

i = 501599990 and count of ones = 501599990

i = 502600000 and count of ones = 502600000

i = 502600001 and count of ones = 502600001

i = 513199998 and count of ones = 513199998

i = 535000000 and count of ones = 535000000

i = 535000001 and count of ones = 535000001

i = 535199981 and count of ones = 535199981

i = 535199982 and count of ones = 535199982

i = 535199983 and count of ones = 535199983

i = 535199984 and count of ones = 535199984

i = 535199985 and count of ones = 535199985

i = 535199986 and count of ones = 535199986

i = 535199987 and count of ones = 535199987

i = 535199988 and count of ones = 535199988

i = 535199989 and count of ones = 535199989

i = 535199990 and count of ones = 535199990

i = 535200000 and count of ones = 535200000

i = 535200001 and count of ones = 535200001

i = 1111111110 and count of ones = 1111111110

i = -1323939513 and count of ones = -1323939513

i = -1000790756 and count of ones = -1000790756

i = -677641999 and count of ones = -677641999

i = 1839279971 and count of ones = 1839279971

i = 1839279972 and count of ones = 1839279972

i = 1839279973 and count of ones = 1839279973

i = 1839279974 and count of ones = 1839279974

i = 1839279975 and count of ones = 1839279975

i = 1839279976 and count of ones = 1839279976

i = 1839279977 and count of ones = 1839279977

i = 1839279978 and count of ones = 1839279978

i = 1839279979 and count of ones = 1839279979

i = 1839279980 and count of ones = 1839279980

i = -2001581512 and count of ones = -2001581512

then i stopped (manually halted the execution of infinite loop program : ctrl+Z)

DFA using Transition Table


PROGRAM Using Transition Table:

#include<stdio.h>
#include<string.h>
int main()
{
char state='A',str[50],input[20],inputstate[20],outputstate[20];
int j,i=0;
strcpy(input, "abcabcabcabcabc");
strcpy(inputstate, "AAABBBCCCDDDEEE");
strcpy(outputstate, "BCDBCDECDEEDEEE");
printf("Enter string : ");
scanf("%s",str);
while(str[i]!='\0')
{
for(j=0;j<15;j++)
{
if(inputstate[j] == state && input[j] == str[i])
{
state = outputstate[j];
break;
}
}
if(j==15)
{
state = 'F';
break;
}
i++;
}
if(state == 'A' || state == 'B' || state == 'C' || state == 'D' || state=='E' )
printf("Valid");
else
printf("InVALid");
return 0;
}

Evaluation of Boolean Expression (2013)

YACC:

%{
#include<stdio.h>
%}
%token DIGIT
%left '|'
%left '&'
%right '!'
%left '<' '>' '='

%%
input: input ans '\n' { printf("Result : %d\n",$2); }
| error '\n'         { printf("\n :( \n"); }
|
;
ans: ans '&''&' expr   { $$ = $1 && $4; }
| ans '|''|' expr                 { $$ = $1 || $4; }
| '!' ans                 { $$ = ! $2; }
| expr           { $$ = $1; }
;
expr: DIGIT '<' DIGIT { $$ = $1 < $3; }
| DIGIT '>' DIGIT         { $$ = $1 < $3; }
| DIGIT '<''=' DIGIT { $$ = $1 <= $4; }
| DIGIT '>''=' DIGIT { $$ = $1 >= $4; }
        | DIGIT '=''=' DIGIT { $$ = $1 == $4; }
| DIGIT         { $$ = $1; }
| '(' expr ')'         { $$ = $2; }
;
%%
int main()
{
printf("Enter boolean expression :\n ");
yyparse();
}
int yyerror(char *s)
{
printf("Error %s",s);
}


LEX:

%{
#include "y.tab.h"
%}
digit [0-9]+
op "&"|"|"|"!"|"<"|">"|"="
%%
{digit} { yylval = atoi(yytext); return DIGIT; }
{op} { return *yytext; }
[ \t] ;
\n { return *yytext; }
.
%%
int yywrap()
{
return 1;
}


HOW TO RUN:
$ yacc -d bool.y
$ lex bool.l
$ gcc lex.yy.c y.tab.h -o bool
$ ./bool

OUTPUT:
Enter boolean expression :
1&&1
Result : 1
1||0
Result : 1
(5<6)&&(8==8)
Result : 1

Recognition of C language statements ("for" statement)

NOTE: THE SAME PROGRAM CAN BE USED FOR ANY KIND OF C STATEMENT (JUST CHANGE THE GRAMMAR IN YACC FILE) LIKE: IF-ELSE, WHILE, STRUCTURE, DO-WHILE, SWITCH-CASE, FUNCTIONS ETC ETC...

YACC:

%{
#include<stdio.h>
%}
%token NUMBER ID KEY

%%
input: input def '\n' { printf("Valid\n"); }
| error '\n' { printf("NO\n"); }
|
;
def: KEY '(' exp ';' exp ';' exp ')' ';'
| KEY '(' exp ';' exp ';' exp ')' '\n' stmts
;
exp: ID '=' NUMBER
| ID '<' NUMBER
| ID '+''+'
|
;
stmts: '{' '\n' dec '\n' '}'
;
dec: KEY var ';'
;
var: var ',' ID
| ID
;
%%
int parser()
{
yyparse();
}
int yyerror(char *s)
{
printf(":( %s",s);
}


LEX:

%{
#include "y.tab.h"
%}
number [0-9]+
id [A-Za-z][A-Za-z0-9]*
key "for"|"int"|"float"|"char"|"double"
other "("|")"|"{"|"}"|";"|","|"="|"<"|"+"
%%
{key} { return KEY; }
{other} { return *yytext; }
{id} { return ID; }
{number} { return NUMBER; }
[ \t] ;
\n { return *yytext; }
.
%%
int main(int argc,char *argv[])
{
yyin = fopen(argv[1],"r");
parser();
fclose(yyin);
}
int yywrap()
{
return 1;
}


FILE: filefor

for ( i = 0 ; j < 10 ; k ++ )
{
int a,b,c,d;
}


HOW TO RUN:
$ yacc -d for.y
$ lex for.l
$ gcc lex.yy.c y.tab.c -o f
$ ./f filefor

OUTPUT:
Valid

Recognize Arithmetic Expression using Yacc

YACC

%{
#include<stdio.h>
%}
%token digit;

%%
input : program { printf("Valid"); }
;
program : program expr
|
;
expr :  digit
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| expr '^' expr
;
%%
int main()
{
printf("Enter the expression : ");
yyparse();
return 0;
}
int yyerror(char *s)
{
printf("%s",s);
}


LEX

%{
#include "y.tab.h"
%}
%%
[0-9]+ { return digit; }
\n { return 0; }
[ \t] ;
. { return (int) yytext[0]; }
%%
int yywrap()
{
return 1;
}


HOW TO RUN:
$ yacc -d arith.y
$ lex arith.l
$ gcc lex.yy.c y.tab.h -o arith
$ ./arith

OUTPUT:
Enter the expression : 3+4-4
Valid
vijay@ubuntu:~/Desktop/lppractice$ ./arith
Enter the expression : 3344---
syntax error

Calculator using lex and yacc

YACC

%{
#include<stdio.h>
#include<math.h>
int sym[26];
%}
%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'
%right NEG
%right '^'
%%
program: program statement '\n'
|
;
statement: expr { printf("%d\n",$1); }
| VARIABLE '=' expr { sym[$1] = $3; }
;
expr: INTEGER
| VARIABLE { $$ = sym[$1]; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '-' expr %prec NEG { $$ = -$2; }
| expr '^' expr { $$ = pow($1,$3); }
| '(' expr ')' { $$ = $2; }
;
%%
int main()
{
printf("Enter Expression: ");
yyparse();
return 0;
}
int yyerror(char *s)
{
printf("%s",s);
return 0;
}


LEX

%{
#include "y.tab.h"
#include<stdio.h>
%}
%%
[0-9]+ { yylval = atoi(yytext); return INTEGER; }
[a-z]  { yylval = *yytext; return VARIABLE; }
[-+()=*/^\n] { return *yytext; }
[ \t] ;
. yyerror("Invalid Character");
%%
int yywrap()
{
return 1;
}


HOW TO RUN:
$ yacc -d cal.y
$ lex cal.l
$ gcc lex.yy.c y.tab.c -lm -o cal
$ ./cal

OUTPUT:
Enter Expression: 2+3
5
1
1
e=1
f=3
g=e+f
g
4