I'm curious about which (if any) real-world programming languages have a regular grammar (i.e. the set of all syntactically correct programs is regular).
See also this question: What programming languages are context-free?.
I'm curious about which (if any) real-world programming languages have a regular grammar (i.e. the set of all syntactically correct programs is regular).
See also this question: What programming languages are context-free?.
Brainfuck and Whitespace and similars are certainly regular.
On the other side, any language that supports (parens) is not regular, as the automaton recognizing it would need a stack. And I don't really know many languages without (){}[] support that would do anything more than assembly.
Only real-worldy example that comes to mind and probably is regular is Forth.
[
marks the beginning of a loop and ]
marks the end. As these must match, brainfuck is not regular. –
Shufu while{}
statements or as push [
and pop+jmp/ret ]
instructions. Technically, there is no need for the bracket count to match. –
Mathematician The answer is YES. There are certainly programming languages with regular grammar!
The A=B programming language is a one instruction esolang which is regular.
https://esolangs.org/wiki/A%3DB
Here is the regular expression that matches any such language:
((\(once\))?([^=()]+|\(start\)[^=()]+|\(end\)[^=()]+)=([^=()]*|\(start\)[^=()]*|\(end\)[^=()]*|\(return\)[^=()]*))*
In fact, it is possible to describe a Turing machine using regular language.
Let the states of the Turing machine be called a
, aa
, aaa
, etc, where a
is the initial state. we can describe a Turing machine as follows, assuming the tape only contains 0
, 1
and 2
, where 0
is the blank symbol:
([012]a+=[012][lr]a+)*
© 2022 - 2024 — McMap. All rights reserved.