James Kanze US/ESC 60/3/141 #40763 wrote:>> Mark Thiehatten <mark@research.techforce.nl> writes:>> |> I am working on a parser for a language that allows keywords to> |> be used as identifiers.

> I had a paper in SIGPLAN notices about this some time back.> The basic idea was to extend the lexer to> be able to return alternate tokens; and yacc to ask the lexer for the> alternate token when it found an error. The modifications to yacc were in> fact done by running an sed script over the generated parser, and so didn't> require access to the sources.

If the alternatives are tried only when you get an error, then this technique
is not very powerful. It will not work when you need to look ahead to
determine which symbol was required.

It is, however, the basic idea used in the programming language ICON.
Functions in ICON (actually, a special type of function called generators)
can return multiple values. If you don't like the first result, ask for
another.

It may be worth your while to build a parser in ICON first, even if you
don't ultimately use it, since you can write the parser as a recursive
descent parser and get the back-tracking "for free". You have to be careful
to not emit semantic actions too early, of course.

It is then not hard to write an interpreter which will do the same
back-track parsing in your own implementation language.
--