0% found this document useful (0 votes)
13 views

SSCD assignment1

Uploaded by

sdswathi790
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)
13 views

SSCD assignment1

Uploaded by

sdswathi790
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/ 11

Department of Computer Science & Engineering

SYSTEM SOFTWARE AND COMPILER DESIGN


Academic Year 2023-2024

1. Title of the Assignment Installation of FLEX tool to execute


Lex and YACC programs

2. Department/Section Computer Science and Engineering / 6C

Name of the student with 1. Ayeshath Hafeeza (4SF22CS402)


3. USN
2. Sushma Y (4SF22CS415)
3. Swathi S D (4SF22CS416)
4. Veekshitha (4SF22CS417)

4. Submitted To Mrs. Suketha


Introduction to Flex

Flex, short for Fast Lexical Analyzer Generator, is a powerful tool designed for generating lexical
analyzers, commonly known as scanners or lexers. These analyzers are essential components in
the process of translating high-level programming languages into a format that can be understood
and executed by a computer. Flex operates in tandem with Yacc (Yet Another Compiler-
Compiler), a tool used for creating parsers. Together, Flex and Yacc form a robust duo for the
development of compilers and interpreters.
Purpose and Functionality
The primary function of Flex is to read an input file containing a set of regular expressions and
corresponding actions. These regular expressions describe patterns in the input text, while the
actions specify what should be done when these patterns are encountered. When Flex processes
the input file, it generates a C source file that implements a scanner.

How Flex Works


1. Specification File: The user provides Flex with a specification file, which consists of
three main sections:
o Definition Section: This section includes declarations and macro definitions.
o Rule Section: Here, the user defines regular expressions paired with actions. Each
rule specifies a pattern to be matched in the input text and an action to be executed
when the pattern is found.
o User Defined Section: This section contains additional C code that is copied
directly into the generated scanner.
2. Generating the Scanner: When the Flex tool processes the specification file, it produces
a C source file. This file contains the implementation of the scanner, which includes the
logic to recognize the specified patterns and execute the corresponding actions.
3. Compilation: The generated C source file is then compiled using a standard C compiler.
The result is an executable scanner that can process input text, match the specified
patterns, and execute the defined actions.

Integration with Yacc


Flex is often used in conjunction with Yacc. While Flex generates the lexical analyzer, Yacc is
responsible for creating the parser. The parser analyzes the structure of the input text according
to a given grammar, which is typically specified in Backus-Naur Form (BNF). The lexical
analyzer produced by Flex feeds tokens to the parser, which then processes these tokens based on
the grammar rules.
This combination allows developers to efficiently create compilers and interpreters. The lexical
analyzer breaks the input text into meaningful tokens, and the parser uses these tokens to construct
a syntax tree or other intermediate representations.

Benefits of Using Flex


• Efficiency: Flex generates highly optimized scanners that can process input text quickly.
• Ease of Use: By using regular expressions, developers can easily specify patterns for the
lexical analyzer.
• Flexibility: The ability to define custom actions for each pattern provides great flexibility
in how input text is processed.
• Integration: Seamless integration with Yacc simplifies the process of creating full-
fledged compilers and interpreters.

Conclusion
Flex is an indispensable tool for developers working on language translation, compiler
construction, and text processing. By automating the generation of lexical analyzers, Flex
streamlines the process of pattern recognition and tokenization. When used in conjunction with
Yacc, Flex enables the creation of robust and efficient parsers, making it a cornerstone in the
realm of compiler design and implementation.
1. Write a LEX program to recognize valid arithmetic expression. Identifiers in the
expression could be only integers and operators could be + and *. Count the identifiers
& operators present and print them separately.
………………………………………………………………………………………………………..
PROGRAM:

%{
#include<stdio.h>
int num=0,op=0,i=0,j=0,number[10],top=-1;
char opr[10],stack[10];
%}
%%
[0-9]+ {number[num++]=atoi(yytext);}
"+" {opr[op++]='+';}
"*" {opr[op++]='*';}
"(" {stack[++top]='(';}
")" {if(stack[top]=='(' && top!=-1)
top--;
else
{
printf("Invalid expression\n");
exit(0);
}
}
%%

void main()
{
printf("Enter Expression:\n");
yylex();
if(top==-1 && num==op+1)
{
printf("Valid Expression:\n");
printf("Number of identifiers=%d\n",num);
printf("Identifier are present\n");
for(i=0;i<num;i++)
{
printf("%d\n",number[i]);
}
printf("Number of operators=%d\n",op);
printf("operators present are\n");
for(j=0;j<op;j++)
{
printf("%c\n",opr[j]);
}
}
else
{
printf("Invalid Expression");
}
}
Output :
2. Write a LEX program to eliminate comment lines in a C program and copy the
resulting program into a separate file.
………………………………………………………………………………………………………..
PROGRAM:

%{
#include<stdio.h>
#include<stdlib.h>
int comment=0;
int state=1;
%}

%%
"//".*\n { comment++;}
"/*" { state=0;}
"*/" {if(state==0)
comment++;
state=1;}
.|\n {if(state==1)
fprintf(yyout,"%s",yytext);}
%%
void main()
{
char inpfile[500],outfile[500];
printf("enter input file name\n");
scanf("%s",inpfile);
printf("Enter output file name\n");
scanf("%s",outfile);
yyin=fopen(inpfile,"r");
yyout=fopen(outfile,"w");
yylex();
printf("Total no. of comment line is: %d\n",comment);
}

Output :
3. Design, develop and implement a C/Java program to generate the machine code using
Triples for the statement A = -B * (C +D) whose intermediate code in three-
address form:
T1 = -B
T2 = C + D
T3 = T1 *
T2
A = T3
………………………………………………………………………………………………………..
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char op[2],arg1[5],arg2[5],result[5];
void main()
{
FILE *fp1,*fp2;
fp1=fopen("input.txt","r+");
fp2=fopen("output.txt","w+");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s%s",result,arg1,op,arg2);
if(strcmp(op,"+")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nADD R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"*")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nADD R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"-")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nADD R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"/")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nADD R0,%s",arg2);
fprintf(fp2,"\nMOV %s,R0",result);
}
if(strcmp(op,"=")==0)
{
fprintf(fp2,"\nMOV R0,%s",arg1);
fprintf(fp2,"\nMOV %s,R0",result);
}
}
fclose(fp1);
fclose(fp2);
}

Output :
4. Design, develop and implement YACC/C program to demonstrate Shift Reduce
Parsing technique for the grammar rules: E E+T | T, T T*F | F, F (E) | id and parse
the sentence: id + id * id.
………………………………………………………………………………………………………..
PROGRAM:

#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];

void check();
void main()
{
puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
puts("enter input string ");

gets(a);
c=strlen(a);
strcpy(act,"SHIFT-
>");

puts("stack \t input \t action");

for(i=0; j<c; i++,j++)


{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);

check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%s symbols",stk,a,act);
check();
}
}
}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0; z<c; z++)

if(stk[z]=='i' && stk[z+1]=='d')


{
stk[z]= 'E';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0; z<c; z++)

if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')


{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)

if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')


{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)

if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')


{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy