What programming languages are context-free?
Asked Answered
M

9

89

Or, to be a little more precise: which programming languages are defined by a context-free grammar?

From what I gather C++ is not context-free due to things like macros and templates. My gut tells me that functional languages might be context free, but I don't have any hard data to back that up with.

Extra rep for concise examples :-)

Millhon answered 22/5, 2009 at 15:31 Comment(0)
C
49

The set of programs that are syntactically correct is context-free for almost all languages.

The set of programs that compile is not context-free for almost all languages. For example, if the set of all compiling C programs were context free, then by intersecting with a regular language (also known as a regex), the set of all compiling C programs that match

^int main\(void\) { int a+; a+ = a+; return 0; }$

would be context-free, but this is clearly isomorphic to the language a^kba^kba^k, which is well-known not to be context-free.

Cole answered 22/5, 2009 at 15:37 Comment(5)
+1 for the answer which is true. The accepted answer is misleading.Cannonry
If anyone desires to see a proof that the language { a^k b a^k b a^k b | k >= 0 } is not regular, see Torben Mogensen's Basics of Compiler Design chapter 2.10.2: diku.dk/~torbenm/BasicsGhetto
hmm the distinction between syntacticaly valid programs and semanticaly valid programs is misleading, since the latter is expressed syntacticaly in context-sensitive grammars (for cases of interest in programming languages). Saying a program is syntacticaly valid because a CFG can accept it, is wrong since the fact that the language of valid programs requires conditions which limit the strings accepted by merely CFGs, means they cannot be described by CFGs. Anyway +1Zymase
The first sentence doesn't make much sense. When is a program syntactically correct? Usually when the compiler accepts it without "syntax errors" and is able to generate code. You argue correctly that most languages are not context-free in this sense. If, on the other hand, you define "syntactically correct" as "accepted by the parsing pass of a compiler", then the sentence is tautological, since the parser uses a context-free grammar and thus accepts a context-free super-set of the programming language.Stolen
See https://cs.stackexchange.com/a/140082/60775 for a better answer.Stolen
G
72

What programming languages are context-free? [...]

My gut tells me that functional languages might be context-free [...]

The short version: There are hardly any real-world programming languages that are context-free in any meaning of the word. Whether a language is context-free or not has nothing to do with it being functional. It is simply a matter of how complex the syntax is.

Here's a CFG for the imperative language Brainfuck:

Program → Instr Program | ε
Instr → '+' | '-' | '>' | '<' | ',' | '.' | '[' Program ']'

And here's a CFG for the functional SKI combinator calculus:

Program → E
E → 'S' E E E
E → 'K' E E
E → 'I'
E → '(' E ')'

These CFGs recognize all valid programs of the two languages because they're so simple.


The longer version: Usually, context-free grammars (CFGs) are only used to roughly specify the syntax of a language. One must distinguish between syntactically correct programs and programs that compile/evaluate correctly. Most commonly, compilers split language analysis into syntax analysis that builds and verifies the general structure of a piece of code, and semantic analysis that verifies the meaning of the program.

If by "context-free language" you mean "... for which all programs compile", then the answer is: hardly any. Languages that fit this bill hardly have any rules or complicated features, like the existence of variables, whitespace-sensitivity, a type system, or any other context: Information defined in one place and relied upon in another.

If, on the other hand, "context-free language" only means "... for which all programs pass syntax analysis", the answer is a matter of how complex the syntax alone is. There are many syntactic features that are hard or impossible to describe with a CFG alone. Some of these are overcome by adding additional state to parsers for keeping track of counters, lookup tables, and so on.

