0

This question is related to this: Using regex to pass syntax-valid c++ declaration/initialization thanks to the answer of Jerry.

But now the program flow has been redesigned. It will now check the syntax of the declaration per data type.

//found int
if( /+sint$/.test(kword_search)){

    //syntax check
    if( //*modified regex for int declaration checking*//.test(kword_syntax_search) ){
        //no error in int;
    }
}

//found char
if( /+schar$/.test(kword_search)){

    //syntax check
    if( //*modified regex for char declaration checking*//.test(kword_syntax_search) ){
        //no error in char;
    }
}

//found float
if( /+sfloat$/.test(kword_search)){

    //syntax check
    if( //*modified regex for float declaration checking*//.test(kword_syntax_search) ){
        //no error in float;
    }
}

//found bool
if( /+sbool$/.test(kword_search)){

    //syntax check
    if( //*modified regex for bool declaration checking*//.test(kword_syntax_search) ){
        //no error in bool;
    }
}

So, basically the previous answer is used as this:

//found int|char|bool|float
if( /+s(?:int|float|char|bool)$/.test(kword_search)){

    //syntax check
    if( /^(?:\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[‌​0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*,)*\s*[A-Z‌​a-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-‌​9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*;/.test(kword_syntax_search) ){
        //no error in declaration/initialization by mixing them;
    }
}

I need some help to chop this regex to be in 4 sets of regex dedicated to evaluate an int,char,float and bool in the first block of code (Jerry's answer):

^(?:\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[‌​0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*,)*\s*[A-Z‌​a-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-‌​9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*;http://regex101.com/r/aD2hA6

My goal is to tell this string inputs in my code editor if they are syntactically valid or not

int a,b,_ab;//valid

int a=0,b=333; //valid

int a, b=; //not valid

int a,,b,,; //not valid

int 1var; //not valid

int a='sts', b=false, c=23.0 // not valid

float a,b;//valid

float a=2, a=3.33//valid

float a='sfsdf';//not valid

float a==3;//not valid

float a,, b=3.0//not valid

char a, b;//valid

char a='a', b='!', c='3';//valid

char a='ab';//not valid

char a,b=3;//not valid

char a,b="a";//not valid

bool a=true, b=false;

bool a="true"; //not valid

(get the idea?)

Again... I know regex is not the right way of doing this. Thanks

Community
  • 1
  • 1

1 Answers1

1

For the integer one, you could perhaps use this:

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+)?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+)?;$

These will match a,b;, a=0,b=333; and reject a, b=; or a='sts' or b=false, c=23.0.

For the float, maybe that one:

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:\.[0-9]+)?)?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:\.[0-9]+)?)?;$

For the character syntax, I guess:

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*'[^']')?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*'[^']')?;$

And for the boolean one, maybe that:

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:true|false))?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:true|false))?;$

Note: Those were specifically made for the examples you provided. I cannot guarantee that it will work for all the different scenarios you have since I don't have much experience with C++.

EDIT: Adding operators to float and integer regexes:

int

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:[+\/*-][0-9]+)?)?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:[+\/*-][0-9]+)?)?;$

float

^(?:[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:\.[0-9]+)?(?:\s*[+\/*-[0-9]+(?:\.[0-9]+)?)?)?,\s*)*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*[0-9]+(?:\.[0-9]+)?(?:\s*[+\/*-[0-9]+(?:\.[0-9]+)?)?)?;$
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • i forgot to include initialization with declaration, can you update it, just like the your previous answer, that supports operators? –  Aug 25 '13 at 20:33
  • 1
    @fireflieslive I assume there's only `int` and `float` to update? Added them to the answer. – Jerry Aug 26 '13 at 09:53