ฉันกำลังศึกษาคอมไพเลอร์อยู่พักหนึ่งและฉันค้นหาสิ่งที่มีความหมายโดย "บริบท" ในไวยากรณ์และความหมายของไวยากรณ์คือ "ปราศจากบริบท" แต่ไม่มีผลลัพธ์
ใครช่วยได้บ้าง
ฉันกำลังศึกษาคอมไพเลอร์อยู่พักหนึ่งและฉันค้นหาสิ่งที่มีความหมายโดย "บริบท" ในไวยากรณ์และความหมายของไวยากรณ์คือ "ปราศจากบริบท" แต่ไม่มีผลลัพธ์
ใครช่วยได้บ้าง
คำตอบ:
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:
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:
where 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.
"Context" is surrounding text. Context-free grammars are context-free in the sense that the rules look like , rather than . 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 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
).
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.