Examples of syntactic features that are not possible to express with a CFG:

  • Indentation- and whitespace-sensitive languages like Python and Haskell. Keeping track of arbitrarily nested indentation levels is essentially context-sensitive and requires separate counters for the indentation level; both how many spaces that are used for each level and how many levels there are.

    Allowing only a fixed level of indentation using a fixed amount of spaces would work by duplicating the grammar for each level of indentation, but in practice this is inconvenient.

  • The C Typedef Parsing Problem says that C programs are ambiguous during lexical analysis because it cannot know from the grammar alone if something is a regular identifier or a typedef alias for an existing type.

    The example is:

      typedef int my_int;
      my_int x;
    

    At the semicolon, the type environment needs to be updated with an entry for my_int. But if the lexer has already looked ahead to my_int, it will have lexed it as an identifier rather than a type name.

    In context-free grammar terms, the X → ... rule that would trigger on my_int is ambiguous: It could be either one that produces an identifier, or one that produces a typedef'ed type; knowing which one relies on a lookup table (context) beyond the grammar itself.

  • Macro- and template-based languages like Lisp, C++, Template Haskell, Nim, and so on. Since the syntax changes as it is being parsed, one solution is to make the parser into a self-modifying program. See also Is C++ context-free or context-sensitive?

  • Often, operator precedence and associativity are not expressed directly in CFGs even though it is possible. For example, a CFG for a small expression grammar where ^ binds tighter than ×, and × binds tighter than +, might look like this:

      E → E ^ E
      E → E × E
      E → E + E
      E → (E)
      E → num
    

    This CFG is ambiguous, however, and is often accompanied by a precedence / associativity table saying e.g. that ^ binds tightest, × binds tighter than +, that ^ is right-associative, and that × and + are left-associative.

    Precedence and associativity can be encoded into a CFG in a mechanical way such that it is unambiguous and only produces syntax trees where the operators behave correctly. An example of this for the grammar above:

      E₀ → EA E₁
      EA → E₁ + EA
      EA → ε
      E₁ → EM E₂
      EM → E₂ × EM
      EM → ε
      E₂ → E₃ EP
      EP → ^ E₃ EP
      E₃ → num
      E₃ → (E₀)
    

    But ambiguous CFGs + precedence / associativity tables are common because they're more readable and because various types of LR parser generator libraries can produce more efficient parsers by eliminating shift/reduce conflicts instead of dealing with an unambiguous, transformed grammar of a larger size.

In theory, all finite sets of strings are regular languages, and so all legal programs of bounded size are regular. Since regular languages are a subset of context-free languages, all programs of bounded size are context-free. The argument continues,

While it can be argued that it would be an acceptable limitation for a language to allow only programs of less than a million lines, it is not practical to describe a programming language as a regular language: The description would be far too large.
     — Torben Morgensen's Basics of Compiler Design, ch. 2.10.2

The same goes for CFGs. To address your sub-question a little differently,

Which programming languages are defined by a context-free grammar?

Most real-world programming languages are defined by their implementations, and most parsers for real-world programming languages are either hand-written or uses a parser generator that extends context-free parsing. It is unfortunately not that common to find an exact CFG for your favourite language. When you do, it's usually in Backus-Naur form (BNF), or a parser specification that most likely isn't purely context-free.

Examples of grammar specifications from the wild:

