Open In App

Lex Program For checking a valid URL

Improve
Improve
Like Article
Like
Save
Share
Report

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 program
void main() 
 {
    printf("\nEnter URL : ");
    yylex();
    printf("\n");
 }


Output:


Last Updated : 02 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads