C ++是上下文無關的還是上下文相關的?

本文翻譯自:Is C++ context-free or context-sensitive?

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 ++是上下文無關的還是上下文相關的?


#1樓

參考:https://stackoom.com/question/zDM2/C-是上下文無關的還是上下文相關的


#2樓

True :) 是的:)

J. Stanley Warford. 斯坦利·沃福德(J. Stanley Warford)。 Computer systems . 計算機系統 Pages 341-346. 341-346頁。


#3樓

C++ is parsed with GLR parser. C ++使用GLR解析器進行解析。 That means during parsing the source code, the parser may encounter ambiguity but it should continue and decide which grammar rule to use later . 這意味着在解析源代碼期間,解析器可能會遇到歧義,但它應該繼續並決定以後使用哪種語法規則。

look also, 也看

Why C++ cannot be parsed with a LR(1) parser? 爲什麼C ++無法使用LR(1)解析器進行解析?


Remember that context-free grammar can not describe ALL the rules of a programming language syntax. 請記住,上下文無關的語法不能描述編程語言語法的所有規則。 For example, Attribute grammar is used to check the validity of an expression type. 例如,屬性語法用於檢查表達式類型的有效性。

int x;
x = 9 + 1.0;

You can not describe the following rule with context-free grammar : The Right Side of the assignment should be of the same type of the Left Hand side. 不能使用上下文無關的語法描述以下規則: 分配的右側應與左側相同。


#4樓

C++ is not context free. C ++不是上下文無關的。 I learned it some time ago in compilers lecture. 我前一段時間在編譯器講座中學到的。 A quick search gave this link, where the "Syntax or semantics" section explains why C and C++ are not context free: 快速搜索給出了此鏈接,其中“語法或語義”部分說明了爲什麼C和C ++不是上下文無關的:

Wikipedia Talk: Context-Free grammar 維基百科談話:無上下文語法

Regards, 問候,
Ovanes 奧萬斯


#5樓

Yes. 是。 The following expression has a different order of operations depending on type resolved context : 以下表達式根據類型解析的上下文具有不同的操作順序

Edit: When the actual order of operation varies, it makes it incredibly difficult to use a "regular" compiler that parses to an undecorated AST before decorating it (propagating type information). 編輯:當實際的操作順序變化時,將很難使用一個“常規”編譯器來解析未修飾的AST,然後再對其進行修飾(傳播類型信息)。 Other context sensitive things mentioned are "rather easy" compared to this (not that template evaluation is at all easy). 與此相比,提到的其他上下文相關的東西“相當容易”(並不是說模板評估一點都不容易)。

#if FIRST_MEANING
   template<bool B>
   class foo
   { };
#else
   static const int foo = 0;
   static const int bar = 15;
#endif

Followed by: 其次是:

static int foobar( foo < 2 ? 1 < 1 : 0 > & bar );

#6樓

有時情況更糟: 當人們說C ++具有“不確定的語法”時,這意味着什麼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章