0% found this document useful (0 votes)
18 views24 pages

CD Contenthalf

The document outlines practical exercises related to Compiler Design, focusing on the implementation of finite automata for string validation, usage of the Lex tool for lexical analysis, and various programming tasks using Lex. It includes code examples for counting words, characters, and lines, as well as converting Roman numerals to decimals and extracting comments from C programs. Additionally, it covers the implementation of a recursive descent parser without backtracking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views24 pages

CD Contenthalf

The document outlines practical exercises related to Compiler Design, focusing on the implementation of finite automata for string validation, usage of the Lex tool for lexical analysis, and various programming tasks using Lex. It includes code examples for counting words, characters, and lines, as well as converting Roman numerals to decimals and extracting comments from C programs. Additionally, it covers the implementation of a recursive descent parser without backtracking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

PRACTICAL-1

Implementation of Finite Automata and String Validation

 Finite automata (FA) are mathematical models used to recognize patterns in strings.
They consist of a finite set of states, a starting state, a set of accepting states, and a
transition function that defines how the automaton moves from one state to another
based on the input symbols.
 String validation using finite automata involves determining whether a given string is
accepted or rejected by the automaton. This is done by starting at the initial state and
processing the input symbols one by one, following the transitions defined by the
transition function. If the automaton reaches an accepting state after processing the
entire string, the string is accepted; otherwise, it is rejected.

class FiniteAutomaton:
def __init__(self, states, alphabet, transitions, start_state, accept_states):
self.states = states
self.alphabet = alphabet
self.transitions = transitions
self.start_state = start_state
self.accept_states = accept_states

def validate(self, string):


current_state = self.start_state

for symbol in string:


if symbol not in self.alphabet:
return False # Invalid symbol
current_state = self.transitions[current_state][symbol]

return current_state in self.accept_states

# Example usage:
states = {'q0', 'q1', 'q2'}
alphabet = {'a', 'b'}
transitions = {
'q0': {'a': 'q1', 'b': 'q0'},
'q1': {'a': 'q2', 'b': 'q1'},
'q2': {'a': 'q2', 'b': 'q2'}

ENROLLMENT NO.:210410107049 Page|1


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

start_state = 'q0'
accept_states = {'q2'}

fa = FiniteAutomaton(states, alphabet, transitions, start_state, accept_states)

string1 = "aabbb"
string2 = "ababa"

if fa.validate(string1):
print("String 1 is accepted.")
else:
print("String 1 is rejected.")

if fa.validate(string2):
print("String 2 is accepted.")
else:
print("String 2 is rejected.")

OUTPUT:

ENROLLMENT NO.:210410107049 Page|2


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

PRACTICAL-2

Introduction to Lex Tool.

Lex, a lexical analyzer generator, is a powerful tool used in computer science for creating
lexical analyzers, also known as scanners. These analyzers are essential components of
compilers, text editors, and other language processing tools.

How Lex Works


 Regular Expressions: Lex uses regular expressions to define patterns that should be
recognized in the input text. These patterns can represent keywords, identifiers,
numbers, operators, and other lexical elements.
 Actions: For each recognized pattern, you specify an action to be taken. This action
could be as simple as printing a message or as complex as generating code.
 Lexical Analyzer Generation: Lex generates a C program (or other language,
depending on the implementation) that reads the input text, identifies the patterns
defined in the regular expressions, and performs the corresponding actions.

Lex File Structure


A typical Lex file consists of three sections:
1. Declarations: This section includes declarations of variables, functions, and other
definitions that will be used in the actions.
2. Rules: This section contains the regular expressions and their corresponding actions.
3. User Subroutines: This section defines any additional functions or subroutines that are
used in the actions.

ENROLLMENT NO.:210410107049 Page|3


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

Example:

Key Features of Lex


 Regular expressions: Provides a concise and flexible way to define patterns.
 Actions: Allows you to specify custom actions for each recognized pattern.
 Flexibility: Can be used for a wide range of applications, including compilers, text
editors, and custom language processors.
 Efficiency: Generated lexical analyzers are typically optimized for performance.

ENROLLMENT NO.:210410107049 Page|4


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

PRACTICAL-3

 Implement following Programs Using Lex

a) Generate Histogram of words

%{
#include<stdio.h>
#include<string.h>
char word[]="students"; int count=0;
%}
%%
[a-zA-Z]+ {if(strcmp(yytext,word)==0) count++;}
%%
int yywrap(){} int main()
{
yyin=fopen("input.txt","r"); yylex();
printf("%d of %s",count,word);
}

INPUT:

Hi students.
How are you students?

ENROLLMENT NO.:210410107049 Page|5


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