Ghetto answered 16/7, 2013 at 20:18 Comment(4)
agree and added relevant comment on Dave's answer about this, +1Zymase
Another advantage of the CFG + associativity table is that some language has user-defined operator and these operators can have their own precedence set by the use. This is also cannot be parsed by CFG alone.Mudskipper
Are you saying that BNF and/or Bison's grammar specification are not "purely" context-free? If so, what do you mean by that? BNF is certainly a way of writing a context-free grammar; it cannot represent a grammar which is not context-free. Bison's only real extension feature is operator-precedence declarations, which do not alter the set of recognised inputs; all they do is resolve certain ambiguities. (Context-free grammars can be ambiguous. That might be a problem for practical parsing, but it doesn't make the grammar or the language not context-free.)Windlass
@Xwtek: Bison's precedence/associativity rules are static, not dynamic, so they don't add anything to the set of languages which can be parsed. If you have a language --like Swift-- with dynamic precedence, you can still parse it with a context-free grammar (sort of) as long as there is a predefined finite set of precedence levels. The grammar then only refers to precedence levels; the tokeniser then needs to return each operator with the syntactic class of its associated precedence level (which I agree isn't strictly context-free, but it's in some sense outside of the parser.)Windlass
C
49

The set of programs that are syntactically correct is context-free for almost all languages.

The set of programs that compile is not context-free for almost all languages. For example, if the set of all compiling C programs were context free, then by intersecting with a regular language (also known as a regex), the set of all compiling C programs that match

^int main\(void\) { int a+; a+ = a+; return 0; }$

would be context-free, but this is clearly isomorphic to the language a^kba^kba^k, which is well-known not to be context-free.

Cole answered 22/5, 2009 at 15:37 Comment(5)
+1 for the answer which is true. The accepted answer is misleading.Cannonry
If anyone desires to see a proof that the language { a^k b a^k b a^k b | k >= 0 } is not regular, see Torben Mogensen's Basics of Compiler Design chapter 2.10.2: diku.dk/~torbenm/BasicsGhetto
hmm the distinction between syntacticaly valid programs and semanticaly valid programs is misleading, since the latter is expressed syntacticaly in context-sensitive grammars (for cases of interest in programming languages). Saying a program is syntacticaly valid because a CFG can accept it, is wrong since the fact that the language of valid programs requires conditions which limit the strings accepted by merely CFGs, means they cannot be described by CFGs. Anyway +1Zymase
The first sentence doesn't make much sense. When is a program syntactically correct? Usually when the compiler accepts it without "syntax errors" and is able to generate code. You argue correctly that most languages are not context-free in this sense. If, on the other hand, you define "syntactically correct" as "accepted by the parsing pass of a compiler", then the sentence is tautological, since the parser uses a context-free grammar and thus accepts a context-free super-set of the programming language.Stolen
See https://cs.stackexchange.com/a/140082/60775 for a better answer.Stolen
L
8

Depending on how you understand the question, the answer changes. But IMNSHO, the proper answer is that all modern programming languages are in fact context sensitive. For example there is no context free grammar that accepts only syntactically correct C programs. People who point to yacc/bison context free grammars for C are missing the point.

Liddie answered 2/8, 2011 at 6:50 Comment(0)
U
6

To go for the most dramatic example of a non-context-free grammar, Perl's grammar is, as I understand it, turing-complete.

Unchartered answered 18/8, 2009 at 3:14 Comment(0)
G
3

If I understand your question, you are looking for programming languages which can be described by context free grammars (cfg) so that the cfg generates all valid programs and only valid programs.

I believe that most (if not all) modern programming languages are therefore not context free. For example, once you have user defined types (very common in modern languages) you are automatically context sensitive.

There is a difference between verifying syntax and verifying semantic correctness of a program. Checking syntax is context free, whereas checking semantic correctness isn't (again, in most languages).

This, however, does not mean that such a language cannot exist. Untyped lambda calculus, for example, can be described using a context free grammar, and is, of course, Turing complete.

Granoff answered 25/6, 2012 at 13:46 Comment(0)
S
2

VHDL is somewhat context sensitive:

VHDL is context-sensitive in a mean way. Consider this statement inside a process:

jinx := foo(1);

Well, depending on the objects defined in the scope of the process (and its enclosing scopes), this can be either:

  • A function call
  • Indexing an array
  • Indexing an array returned by a parameter-less function call

To parse this correctly, a parser has to carry a hierarchical symbol table (with enclosing scopes), and the current file isn't even enough. foo can be a function defined in a package. So the parser should first analyze the packages imported by the file it's parsing, and figure out the symbols defined in them.

This is just an example. The VHDL type/subtype system is a similarly context-sensitive mess that's very difficult to parse.

(Eli Bendersky, “Parsing VHDL is [very] hard”, 2009)

Spue answered 22/5, 2009 at 15:46 Comment(2)
I'm a new user and am not allowed to post links.Spue
@Graham: no, you absolutely may. There's no restriction for new users on posting links.Stancil
S
2

Most of the modern programming languages are not context-free languages. As a proof, if I delve into the root of CFL its corresponding machine PDA can't process string matchings like {ww | w is a string}. So most programming languages require that.

Example:

int fa; // w
fa=1; // ww as parser treat it like this
Scrape answered 1/4, 2013 at 14:51 Comment(0)
P
1

Let's take Swift, where the user can define operators including operator precedence and associativity. For example, the operators + and * are actually defined in the standard library.

A context free grammar and a lexer may be able to parse a + b - c * d + e, but the semantics is "five operands a, b, c, d and e, separated by the operators +, -, * and +". That's what a parser can achieve without knowing about operators. A context free grammar and a lexer may also be able to parse a +-+ b -+- c, which is three operands a, b and c separated by operators +-+ and -+-.

A parser can "parse" a source file according to a context-free Swift grammar, but that's nowhere near the job done. Another step would be collecting knowledge about operators, and then change the semantics of a + b - c * d + e to be the same as operator+ (operator- (operator+ (a, b), operator* (c, d)), e).

So there is (or maybe there is, I havent checked to closely) a context free grammar, but it only gets you so far to parsing a program.

Paroxysm answered 8/9, 2020 at 22:13 Comment(0)
P
-1

I think Haskell and ML are supporting context free. See this link for Haskell.

Polloch answered 22/6, 2012 at 7:39 Comment(1)
There's no way Haskell is context-free because you can define custom operators with custom precedence and associativity. You have to strip Haskell down to a really limited subset to make it context-free.Telly

© 2022 - 2024 — McMap. All rights reserved.