0% found this document useful (0 votes)
37 views

Sodapd

Uploaded by

Planet
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)
37 views

Sodapd

Uploaded by

Planet
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/ 1

 Home  Coding Ground  Jobs  Whiteboard  Tools  Business   Teach with us     


Category  Search tutorials ...   Prime Packs  Courses  eBooks  Library  Q/A  Login

C program to detect tokens in a C program


C Server Side Programming Programming

 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

More Detail More Detail More Detail


Java

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.

Examples of different types of tokens in C.


CSS

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){

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


UPSC IAS Exams Notes    ch == '/' || ch == '>' || ch == '<' ||
   ch == '=')
   return (true);
Developer's Best Practices
   return (false);
}
Questions and Answers
// Returns 'true' if the string is a VALID IDENTIFIER.

bool isvalidIdentifier(char* str){

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)

Computer Glossary    return (false);


   return (true);
}
Who is Who
bool isValidKeyword(char* str) {

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


   || !strcmp(str, "double") || !strcmp(str, "float") || !strcmp(str, "return") || !strcmp(str,    "c
   || !strcmp(str, "sizeof") || !strcmp(str, "long") || !strcmp(str, "short") || !strcmp(str, "typede
   || !strcmp(str, "void") || !strcmp(str, "static") || !strcmp(str, "struct") || !strcmp(str, "goto
   return (true);
   return (false);
}
bool isValidInteger(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]
      && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' || (str[i] == '-' && i > 0
      return (false);
   }
   return (true);
}
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]
      && 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];

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


   return (subStr);
}
void detectTokens(char* str) {

   int left = 0, right = 0;


   int length = strlen(str);
   while (right <= length && left <= right) {
      if (isValidDelimiter(str[right]) == false)

      right++;

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

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

         printf("Valid operator : '%c'\n", str[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)

            printf("Valid keyword : '%s'\n", subStr);

         else if (isValidInteger(subStr) == true)

            printf("Valid Integer : '%s'\n", subStr);

         else if (isRealNumber(subStr) == true)

            printf("Real Number : '%s'\n", subStr);

         else if (isvalidIdentifier(subStr) == true

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

         printf("Valid Identifier : '%s'\n", subStr);

         else if (isvalidIdentifier(subStr) == false

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

         printf("Invalid Identifier : '%s'\n", subStr);

         left = right;

      }
   }
   return;
}
int main(){
   char str[100] = "float x = a + 1b; ";

   printf("The Program is : '%s' \n", str);

   printf("All Tokens are : \n");

   detectTokens(str);

   return (0);
}

Output
The Program is : 'float x = a + 1b; '

All Tokens are :

Valid keyword : 'float'

Valid Identifier : 'x'

Valid operator : '='

Valid Identifier : 'a'

Valid operator : '+'

Invalid Identifier : '1b'

sudhir sharma 
Updated on 17-Jul-2020 12:54:18

 Related Questions & Answers

C program to print string tokens

C/C++ Tokens?

Tokens in C

Explain C tokens in C Language

What are tokens in C#?

Bag of Tokens in C++

What are the C Tokens?

Java Program to Detect loop in a LinkedList

What are the tokens in C ?

Tokens vs Identifiers vs Keywords in C++

Python Program to Detect the Cycle in a Linked List

How to detect integer overflow in C/C++?

C/C++ program to shutdown a system?

What do you mean by C++ Tokens?

How to launch a program using C++ program?

 Previous Page  Print Page Next Page  

 About us  Refund Policy  Terms of use  Privacy Policy FAQ's  Contact


We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy.
Agree
Learn more
© Copyright 2022. All Rights Reserved.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy