0
votes

So my objective here is to be able to determine whether an input is acceptable or not. The following are acceptable inputs:

Any combination of "u", "d", "l", "r", "n"

**Example of valid inputs:**
udlrn
uuuuuuuuuuuu
dunrldd
dddddllll
dldnrrrrrrrrrrr

**Example of invalid inputs:**
abc
abcudlr
xudz
dclrxy

Here is my Flex code

%%
"u"   return UP;
"d"   return DOWN;
"l"   return LEFT;
"r"   return RIGHT;
"n"   return NONE;
\n    return END;
%%

And here is my bison code

%token UP
%token DOWN
%token LEFT
%token RIGHT
%token NONE
%token END

%%
start:   directions END
         { 
             printf("\nParse complete with acceptable input\n"); 
         }
;

directions: direction
            |
            directions direction
;

direction: UP | DOWN | LEFT | RIGHT | NONE
;
%%

However when I provide an input such as:

  • ldruabc

I am getting the parse complete message even though this input is invalid.

1

1 Answers

3
votes

Anything that doesn't match any pattern in your flex code will be echoed to stdout and ignored, so as currently constituted, any input that consists of a single line will be acceptable.

You should probably add a catch-all rule after your other flex rules:

.    return *yytext;

that way, any other character in the input will be returned to the parser where it will trigger a syntax error.