0

If I have something like var string = "var";, then after the first double quote the rules change, and the var does not mean the same thing as it means at the beginning of the text. After the second double quote things turn back to normal. How is that not considered context?

(Please don't use those arrows in your answer, try a natural language instead!)

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • Have you read [this Q/A](https://cs.stackexchange.com/questions/42988/what-does-context-in-context-free-grammar-refer-to)? Seems to provide a full answer, and explains the arrows! The "context" of being text within quotes isn't the same definition of context that the terminology refers to, it's more like being in a different state in a state transition machine. – hnefatl Feb 22 '19 at 22:22
  • @hnefatl I still do not understand. Isn't there a simple explanation without "A"-s and "x"-s and arrows? I have a very simple example in the question... – inf3rno Feb 22 '19 at 22:25

1 Answers1

2

The direction of the arrow is important, so if I can't talk about it, it's going to be difficult to explain. So, sorry, I'm going to use arrows. They really aren't complicated.

The expression A -> ... means "an A is ...". It does not mean "... is an A". Context-free means that if A can be "..." in some context, it can be "..." in any context. But the arrow always points from category to specific; never backwards.

In your example, an identifier is a letter followed by a bunch of alphanumeric symbols:

 identifier -> letter (letter OR digit)...

So identifier could be var. That doesn't mean that var is always an identifier, as your example shows. The arrow points in one direction.

Because the grammar is context-free, if when we are looking for an identifier in some context and we accept var as an identifier, then in any other context where we are looking for an identifier, we must also accept var.

But there are contexts (between quotes) where we are not looking for an identifier. That's fine; the context-free condition has not been broken. The context applies in the direction of the arrow.

rici
  • 234,347
  • 28
  • 237
  • 341
  • If so, then how do you call the change/difference between "when we are looking for an identifier" and "we are not looking for an identifier"? – inf3rno Feb 23 '19 at 00:45
  • @inf3rno: I don't call it anything. We're looking for an identifier because we were looking for something larger, perhaps an expression, which includes an identifier. The expression also includes a `string literal`, which starts with a quote. When we encounter the quote, it becomes evident that the only way to continue does not include an identifier. "Context-free" is a technical term, like it or not, and it has a technical meaning. I attempted why the word "context" is used in that term but it could just as well have been called a "Type-2 grammar", with exactly the same definition. – rici Feb 23 '19 at 03:43
  • I thought the topic has a more rich vocab. I can hardly believe that people are not talking about it, just writing symbols... Well I think the "we are looking for an identifier, which can be ...", and the "we are looking for a text, which can be any symbol until we reach the second double qoute" are 2 different contexts. The "identifier -> letter + alphanumeric*" is a rule. In context-free grammar we have a fixed rule set and every context can apply a subset of that. In context-sensitive grammar the same is true, but the rules can change by changing the context. – inf3rno Feb 23 '19 at 09:59
  • But if this is true I just don't understand why we make a difference. Why not just add another rule to the set instead of changing existing rules? – inf3rno Feb 23 '19 at 10:00
  • I think the "when we are looking for an identifier" part is unique in our rule set if we are talking about context-free grammar. This way the rule set does not depend on the context (it is context-free), while the rule we can apply from that rule set still depends on the context. In context-sensitive languages we can have a different rule set in each context. Or I totally misunderstood something a few comments earlier. :D – inf3rno Feb 23 '19 at 12:53
  • @inf3rno: when I get back home, I'll decide whether to try again or just delete this answer. Evidently, it's not helping provide you with an intuition and when I reread it, I thought it might too oversimplified for someone who is really trying to understand the theory. – rici Feb 23 '19 at 14:33
  • Do you have an article or book where these terms are defined in a non math language? – inf3rno Feb 23 '19 at 15:42
  • Actually I just want to understand the basic model behind these grammars before writing an own parser to practice on a hobby project. But if it is that complicated, then I just write a simple parser with my own model. It will be slow and probably ugly too, but good for starting... – inf3rno Feb 23 '19 at 16:34
  • @inf3rno: it is not complicated. The fact that it uses mathematical logic and a couple of symbols does not necessarily make it more complicated than `2 + 2 = 4`. Explaining it is complicated only if I have to do it with one hand tied behind my back. – rici Feb 23 '19 at 18:24
  • https://www.quora.com/What-does-context-mean-by-context-sensitive-grammar-in-simple-terms-no-arrows-cryptic-letters-etc/answer/Peter-Koves – inf3rno Feb 24 '19 at 09:39
  • I ended up watching the "Languages and Grammars" PPT here: https://courses.cit.cornell.edu/info2950_2011sp/syllabus-sp2011.htm These rules are very simple after you understand what a terminal and a non-terminal mean. I think they simply did not have a better word than "context" for those special rules. I don't think it has anything to do with the definition of context in natural languages. The only common thing that it comes prior to something. – inf3rno Feb 24 '19 at 22:04