Index: Ex No Title No Date Sign
Index: Ex No Title No Date Sign
INDEX
Ex Page
Title Date Sign
No No
Design a lexical analyzer for given language and
the lexical analyzer should ignore redundant
spaces, tabs and new lines. It should also ignore
1 comments. Although the syntax specification
states that identifiers can be arbitrarily long, you
may restrict the length to some reasonable value.
Simulate the same in C language
Write a C program to identify whether a given line
2
is a comment or not.
Write a C program to test whether a given
3
identifier is valid or not.
Write a C program to simulate lexical analyzer for
4
validating operators
To Study about Lexical Analyzer Generator (LEX)
5
and Flex (Fast Lexical Analyzer).
Implement following programs using Lex.
a. Create a Lexer to take input from text file and
count no of characters, no. of lines & no. of
6
words.
b. b. Write a Lex program to count number of
vowels and consonants in a given input string.
Implement following programs using Lex.
a. Write a Lex program to print out all numbers
from the given file.
b. Write a Lex program to printout all HTML
7
tags in file.
c. Write a Lex program which adds line numbers
to the given file and display the same onto the
standard output.
Write a Lex program to count the number of
8 comment lines in a given C program. Also eliminate
them and copy that program into separate file.
Write a C program for implementing the
9 functionalities of predictive parser for the mini
language.
Experiment 11
Description:
A recursive descent parser is a kind of top-down parser built from a set of mutually recursive
procedures (or a non-recursive equivalent) where each such procedure usually implements one of
the productions of the grammar. Thus the structure of the resulting program closely mirrors that
of the grammar it recognizes.
Recursive descent with backtracking is a technique that determines which production to use by
trying each production in turn. Recursive descent with backtracking is not limited to LL(k)
grammars, but is not guaranteed to terminate unless the grammar is LL(k). Even when they
terminate, parsers that use recursive descent with backtracking may require exponential time.To
implement Recursive Decent Parser, perform the following steps.
Program:
#include <stdio.h>
#include <conio.h>
char input[100];
char prod[100][100];
int pos=-1,l,st=-1;
char id,num;
void E();
void T();
void F();
void advance();
void Td();
void Ed();
void advance()
{
pos++;
if(pos<l)
{
if(input[pos]>='0'&& input[pos]<='9')
{
num=input[pos];
id='\0';
Compiler Design(2170701) 161160107025
MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING
}
if((input[pos]>='a' || input[pos]>='A')&&(input[pos]<='z' || input[pos]<='Z'))
{id=input[pos];
num='\0';
}
}
}
void E()
{
strcpy(prod[++st],"E->TE'");
T();
Ed();
}
void Ed()
{
int p=1;
if(input[pos]=='+')
{
p=0;
strcpy(prod[++st],"E'->+TE'");
advance();
T();
Ed();
}
if(input[pos]=='-')
{ p=0;
strcpy(prod[++st],"E'->-TE'");
advance();
T();
Ed();
}
if(p==1)
{
strcpy(prod[++st],"E'->null");
}
}
void T()
{
strcpy(prod[++st],"T->FT'");
F();
Td();
}
void Td()
{
int p=1;
if(input[pos]=='*')
{
p=0;
strcpy(prod[++st],"T'->*FT'");
advance();
F();
Td();
}
if(input[pos]=='/')
{ p=0;
strcpy(prod[++st],"T'->/FT'");
advance();
F();
Td();
}
if(p==1)
strcpy(prod[++st],"T'->null");
}
void F()
{
if(input[pos]==id) {
strcpy(prod[++st],"F->id");
advance(); }
if(input[pos]=='(')
{
strcpy(prod[++st],"F->(E)");
advance();
E();
if(input[pos]==')') {
//strcpy(prod[++st],"F->(E)");
advance(); }
}
if(input[pos]==id)
{
strcpy(prod[++st],"F->id");
advance();
}
}
int main()
{
int i;
printf("Enter Input String ");
scanf("%s",input);
l=strlen(input);
input[l]='$';
advance();
E();
if(pos==l)
{
printf("String Accepted\n");
for(i=0;i<=st;i++)
{
printf("%s\n",prod[i]);
}
}
else
{
printf("String rejected\n");
}
getch();
return 0;
}
Output: