flex/byacc cooperation question

ast@coho.halcyon.com (Andrew Tucker)
Tue, 27 Jun 1995 17:31:06 GMT

          From comp.compilers

Related articles
flex/byacc cooperation question ast@coho.halcyon.com (1995-06-27)
| List of all articles for this month |

Newsgroups: comp.compilers
From: ast@coho.halcyon.com (Andrew Tucker)
Keywords: yacc, lex, parse, question, comment
Organization: Northwest Nexus, Inc. - Professional Internet Services
Date: Tue, 27 Jun 1995 17:31:06 GMT

I'm writing a compiler in flex and byacc and ran across a problem. I
was able to work around it, but I want to know if it's me or a problem
with the software.

The problem is with tracking line numbers. The lexer just consumes newlines
and increments a counter, not passing anything back to the parser. The
problem is that given a byacc rule like this:

        SYMBOL EQUAL expr

when ErrorMessage prints using the current value of the line number variable,
the value is always the next line of code. For example:

1: int j
3: j = H
5: j = 5

would report that the use of the undeclared variable H occurred on line 5.
Basically, flex is consuming all the newlines up to the next valid statement
before byacc fires the semantic actions for that rule.

Like I mentioned, I did work around this, but is this the expected behavior
from flex and byacc or a problem with them?

| /* Andrew */ |
| ast@halcyon.com |
[That's what happens. My usual hack is to keep two line number
counters, one with the actual current line number, the other with the
line number of the last token returned to the parser, and have
yyerror report the latter. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.