# DFA (Recognizer) for valid Pascal identifiers

**Problem – ** Implement a recognizer for pascal identifiers based on a DFA that accepts strings belonging to the definition of the language of the same.

Here is a regular definition for the set of Pascal identifiers that are defined as the set of strings of letters and digits beginning with a letter.

letter : A | B | . . . | Z | a | b | . . . | z digit : 0 | 1 | 2 | . . . | 9 ID : letter (letter | digit)*

The regular expression ID is the pattern for the Pascal identifier token and defines letter and digit where a letter is a regular expression for the set of all upper-case and lowercase letters in the alphabet and digit is regular for the set of all decimal digits.

**State diagram of the DFA**

**Working code for the recognizer:**

`// C++ program to implement DFA based regonizer that accepts ` `// all strings which follow the language ` `// L = { letter (letter | digit)* } ` ` ` `#include <bits/stdc++.h> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// dfa tells the number associated ` `// with the present state ` `int` `dfa; ` ` ` `// This function is for ` `// the starting state (zeroth) of DFA ` `void` `start(` `char` `c) ` `{ ` ` ` `if` `(` `isalpha` `(c)) ` ` ` `dfa = 1; ` ` ` `else` ` ` `// -1 is used to check for any invalid symbol ` ` ` `dfa = -1; ` `} ` ` ` `// This function is for the first state of DFA ` `void` `state1(` `char` `c) ` `{ ` ` ` `if` `(` `isalnum` `(c)) ` ` ` `dfa = 1; ` ` ` `else` ` ` `dfa = -1; ` `} ` ` ` `bool` `DFA_for_ID(string token) ` `{ ` ` ` `dfa = 0; ` ` ` `int` `i, len = token.length(); ` ` ` `for` `(i = 0; i < len; i++) { ` ` ` `if` `(dfa == 0) ` ` ` `start(token[i]); ` ` ` `else` `if` `(dfa == 1) ` ` ` `state1(token[i]); ` ` ` `else` ` ` `return` `0; ` ` ` `} ` ` ` `if` `(dfa == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `string input = ` `"Geeks for Geeks is 9ice platfo$m for every1 "` `; ` ` ` `// to seprate all the tokens by space in the string ` `// and cheking for each token ` ` ` `stringstream ss(input); ` ` ` `string token; ` ` ` `while` `(ss >> token) { ` ` ` `bool` `isValid = DFA_for_ID(token); ` ` ` `if` `(isValid) ` ` ` `cout << token << ` `" : "` ` ` `<< ` `"Valid"` `<< endl; ` ` ` `else` ` ` `cout << token << ` `" : "` ` ` `<< ` `"Invalid"` `<< endl; ` ` ` `} ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Geeks : Valid for : Valid Geeks : Valid is : Valid 9ice : Invalid platfo$m : Invalid for : Valid every1 : Valid

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Introduction to Queue Automata
- DFA that recognizes number of 0 is multiple of 3 on input {0,1}
- Polytime Manyone reduction: Clique to E-TM
- DFA that begins with 'a' but does not contain substring 'aab'
- Intersection process of two DFAs
- Post Correspondence Problem
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- CYK Algorithm for Context Free Grammar
- Proof that Path Selection Decision problem is NP-Complete
- Build a DFA to accept Binary strings that starts or ends with "01"
- Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
- Construct a Turing Machine for language L = {0
^{2n}1^{n}| n>=0} - DFA for exactly one of a and at least one of b

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.