OUTPUT:

b) Create a Lexer to take input from text file and count no of


Characters, no. of lines & no. of words.

%option noyywrap
%{
#include<stdio.h>
int word;
int line;
int ch;
%}
%%
[a-zA-Z0-9] {++ch;}
[.]*[ |\t] {++ch, ++word;}
[.]*[\n] {++ch, ++word, ++line;}
.;
%%
int main(void)

ENROLLMENT NO.:210410107049 Page|6


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

{
yyin=fopen("input.txt","r");
yylex();
printf("Characters : %d\n",ch);
printf("Words : %d\n",word);
printf("Lines : %d\n",line);
return 0;
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|7


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

c) Ceasor Cypher.

%{
#include<stdio.h>
#include<string.h>
%}
%%
[a-z] {char ch = yytext[0]; ch += 3;
if (ch> 'z') ch -= ('z'+1- 'a'); printf ("%c" ,ch );
}
[A-Z] { char ch = yytext[0] ; ch += 3;
if (ch> 'Z') ch -= ('Z'+1- 'A');
printf("%c",ch);
}
%%
int yywrap(){} int main()
{
yylex();
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|8


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

d) Extract single and multiline comments from C Program (also count).

%{
#include<stdio.h>
%}
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
int yywrap(){
return 1;
}
int main(){
yyin = fopen("input.c","r");
yyout = fopen("output.c","w");
yylex(); return 0;
}

Input File: input.c


//This is a single comment; This is not a comment;
/*But..
These are multiple comments
*/
Again, It is not a comment The output will be,
This is not a comment; Again, It is not a comment

ENROLLMENT NO.:210410107049 Page|9


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

OUTPUT:

Output File : output.c


This is not a comment;
Again, It is not a comment The output will be,
This is not a comment; Again, It is not a comment

ENROLLMENT NO.:210410107049 Page|10


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

PRACTICAL-4

 Implement following Programs Using Lex

a) Convert Roman to Decimal

%option noyywrap
%{
# include "roman.tab.h"
%}
%%
"I" {yylval = 1;return I;}
"II" {yylval = 2;return II;}
"III" {yylval = 3;return III;}
"IV" {yylval = 4;return IV;}
"V" {yylval = 5;return V;}
"VI" {yylval = 6;return VI;}
"VII" {yylval = 7;return VII;}
"VIII" {yylval = 8;return VIII;}
"IX" {yylval = 9;return IX;}
"X" {yylval = 10;return X;}
"XX" {yylval = 20;return XX;}
"XXX" {yylval = 30;return XXX;}
"XL" {yylval = 40;return XL;}
"L" {yylval = 50;return L;}
"LX" {yylval = 60;return LX;}
"LXX" {yylval = 70;return LXX;}
"LXXX" {yylval = 80;return LXXX;}

ENROLLMENT NO.:210410107049 Page|11


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

"XC" {yylval = 90;return XC;}


"C" {yylval = 100;return C;}
"CC" {yylval = 200;return CC;}
"CCC" {yylval = 300;return CCC;}
"CD" {yylval = 400;return CD;}
"D" {yylval = 500;return D;}
"DC" {yylval = 600;return DC;}
"DCC" {yylval = 700;return DCC;}
"DCCC" {yylval = 800;return DCCC;}
"CM" {yylval = 900;return CM;}
"M" {yylval = 1000;return M;}
\n {return EOL;}
int main()
{
printf("Enter Roman Value :\n");
yylex();
printf("\n%d");
}

OUTPUT:

ENROLLMENT NO.:210410107049 Page|12


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

b) Check weather given statement is compound or simple.

%option noyywrap
%{
int flag=0;
%}
%%
(""[aA][nN][dD]"")|(""[oO][rR]"")|(""[bB][uU][tT]"") {flag=1;}
%%
int main()
{
printf("Enter the sentence\n");
yylex();
if(flag==1)
printf("\nCompound sentence\n");
else
printf("\nSimple sentence\n");
return 0;
}

ENROLLMENT NO.:210410107049 Page|13


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

OUTPUT:

ENROLLMENT NO.:210410107049 Page|14


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

c) Extract html tags from .html file.

%{
%}
%%
"<"[^>]*> {printf("%s\n", yytext); } . ;
%%
int yywrap(){}
int main(int argc, char*argv[])
{
yyin=fopen("tags.txt","r");
yylex();
return 0;
}

Tags.txt
<html>
<head>
</head>
<body>
<h1>My first heading</h1>
<p>My first paragraph</p>
</body>
</html>

ENROLLMENT NO.:210410107049 Page|15


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

OUTPUT:

d) Write a Lex program which adds line numbers to the given file and
display the same onto the standard output.

