Experiment No 3 PDF
Experiment No 3 PDF
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 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.
• 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.
• 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>
char keywords[8][10] = {"int", "float", "if", "else", "while", "do", "return", "for"};
if (strcmp(word, keywords[i]) == 0)
return 1;
return 0;
int main() {
int i = 0, j = 0;
if (isalnum(str[i])) {
token[j++] = str[i];
} else {
if (j > 0) {
token[j] = '\0';
if (isKeyword(token))
else if (isalpha(token[0]))
else
j = 0;
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '=') {
} else if (str[i] == ';' || str[i] == '(' || str[i] == ')' || str[i] == '{' || str[i] == '}') {
}
i++;
return 0;
7. Output :
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.