RECURSIVE DESCENT --------------------------------- #include #include char str[50]; int pt = 0, flag = 0; void E(); void Eprime(); void T(); void Tprime(); void F(); void E() { T(); Eprime(); } void Eprime() { if (str[pt] == '+') { pt++; T(); Eprime(); } } void T() { F(); Tprime(); } void Tprime() { if (str[pt] == '*') { pt++; F(); Tprime(); } } void F() { if (str[pt] == '(') { pt++; E(); if (str[pt] == ')') pt++; else flag = 1; } else if (str[pt] == 'a') { pt++; } else { flag = 1; } } int main() { printf("Enter string: "); scanf("%s", str); E(); if (flag == 0 && pt == strlen(str)) printf("String is Accepted\n"); else printf("String is Rejected\n"); return 0; } ---------------------------------------------- E CLOSURE ---------------------------------------------- #include #include int main(){ char states[20][10],s1[20][10],in[20][10],s2[20][10],curr[10]; int i,k,n,t; printf("Enter the Number of states: "); scanf("%d",&n); printf("\nEnter the states: "); for(k=0;k #include #include "y.tab.h" extern int yylval; %} %% [ \t]+ { ; } [0-9]+ { yylval = atoi(yytext); return NUMBER; } \n { return 0; } . { return yytext[0]; } %% int yywrap() { return 1; } **********calc.y*************** %{ #include #include int flag = 0; int yylex(void); int yyerror(char *s); %} %token NUMBER %left '+' '-' %left '*' '/' %left '(' ')' %% ArithmeticExpression: E { printf("\nResult = %d\n", $1); return 0; } ; E: E '+' E { $$ = $1 + $3; } | E '-' E { $$ = $1 - $3; } | E '*' E { $$ = $1 * $3; } | E '/' E { if ($3 == 0) { yyerror("Division by zero"); flag = 1; YYABORT; } else { $$ = $1 / $3; } } | '(' E ')' { $$ = $2; } | NUMBER { $$ = $1; } ; %% int main() { printf("\nEnter any arithmetic expression:\n"); yyparse(); if (!flag) { printf("Entered arithmetic expression is valid\n"); } return 0; } int yyerror(char *s) { printf("\nEntered arithmetic expression is invalid\n"); flag = 1; return 0; }