問題:
I often hear claims that C++ is a context-sensitive language. 我經常聽到有人聲稱C ++是上下文相關的語言。 Take the following example: 請看以下示例:
a b(c);
Is this a variable definition or a function declaration? 這是變量定義還是函數聲明? That depends on the meaning of the symbol c
. 這取決於符號c
的含義。 If c
is a variable , then ab(c);
如果c
是一個變量 ,則ab(c);
defines a variable named b
of type a
. 定義了一個名爲變量b
類型的a
。 It is directly initialized with c
. 它直接用c
初始化。 But if c
is a type , then ab(c);
但是如果c
是一個type ,則ab(c);
declares a function named b
that takes a c
and returns an a
. 聲明一個名爲b
的函數,該函數採用c
並返回a
。
If you look up the definition of context-free languages, it will basically tell you that all grammar rules must have left-hand sides that consist of exactly one non-terminal symbol. 如果您查找無上下文語言的定義,它將基本上告訴您所有語法規則的左手邊必須由一個非終結符組成。 Context-sensitive grammars, on the other hand, allow arbitrary strings of terminal and non-terminal symbols on the left-hand side. 另一方面,上下文相關的語法允許在左側使用任意字符串的終止符和非終止符。
Browsing through Appendix A of "The C++ Programming Language", I couldn't find a single grammar rule that had anything else besides a single non-terminal symbol on its left-hand side. 瀏覽“ C ++編程語言”的附錄A,我找不到一個語法規則,該語法規則的左側除了單個非終止符號外還具有其他內容。 That would imply that C++ is context-free. 這意味着C ++是無上下文的。 (Of course, every context-free language is also context-sensitive in the sense that the context-free languages form a subset of the context-sensitive languages, but that is not the point.) (當然,每種上下文無關的語言也是上下文敏感的,因爲上下文無關的語言構成了上下文敏感語言的子集,但這不是重點。)
So, is C++ context-free or context-sensitive? 那麼,C ++是上下文無關的還是上下文相關的?
解決方案:
參考一: https://en.stackoom.com/question/zDM2參考二: https://stackoom.com/question/zDM2