0

First off, I didn't study computer science, I'm just interested in the subject.

I have read that e.g. having to declare a variable before its use is a context-sensitive aspect of programming languages.

My question is: Could you provide a production rule that is the equivalent of "declare a variable before its use" along with the actual code?

I haven't seen a context sensitive production rule yet where it was clear to me how that would actually look in the code later or vice versa, look at code and then derive the production rule.

Or maybe some other context-sensitive feature or Java (aren't generics context-sensitive, as well?).

EDIT:

As an example:

https://en.wikipedia.org/wiki/Context-sensitive_grammar#Examples

Can you provide a piece of code that would show this grammar in use? How would this grammar look in an actual programme? It's just that those aabababccbab-type examples are a bit abstract.

user3629892
  • 2,960
  • 9
  • 33
  • 64
  • Did you mean context-sensitive in that there is a context (as there is in most modern programming languages - there are variables whcih hold values whcih make up the context for any programming line) vs. context-free programming, such as a functional language, where the only context is the list of parameters passed into the functon - each line in the function works on the same context ? – user2808054 Jun 19 '15 at 16:26
  • well, as far as I understand it, a programming language can basically be described with a context-free grammar, but modern languages (such as java) have language features that can only be described with context-sensitive grammars, e.g. declaring variables before using them or generics. So if I want to design a language that uses generics, I cannot do that by only using a context-free grammar. See here: https://stackoverflow.com/questions/9652436/are-the-grammars-of-modern-programming-languages-context-free-or-context-sensiti – user3629892 Jun 19 '15 at 17:31
  • I also edited the question. – user3629892 Jun 19 '15 at 17:38
  • 1
    Usually, programming language syntax is specified with a CFG and then a separate set of rules that are not specified with a grammar at all are used to check for properties like "variables must be declared before use" or "classes must not extend interfaces or implement classes." As a result, I doubt that you'll actually find context-sensitive grammars for any major programming language. – templatetypedef Jun 19 '15 at 18:02
  • 1
    @user3629892 I don't think computer languages are a good example, as they are rarely (if ever) parsed in a context-sensitive way. In theory it's possible, in practice it's easier to check for the more complex stuff later in the compilation process. – biziclop Jun 19 '15 at 18:03
  • 1
    Notably: [parsing Perl is undecidable](http://www.perlmonks.org/?node_id=663393), i.e. way harder than context-sensitive. – G. Bach Jun 19 '15 at 18:06
  • 1
    @G.Bach So it isn't just us, humans who have trouble reading Perl code. Makes me feel slightly better about it. :) – biziclop Jun 19 '15 at 18:10
  • Alright, thank you for your replies! – user3629892 Jun 19 '15 at 19:35
  • I've learnt and remembered (from my now-ancient degree) a lot while thinking about this question. I'm not sire I can answer it directly, hence a comment rather than an answer, but for an exampe of what Generics code looks like see here: https://en.wikipedia.org/wiki/Generics_in_Java#Generic_class_definitions – user2808054 Jun 22 '15 at 09:41
  • @user2808054 Thanks, but I know what they look like in code, I also use them :) If you could now find me the corresponding (presumably context-sensitive) grammar which would be necessary to define this language feature, you'd have answered my question. My question was about seeing the grammar definition and the code side by side. – user3629892 Jun 24 '15 at 08:55

0 Answers0