0% found this document useful (0 votes)
13 views15 pages

Ex: 1.a Program To Recognize A Few Patterns in C Aim

The document outlines the implementation of a lexical analyzer and a symbol table in C, detailing algorithms and source code for both programs. The lexical analyzer recognizes patterns in C code, while the symbol table manages identifiers and their types. Both programs have been executed successfully, demonstrating their functionality.

Uploaded by

joshua0922005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Ex: 1.a Program To Recognize A Few Patterns in C Aim

The document outlines the implementation of a lexical analyzer and a symbol table in C, detailing algorithms and source code for both programs. The lexical analyzer recognizes patterns in C code, while the symbol table manages identifiers and their types. Both programs have been executed successfully, demonstrating their functionality.

Uploaded by

joshua0922005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Ex: 1.

Program to Recognize a Few Patterns in C

Aim

To write a C program to develop a lexical analyzer to recognize a few patterns in C.

Algorithm

Step1: Start the program.

Step2: Read a simple C language program as input string.

Step3: Display the input string.

Step4: Separate the tokens of the string.

Step5: Display each token with its type.

Step6: Also display whether the token is valid or invalid.

Step7: Stop the program.

Source Code

//Develop a lexical analyzer to recognize a few patterns in C.

#include <stdbool.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

// Returns 'true' if the character is a DELIMITER.

bool isDelimiter(char ch)

if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||

ch == '/' || ch == ',' || ch == ';' || ch == '>' ||

ch == '<' || ch == '=' || ch == '(' || ch == ')' ||

ch == '[' || ch == ']' || ch == '{' || ch == '}')

return (true);
return (false);

// Returns 'true' if the character is an OPERATOR.

bool isOperator(char ch)

if (ch == '+' || ch == '-' || ch == '*' ||

ch == '/' || ch == '>' || ch == '<' ||

ch == '=')

return (true);

return (false);

// Returns 'true' if the string is a VALID IDENTIFIER.

bool validIdentifier(char* str)

if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||

str[0] == '3' || str[0] == '4' || str[0] == '5' ||

str[0] == '6' || str[0] == '7' || str[0] == '8' ||

str[0] == '9' || isDelimiter(str[0]) == true)

return (false);

return (true);

// Returns 'true' if the string is a KEYWORD.

bool isKeyword(char* str)

{
if (!strcmp(str, "if") || !strcmp(str, "else") ||

!strcmp(str, "while") || !strcmp(str, "do") ||

!strcmp(str, "break") ||

!strcmp(str, "continue") || !strcmp(str, "int")

|| !strcmp(str, "double") || !strcmp(str, "float")

|| !strcmp(str, "return") || !strcmp(str, "char")

|| !strcmp(str, "case") || !strcmp(str, "char")

|| !strcmp(str, "sizeof") || !strcmp(str, "long")

|| !strcmp(str, "short") || !strcmp(str, "typedef")

|| !strcmp(str, "switch") || !strcmp(str, "unsigned")

|| !strcmp(str, "void") || !strcmp(str, "static")

|| !strcmp(str, "struct") || !strcmp(str, "goto"))

return (true);

return (false);

// Returns 'true' if the string is an INTEGER.

bool isInteger(char* str)

int i, len = strlen(str);

if (len == 0)

return (false);

for (i = 0; i < len; i++) {

if (str[i] != '0' && str[i] != '1' && str[i] != '2'

&& str[i] != '3' && str[i] != '4' && str[i] != '5'

&& str[i] != '6' && str[i] != '7' && str[i] != '8'


&& str[i] != '9' || (str[i] == '-' && i > 0))

return (false);

return (true);

// Returns 'true' if the string is a REAL NUMBER.

bool isRealNumber(char* str)

int i, len = strlen(str);

bool hasDecimal = false;

if (len == 0)

return (false);

for (i = 0; i < len; i++) {

if (str[i] != '0' && str[i] != '1' && str[i] != '2'

&& str[i] != '3' && str[i] != '4' && str[i] != '5'

&& str[i] != '6' && str[i] != '7' && str[i] != '8'

&& str[i] != '9' && str[i] != '.' ||

(str[i] == '-' && i > 0))

return (false);

if (str[i] == '.')

hasDecimal = true;

return (hasDecimal);

}
// Extracts the SUBSTRING.

char* subString(char* str, int left, int right)

int i;

char* subStr = (char*)malloc(

sizeof(char) * (right - left + 2));

for (i = left; i <= right; i++)

subStr[i - left] = str[i];

subStr[right - left + 1] = '\0';

return (subStr);

// Parsing the input STRING.

void parse(char* str)

int left = 0, right = 0;

int len = strlen(str);

while (right <= len && left <= right) {

if (isDelimiter(str[right]) == false)

right++;

if (isDelimiter(str[right]) == true && left == right) {

if (isOperator(str[right]) == true)

printf("'%c' IS AN OPERATOR\n", str[right]);


right++;

left = right;

} else if (isDelimiter(str[right]) == true && left != right

|| (right == len && left != right)) {

char* subStr = subString(str, left, right - 1);

if (isKeyword(subStr) == true)

printf("'%s' IS A KEYWORD\n", subStr);

else if (isInteger(subStr) == true)

printf("'%s' IS AN INTEGER\n", subStr);

else if (isRealNumber(subStr) == true)

printf("'%s' IS A REAL NUMBER\n", subStr);

else if (validIdentifier(subStr) == true

&& isDelimiter(str[right - 1]) == false)

printf("'%s' IS A VALID IDENTIFIER\n", subStr);

else if (validIdentifier(subStr) == false

&& isDelimiter(str[right - 1]) == false)

printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);

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

Implementation of Symbol Table using C

Aim

To write a program for implementing symbol table using C.

Algorithm

Step 1: Start the program.

Step 2: Read an expression in C.

Step 3: Create an entry in the symbol table for each valid identifier.

Step 4: Display each identifier with its type.


Step 5: Stop the program.

Source Code

//Implementation of symbol table

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

printf("Expression terminated by $:");

while((c=getchar())!='$')

b[i]=c;

i++;

n=i-1;

printf("Given Expression:");

i=0;

while(i<=n)

printf("%c",b[i]);
i++;

printf("\n Symbol Table\n");

printf("Symbol \t addr \t type");

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

Implementation of a Lexical Analyzer using LEX

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

/*Implementation of Lexical Analyzer using LEX tool – lexical.l */

%{

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 |

goto {printf("\n\t%s is a keyword",yytext);}

"/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}

{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}

\{ {if(!COMMENT)printf("\n BLOCK BEGINS");}

\} {if(!COMMENT)printf("BLOCK ENDS ");}

{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}

\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}

[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}

\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}

\( ECHO;
= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}

\<= |

\>= |

\< |

== |

\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}

%%

int main(int argc, char **argv)

FILE *file;

file=fopen("input.txt","r");

if(!file)

printf("could not open the 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.

You might also like