Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #2771
| Path | csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end |
|---|---|
| From | Christopher F Clark <christopher.f.clark@compiler-resources.com> |
| Newsgroups | comp.compilers |
| Subject | Re: Why are ambiguous grammars usually a bad idea? Why are languages usually defined and implemented with ambiguous grammars? |
| Date | Tue, 28 Dec 2021 16:18:13 +0200 |
| Organization | Compilers Central |
| Lines | 57 |
| Sender | news@iecc.com |
| Approved | comp.compilers@iecc.com |
| Message-ID | <21-12-013@comp.compilers> (permalink) |
| References | <21-12-003@comp.compilers> <21-12-008@comp.compilers> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Injection-Info | gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="39181"; mail-complaints-to="abuse@iecc.com" |
| Keywords | parse, design |
| Posted-Date | 28 Dec 2021 12:41:37 EST |
| X-submission-address | compilers@iecc.com |
| X-moderator-address | compilers-request@iecc.com |
| X-FAQ-and-archives | http://compilers.iecc.com |
| Xref | csiph.com comp.compilers:2771 |
Show key headers only | View raw
You already have lots of relevant commentary on your answer. However, I want to offer some counterpoint. The 1973 paper by Aho, Johnson, and Ullman: Deterministic Parsing of Ambiguous Grammars, gives the background for how this is handled in yacc. https://www.researchgate.net/publication/234791287_Deterministic_parsing_of_ambiguous_grammars Thus, the use of the various directives is a long-known solution to the issue. However, LR conflicts frighten people. Their resolution hides the fact that the grammar is ambiguous and at least two parses are possible. But, with practice, one can get good at using them judiciously and knowing when they are simply expressing the desired intent or hiding a potential source of confusion. More recently, GLR parsing has deferred the resolution and builds a parse forest when the grammar is truly ambiguous and not the result of the limitations of the LR parsing methodology. When that happens, one can use semantics to determine which parse was correct or declare a relevant error. By the way, an LR conflict doesn't guarantee that the grammar is actually ambiguous, just that resolving whether it is or not is beyond the capacity of the LR algorithm. So, while it is good to know when your grammar is ambiguous (or even just potentially ambiguous) is useful in knowing whether your grammar describes the language you think it describes, it is not always the worst thing. Ambiguous grammars can have valuable properties. As you noted, writing an unambiguous grammar can be quite complicated (and in some cases not even possible), and the comparable ambiguous grammar can be quite concise, simple, and easy to read. And, putting the disambiguation outside the grammar can be a more maintainable solution. This is particularly, true if you want a language with user defined operators or ones where the user might want to adjust the precedence or associativity of the existing operators. Using numbers to indicate precedence is a well-known technique. It maps well onto things people understand. And the ability to compare the precedence of two operators based upon an associated number is easy to implement (while strictly outside the capacity of most parser generators). Kind regards, Chris -- ****************************************************************************** Chris Clark email: christopher.f.clark@compiler-resources.com Compiler Resources, Inc. Web Site: http://world.std.com/~compres 23 Bailey Rd voice: (508) 435-5016 Berlin, MA 01503 USA twitter: @intel_chris ------------------------------------------------------------------------------
Back to comp.compilers | Previous | Next — Previous in thread | Find similar
Re: Why are ambiguous grammars usually a bad idea? Why are languages usually defined and implemented with ambiguous grammars? gah4 <gah4@u.washington.edu> - 2021-12-27 04:18 -0800 Re: Why are ambiguous grammars usually a bad idea? Why are languages usually defined and implemented with ambiguous grammars? gah4 <gah4@u.washington.edu> - 2021-12-27 19:45 -0800 Re: Why are ambiguous grammars usually a bad idea? Why are languages usually defined and implemented with ambiguous grammars? Christopher F Clark <christopher.f.clark@compiler-resources.com> - 2021-12-28 16:18 +0200
csiph-web