What is an example of some production rules which would allow parentheses, curly braces, and square-brackets to be interchangeable?
In computer programming, there are many different ways to write a for-loop.
Some examples are shown below:
lang-none +--------------------------------------+----------------------------------------+ | | | | for k in range(0, 10) { x = "ab" } | The loop body is wrapped | | | with curly braces | | | | +--------------------------------------+----------------------------------------+ | | The loop body is wrapped | | for k in range(0, 10) : ( x = "ab" ) | with parentheses | | | | | | An infix colon separates the | | | loop body from the loop-preamble | +--------------------------------------+----------------------------------------+ | | | | for k in range(0, 10) { | The body of the for-loop is wrapped | | x = "ab" | twice. | | } | | | | | | | It is wrapped once with curly braces | | | the loop body is wrapped again with a | | | change in indentation | | | | +--------------------------------------+----------------------------------------+
What set of production rules, defining a formal grammar, would describe the syntax for a simple language of for-loops with the following requirements?
* REQ-0 The language must have assignment statements which assign string literals to a variable. Uses python style syntax.
$\qquad$ $\qquad$ Example: myString = "hello world\n"
* REQ-1 In this language, parentheses, curly braces, and square-brackets are interchangeable so long as they match and are outside of string literals.
$\qquad$ $\qquad$
$\qquad$ $\qquad$ left-parentheses must be matched to right-parentheses.
$\qquad$ $\qquad$
$\qquad$ $\qquad$ for k in range(10)(x] is illegal syntax
$\qquad$ $\qquad$ a left parentheses is matched to a right-square-bracket.
$\qquad$ $\qquad$ (x] will be allowed inside of a string literal.
$\qquad$ $\qquad$ (x] is allowed in myString = "(x]"
$\qquad \qquad$
* REQ-2 Outside of string literals, infix delimiters must be be interchangeable. We define infix delimiters to be space characters (), colons (:), commas (,) and full-stops (.)
* REQ-3 Our language shall have for-loops
For the production rules in your answer, a change in indentation must be interchangeable with parentheses.
However, we cannot simply have a production rule to replace an increase increase in indentation by one of
$\begin{Bmatrix} \mathtt{"("}, \mathtt{"{"}, \mathtt{"["} \end{Bmatrix} $
This is because the left-delimiter will not match the right-delimiter.
In your answer:
left parentheses must be matched to right-parentheses * left-curly-braces matched to right-curly-braces.
an increase in indentation must be matched to a decrease in indentation
You can stop reading now if you like.
However, I realized that detecting change in indentation would be difficult, so I will allow answers which have a special change in indentation character be a terminal symbol. 592