1

What's the difference between natural languages and programming languages in the context of their grammars?

Spencer
  • 43
  • 8

1 Answers1

2

Natural languages are considerably more flexible.

Also, natural languages often can't be fully described by a Context-Free Grammar. For example, in English, "respectively" clauses (along with a few other constructs) mess up the grammar. Granted, neither C# nor Java are context-free either (although this article claims to have a context-free grammar for early C#), but programming languages are much more likely to have a context-free grammar.

The biggest difference is that the grammars of natural languages all allow for ambiguity whereas programming languages have been carefully designed to avoid ambiguity. Consider the following statement (I unfortunately no longer recall the math textbook I originally read this example in):

Kevin saw John with the telescope in the park.

Who was using the telescope - John or Kevin? And which of them was in the park - Kevin, John, or the telescope? A few interpretations:

  • The telescope was in the park. John was using it. Kevin saw John there while he was using it.
  • John was in the park. Kevin used the telescope to watch John at the park.
  • The telescope was in the park. Kevin used it to see John.
  • Etc.

Programming languages are carefully designed to avoid you being able to write down sentences like that.