0

So, I am implementing a context sensitive syntactical analyzator. It's kind of experimantal thing and one of the things I need are usable and practical syntactical contructs to test it on.

For example the following example isn't possible to parse using standard CFG (context free grammar). Basically it allows to declare multiple variables of unrelated data types and simultaneously initialize them.

int bool string number flag str = 1 true "Hello";

If I omit a few details, it can be formally described like this:

L = {anbncn | n >= 1}

So, I would appreciate as much of similar examples as you can think of, however, they really should be practical. Something that actual programmers would appreciate.

svick
  • 236,525
  • 50
  • 385
  • 514
Tedd Parsile
  • 65
  • 1
  • 10
  • I think your example is already not practical. It takes quite some effort to visually 'parse' that line, and I wouldn't know why `int bool string number flag str = 1 true "Hello";` would be more convenient than a more classical order like `int number 1, bool flag true, string str "hello"`. – GolezTrol Sep 09 '15 at 17:56
  • 1
    What about simply having to declare a variable before using that variable? That strikes me as a canonical example of a common feature of programming languages that is not context free. – Patrick87 Sep 09 '15 at 18:26
  • @GolezTrol you are probably right that the syntaxe you described is more readable, though on the other hand it's completely useless information. It's not like I will just stop implmenting... Some actual context sensitive example would be much better. Or is it that you think that context sensitive parsers are unnecesarilly powerful? – Tedd Parsile Sep 13 '15 at 14:01
  • @Patrick87 you are right of course, but I am currently looking more for the syntactical advantages of CFG and not the semantical. But again, thanks a lot. – Tedd Parsile Sep 13 '15 at 14:03
  • @TeddParsile Well, that's why my comment was a comment and not an answer. And by all means, keep implementing. Anyway, I meant to say that the example you gave seems to be purposely hard to read, while for the parser it shouldn't matter much. I believe my example is contextual as well, just following a different contextual grammar. – GolezTrol Sep 13 '15 at 14:44
  • @GolezTrol Hmm, if I am not mistaken, your example is context-free. It can be described by grammar: S->A; A-> type id E B; B-> , type id E B; B-> , type id E; plus E-> ... for expression. And that is CFG. – Tedd Parsile Sep 16 '15 at 16:57

1 Answers1

2

Just about all binary formats have some context-sensitivity, one of the simplest examples being a number of elements followed by an undelimited array of that length. (Technically, this could be parsed by a CFG if the possible array lengths are a finite set, but only with billions and billions of production rules.) Pascal and other languages traditionally represented strings this way. Another context-sensitive grammar that programmers often use is two-dimensional source-code layout, which right now gets translated into an intermediate CFG during preprocessing. References to another part of the document, such as looking up a label. Turing-complete macro languages. Not sure exactly what kind of language your parser is supposed to recognize.

Davislor
  • 14,674
  • 2
  • 34
  • 49