Dimitris Delis
Dimitris Delis

Reputation: 55

correct pattern in flex, error : rule cannot me matched

hello I'm trying to use the following pattern in flex to match information in the form of

when I compile it it show warning errors that the rule cannot me matched. My code is this :

%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%option noyywrap
letter [a-zA-Z]
digit [0-9]
other_characters [./-_]
whitespace [ \t]
newline [\n]
string ({letter}|{digit}|{other_characters})({letter}|{digit}|{other_characters})+
%%
{string}({whitespace}|{string})     {printf ("%s", yytext); return TEXTMSG;}
"ss\:Workshop"      {printf("%s", yytext); return WORKSHOP;}
"ss\:Name\=\"Number\""|"ss\:Name\="\"{string}\"|    {printf("%s", yytext); return NAME;}

Any clues as to why this is not ok? Kinda new to flex here so I believe I've missed something, not sure what though

Upvotes: 0

Views: 204

Answers (1)

rici
rici

Reputation: 241671

The problem is in this character class:

other_characters [./-_]

A dash in a character class indicates a range of possible characters (as in [a-z], which matches any lower-case letter). So /-_ matches any character whose codepoint is between / (0x2F) and _ (0x5F) inclusive. That range includes digits, upper case letters, and some punctuation, including colon and semicolons.

That makes colon a valid character in a {string}, so {string} will match ss:Workshop. And since flex gives priority to the first rule which matches an input, that makes it impossible for the ss:Workshop rule to ever be matched.

You can fix that by putting the dash either at the beginning or the end of the character class: [-./_] or [./_-]. Those will just match one of the four characters listed.

By the way, there is no need to backslash-escape a colon, or even to quote it. It has no special significance in flex patterns.

Upvotes: 2

Related Questions