Alen Ying
Alen Ying

Reputation: 13

Flex and Bison accept a line that contains only a comment

The exercise 1.1 in flex and bison ask me to accept a line that contains only a comment. Here is part of my .l file:

%{   
   #include"fb1-5.tab.h"
%}

%%

...
"//".* {  return COMMENT;}
\n     {  return EOL;}

%%

Then is my .y file:

%{ 
    #include <stdio.h> 
%}

....  // the token about the calculator

%token COMMENT
%token EOL

%%

....    // the rulse about the calculator

comment: COMMENT EOL { printf("%s",yytext);}

%%

When I run the project and input //test

outputs:

error: syntax error

Upvotes: 1

Views: 378

Answers (1)

rici
rici

Reputation: 241721

It's clear that the calculator reports a syntax error if you enter a line containing only a comment. That's a problem, particularly with this simple implementation which fails as soon as a syntax error is encountered. (A real calculator would have to recover from the error. That's covered later in the book.)

But let's forget about comments. What if we just enter a blank line?

$ ./fb1-5

syntax error
$

It shouldn't take too much thought to see that these two problems are related. The scanner just removes comments from the input stream, so if we type a line containing only a comment, what the parser sees is exactly the same as what it sees when we type a blank line: just the EOL character.

So what's wrong with that? The grammar specifies what the input looks like:

calclist: /* Nothing */
 | calclist exp EOL { /* print answer */ }

It's important to understand what these rules mean, and this exercise is encouraging you to think about the meaning of these rules. You'll find a longer (and perhaps friendlier) explanation in John Levine's book (and in any other book about parsing), but the basic idea is quite simple. The rules say that a calclist is either empty, or it's a (previously identified) calclist followed by an exp followed by an EOL. No other possibilities exist.

How, then, could an empty line be matched? An empty line is just an EOL; there is no exp, so it won't match the second rule. (That's good, because we don't want to print a non-existent answer to an empty line.) Since there is nothing else, we need to add another rule:

 | calclist EOL { /* Do nothing */ }

Now, how will lines containing only a comment be handled? Why?

The above didn't touch the scanner at all, but exercise 1-1 suggests that there could be solution in the scanner, too. Could there? How complicated is it? Could it deal appropriately with blank lines? You might want to spend a bit of time trying to see how to do this with the scanner. It is possible, but it's nowhere near as simple as adding one line to the parser.

Upvotes: 4

Related Questions