0% found this document useful (0 votes)
0 views4 pages

Experiment No 3 PDF

The document outlines an experiment focused on generating tokens from source code using a C program. It explains the role of a lexical analyzer, its advantages and disadvantages, and includes a sample program that identifies keywords, identifiers, numbers, operators, and special symbols from the input code. The experiment concludes with an understanding of implementing the algorithm for token generation from grammar.
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)
0 views4 pages

Experiment No 3 PDF

The document outlines an experiment focused on generating tokens from source code using a C program. It explains the role of a lexical analyzer, its advantages and disadvantages, and includes a sample program that identifies keywords, identifiers, numbers, operators, and special symbols from the input code. The experiment concludes with an understanding of implementing the algorithm for token generation from grammar.
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/ 4

EXPERIMENT NO.

03

1. Title:
Illustrate how to generate the token from source code.
2. Aim:
Write a C program to generate Tokens from the given Grammer.

3. Theory:

The lexical analyzer performs the following tasks-

• The lexical analyzer is responsible for removing the white spaces and comments from the
source program.
• It corresponds to the error messages with the source program.
• It helps to identify the tokens.
• The input characters are read by the lexical analyzer from the source code.

Advantages of Lexical Analysis

• Lexical analysis helps the browsers to format and display a web page with the help of
parsed data.
• It is responsible to create a compiled binary executable code.
• It helps to create a more efficient and specialised processor for the task.

Disadvantages of Lexical Analysis

• It requires additional runtime overhead to generate the lexer table and construct the tokens.
• It requires much effort to debug and develop the lexer and its token description.
• Much significant time is required to read the source code and partition it into tokens.

4. Pre-Questions:
1. What is the function of lexical analyzer?
2. what is symbol table?
5. Post-Questions:

1. Define Scanner.
2. What are the advantages and Disadvantages of Lexical analysis.

6. Program Code:

#include <stdio.h>

#include <ctype.h>

#include <string.h>

int isKeyword(char *word) {

char keywords[8][10] = {"int", "float", "if", "else", "while", "do", "return", "for"};

for (int i = 0; i < 8; i++) {

if (strcmp(word, keywords[i]) == 0)

return 1;

return 0;

int main() {

char str[100], token[20];

int i = 0, j = 0;

printf("Enter source code: ");


fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {

if (isalnum(str[i])) {

token[j++] = str[i];

} else {

if (j > 0) {

token[j] = '\0';

if (isKeyword(token))

printf("%s : Keyword\n", token);

else if (isalpha(token[0]))

printf("%s : Identifier\n", token);

else

printf("%s : Number\n", token);

j = 0;

if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '=') {

printf("%c : Operator\n", str[i]);

} else if (str[i] == ';' || str[i] == '(' || str[i] == ')' || str[i] == '{' || str[i] == '}') {

printf("%c : Special Symbol\n", str[i]);

}
i++;

return 0;

7. Output :

Input => int x = 5 + 3;

Output =>

int : Keyword

x : Identifier

= : Operator

5 : Number

+ : Operator

3 : Number

; : Special Symbol

8. Outcome: Able to understand how to implement the algorithm to get the token from
grammar.

9. Conclusion: Thus we have studied how to get token for the given grammar.

You might also like