sel
sel

Reputation: 493

Lex pattern doesn't react to inputs

I've defined the following aliases:

WS [ \t\n]
WSS {WS}*
NEWSS {WS}+
NAME [A-Za-z_][A-Za-z0-9_-]*
WORD [^;]+
VAR_USE ${WSS}{NAME}{WSS}:
VAR_DEF ${VAR_USE}{WSS}{WORD}{WSS};

And the two simple rules:

{VAR_DEF} cout << "VAR DEF";
{VAR_USE} cout << "VAR USE";

When I run the program and I start writing words, whenever I write words that should be detected by second rule, it just doesn't react until I write a word detected by the first rule. (It doesn't echo nor detected)
For example here's a screenshot of a short run:
enter image description here

First input is echoed, second input is detected by the second rule, third input should be detected by first rule but it doesn't. What may be the problem?

Upvotes: 0

Views: 61

Answers (1)

rici
rici

Reputation: 241721

VAR_USE can only be matched if VAR_DEF fails (because it is a prefix of VAR_DEF). In order to fail, the suffix

{WSS}{WORD}{WSS};

must be unmatchable. But {WORD} matches any string not containing a semicolon, even if it includes a newline. If there is a semicolon somewhere in the input, {VAR_DEF} will match up to that semicolon. If not, {VAR_DEF} will fail and the lexer will fall back to {VAR_USE}, but the scanner can't tell that there is no following semicolon until it reaches the end of the input. (I.e. when you type ctl-D followed by Enter.).

Upvotes: 1

Related Questions