Use this tag in the cases where a parser cannot resolve an ambiguous grammar. For example, when a compiler cannot determine the right choice because an identifier can signify multiple different things.
Questions tagged [ambiguous-grammar]
121 questions
32
votes
1 answer
How are ambiguous enum values resolved in C#?
I checked the section of the C# language specification regarding enums, but was unable to explain the output for the following code:
enum en {
a = 1, b = 1, c = 1,
d = 2, e = 2, f = 2,
g = 3, h = 3, i = 3,
j = 4, k = 4, l = 4
}
en[]…

Resigned June 2023
- 4,638
- 3
- 38
- 49
21
votes
2 answers
Removing sensitive data from Git. "fatal: ambiguous argument 'rm'"
I'm trying to run this command:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filename.js' --prune-empty --tag-name-filter cat -- --all
but I keep getting this error:
fatal: ambiguous argument 'rm': unknown revision or…

Jonathan
- 3,016
- 9
- 43
- 74
11
votes
2 answers
What is the easiest way of telling whether a BNF grammar is ambiguous or not?
Namely, is there a tool out there that will automatically show the full language for a given grammar, including highlighting ambiguities (if any)?

user456584
- 86,427
- 15
- 75
- 107
9
votes
2 answers
Parsing an int(x) parameter
Here is a simple function with one int parameter:
void f(int x) {}
f(42);
And here is another function with one int parameter:
void g(int(x)) {}
g(42);
Now let us define x to be a type:
typedef int x;
void h(int(x)) {}
h(42);
// warning:…

fredoverflow
- 256,549
- 94
- 388
- 662
8
votes
3 answers
Algorithms or data structures for dealing with ambiguity
I'm looking for algorithms or data structures specifically for dealing with ambiguities.
In my particular current field of interest I'm looking into ambiguous parses of natural languages, but I assume there must be many fields in computing where…

hippietrail
- 15,848
- 18
- 99
- 158
6
votes
1 answer
Preferring shift over reduce in parser for language without statement terminators
I'm parsing a language that doesn't have statement terminators like ;. Expressions are defined as the longest sequence of tokens, so 5-5 has to be parsed as a subtraction, not as two statements (literal 5 followed by a unary negated -5).
I'm using…

Moritz Mahringer
- 1,240
- 16
- 28
6
votes
1 answer
Is there a set way to determine ambiguity in a grammar?
We are learning about ambiguity in class, and the following grammar was given as an example of an ambiguous grammar. I just am not seeing how it is ambiguous. Is there a set pattern or method people use to determine ambiguity or is it just like a…

Mint Dreyer
- 61
- 4
5
votes
1 answer
Is it possible to make this YACC grammar unambiguous? expr: ... | expr expr
I am writing a simple calculator in yacc / bison.
The grammar for an expression looks somewhat like this:
expr
: NUM
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 /…

wefwefa3
- 3,872
- 2
- 29
- 51
5
votes
2 answers
How does Swift disambiguate generic constructors?
Consider the following Swift expression
println(Generic(1))
Normally, one would read this as a generic call to the constructor Generic with the arguments (1).
println( Generic(1) )
However, when re-arranging the tokens…

Clashsoft
- 11,553
- 5
- 40
- 79
5
votes
1 answer
ANTLR ambiguity
I need to match in ANTLR a message containing 2 fields separated by a /
First field can have 1..3 digits, the second field can have 1..2 digits,
this does not work
msg: f1 '/' f2;
f1: DIGIT(DIGIT(DIGIT)?)? ;
f2: DIGIT(DIGIT)?
How can I avoid…

chris
- 398
- 2
- 11
5
votes
1 answer
Ambiguous grammar
I am looking at the following grammar and I believe its Ambiguous at line 3, but not sure.
→
→
→ i e
→ i
→ x
→ y
→ 5
→ 13
I found this string xi13yi5xeyx I believe generates two…

jg943
- 309
- 3
- 21
4
votes
1 answer
Left-factoring a grammar
So i have this grammar (below) and i need to build a parse table. I need to make this suitable for a predictive parser. I know the first think is to make it unambiguous, but for me it's already unambiguous (since i can't find a string for which i…

Bobby
- 496
- 5
- 18
4
votes
1 answer
Advice on handling an ambiguous operator in an ANTLR 4 grammar
I am writing an antlr grammar file for a dialect of basic. Most of it is either working or I have a good idea of what I need to do next. However, I am not at all sure what I should do with the '=' character which is used for both equality tests as…

Kelvin Johnson
- 85
- 1
- 8
4
votes
2 answers
How to iterate "along" a Marpa parse forest rather than "through" its parse trees?
Say I have a nice ambiguous Marpa grammar and a nice ambiguous input string.
I can parse the string with Marpa and end up with a parse forest. I can even iterate through each parse tree in the forest.
But how can I iterate "along" the parse…

hippietrail
- 15,848
- 18
- 99
- 158
4
votes
1 answer
Converting to unambiguous grammar from ambiguous grammar
I've ambiguous context-free grammar which has products:
s --> [0],s,[1].
s --> [0],s.
s --> [].
It's of course ambiguous because for 00011 I can draw two others parsing trees. I have to write my grammar which is unambiguous grammar and describes…

Vous
- 67
- 5