Posts

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"); }