Lex Program For checking a valid URL
Problem: Write a Lex program for checking a valid URL.
Explanation:
Lex is a computer utility that generates some lexical analyzers. Lex reads a stream of characters as input specifying the lexical analyzer and give source code as the output implementing the lexer in the C programming language.
Prerequisite: Flex (Fast Lexical Analyzer Generator)
Examples:
Input: geeksforgeeks Output: INVALID URL Input: https://www.geeksforgeeks.org Output: VALID URL
Implementation:
%%((http)|(ftp))s?:\/\/[a-zA-Z0-9]{2, }(\.[a-z]{2, }) +(\/[a-zA-Z0-9+=?]*)* {printf("\nURL Valid\n");} .+ {printf("\nURL Invalid\n");} %% // driver programvoid main() { printf("\nEnter URL : "); yylex(); printf("\n"); } |
Output:

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.



