Lex is a computer program that generates lexical analyzers, which is commonly used with the YACC parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975,is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

**Deterministic Finite Acceptor –**

In the theory of computation, a branch of theoretical computer science, a deterministic finite automaton (DFA)— also known as a deterministic finite acceptor (DFA) and a deterministic finite state machine (DFSM)— is a finite-state machine that accepts and rejects strings of symbols and only produces a unique computation (or run) of the automaton for each input string. Deterministic refers to the uniqueness of the computation. In search of the simplest models to capture finite-state machines, McCulloch and Pitts were among the first researchers to introduce a concept similar to finite automata in 1943.

**Approach –**

LEX provides us with an **INITIAL** state by default. So in order to make a DFA, use this initial state as the initial state of the DFA. Define two more states **A and B** where B is the dead state that would be use if encounter a wrong or invalid input. When user get input which is an invalid input, move to state B and print message “INVALID” and if user reaches INITIAL state from state A with a “\n” then display a message “Not Accepted”. But if user get a \n on the initial state, user display a message “Accepted”.

**Examples –**

Input : 1001 Output : Accepted Input : hjabdba Output : INVALID

To implement the above DFA, user needs to write the below code in a lex file with a .l extention.

**NOTE :**

To compile a lex program, user need a UNIX system and flex which can be installed using

sudo apt-get install flex

With all the above specification open unix terminal and do the following:

1.Use the lex program to change the specification file into a C language program. The resulting program is in the lex.yy.c file.

2.Use the cc command with the -ll flag to compile and link the program with a library of lex subroutines. The resulting executable program is in the a.out file.lex lextest cc lex.yy.c -lfl

**Code –**

`%{ ` `%} ` ` ` `%s A B ` ` ` `%% ` `<INITIAL>1 BEGIN INITIAL; ` `<INITIAL>0 BEGIN A; ` `<INITIAL>[^0|\n] BEGIN B; ` `<INITIAL>\n BEGIN INITIAL; ` `printf` `(` `"Accepted\n"` `); ` `<A>1 BEGIN A; ` `<A>0 BEGIN INITIAL; ` `<A>[^0|\n] BEGIN B; ` `<A>\n BEGIN INITIAL; ` `printf` `(` `"Not Accepted\n"` `); ` `<B>0 BEGIN B; ` `<B>1 BEGIN B; ` `<B>[^0|\n] BEGIN B; ` `<B>\n {BEGIN INITIAL; ` `printf` `(` `"INVALID\n"` `);} ` `%% ` ` ` `void` `main() ` `{ ` `yylex(); ` `} ` |

*chevron_right*

*filter_none*

**Output –**

nickhil@NICKHIL:~$ lex prpg11.l nickhil@NICKHIL:~$ cc lex.yy.c -lfl nickhil@NICKHIL:~$ ./a.out 1000 Not Accepted hello INVALID 01010101 Accepted

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- DFA in LEX code which accepts Odd number of 0’s and even number of 1’s
- DFA in LEX code which accepts strings ending with 11
- Program to build a DFA that accepts strings starting and ending with different character
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Program to build DFA that accepts the languages ending with "01" over the characters {0, 1}
- NFA which accepts set of strings over an alphabet {0, 1, 2} such that the final digit has appeared before
- Lex code to count total number of tokens
- LEX code to extract HTML tags from a file
- Lex code to replace a word with another word in a file
- DFA machines accepting odd number of 0’s or/and even number of 1’s
- Lex program to check whether a given number is even or odd
- Lex program to check whether input number is odd or even
- Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Construct DFA which interpreted as binary number is divisible by 2, 3, 4
- DFA of a string in which 2nd symbol from RHS is 'a'
- DFA of a string in which 3rd symbol from RHS is ‘a’
- Program to construct a DFA which accept the language L = {a
^{n}b^{m}| n mod 2=0, m≥1} - Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1} - DFA for accepting the language L = { a
^{n}b^{m}| n+m=even } - Lex Program to Identify and Count Positive and Negative Numbers

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.