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 ++具有“不确定的语法”时,这意味着什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值