1. Program 5
WAP 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
4. Program 6
WAP to implement the stack using array
#include<stdio.h>
#include<conio.h>
# define MAXSIZE 10
void 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. 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. 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
9. Program 7
WAP 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",¬);
printf("n Enter the Terminals in the grammer:");
for(i=0;i<not||t[i]=='$';i++)
Seema 1136619815
14. Program 8
WAP 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");
else
printf("n it is a keyword");
getch(); }
Seema 1136619815
16. Program 9
Study of converting NFA from Regular Expression.
Definition of Regular Expression
A regular expression is another representation of a regular language, and is defined
over an alphabet (defined as Σ). The simplest regular expressions are symbols from
λ, ∅, and symbols from Σ. Regular expressions can be built from these simple
regular expressions with parenthesis, in addition to union, Kleene star and
concatenation 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 the
choice of two or more edges labeled with the same symbol or special edge labeled
with ε, edges that may be taken without using a symbol.
Example - the following NFA accepts either even number of a's or multiple of 3 a's;
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 Q
Seema 1136619815
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 NFA
Seema 1136619815
18. Program 10
Study of Left Recursion.
Definition
"A grammar is left-recursive if we can find some non-terminal A which will
eventually derive a sentential form with itself as the left-symbol.
Immediate left recursion
Immediate left recursion occurs in rules of the form
where and are sequences of nonterminals and terminals,
and doesn't start with . For example, the rule
is 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 to
parse a grammar which contains this rule.
Indirect left recursion
Indirect left recursion in its simplest form could be defined as:
possibly giving the derivation
Seema 1136619815
19. More generally, for the nonterminals , indirect left recursion can be
defined as being of the form:
where are sequences of nonterminals and terminals.
Removing left recursion
Removing immediate left recursion
The general algorithm to remove immediate left recursion follows. Several
improvements 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 form
where:
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 nonterminal
Seema 1136619815
20. This newly created symbol is often called the "tail", or the "rest".
As an example, consider the rule
This could be rewritten to avoid left recursion as
The last rule happens to be equivalent to the slightly shorter form
Seema 1136619815