Ex: 1.a Program To Recognize A Few Patterns in C Aim
Ex: 1.a Program To Recognize A Few Patterns in C Aim
Aim
Algorithm
Source Code
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
return (true);
return (false);
ch == '=')
return (true);
return (false);
return (false);
return (true);
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "break") ||
return (true);
return (false);
if (len == 0)
return (false);
return (false);
return (true);
if (len == 0)
return (false);
return (false);
if (str[i] == '.')
hasDecimal = true;
return (hasDecimal);
}
// Extracts the SUBSTRING.
int i;
return (subStr);
if (isDelimiter(str[right]) == false)
right++;
if (isOperator(str[right]) == true)
left = right;
if (isKeyword(subStr) == true)
left = right;
return;
}
OUTPUT
RESULT
Thus the program for developing a lexical analyzer to recognize a few patterns in C
has been executed successfully.
Ex: 1.b
Aim
Algorithm
Step 3: Create an entry in the symbol table for each valid identifier.
Source Code
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
int i=0,j=0,x=0,n;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
while((c=getchar())!='$')
b[i]=c;
i++;
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
printf("%c",b[i]);
i++;
while(j<=n)
c=b[j];
if(isalpha(toascii(c)))
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c \t %d \t identifier\n",c,p);
x++;
j++;
else
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
p=malloc(ch);
add[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}}}
getch();
OUTPUT
RESULT
Thus the program for symbol table has been executed successfully.
Ex: 2
Aim
To write a program for implementing a Lexical Analyzer using LEX tool.
Procedure
1. A LEX program contains three sections: definitions, rules, and user subroutines. Each
section must be separated from the others by a line containing only the delimiter, %
%. The format is as follows: definitions %% rules %% user_subroutines
2. In definition section, the variables make up the left column, and their definitions make
up the right column. Any C statements should be enclosed in %{..}%. An identifier is
defined such that the first letter of an identifier is alphabet and the remaining letters
are alphanumeric.
3. In rules section, the left column contains the pattern to be recognized in an input file
named yylex(). The right column contains the C program fragment executed when
that pattern is recognized. The various patterns are keywords, operators, new line
character, number, string, identifier, beginning and end of block, comment statements,
preprocessor directive statements etc.
4. Each pattern may have a corresponding action, that is, a fragment of C source code to
execute when the pattern is matched.
5. When yylex() matches a string in the input stream, it copies the matched text to an
external character array, yytext, before it executes any actions in the rules section.
6. In user subroutine section, main routine calls yylex(). yywrap() is used to get more
input.
7. The LEX command uses the rules and actions contained in file to generate a program,
lex.yy.c, which can be compiled with the gcc command. That program can then
receive input, break the input into the logical pieces defined by the rules in file, and
run program fragments contained in the actions in file.
8. This LEX program reads a C file as input and displays the tokens of the input file as
output.
Source Code
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s is a preprocessor directive",yytext);}
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
%%
FILE *file;
file=fopen("input.txt","r");
if(!file)
exit(0);
yyin=file;
yylex();
printf("\n");
return(0);
int yywrap()
return(1);
}
INPUT
//input.txt
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
OUTPUT
RESULT
Thus the program for implementation of a Lexical Analyzer using LEX tool has been
executed successfully.