I write grammar for a formal language. Ideally, I would like this grammar to be unambiguous, but that would be impossible. In any case, I want to know about all the possible ambiguities in developing grammar. How can i do this?
Until then, in most cases when I developed the language, I turned to Bison, wrote LR (1) grammar for it, launched Bison in verbose mode and looked at all the shifts and reduced - to change the conflicts that they tell me about. Make sure that I agree with his choice in each case.
But now I'm in a project where Bison does not have a code generator for one of the required target languages ββand where ANTLR is already in use. In addition, the language is not LR (1), and rewriting it as LR (1) will lead to additional syntax checks after the analyzer completes, which will reduce the expressiveness of the grammar as a tool for describing the language.
So now I work with ANTLR, fed it with my grammar, and everything seems to work well. But ANTLR does not seem to check for ambiguity at compile time. For example, the following grammar is ambiguous:
grammar test;
lst: '(' ')' {System.out.println("a");}
| '(' elts ')' {System.out.println("b");} ;
elts: elt (',' elt)* ;
elt: 'x' | ;
Input ()can be interpreted as an empty list, or it can be interpreted as a list consisting of one empty element. The generated parser selects the previous interpretation, but I would like to be able to manually check this selection.
- , , ANTLR ?
- , , , ?
- - ?
- , , ANTLR, BNF?
ANTLRErrorListener.reportAmbiguity , ANTLR . , , . - , , ?