Programm 1
Programm 1
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
return (true);
return (false);
ch == '=')
return (true);
return (false);
}
// Returns 'true' if the string is a VALID IDENTIFIER.
return (false);
return (true);
!strcmp(str, "break") ||
return (true);
return (false);
}
// Returns 'true' if the string is an INTEGER.
if (len == 0)
return (false);
return (false);
return (true);
if (len == 0)
return (false);
if (str[i] == '.')
hasDecimal = true;
return (hasDecimal);
int i;
return (subStr);
if (isDelimiter(str[right]) == false)
right++;
right++;
left = right;
if (isKeyword(subStr) == true)
left = right;
return;
}
// DRIVER FUNCTION
int main()
return (0);
'int' IS A KEYWORD
'=' IS AN OPERATOR
'+' IS AN OPERATOR
isDelimiter(char ch): This function checks if a character is a delimiter (e.g., space, operator,
parentheses, etc.). If it is, the function returns true; otherwise, it returns false.
validIdentifier(char* str): This function checks if a given string is a valid identifier. A valid
identifier does not start with a digit and should not be a delimiter. If it meets these
conditions, the function returns true; otherwise, it returns false.
isKeyword(char* str): This function checks if a given string matches a predefined list of
keywords (like if, else, while, etc.). If it matches any keyword, the function returns true;
otherwise, it returns false.
isInteger(char* str): This function checks if a string is a valid integer. A valid integer string
only contains digits (0-9). If the string represents an integer, the function returns true;
otherwise, it returns false.
isRealNumber(char* str): This function checks if a string represents a real number. A real
number contains digits and exactly one decimal point. If it is a real number, the function
returns true; otherwise, it returns false.
subString(char* str, int left, int right): This function extracts a substring from the input string
str starting from index left to right. The function returns the extracted substring.
2. Parsing Function
parse(char* str): This is the core function that parses the input string. It reads through the
input, identifies tokens (like keywords, operators, identifiers, etc.), and categorizes them
accordingly.
o Initialization: The function starts by initializing left and right pointers, which help in
traversing and extracting substrings from the input.
o Token Identification Loop: The loop runs while right is less than or equal to the
length of the string.
Token Expansion: The loop expands the right pointer to find tokens between
delimiters.
If left does not equal right, the substring between left and right-1 is
checked for whether it is a keyword, integer, real number, valid
identifier, or an invalid identifier.
The left and right pointers are updated accordingly for the next token.
3. Driver Function
main(): The driver function initializes a string str with a sample code snippet and calls the
parse() function to analyze it. It prints the type of each token found in the input string.
4. Example Execution
o int as a keyword
o a as a valid identifier
o = as an operator
o b as a valid identifier
o + as an operator
o 1c as not a valid identifier (because identifiers can't start with digits or contain them
in this context)
Conclusion
The code serves as a basic lexical analyzer for a simple language (similar to C). It
breaks down a source string into its basic tokens and categorizes each one based on
predefined rules for identifiers, keywords, operators, and numbers. This kind of
program is a simplified version of what a compiler's lexical analysis phase might look
like.