%{
int line_number = 1;
%}
line .*\n
%%
{line} { printf("%10d %s", line_number++, yytext); }
%%
int yywrap(){}
int main(int argc, char*argv[])
{

ENROLLMENT NO.:210410107049 Page|16


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

extern FILE *yyin;


yyin = fopen("input.txt","r");
yylex();
return 0;
}

Input.txt
hii

OUTPUT:

ENROLLMENT NO.:210410107049 Page|17


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

PRACTICAL-5

 Implementation of Recursive Descent Parser without backtracking

Input: The string to be parsed.


Output: Whether string parsed successfully or not.
Explanation: Students have to implement the recursive procedure for RDP for
a typical grammar. The production no are displayed as they are used to derive
the string.

#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
#include"ctype.h"
char ip_sym[15], ip_ptr = 0, op[50], tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n = 0;
void e() {
strcpy(op, "TE'");
printf("E=%-25s", op);
printf("E->TE'\n");

ENROLLMENT NO.:210410107049 Page|18


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

t();
e_prime();
}
void e_prime() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++)
if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);
for (n = 0; n < l && op[n] != 'E'; n++);
if (ip_sym[ip_ptr] == '+') {
i = n + 2;
do {
op[i + 2] = op[i];
i++;
} while (i <= l);
op[n++] = '+';
op[n++] = 'T';
op[n++] = 'E';
op[n++] = 39;
printf("E=%-25s", op);
printf("E'->+TE'\n");
advance();
t();
e_prime();
} else {
op[n] = 'e';
for (i = n + 1; i <= strlen(op); i++) op[i] = op[i + 1];

ENROLLMENT NO.:210410107049 Page|19


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

printf("E=%-25s", op);
printf("E'->e");
}
}
void t() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++) if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);
for (n = 0; n < l && op[n] != 'T'; n++); i = n + 1;
do {
op[i + 2] = op[i];
i++;
} while (i < l);
op[n++] = 'F';
op[n++] = 'T';
op[n++] = 39;
printf("E=%-25s", op);
printf("T->FT'\n");
f();
t_prime();
}
void t_prime() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++) if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);

ENROLLMENT NO.:210410107049 Page|20


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

for (n = 0; n < l && op[n] != 'T'; n++); if (ip_sym[ip_ptr] == '*') {


i = n + 2;
do {
op[i + 2] = op[i];
i++;
} while (i < l);
op[n++] = '*';
op[n++] = 'F';
op[n++] = 'T';
op[n++] = 39;
printf("E=%-25s", op);
printf("T'->*FT'\n");
advance();
f();
t_prime();
} else {
op[n] = 'e';
for (i = n + 1; i <= strlen(op); i++)
op[i] = op[i + 1];
printf("E=%-25s", op);
printf("T'->e\n");
}
}
void f() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++)
if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);

ENROLLMENT NO.:210410107049 Page|21


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

l = strlen(op);
for (n = 0; n < l && op[n] != 'F'; n++);
if ((ip_sym[ip_ptr] == 'i') || (ip_sym[ip_ptr] == 'I')) { op[n] = 'i';
printf("E=%-25s", op);
printf("F->i\n");
advance();
} else {
if (ip_sym[ip_ptr] == '(') {
advance();
e();
if (ip_sym[ip_ptr] == ')') {
advance();
i = n + 2;
do {
op[i + 2] = op[i];
i++;
} while (i <= l);
op[n++] = '(';
op[n++] = 'E';
op[n++] = ')';
printf("E=%-25s", op);
printf("F->(E)\n");
}
} else {
printf("\n\t syntax error");
getch();
exit(1);
}
}

ENROLLMENT NO.:210410107049 Page|22


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

}
void advance() {
ip_ptr++;
}
void main() {
int i;
clrscr();
printf("\nGrammar without left recursion");
printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' "); printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i");
printf("\n Enter the input expression:");
gets(ip_sym);
printf("Expressions");
printf("\t Sequence of production rules\n");
e();
for (i = 0; i < strlen(ip_sym); i++) {
if (ip_sym[i] != '+' && ip_sym[i] != '*' && ip_sym[i] != '(' && ip_sym[i] != ')' &&
ip_sym[i] != 'i' && ip_sym[i] != 'I') { printf("\nSyntax error");
break;
}
for (i = 0; i <= strlen(op); i++)
if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
printf("\nE=%-25s", op);
}
getch();
}

ENROLLMENT NO.:210410107049 Page|23


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME :COMPILER DESIGN SUBJECT CODE:3170701

OUTPUT:

ENROLLMENT NO.:210410107049 Page|24

You might also like