Posts

Showing posts from December, 2017

Calculating the first of a given grammar (without epsilon)

#include<stdio.h> #include<ctype.h> int i; int n; chat p[25][25]; char c; char b; char h; void first(char c); void result(char h); main() { printf("Enter the number of production rules\n"); scanf("%d",&c); for(i=0;i<n;i++) { printf("Rule %d:  ",i+1); scanf("%s",p[i]); } printf("Enter the non terminal for which first should be calculated\n"); scanf("%s",&c); first(c); } void first(char c) { b=c; for(i=0;i<n;i++) { if(p[i][0]==b) { if(p[i][2]>64 && p[i][2]<97) { first(p[i][2]); } if(p[i][2]>96 && p[i][2]<122) { result(p[i][2]); } } } } void result(char c) { h=b; printf("The first of the non terminal  %s is : %s",c,p[i][2]); }

Lex and Yacc program to calculate the value of a binary number.

Lex Program:   %{ #include"y.tab.h" extern int yylval; %} %% [0] {yylval=atoi(yytext); return ZERO;} [1] {yylval=atoi(yytext); return ONE;} [\t] ; [\n] return 0; . return yytext[0]; %% Yacc Program: %{ #include<stdio.h> #include<string.h> %token ZERO ONE; %% s : e {printf("Value : %d",$1);} e : e s {$$=($1*2)+$2}     | s {$$=$1;} s : ZERO {$$=0;}    |ONE {$$=1;}    ; %%  main() { yyparse(); }  yyerror(char * arg) { printf("Invalid expression"); }

Lex and Yacc program that calculates number of zeros in a given string:

LEX file: %{ #include "y.tab.c" extern int yylval; %} %% [0] {yylval=atoi(yytext); return zero;} [1] {yylval=atoi(yytext); return one;} %% Yacc file: %{ #include<stdio.h> #include<string.h> %} %token zero one %% s : e {printf("No of zeros: %d",$1);}    ; e : e t {$$=$1+$2;}    | t {$$=$1;}    ; t  : zero {$$=1;}    | one {$$=0;}    ; %%  main() { yyparse(); } yyerror(char * arg) { printf("Invalid expression"); }

Lex and Yacc program that accepts bits(0 and 1)

LEX file: %{ #include "y.tab.c" extern int yylval; %} %% [0] {yylval=atoi(yytext); return zero;} [1] {yylval=atoi(yytext); return one;} %% Yacc file: %{ #include<stdio.h> #include<string.h> %} %token zero one %% s : e {printf("String acepted");}    ; e : e t    | t    ; t  : zero    | one    ; %%  main() { yyparse(); } yyerror(char * arg) { printf("Invalid expression"); }

Lex program for counting keyword and identifier

%{ #include<stdio.h> }% %{ int keywords=0; int identifiers=0; %} %% (int|float|char) {keywords++; printf("%s is keyword",yytext);} [_a-zA-Z]([a-z|[0-9])* {identifiers++; printf("%s is identifier",yytext);} %% int main() { yylex(); printf("No of keywords is %d \n no of identifiers is %d",keywords,identifiers); return(0); }

Write a program to recognize the grammar anbman 0<=n<=m

Lex file: %{ #include "y.tab.h" %} %% [a] return a; [b] return b; [bb] return bb; %%     Yacc file: %{ #include<stdio.h> %} %token a b bb %% S : E {printf("String accepted");}    : E : E T     | T     ; T : a     | b     | bb     ; %% main() { yyparse(); } yyerror(char **arg) { printf("Invalid string"); }

Write a grammar to accept the string that starts with 0,1 and the length of the string should be odd.

Lex file: %{ #include "y.tab.h" extern int yylval; %} %% [0] yylval=atoi(yytext); return zero; [1] yylval=atoi(yytext); return one; [\t]; [\n] return 0; %%     Yacc file: %{ #include<stdio.h> %} %token zero one %% S : E {printf("String accepted");}    : E : E T T     | T     ; T : zero     | one     ; %% main() { yyparse(); } yyerror(char **arg) { printf("Invalid string"); }

identify a sentence and return the length of each word.

%{ int a=0; int a2; %} %% " " {a++;} [a-zA-Z]+ {printf("word: %s \t\t letters in the word: %d\n",yytext,yyleng);} %% main() { yylex(); a=a+1; printf("Sentence lenght: %d",a); }

String starts with 'a' and should contain at least 3 b's

%{ %include<stdio.h> %} %% [a]+[b]+[a]*[b]+[a]*[b]+[ab]* {prinf("valid");} %% main() { yylex(); }

Identifying words of length 4-8 characters long.

%{ #include<stdio.h.> int a=0; %} %% [a-zA-Z0-9._]+ {a=yyleng; if(a==4||a==5||a==6||a==7||a==8) printf("Word accepted");} %% main() { yylex(); }

To find number of blank spaces and new lines

%{ #include<stdio.h> int a=0; int b=0' %} %% [""] {a++;} [\n] {b++;} %% main() { yylex(); printf("No. of spaces: %d",a); printf("No. of new lines: %d",b); }

Finding the number of operators in a given C file

%{ #include<stdio.h> int a; FILE **yyin; %} %% [+*/-] {a++;} %% main() { yyin=fopen("operators.txt","r");  //file name of a C file yylex(); printf("No. of operators are: %d",a); }

Identifying single line and multi line comments.

%{ #include<stdio.h> %} %% ['//]+{2} [a-zA-Z0-9]+ {printf("single line comment");} [/][*][a-zA-Z0-9]+[*][/] {printf("multi line comment");} %% main() { yylex(); }