0% found this document useful (0 votes)
75 views7 pages

Index: Ex No Title No Date Sign

The document describes a C program to implement recursive descent parsing. It defines procedures for non-terminals like E, T, F, Ed and Td. It reads an input string, calls the E procedure and tracks the parsing steps by storing the parsing configurations in an array. If the whole string is parsed, it prints "String Accepted", otherwise it prints "String Rejected".
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

Index: Ex No Title No Date Sign

The document describes a C program to implement recursive descent parsing. It defines procedures for non-terminals like E, T, F, Ed and Td. It reads an input string, calls the E procedure and tracks the parsing steps by storing the parsing configurations in an array. If the whole string is parsed, it prints "String Accepted", otherwise it prints "String Rejected".
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS

FACULTY OF ENGINEERING, GAURIDAD CAMPUS.


DEPARTMENT OF COMPUTER ENGINEERING

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.

Compiler Design(2170701) 161160107025


MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING

Write a C program for constructing of LL (1)


10
parsing.
Write a C program for constructing recursive descent
11
parsing.
12 Write a C program to implement LALR parsing.
Write a C program to implement operator
13
precedence parsing.
To Study about Yet Another Compiler-
14
Compiler(YACC).

Create Yacc and Lex specification files to recognizes


15
arithmetic expressions involving +, -, * and / .

Create Yacc and Lex specification files are used to


16 generate a calculator which accepts,integer and float
type arguments.

Compiler Design(2170701) 161160107025


MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING

Experiment 11

Title: Write a C program for constructing recursive descent parsing.

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.

• Read the input string.


• Write procedures for the non terminals
• Verify the next token equals to non terminals if it satisfies match the non terminal. If the
input string does not match print error.

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()
{

Compiler Design(2170701) 161160107025


MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING

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);

Compiler Design(2170701) 161160107025


MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING

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:

Compiler Design(2170701) 161160107025


MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS
FACULTY OF ENGINEERING, GAURIDAD CAMPUS.
DEPARTMENT OF COMPUTER ENGINEERING

Compiler Design(2170701) 161160107025

You might also like