How to write a Flex code for matches protocol (http, https), domain name and optional port number, and path of the URL
sample input:
http://google.com
https://google.com:6060
https://google.com:6060/gemin
google.com:6060/gemin
sample output :
valid URL
protocol:http
domain name:google.com
valid URL
protocol:https
domain name:google.com
optional port number:6060
valid URL
protocol:https
domain name:google.com
optional port number:6060
path:gemin
Invalid URL
this is the code that i have written
%%file task1.l
%{
#include <stdio.h>
%}
%option noyywrap
%%
(http|https)://([a-zA-Z0-9.-]+)(:([0-9]+))?(/[^ tnrfv]*)? {
printf("valid URLn");
printf("protocol:%sn", yytext);
printf("domain name:%sn", yytext + 7); // Skip "http://" or "https://"
if (yytext[strlen(yytext) - 1] == '/') {
printf("path:/n");
} else if (yytext[strcspn(yytext, "nrtfv:")] == ':') {
printf("optional port number:%sn", yytext + strcspn(yytext, ":") + 1);
printf("path:%sn", yytext + strcspn(yytext, "/"));
} else {
printf("path:%sn", yytext + strcspn(yytext, "/"));
}
}
.|n { printf("Invalid URLn"); }
%%
int main() {
yylex();
return 0;
}
there is an error while compile the code,
task1.l:9: unrecognized rule
task1.l:9: unrecognized rule