สิ่งที่แท้จริงหมายถึงอะไรโดยบริบทในไวยากรณ์คำศัพท์บริบท?


29

ฉันกำลังศึกษาคอมไพเลอร์อยู่พักหนึ่งและฉันค้นหาสิ่งที่มีความหมายโดย "บริบท" ในไวยากรณ์และความหมายของไวยากรณ์คือ "ปราศจากบริบท" แต่ไม่มีผลลัพธ์

ใครช่วยได้บ้าง


7
What do you mean by "really"? Which explanations have you read and what don't you understand? IIRC, every halfway decent textbook on the matter will explain what they mean.
Raphael

2
Here's a relatable example. Consider the word "read". It's a single word that has two completely different meanings. One is the present tense "to read", the other is the past tense "I read". If you saw the word "read" in a piece of text, you cannot disambiguate which of the two meanings it represents, without looking at the context. Thus, English is a context-sensitive, because you can't parse each token (word) without considering it in context. A context-sensitive grammar is one in which the meaning of every token is unambiguously deducible from the single token that represents it.
Alexander - Reinstate Monica

คำตอบ:


30

The context can be explained with regards to the production rules allowed for different grammars in Chomsky hierarchy.

If you consider context-free grammars, their production rules have the following form:

Aα

So, you can observe that the left part of this kind of rules is made up of only one non-terminal symbol; thus, the substitution of the non-terminal symbol takes place without considering its "context", that is the other symbols it is surrounded by.

On the other hand, if you consider production rules of context-sensitive grammars, they have the following form:

βAγβαγ

where A is a non-terminal and α, β, γ are sequences of non-terminals and terminals.

In this case the "context" (i.e., β and γ) of the non-terminal symbol to be substituted influences the effect of the substitution and it is part of the rule itself.

You can find more details in this answer on mathematics and in this answer on software engineering.


Thank you for the answer. But the strange thing for me is that a similar question was asked on mathematics SE.
Shady Atef

1
Note that β and γ don't need to be part of the production's outcome. They also could've been substituted by some other sequence as one can see in @David Richerby's answer.
Frozn

1
@Frozn AFAIK the one given here is the standard definition according to the Chmosky hierarchy. Sure, there are grammars more powerful than context-sensistive that allow any type of production, but the standard context-sensitive grammars do not.
Bakuriu

2
@Frozn: Bakariu is right, here we are talking about grammars defined according to the Chomsky hierarchy, that is based on increasingly restrictive conditions on the production rules. In particular, context-free grammars are type-2 grammars, while context-sensitive ones are type-1. However, type-0 grammars have production rules that are not limited by any restriction and thus are called unrestricted rewriting systems. Here you can find a short description of Chomsky hierarchy with some examples.
PieCot

@Bakuriu and PieCot Well thanks for that, I knew the Chomsky hierarchy. Somewhen someone introduced monotonic grammars as context-sensitiv and together with type-0 and type-1 from the Chomsky hierarchy this resulted in the general rule βAγδ as long as |βAγ||δ|.
Frozn

17

"Context" is surrounding text. Context-free grammars are context-free in the sense that the rules look like Athings, rather than stuffAmore-stuffthings. The left-hand side of a rule is always a single non-terminal symbol. That is, the rules for expanding a non-terminal symbol don't depend on what text appears around that symbol (its context), but only depend on the symbol itself. For example, in the grammar for a programming language, the term Expr expands to the same kind of expression whether you're writing an assignment (e.g., x:=y+z), passing arguments to a function (e.g., f(y+z)) or returning a value from a function (e.g., return y+z).


4

Generically speaking, even regular languages may have context dependencies, meaning that you can determine - to some extent - in what ways symbols can appear in the vicinity of other symbols in a string that belongs to that language.

What is specific to context-free grammars is that when there are multiple ways of substituting a non-terminal symbol, by applying different rules with the same non-terminal on the right side, the choice of which rule to apply is never dependent on what is happening around this symbol during the derivation process.

You can think of them as context-free derivation languages, context-free languages for short.

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.