Source locations with Flex+Bison

Bjarni Juliusson <>
31 Mar 2005 23:36:17 -0500

          From comp.compilers

Related articles
Source locations with Flex+Bison (Bjarni Juliusson) (2005-03-31)
Re: Source locations with Flex+Bison (Laurence Finston) (2005-04-02)
| List of all articles for this month |

From: Bjarni Juliusson <>
Newsgroups: comp.compilers
Date: 31 Mar 2005 23:36:17 -0500
Organization: Telia Internet
Keywords: lex, yacc, question
Posted-Date: 31 Mar 2005 23:36:17 EST

Hi all. I have a problem that is driving me nuts. Maybe I'm just a
complete moron, but I can't seem to make heads or tails of the Flex and
Bison manuals, and Google doesn't seem to find anything useful either.

The problem:
I have a lexer, written for Flex, and a parser, written for Bison. Now I
want the source code locations of the tokens to be propagated up to the
parser to allow meaningful error messages. This seems to involve telling
Flex %options bison-bridge and bison-locations, and then defining a
YYLTYPE for the location values. This gives me a bipartite headache in
that I can't seem to figure out the right place to put the definition of
YYLTYPE (which should be a typedef for, say, location_t and a "#define
YYLTYPE location_t" I assume?), and I can't figure out where to
initialise the location values.

If I don't make the parser pure and put the initialisations in my main()
before calling the parser, everything seems fine except that the YYLTYPE
doesn't appear in the header that Bison generates. This leads to a
situation where the order in which headers are included matters, and
that means something is wrong.

If I do make the parser pure I have really no idea where to initialise
the locations, which means that line numbers start at a random value at
the beginning of lexing, but is set to correct values at the start of
any included input source files (which are handled by the lexer).

I have no reason to make the parser pure, by the way.

If anyone can shed any light on this I'd be extremely grateful. I know
I'm probably just misunderstanding some detail over and over, especially
since it doesn't seem like anyone has ever bothered to answer the
question and I can't find it having been recently asked in this newsgroup.



Post a followup to this message

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