- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在经典的编译器理论中,前两个阶段是词法分析和语法分析。他们正在筹备中。词法分析将标记识别为解析的输入。
但是我遇到了一些在词法分析中很难被正确识别的情况。例如下面关于C++模板的代码:
map<int, vector<int>>
>>
在“常规”词法分析中会被认为是按位右移,但这是不正确的。我的感觉是很难将这种语法的处理分为两个阶段,词法分析工作必须在解析阶段完成,因为要正确解析 >>
。依赖于语法,而不仅仅是简单的词汇规则。
我想知道关于这个问题的理论和实践。另外,我想知道 C++ 编译器如何处理这种情况?
最佳答案
C++ 标准要求实现在解析阶段之前执行词法分析以生成标记流。根据词法分析规则,连续两个>
字符(后面没有 =
)将始终被解释为一个 >>
token 。 C++ 标准提供的语法是根据这些标记定义的。
要求在某些上下文中(例如在模板 ID 中期望 >
时)实现应解释 >>
作为两个 >
未在语法中指定。相反,该规则被指定为一种特殊情况:
14.2 Names of template specializations [temp.names] ###
After name lookup (3.4) finds that a name is a template-name or that an operator-function-id or a literal-operator-id refers to a set of overloaded functions any member of which is a function template if this is followed by a
<
, the<
is always taken as the delimiter of a template-argument-list and never as the less-than operator. When parsing a template-argument-list, the first non-nested>
is taken as the ending delimiter rather than a greater-than operator. Similarly, the first non-nested>>
is treated as two consecutive but distinct>
tokens, the first of which is taken as the end of the template-argument-list and completes the template-id. [ Note: The second>
token produced by this replacement rule may terminate an enclosing template-id construct or it may be part of a different construct (e.g. a cast).—end note ]
注意前面的规则,在某些情况下 <
应解释为 <
在模板参数列表中。这是另一个需要上下文来消除解析歧义的构造示例。
C++ 语法包含许多这样的歧义,在没有上下文信息的情况下无法在解析过程中解决这些歧义。其中最著名的是Most Vexing Parse。 ,其中标识符可能会根据上下文解释为类型名称。
在 C++ 中跟踪上述上下文需要一个实现来与解析阶段并行执行一些语义分析。这通常以语义 Action 的形式实现,当在给定的上下文中识别出特定的语法结构时,这些语义 Action 就会被调用。然后,这些语义操作构建了一个表示上下文并允许高效查询的数据结构。这通常称为 symbol table , 但 C++ 所需的结构几乎是整个 AST .
这些上下文相关的语义 Action 也可以用来解决歧义。例如,在 namespace-body 上下文中识别identifier 时,语义操作将检查该名称是否先前被定义为模板。然后将结果反馈给解析器。这可以通过用结果标记 identifier 标记,或用匹配不同语法规则的特殊标记替换它来完成。
可以使用相同的技术来标记 <
作为 template-argument-list 的开头,或者 >
作为结束。 >>
的上下文相关替换规则有两个>
提出本质上相同的问题,可以使用相同的方法解决。
关于c++ - 编译器 : limitation of lexical analysis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106622/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
查询 Analysis Services 的服务器版本信息,是否有任何明确的方法可以确定您访问的服务器是 Azure Analysis Services 托管服务还是您访问的是在 Azure 中的 V
嗨,我正在使用Travis CI for Maven应用程序在SonarCloud上自动执行代码覆盖。 现在,在本地运行sonar:sonar命令在SoundCloud上提交报告,我可以通过Branc
我已经将我的项目从 Vite 2.x 更新到 Vite 3.0.2,突然出现这个错误: [plugin:vite:import-analysis] Failed to parse source for
我怎么知道我是否过度分析了? 过去 3 天我一直在追寻一个问题。我经历过许多设计并使用大约 3 个类得出了一个复杂的解决方案。与同事讨论后,我意识到我只需要一个方法和一个struct。我怎样才能避免成
我有不同的供应商,它们通过不同城市向我传递了一个 Excel,在每个城市,他们使用一些特殊的代码进行他们的操作和更多对我的业务有用的数据。 问题是我对所有这些城市都一团糟: 我的数据库中有自己的城市,
我认为这可能是一个关于大 O 符号的初学者问题。举例来说,我有一个算法,它递归地分解整个列表(O(n)),然后将其重新组合在一起(O(n))。我假设这意味着效率是 O(n) + O(n)。这是否简化为
我一直在挑战维基百科 pageviews分析。对我来说,这是第一个拥有如此大量数据的项目,我有点迷茫。当我从链接下载文件并将其解压缩时,我可以看到它具有类似表格的结构,其中的行如下所示: 1 |
我刚刚偶然发现了一份为我公司的一位客户(SMB)分析项目的工作。我是一名 .Net 开发人员(5 年),在分析软件方面经验很少(来自学校的 UML)。 我们刚刚结束了与用户两周的交谈,并得到了一份很好
对于准确的 Web 日志分析工具来生成 IIS 日志报告有什么建议吗?我们使用了 WebTrends,但我认为它不准确。 最佳答案 要分析博客,我认为使用模拟不会出错:http://www.analo
在寻找专门针对 K-map 最优性的文献时,我将不胜感激。 例如,我了解如何在 SOP(乘积和)表达式和 K-map 之间进行映射,以及为什么通常您希望 K-map 优化表达式更简单,因为找到了 1
首先,我在发布这个问题之前已经进行了搜索。我已经看过这个问题 Why is quicksort better than mergesort?但它有一些相互矛盾的答案。 根据我的观察,人们说快速排序比合
我有兴趣使用像 OllyDbg 这样的东西来检查一个可能具有类似木马特征的程序。 有谁知道关于如何安全地做到这一点的任何好的教程? 基本上该程序是一个视频游戏的“机器人”,但我怀疑它有后门和/或将它收
我可以对单个属性使用 k-means 算法吗? 属性和簇数有关系吗? 我有一个属性的性能,我想将数据分为 3 个集群:差、中和好。 是否可以用一个属性创建 3 个集群? 最佳答案 K-Means 在您
现在我对符号执行(SE)和可达性分析(RA)感到困惑。据我所知,SE使用符号来执行一些代码以到达具有分支条件的每个分支。而RA可以用来求每个分支的可达性,对吧?当使用 RA 时,我们可以提取每个分支的
假设我有一个 ActivePivot 多维数据集,其中的事实仅包含值和货币。假设我的多维数据集将货币作为常规维度。 我们用具有多种货币的事实填充立方体。 我们有一个外汇服务,它使用货币和引用货币来计算
在Weka中使用Kmeans时,可以在模型的结果输出上调用getAssignments()以获取每个给定实例的集群分配。这是一个(截断的)Jython示例: >>>import weka.cluste
确切地说,我有两个问题。首先,我想知道是否有一种简单的方法来适应马尔可夫聚类算法,以便我可以提前指定最后我想要有多少个聚类。如果没有,您会推荐哪种类似的算法? 其次应该如何处理马尔可夫世界中的重叠集群
在进行情感分析时,如何让机器理解我指的是苹果(iphone),而不是苹果(水果)? 谢谢你的建议! 最佳答案 嗯,有几种方法, 我会从检查大写字母开始,通常,当提到一个名字时,第一个字母是大写的。 在
我在学习非线性聚类算法时遇到了这个二维图。我想知道哪种聚类算法和超参数的组合可以很好地聚类这些数据。 就像人类将这 5 个尖峰聚集在一起一样。我希望我的算法能够做到这一点。 我尝试了 KMeans,但
我是一名优秀的程序员,十分优秀!