Friday, April 12, 2019

lex program to check whether IP address entered by user is valid or not


%{
#include<stdio.h>
%}

%%
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ printf("Valid IP Address");
[a-zA-Z0-9.]* printf("Invaild IP Address");
0 { return 0;}
%%

int main()
{
 yylex();
 return 0;
}

ScreenShot:

2 comments:

  1. ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) - match an integer between 0 and 255
    [0-9] is between 0 to 9
    [1-9][0-9] is between 10 to 99
    1[0-9]{2} (with the {2} it's actually 1[0-9][0-9]) is between 100 to 199
    2[0-4][0-9] is between 200 to 249
    And 25[0-5] is between 250 to 255

    {3} repeat the previous pattern three times for the first three octets,
    And then ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) again for the final octet.

    ReplyDelete