Showing posts with label Language processing programs. Show all posts
Showing posts with label Language processing programs. Show all posts

Thursday, January 22

C code implementing a simple DFA

  Hello freinds,  Here I am posting my C implementation of DFA shown below. This DFA will accept any string containing 'a's and 'b' and last symbol should be 'a'. #include <stdio.h> #define TOTAL_STATES 2 #define FINAL_STATES 1 #define ALPHABET_CHARCTERS 2 #define UNKNOWN_SYMBOL_ERR 0 #define NOT_REACHED_FINAL_STATE 1 #define REACHED_FINAL_STATE 2 enum DFA_STATES{q0,q1}; enum input{a,b}; int Accepted_states[FINAL_STATES]={q1}; char alphabet[ALPHABET_CHARCTERS]={'a','b'}; int Transition_Table[TOTAL_STATES][ALPHABET_CHARCTERS]; int Current_state=q0; void DefineDFA() { Transition_Table[q0][a] = q1; ...

Sunday, November 20

Quadruple ~ C code

#include<stdio.h> #include<string.h> main() {  char line[20]; int s[20]; int t=1;       int i=0;      printf("Enter string..  :"); gets(line); for(i=0;i<20;i++)s[i]=0; printf("op\ta1\ta2\tres\n"); for(i=2;line[i]!='\0';i++) { if(line[i]=='/' || line[i]=='*') {                 printf("\n"); if(s[i]==0) { if(s[i+1]==0) { printf(":=\t%c\t\t t%d\n",line[i+1],t); s[i+1]=t++; } printf("%c\t",line[i]); (s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]); printf("t%d \t t%d",s[i+1],t); s[i-1]=s[i+1]=t++; s[i]=1; } } } for(i=2;line[i]!='\0';i++) { if(line[i]=='+' || line[i]=='-') {                ...

Tuesday, November 15

LEX program to check a Date (dd/mm/yyyy)

Hello dude..                I would like to discuss a problem asked by my teacher in our internal lab exam- LEX program to check the validity of a Date. Ahem..In normal programming (i.e, that with C or Java) this is not a big deal!! But in Lex paradigm,it require some more effort..!!                                                 ...

Tuesday, November 1

FOLLOW OF GIVEN GRAMMAR USING C PROGRAM

Hello reader, In this post I am posting implementation of FOLLOW in C. Note: If your production statements contain multiple terms connected by '|'  then give these terms as separate productions   #include<stdio.h> #include<string.h> int n,m=0,p,i=0,j=0; char a[10][10],followResult[10]; void follow(char c); void first(char c); void addToResult(char); int main() { int i; int choice; char c,ch; printf("Enter the no.of productions: "); scanf("%d", &n); printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n); for(i=0;i<n;i++) scanf("%s%c",a[i],&ch); ...

Monday, October 17

Recursive Descent Parser using C program

Updated on 09/02/15 Hello reader, Here is the updated post considering your valuable suggestions. check it out. The grammar on which we are going to do recursive descent parsing is: E -> E+T | T T -> T*F | F F -> (E) | id   RD parser will verify whether the syntax of the input stream is correct by checking each character  from left to right. A basic operation necessary is reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input.  The given grammar can accept all arithmetic equations involving +, * and (). eg:  a+(a*a)  a+a*a...

Saturday, October 8

Calculator using YACC program

Here is the Yacc program implementing a simple calculator: %{ #include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*''/' %right '^' %% s:e'\n'{printf("%d",$1);} e:    e '+' e{$$=$1+$3;}  |e '-' e{$$=$1-$3;}  |e '*' e{$$=$1*$3;}  |e '/' e{$$=$1/$3;}  |e '^' e {   int i,j=$1;   for(i=1;i<$3;i++)   {   j=j*$1;   $$=j;   }   }  |'('e')'{$$=$2;}  |num1; num1:num1 num{$$ = $1*10 + $2;}  |num  ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0'; return num; } return c; } int...

YACC program to convert infix

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %% s:e'\n'{} e:e'+'e{printf("+");} |e'-'e{printf("-");} |e'/'e{printf("/");} |e'*'e{printf("*");} |num1{printf("%d",$1);} num1:num1 num {$$=$1*10+$2;} |num ; %% yylex() {  int c;  c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1;...

Yacc program to evaluate POSTFIX expression

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %right '^' %% s:e'\n'{printf("\n%d",$1);} e:e e'+'{$$=$1+$2;} |e e'-'{$$=$1-$2;} |e e'*'{$$=$1*$2;} |e e'/'{$$=$1/$2;} |num ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1; } Illustraion: Evaluation of postfix expression can be done very easily. 34+ 3--> store value in $1 4--> store value in $2 +--> add $1 and $2 and store result in $$ For more Yacc programs click here.....

C Program to generate Intermediate code

#include<stdio.h> #include<ctype.h> #include<string.h> main() { char a[20]; int i=2,n; printf("Exp  :"); scanf("%s", a); if(isdigit(a[0])) printf("MVI A,%c\n",a[0]); else printf("MOV A,%c\n",a[0]); n=strlen(a); while(i<n) { switch(a[i]) { case '+':printf("ADD B\n");i+=3;break; case '-':printf("SUB B\n");i+=3;break; default:if(isdigit(a[i])) printf("MVI B,%c\n",a[i]); else printf("MOV B,%c\n",a[i]); i--; } }...