<p>First you claim that "<em>most languages are context-free with some exceptions</em>", this is not totally true. When designing a computer language, we mostly try to keep it as context-free as possible, since CFGs are the de-facto standard for that. It will ease a lot of work. This is not always feasible, though, and a lot<sup>[?]</sup> of languages depend on the semantic analysis phase to disambiguate any possible ambiguities.</p>

<p>Parser combinators do not use a formal model usually; PEGs, on the other hand, are a formalism for grammars, as are CFGs. On the last decade a few people have decided to use PEGs over CFGs due to two facts: PEGs are, by design, unambiguous, and they might always be parsed in linear time. A parser combinator library <em>might</em> use PEGs as underlying formalism, but might as well use CFGs or even none.</p>

<p>PEGs are attractive for designing computer languages because we usually do not want to handle ambiguities, which is something hard (or even impossible) to avoid when using CFGs. And, because of that, they might be parsed O(n) time by using dynamic programming (the so called packrat parser). <a href="https://arxiv.org/pdf/1304.3177.pdf" rel="nofollow">It's not simple to "add ambiguities to them" for a few reasons, most importantly because the language they recognize depend on the fact that the options are deterministic, which is used for example when checking for lookahead</a>. It isn't as simple as "just picking the first choice". For example, you could define a PEG:</p>

<pre><code>S = "a" S "a" / "aa"
</code></pre>

<p>Which only parse <em>sequences of N "a", where N is a power of 2</em>. So it recognizes a sequence of 2, 4, 8, 16, 32, 64, etc, letter "a". By adding ambiguity, as a CFG would have, then you would recognize any even number of "a" (2, 4, 6, 8, 10, etc), <strong>which is a different language</strong>.</p>

<p>To answer your question,</p>

<blockquote>
  <p>How would you go about solving it? Is it possible to add ambiguating combinator to PEG?</p>
</blockquote>

<p>First I must say that this is probably not a good idea. If you wish to keep ambiguity on the AST, you probably should use a CFG parser instead.</p>

<p>One could, for example, make a parser for PEGs which is similar to a parser for <a href="https://en.wikipedia.org/wiki/Boolean_grammar" rel="nofollow">boolean grammars</a>, but then our asymptotic parsing time would grow from O(n) to O(n<sup>3</sup>) by keeping all alternatives alive while keeping the same language. And we actually lose both good things about PEGs at once.</p>

<p>Another way would be to keep a packrat parser in memory, and transverse its table to handle the semantics from the AST. Not really a good idea either, since this would imply a large memory footprint.</p>

<p>Ideally, one should build an AST which already has information regarding possible ambiguities by changing the grammar structure. While this requires manual work, and usually isn't simple, you wouldn't have to go back a phase to check the grammar again.</p>
How do I convert PEG parser into ambiguous one?
<p>As far as I understand, most languages are context-free with some exceptions. For instance, <code>a * b</code> may stand for <code>type * pointer_declaration</code> or multiplication in C++. Which one takes place depends on the context, the meaning of the first identifier. Another example is <code>name</code> production in VHDL</p>

<pre><code>enum_literal ::= char_literal | identifer
physical_literal ::= [num] unit_identifier
func_call ::= func_identifier [parenthized_args]
array_indexing ::= arr_name (index_expr)
name ::= func_call | physical_literal | enum_litral | array_indexing
</code></pre>

<p>You see that syntactic forms are different but they can match if optional parameters are omitted, like <code>f</code>, does it stand for func_call, physical_literal, like 1 meter with optional amount 1 is implied, or enum_literal. </p>

<p>Talking to Scala plugin designers, I was educated to know that you build AST to re-evaluate it when dependencies change. There is no need to re-parse the file if you have its AST. AST also worth to display the file contents. But, AST is invalidated if grammar is context-sensitive (suppose that <code>f</code> was a function, defined in another file, but later user requalified it into enum literal or undefined). AST changes in this case. AST changes on whenever you change the dependencies. Another option, that I am asking to evaluate and let me know how to make it, is to build an ambiguous AST.</p>

<p>As far as I know, parser combinators are of <a href="http://blog.reverberate.org/2013/09/ll-and-lr-in-context-why-parsing-tools.htm" rel="nofollow">PEG kind. They hide the ambiguity by returning you the first matched production</a> and <code>f</code> would match a function call because it is the first alternative in my grammar. I am asking for a combinator that instead of falling back on the first success, it proceeds to the next alternative. In the end, it would return me a list of all matching alternatives. It would return me an ambiguity. </p>

<p>I do not know how would you display the ambiguous file contents tree to the user but it would eliminate the need to re-parse the dependent files. I would also be happy to know how modern language design solve this problem.</p>

<p>Once ambiguous node is parsed and ambiguity of results is returned, I would like the parser to converge because I would like to proceed parsing beyond the <code>name</code> and I do not want to parse to the end of file after every ambiguity. The situation is complicated by situations like <code>f(10)</code>, which can be a function call with a single argument or a nullary function call, which return an array, which is indexed afterwards. So, f(10) can match name two ways, either as <code>func_call</code> directly or recursively, as <code>arr_indexing -&gt; name ~ (expr)</code>. So, it won't be ambiguity like several parallel rules, like <code>fcall | literal</code>. Some branches may be longer than 1 parser before re-converging, like <code>fcall ~ (expr) | fcall</code>.</p>

<p>How would you go about solving it? Is it possible to add ambiguating combinator to PEG?</p>