Program 5WAP to count number of whitespaces and newline characters#include<stdio.h>#include<conio.h>#include<string.h>void...
newline++;printf("n");}a++;ch=getche();}printf("n The number of lines used : %d",newline+1);printf("n The number of spaces...
OUTPUTSeema            1136619815
Program 6              WAP to implement the stack using array#include<stdio.h>#include<conio.h># define MAXSIZE 10void pus...
printf("n4. Enter your choice");scanf("%d",&choice);switch(choice){case 1: push();        break;case 2: printf("nThe delet...
if(top==MAXSIZE-1){printf("nThe stack is full");getch();exit(0);}else{printf("Enter the element to be inserted");scanf("%d...
}else{item=stack[top];top=top-1;}return(item);}void traverse(){int i;if(top==-1){printf("The stack is empty");getch();exit...
printf("nTraverse element is : ");        printf("%d",stack[i]);        }}}                                        OUTPUTS...
Program 7WAP for computation of FIRST in grammer.#include<stdio.h>#include<conio.h>#include<string.h>void main(){char t[5]...
{scanf("n%c",&t[i]);}for(i=0;i<nont;i++){p[i][0]=nt[i];first[i][0]=nt[i];}printf("nEnter the productions :n");for(i=0;i<no...
for(j=1;p[i][j]!=$;j++){printf("%c",p[i][j]);}}for(i=0;i<nont;i++){f=0;for(j=1;p[i][j]!=$;j++){for(k=0;k<not;k++){if(f==1)...
{first[i][j]=first[k][j] ;if(first[i][j]==e)continue;first[i][j+1]=$;f=1;break;}}}}for(i=0;i<nont;i++){printf("nnThe first...
OUTPUTSeema            1136619815
Program 8WAP to check whether the string is a keyword or not.#include<stdio.h>#include<conio.h>#include<string.h>void main...
OUTPUTSeema            1136619815
Program 9Study of converting NFA from Regular Expression.Definition of Regular ExpressionA regular expression is another r...
a finite set of input symbols Σ        a transition relation Δ : Q × Σ → P(Q).        an initial (or start) state q0 ∈ Q  ...
Program 10Study of Left Recursion.Definition"A grammar is left-recursive if we can find some non-terminal A which willeven...
More generally, for the nonterminals                 , indirect left recursion can bedefined as being of the form:where   ...
This newly created symbol is often called the "tail", or the "rest".As an example, consider the ruleThis could be rewritte...
Upcoming SlideShare
Loading in …5
×

Cd

413 views
364 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
413
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cd

  1. 1. Program 5WAP to count number of whitespaces and newline characters#include<stdio.h>#include<conio.h>#include<string.h>void main(){char str[200],ch;int a=0,space=0,newline=0;clrscr();printf("n Enter a string(press escape to entering):");ch=getche();while((ch!=27)&&(a<199)){str[a]=ch;if(str[a]== ){space++;}if(str[a]==13){Seema 1136619815
  2. 2. newline++;printf("n");}a++;ch=getche();}printf("n The number of lines used : %d",newline+1);printf("n The number of spaces used is : %d",space);getch();}Seema 1136619815
  3. 3. OUTPUTSeema 1136619815
  4. 4. Program 6 WAP to implement the stack using array#include<stdio.h>#include<conio.h># define MAXSIZE 10void push();int pop();void traverse();int stack[MAXSIZE];int top=-1;void main(){int choice;char ch;do{clrscr();printf("n1. PUSH");printf("n2. POP");printf("n3. TRAVERSE");Seema 1136619815
  5. 5. printf("n4. Enter your choice");scanf("%d",&choice);switch(choice){case 1: push(); break;case 2: printf("nThe deleted element is %d",pop()); break;case 3: traverse(); break;default: printf("nYou have entered wrong choice");}printf("nDo you wish to continue(Y/N)");fflush(stdin);scanf("%c",&ch);}while(ch==Y||ch==N);}void push(){int item;Seema 1136619815
  6. 6. if(top==MAXSIZE-1){printf("nThe stack is full");getch();exit(0);}else{printf("Enter the element to be inserted");scanf("%d",&item);top=top+1;stack[top]=item;}}int pop(){int item;if(top==-1){printf("The stack is empty");getch();exit(0);Seema 1136619815
  7. 7. }else{item=stack[top];top=top-1;}return(item);}void traverse(){int i;if(top==-1){printf("The stack is empty");getch();exit(0);}else{for(i=top;i>=0;i--) {Seema 1136619815
  8. 8. printf("nTraverse element is : "); printf("%d",stack[i]); }}} OUTPUTSeema 1136619815
  9. 9. Program 7WAP for computation of FIRST in grammer.#include<stdio.h>#include<conio.h>#include<string.h>void main(){char t[5],nt[10],p[5][5],first[5][5],temp;int i,j,not,nont,k=0,f=0;clrscr();printf("n Enter the no. of Non-terminals in the grammer:");scanf("%d",&nont);printf("n Enter the Non-terminals in the grammer:");for(i=0;i<nont;i++){scanf("n%c",&nt[i]);}printf("n Enter the no. of Terminals in the grammer:(Enter e for absiline)");scanf("%d",&not);printf("n Enter the Terminals in the grammer:");for(i=0;i<not||t[i]==$;i++)Seema 1136619815
  10. 10. {scanf("n%c",&t[i]);}for(i=0;i<nont;i++){p[i][0]=nt[i];first[i][0]=nt[i];}printf("nEnter the productions :n");for(i=0;i<nont;i++){scanf("%c",&temp);printf("n Enter the production for %c(End the production with $sign):",p[i][0]);for(j=0;p[i][j]!=$;){j+=1;scanf("%c",&p[i][j]);}}for(i=0;i<nont;i++){printf("n The production for %c _>",p[i][0]);Seema 1136619815
  11. 11. for(j=1;p[i][j]!=$;j++){printf("%c",p[i][j]);}}for(i=0;i<nont;i++){f=0;for(j=1;p[i][j]!=$;j++){for(k=0;k<not;k++){if(f==1)break;if(p[i][j]==t[k]){first[i][j]=t[k];first[i][j+1]=$;f=1;break;}else if(p[i][j]==nt[k])Seema 1136619815
  12. 12. {first[i][j]=first[k][j] ;if(first[i][j]==e)continue;first[i][j+1]=$;f=1;break;}}}}for(i=0;i<nont;i++){printf("nnThe first of %c->",first[i][0]);for(j=1;first[i][j]!=$;j++){printf("%ct",first[i][j]);}}getch();}Seema 1136619815
  13. 13. OUTPUTSeema 1136619815
  14. 14. Program 8WAP to check whether the string is a keyword or not.#include<stdio.h>#include<conio.h>#include<string.h>void main(){int i,flag=0,m;char s[5][10]={"if","else","goto","continue","return"},st[10];clrscr() ;printf("n Enter the string:");gets(st);for(i=0;i<5;i++){ m=strcmp(st,s[i]);if(m==0)flag=1; }if(flag==0)printf("nit is not a keyword");elseprintf("n it is a keyword");getch(); }Seema 1136619815
  15. 15. OUTPUTSeema 1136619815
  16. 16. Program 9Study of converting NFA from Regular Expression.Definition of Regular ExpressionA regular expression is another representation of a regular language, and is definedover an alphabet (defined as Σ). The simplest regular expressions are symbols fromλ, ∅, and symbols from Σ. Regular expressions can be built from these simpleregular expressions with parenthesis, in addition to union, Kleene star andconcatenation operators.( , ) are used to help define the order of operations* is the Kleene star+ is the union operator! is used to represent the empty string.NONDETERMINISTIC FINITE AUTOMATA (NFA) - Automata with thechoice of two or more edges labeled with the same symbol or special edge labeledwith ε, edges that may be taken without using a symbol.Example - the following NFA accepts either even number of as or multiple of 3 as;the NFA must correctly choose which path to follow at the first transition.An NFA is represented formally by a 5-tuple, (Q, Σ, Δ, q0, F), consisting of a finite set of states QSeema 1136619815
  17. 17. a finite set of input symbols Σ a transition relation Δ : Q × Σ → P(Q). an initial (or start) state q0 ∈ Q a set of states F distinguished as accepting (or final) states F ⊆ Q.Here, P(Q) denotes the power set of Q.CONVERTING A REGULAR EXPRESSION TO AN NFASeema 1136619815
  18. 18. Program 10Study of Left Recursion.Definition"A grammar is left-recursive if we can find some non-terminal A which willeventually derive a sentential form with itself as the left-symbol.Immediate left recursionImmediate left recursion occurs in rules of the formwhere and are sequences of nonterminals and terminals,and doesnt start with . For example, the ruleis immediately left-recursive. The recursive descent parser for this rule might look like:function Expr(){ Expr(); match(+); Term();}and a recursive descent parser would fall into infinite recursion when trying toparse a grammar which contains this rule.Indirect left recursionIndirect left recursion in its simplest form could be defined as:possibly giving the derivationSeema 1136619815
  19. 19. More generally, for the nonterminals , indirect left recursion can bedefined as being of the form:where are sequences of nonterminals and terminals.Removing left recursionRemoving immediate left recursionThe general algorithm to remove immediate left recursion follows. Severalimprovements to this method have been made, including the ones described in"Removing Left Recursion from Context-Free Grammars", written by Robert C.Moore.[5] For each rule of the formwhere: A is a left-recursive nonterminal is a sequence of nonterminals and terminals that is not null ( ) is a sequence of nonterminals and terminals that does not start with A.replace the A-production by the production:And create a new nonterminalSeema 1136619815
  20. 20. This newly created symbol is often called the "tail", or the "rest".As an example, consider the ruleThis could be rewritten to avoid left recursion asThe last rule happens to be equivalent to the slightly shorter formSeema 1136619815

×