当前位置:网站首页>LL(1)文法 :解决 if-else/if-else 产生式二义性问题
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
2022-08-02 03:30:00 【ChivenZhang】
注:本文适合对编译感兴趣的朋友阅读。
最近尝试用LL(1)文法构造 类似C语言结构的产生式。在构造 if/else 语句时,发现语法存在二义性(Statement、Expression部分省略 ):
Statement-> if ( Expression ) IfBlock
IfBlock-> Statement ElseBlock
ElseBlock-> else Statement |∑
算法分析结果指出:ElseBlock的首符集和随符集 同时出现 else 助记符;
结果一出,我顿时发懵,努力去修改产生式去消除 二义性,但是尝试2、3天后无果。于是在网上的一篇帖子中发现:这是if-else固有的二义属性;同时指出具体解决方法参考紫龙书 例4.33:


解决方法:
产生式采用:
Statement-> if (Expression) IfBlock
ifBlock-> Statement ElseBlock
ElseBlock-> else Statment
我使用了一种捷径:在词法分析阶段,对Tokens序列进行检查。若if 分支后未监测到else,则在适当位置插入一个‘else;’序列,此阶段后在进行常规编译;
优点:既实现了if-else的灵活组合,又降低编译器的实现难度(不存在二义性问题)。
自研产品介绍:Plang高级编程语言
边栏推荐
猜你喜欢

Two-Stream Convolutional Networks for Action Recognition in Videos双流网络论文精读

R语言 —— 多元线性回归

Arduino lights up nixie tubes

【科普贴】I2C通讯协议详解——偏软件分析和逻辑分析仪实例分析

网站开发方案研究

2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》
![[Spark]-协同过滤](/img/18/8129e96758160b7e5cc9814d50d882.jpg)
[Spark]-协同过滤

【Arduino 连接GP2Y1014AU0F 灰尘传感器】

【科普贴】I2C接口详解——偏硬件解析

2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
随机推荐
【plang 1.4.6】Plang高级编程语言(发布)
字符串匹配(蛮力法+KMP)
连接本地MySql时出现2003-Can‘t connect to MySql server on ‘localhost‘(10061)
树莓派入门(1)系统镜像烧录
Out of memory error on GPU 0. Cannot allocate xxxGB memory on GPU 0, available memory is only xxx
最第k大的数的一般性问题
Case | industrial iot solutions, steel mills high-performance security for wisdom
PCB设计思路
PCB Design Ideas
蛮力法求解凸包问题
Scala 中的集合(二):集合性能比较
【Arduino使用旋转编码器模块】
联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
基于阿里云OSS+PicGo的个人图床搭建
[Arduino connects the clock module to display the time on LCD1602]
[Arduino connected to GPS module (NEO-6M) to read positioning data]
Vision Transformer(ViT)论文精读和Pytorch实现代码解析
【科普贴】I2C通讯协议详解——偏软件分析和逻辑分析仪实例分析
【Popular Science Post】UART Interface Communication Protocol
IoT solution