Aymanadou
Aymanadou

Reputation: 1220

Lex - recognizing ambiguant tokens

All,

In my Lex file we recognize some operators as Tokens some of this operator are

":p" 
and 
":"  

the problem is that any word witch starts with :p like ":presentation" is not recognized as

':' word             /*grammar*/

and a parse error is trigged due to the returned lex value ":p" as next Token

how can i resolve this ambiguity ?

Upvotes: 0

Views: 182

Answers (1)

Asherah
Asherah

Reputation: 19347

You should instead define a lexer rule to match the entire "symbol" token, i.e. something like

:[a-zA-Z_][a-zA-Z0-9_]*    { yylval.symbol = strdup(yytext + 1); return SYMBOL; }

If you need to, you can do a check in this rule for :p and special-case it, e.g.:

:[a-zA-Z_][a-zA-Z0-9_]*    {
    if (strcmp(yytext, ":p") == 0) {
        return OP_P;
    }

    yylval.symbol = strdup(yytext + 1);
    return SYMBOL;
}

Upvotes: 1

Related Questions