Sodapd
Sodapd
Category Search tutorials ... Prime Packs Courses eBooks Library Q/A Login
Trending Categories
Data Structure
Networking
C In Depth: The Complete C Practical C++: Learn C++ Basics Step Master C And Embedded C
Programming Guide For Beginners By Step Programming- Learn As You Go
RDBMS
45 Lectures 4.5 hours 50 Lectures 4.5 hours 66 Lectures 5.5 hours
Operating System TELCOMA Global Edouard Renard NerdyElectronics
Here, we will create a c program to detect tokens in a C program. This is called the lexical analysis phase of the compiler. The
iOS
lexical analyzer is the part of the compiler that detects the token of the program and sends it to the syntax analyzer.
HTML Token is the smallest entity of the code, it is either a keyword, identifier, constant, string literal, symbol.
Example
Android
Keywords: for, if, include, etc
Identifier: variables, functions, etc
Python separators: ‘,’, ‘;’, etc
operators: ‘-’, ‘=’, ‘++’, etc
C Programming
Program to detect tokens in a C program−
C++ Example
Live Demo
C# #include <stdbool.h>
#include <stdio.h>
MongoDB #include <string.h>
#include <stdlib.h>
bool isValidDelimiter(char ch) {
MySQL
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
Javascript ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
PHP return (true);
return (false);
}
Selected Reading bool isValidOperator(char ch){
Effective Resume Writing if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
HR Interview Questions str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isValidDelimiter(str[0]) == true)
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]
&& str[i] != '9' && str[i] != '.' || (str[i] == '-' && i > 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}
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];
right++;
right++;
left = right;
} else if (isValidDelimiter(str[right]) == true && left != right || (right == length && left !=
char* subStr = subString(str, left, right - 1);
if (isValidKeyword(subStr) == true)
left = right;
}
}
return;
}
int main(){
char str[100] = "float x = a + 1b; ";
detectTokens(str);
return (0);
}
Output
The Program is : 'float x = a + 1b; '
sudhir sharma
Updated on 17-Jul-2020 12:54:18
C/C++ Tokens?
Tokens in C