SSCD assignment1
SSCD assignment1
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.
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-
>");
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++)