Home > Net >  Why I am getting "unbalanced parenthesis" error in flex
Why I am getting "unbalanced parenthesis" error in flex

Time:04-22

I was testing something with lexer, and wrote this regex rule:
[0-9([a-b])[c-f]] ;

According this docs, this is equivalent to:
[0-9a-f()] ;

For example, the following character classes are all equivalent:
[[:alnum:]]
[[:alpha:][:digit:]]
[[:alpha:][0-9]]
[a-zA-Z0-9]

But this is giving me error:

my_lexer.l:56: unbalanced parenthesis

Surprisingly, this rule won't give the same error!
[0-9([a-b][c-f]] ;

Can someone tell where I'm wrong.. I am using win_flex v2.6.4

CodePudding user response:

Inside a character class, no character has special significance except \, ], and -. (But see below.) So [0-9([a-b])[c-f]] starts with the character class [0-9([a-b] (digits, letters a, and b and the symbols ( and [). The next character is an unbalanced close parenthesis , which is an error.

You may have been misled by the syntax of Posix character classes. The sequence [: is special inside a character class, but that doesn't apply to a [ not followed by a :.

  • Related