User loginNavigation |
archivesA new implementation of recursive-descent parsing?It is well known that the classic way to model a recursive-descent parser is to write (or automatically produce) a set of mutually-recursive functions either by writing procedures or combining parser modules using a DSL (like in Boost::Spirit) or using templates/generics (for those languages that support such a concept). All the different types of implementations work in a similar manner: each procedure invokes one or more procedures. This approach has some drawbacks:
While trying to approach the issue of RD parsers from a different perspective, I think I have found an implementation which solves some of the problems mentioned above. I hope I am not repeating someone else's work (a brief search over the internet found no similar approaches). The idea is that instead of the input data travelling down the tree of parsers, each parser processes the current input and then leaves a continuation on a stack. The main parse engine pops the continuation off that stack and executes it; the new continuation puts another state on the stack etc. Thus an RD parser becomes a sort of state machine. Generally an RD parser consists of the following operations:
The traditional approach handles these operations in the following ways:
For example (in pseudo-Java): class sequence_parser implements parser { list parsers; iterator parse(iterator input) { foreach(p, parser) { input = p.parse(input); } return input; } } class choice_parser implements parser { list parsers; iterator parse(iterator input) { foreach(p, parser) { iterator output = p.parse(input); if (output != null) return output; } return null; } } class loop_parser implements parser { parser other; iterator parse(iterator input) { input = other.parse(input); return parse(input); } } The new approach handles these tasks differently. The approach uses the following context for parsing:
Parsing operations are handled like this:
For example: class sequence_parser implements parser { parser sequence_next; parser this_parser; iterator parse(context c) { c.sequence_stack.push(sequence_next); return this_parser.parse(c); } } class choice_parser implements parser { parser choice_next; parser this_parser; iterator parse(context c) { c.save(); c.choice_stack.push(choice_next); return this_parser.parse(c); } } class loop_parser implements parser { parser this_parser; iterator parse(iterator input) { c.sequence_stack.push(this); return this_parser.parse(c); } } The parsing algorithm then becomes a very simple loop: void parse(context c) { while (true) { parser = c.sequence_stack.pop(); if (parser != null) { iterator output = parser.parse(c); if (output == null) { parser = c.choice_stack.pop(); if (parser) { c.restore(); c.sequence_stack.push(parser); } else throw parse_exception(); } } else (if c.input_exhausted()) { return; else { throw parse_exception(); } } } The above does the following:
This approach solves some of the problems of the traditional approach in the following ways:
2006 ICFP Programming Contest registration opensLanguage lovers: Registration is now open for the 9th Annual ICFP Programming Contest! The contest, associated with the International Conference on Functional Programming, will be held on the weekend of July 21-24. The contest task will be released at noon EDT on Friday, and entries will be accepted until noon EDT on Monday. Registration is free and open to all. Teams may participate from any location, and may use any programming language(s). Last year, 360 participants formed 161 teams from 26 countries. Prize money totaling $1750 US will be awarded to help defray the costs of travel to the conference for the winners and for small cash prizes. In addition, the winners of the contest will receive bragging rights for the programming language of their choice. This makes the contest a popular avenue for demonstrating the superiority of a favorite language, or for exercising an experimental tool. Though the specifics are secret until the contest begins, we promise that this year's task will be very different from past competitions. This year's theme is "computational archaeolinguistics." Stay tuned for more information as the contest approaches! - 2006 Contest Organizers 2006 ICFP Contest registration opensRegistration is now open for the 9th Annual ICFP Programming Contest. The ICFP contest is an event that traditionally raises interest in the LtU community. A more detailed announcement found in the forum mentions that this year's theme is "computational archaeolinguistics." Intriguing. By Ehud Lamm at 2006-07-03 22:40 | Fun | Functional | login or register to post comments | other blogs | 8296 reads
|
Browse archivesActive forum topics |
Recent comments
21 weeks 6 days ago
22 weeks 3 hours ago
22 weeks 3 hours ago
44 weeks 1 day ago
48 weeks 3 days ago
50 weeks 11 hours ago
50 weeks 11 hours ago
1 year 4 days ago
1 year 5 weeks ago
1 year 5 weeks ago