# What real-world computer languages cannot be described by deterministic grammars?

Are there any examples of real-world computer languages that are non-deterministic?

By computer languages I include programming languages, markup languages, query languages, modeling language, transformation languages, etc.

By non-deterministic I mean they cannot be parsed with deterministic grammars.

• Nitpick: grammars don't parse languages. Answer: famously, Perl can't be parsed at all. – Raphael Nov 3 '15 at 21:22
• think there is some decent question here but actually there is not really a concept of "nondeterministic grammars" afaik. how would it be defined? there are nondeterministic automata that correlate with multiple definitions of grammars etc... also this question seems much different for eg RLs vs CFLs. note there is a way to build an equivalent DFA out of any NFA (standard algorithm) so this is tricky. you might want to study some CFL theory to try to formulate this better/ more precisely... – vzn Nov 3 '15 at 22:55
• Uhhh .. that should probably read "nondeterministic pushdown automata". But the question is pretty meaningless, it all depends what sort of analysis you expect the parsing to do. E.g. recognizing that all variables are declared before use is not context-free. – reinierpost Sep 19 '16 at 13:42

Yes. Many modern programming languages have this property, including Algol 60, C, and C++; see below for details.

Algol 60 famously had the dangling else problem, for some programs, it was ambiguous how they should be parsed (what parse tree should be result).

Many modern languages resolve this by picking a grammar that resolves the ambiguity in a particular way. However, this requires carefully constructing the grammar to eliminate ambiguity. Often the most natural way to write the grammar leads to a grammar that is ambiguous, and you have to transform the grammar to avoid the ambiguity.

Another approach is to use a GLR parser, which can handle these grammars. Then you can check at runtime whether there are multiple possible parsers, and resolve the ambiguity.

Another classic example is the C and C++ programming languages. The reference grammar for C is not context-free, because when you see a name, you need to know whether it is the name of a type or of a variable to know how to parse the statement. For instance, consider the expression

(A)*B


If A is a variable, this is the multiplication of two variables and is equivalent to A*B. However, if A is the name of a type, then this is a pointer dereference and type-cast (so B is a variable name that holds a pointer type), and equivalent to (A) *B.

Similarly,

A*B;


could be either a variable declaration (if A is the name of a type, this is declaring a variable named B whose type is pointer-to-A) or a multiplication (if A and B are the names of variables).

Compilers typically fix this by including extra code that "goes outside" the language of pure context-free grammars. They keep track of all variable names and type names in scope, and use this to guide parsing in case of this ambiguity. See the lexer hack.

See also LL and LR in Context: Why Parsing Tools Are Hard and Bison's manual pages on its GLR parser (which includes a discussion of another ambiguous case in the C++ grammar).

Lastly: I think the question has some confusion about the difference between ambiguity vs non-determinism. Automata and languages can be deterministic/non-deterministic, but not grammars. Grammars and languages can be ambiguous/unambiguous. See Are there inherently ambiguous and deterministic context-free languages? and Grammatical characterization of deterministic context-free languages and If a parser can parse a non-deterministic grammar, is the parser non-deterministic?. The above examples actually qualify both as ambiguous grammars and non-deterministic languages.

• this seems to be close to an answer but ambiguity in languages/ CFLs is not the same as nondeterminism.... – vzn Nov 3 '15 at 22:57