Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App

C program to detect tokens in a C program

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. 
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
For Example: 

1) Keywords: 
Examples- for, while, if etc.

2) Identifier
Examples- Variable name, function name etc.

3) Operators:
Examples- '+', '++', '-' etc.

4) Separators:
Examples- ', ' ';' etc

Below is a C program to print all the keywords, literals, valid identifiers, invalid identifiers, integer number, real number in a given C program:

C
#include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  // Returns 'true' if the character is a DELIMITER. bool isDelimiter(char ch) {     if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||          ch == '/' || ch == ',' || ch == ';' || ch == '>' ||          ch == '<' || ch == '=' || ch == '(' || ch == ')' ||          ch == '[' || ch == ']' || ch == '{' || ch == '}')         return (true);     return (false); }  // Returns 'true' if the character is an OPERATOR. bool isOperator(char ch) {     if (ch == '+' || ch == '-' || ch == '*' ||          ch == '/' || ch == '>' || ch == '<' ||          ch == '=')         return (true);     return (false); }  // Returns 'true' if the string is a VALID IDENTIFIER. bool validIdentifier(char* str) {     if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||         str[0] == '3' || str[0] == '4' || str[0] == '5' ||          str[0] == '6' || str[0] == '7' || str[0] == '8' ||          str[0] == '9' || isDelimiter(str[0]) == true)         return (false);     return (true); }  // Returns 'true' if the string is a KEYWORD. bool isKeyword(char* str) {     if (!strcmp(str, "if") || !strcmp(str, "else") ||         !strcmp(str, "while") || !strcmp(str, "do") ||          !strcmp(str, "break") ||           !strcmp(str, "continue") || !strcmp(str, "int")         || !strcmp(str, "double") || !strcmp(str, "float")         || !strcmp(str, "return") || !strcmp(str, "char")         || !strcmp(str, "case") || !strcmp(str, "char")         || !strcmp(str, "sizeof") || !strcmp(str, "long")         || !strcmp(str, "short") || !strcmp(str, "typedef")         || !strcmp(str, "switch") || !strcmp(str, "unsigned")         || !strcmp(str, "void") || !strcmp(str, "static")         || !strcmp(str, "struct") || !strcmp(str, "goto"))         return (true);     return (false); }  // Returns 'true' if the string is an INTEGER. bool isInteger(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] != '5'             && str[i] != '6' && str[i] != '7' && str[i] != '8'             && str[i] != '9' || (str[i] == '-' && i > 0))             return (false);     }     return (true); }  // Returns 'true' if the string is a REAL NUMBER. 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] != '5'             && str[i] != '6' && str[i] != '7' && str[i] != '8'             && str[i] != '9' && str[i] != '.' ||              (str[i] == '-' && i > 0))             return (false);         if (str[i] == '.')             hasDecimal = true;     }     return (hasDecimal); }  // Extracts the SUBSTRING. 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); }  // Parsing the input STRING. void parse(char* str) {     int left = 0, right = 0;     int len = strlen(str);      while (right <= len && left <= right) {         if (isDelimiter(str[right]) == false)             right++;          if (isDelimiter(str[right]) == true && left == right) {             if (isOperator(str[right]) == true)                 printf("'%c' IS AN OPERATOR\n", str[right]);              right++;             left = right;         } else if (isDelimiter(str[right]) == true && left != right                    || (right == len && left != right)) {             char* subStr = subString(str, left, right - 1);              if (isKeyword(subStr) == true)                 printf("'%s' IS A KEYWORD\n", subStr);              else if (isInteger(subStr) == true)                 printf("'%s' IS AN INTEGER\n", subStr);              else if (isRealNumber(subStr) == true)                 printf("'%s' IS A REAL NUMBER\n", subStr);              else if (validIdentifier(subStr) == true                      && isDelimiter(str[right - 1]) == false)                 printf("'%s' IS A VALID IDENTIFIER\n", subStr);              else if (validIdentifier(subStr) == false                      && isDelimiter(str[right - 1]) == false)                 printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);             left = right;         }     }     return; }  // DRIVER FUNCTION int main() {      // maximum length of string is 100 here      char str[100] = "int a = b + 1c; ";      parse(str); // calling the parse function      return (0); } 

Output: 

'int' IS A KEYWORD
'a' IS A VALID IDENTIFIER
'=' IS AN OPERATOR
'b' IS A VALID IDENTIFIER
'+' IS AN OPERATOR
'1c' IS NOT A VALID IDENTIFIER


 


C

codestorywithMIK
Improve
Article Tags :
  • Misc
  • Compiler Design
  • C Language
Practice Tags :
  • Misc

Similar Reads

    Introduction of Compiler Design
    A compiler is software that translates or converts a program written in a high-level language (Source Language) into a low-level language (Machine Language or Assembly Language). Compiler design is the process of developing a compiler.The development of compilers is closely tied to the evolution of
    9 min read
    Phases of a Compiler
    A compiler is a software tool that converts high-level programming code into machine code that a computer can understand and execute. It acts as a bridge between human-readable code and machine-level instructions, enabling efficient program execution. The process of compilation is divided into six p
    10 min read
    Symbol Table in Compiler
    Every compiler uses a symbol table to track all variables, functions, and identifiers in a program. It stores information such as the name, type, scope, and memory location of each identifier. Built during the early stages of compilation, the symbol table supports error checking, scope management, a
    8 min read
    Static and Dynamic Scoping
    The scope of a variable x in the region of the program in which the use of x refers to its declaration. One of the basic reasons for scoping is to keep variables in different parts of the program distinct from one another. Since there are only a small number of short variable names, and programmers
    6 min read
    Generation of Programming Languages
    Programming languages have evolved significantly over time, moving from fundamental machine-specific code to complex languages that are simpler to write and understand. Each new generation of programming languages has improved, allowing developers to create more efficient, human-readable, and adapta
    6 min read
    Error Handling in Compiler Design
    During the process of language translation, the compiler can encounter errors. While the compiler might not always know the exact cause of the error, it can detect and analyze the visible problems. The main purpose of error handling is to assist the programmer by pointing out issues in their code. E
    5 min read
    Error Detection and Recovery in Compiler
    Error detection and recovery are essential functions of a compiler to ensure that a program is correctly processed. Error detection refers to identifying mistakes in the source code, such as syntax, semantic, or logical errors. When an error is found, the compiler generates an error message to help
    6 min read
    Linker
    A linker is an essential tool in the process of compiling a program. It helps combine various object modules (output from the assembler) into a single executable file that can be run on a system. The linker’s job is to manage and connect different pieces of code and data, ensuring that all reference
    8 min read
    Introduction of Lexical Analysis
    Lexical analysis, also known as scanning is the first phase of a compiler which involves reading the source program character by character from left to right and organizing them into tokens. Tokens are meaningful sequences of characters. There are usually only a small number of tokens for a programm
    6 min read
    C program to detect tokens in a C program
    As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences