To design an online simulator that checks whether a user input string (program snippet) is a valid or invalid identifier.
Given a string as user input, representing a program snippet, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy all the following conditions:
Traverse the string character by character and check whether all the requirements are met for it to be a valid identifier i.e., first character can only be either ‘_’ or an English alphabet and the rest of the characters must neither be a white space or any special character nor must exceed 31 characters.
Input: Enter String = "Compiler_Design_Project"
Output: Valid Identifier
Input: Enter String = "03_Compiler_Design_Project_Simulator"
Output: Invalid Identifier
Go to this Repo to get the code.
Click on this Link to go to the simulator.
Time Complexity: O(n^2)
Space Complexity: O(1)
We wrote the online simulator using C programming language and integrated it with the website designed using HTML, CSS, JavaScript. The simulator takes a string as user input and checks whether it is a valid or invalid identifier.
There can be two possible sources of error for this string in the program snippet:
Sometimes the user input string can be a long one, particularly exceeding 31characters in length. In those cases, the string, although having all required syntax, will be considered as an invalid identifier. Example: RohanD_DebangshuK_SohamM_AbhirupSG
Although there are some of error recovery methods that are automatically performed by the compiler, it is not recommended as it may lead to problems in other parts of code if those are not changed accordingly. These are four methods by which an invalid identifier is turned into a valid one by the compiler
Delete: This method deletes extra illegal characters from the string.
Insert: This method is used only when there is digit(s) present at the start of the input string. Inserting character (namely L/_) at the of the string can make it a valid identifier.
Transpose: This method works only when there is a single digit present at the start of the string followed by a letter or underscore. Transposing or interchanging two adjacent characters at the first and second places make the string a valid identifier.
Replace: This method works by changing the illegal characters to a letter or digit or underscore making the string a valid identifier.