Prerequisite: Designing Finite Automata
Problem: Design a LEX code to construct a DFA which accepts the language: all strings with Odd number of 0’s and even number of 1’s over inputs ‘0’ and ‘1’.
Input: 10001 Output: Accepted Input: 10011 Output: Not Accepted Input: asdf Output: Invalid
LEX provides us with an INITIAL state by default. So in order to make a DFA, use this as the initial state of the DFA. Now we define four more states A, B, C and DEAD where DEAD state would be use if encounter a wrong or invalid input. When user input invalid character, move to DEAD state and print message “INVALID” and if input string ends at state B then display a message “Accepted”. If input string ends at state INITIAL, A, C then display a message “Not Accepted”. (Refer here).
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:
- Use the lex program to change the specification file into a C language program. The resulting program is in the lex.yy.c file.
- 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
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.
- DFA in LEX code which accepts even number of zeros and even number of ones
- DFA in LEX code which accepts strings ending with 11
- Lex code to count total number of tokens
- Need for Intermediate Code and Code Optimization
- Lex program to check whether given number is armstrong number or not
- Three address code in Compiler
- Issues in the design of a code generator
- Frequency Reduction in Code Optimization
- Code Optimization in Compiler Design
- Introduction of Object Code in Compiler Design
- LEX code to extract HTML tags from a file
- Intermediate Code Generation in Compiler Design
- Compiler Design | Detection of a Loop in Three Address Code
- Lex program to check whether a given number is even or odd
- Lex Program to check whether a number is Prime or Not
- Lex program to check whether input number is odd or even
- Lex Program to count number of words
- Lex program to check valid Mobile Number
- LEX program to count the number of vowels and consonants in a given string
- Lex program to count the number of lines, spaces and tabs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.