0

I want to write the grammar for a list containing sequentially increasing integers like this

1 X
1 X 2 X
1 X 2 X 3 X
1 X 2 X 3 X 4 X
// and so on

I want to use a recursive definition to avoid defining separate rules for each case manually.

Without the increasing integers, I could write the following recursive grammar (in McKeeman Form)

NumberedList
    Int X
    Int X NumberedList

How can I specify the increasing integers?

mdcq
  • 1,593
  • 13
  • 30

1 Answers1

2

No context-free grammar can represent this language because it is not context-free. Each expansion depends on the preceding expansion, which is an example of what the "context" in "context-free" is referring to.

It would be straightforward but tedious to write a context-sensitive grammar. That's the result of having to write out decimal arithmetic as a set of string substitution rules.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thank you! Can you point me towards the way how I’d start writing this in a context-sensitive grammar? – mdcq Aug 31 '22 at 18:30
  • 1
    @philmcole: to what end? Writing CSGs is like writing Turing Machines; possibly an interesting intellectual exercise although highly repetitive, bit of very little practical use. – rici Aug 31 '22 at 19:30
  • 1
    @philmcole It would be much simpler to just use a context-free grammar, and if implementing the language add semantic checks to enforce the increasing numbers. – Some programmer dude Sep 01 '22 at 11:03