views:

55

answers:

1

Hello I want to define char (ie 'a AND 'a') but I am having issues in checking errors. Here how I write the rule and check:

char         " ' " {letter}

code

{char}    {
          int x =input() ;
          //printf("%d",'a');

                if(x == 10)
                {
                    return(tCharunterm);
                }
                else if(x == '\'')
                {
                    return(tChar);
                }
                else
                {
                    yyerror("char overflow");
                }

And finally checking it:

'a
token = tCharunterm, value = "(null)"
'a'  
token = tChar, value = "(null)"
'as
char overflow
'asddd
char overflow
token = tIdentifier, value = "ddd"
^Z
+2  A: 

Generally, you NEVER want to call 'input' directly in your flex code -- that's the routine flex uses to get more input, so if you call it, you're pulling in random characters out of the middle of the input and confusing flex into thinking they don't exist. The best way to do this is to define multiple rules and rely on the longest match to get the right one.

"'"{letter}"'"  { return(tChar); }
"'"{letter}"\n" { return(tCharunterm); }
"'"{letter}     { yyerror("char overflow"); return(rCharunterm); }

you might also want yylval.ch = yytext[1]; in these rules to return the actual character value you've matched.

Chris Dodd