parsing - bison info doc - precedence in recursive parsing
问题描述
in info doc of bison, it is mentioned that rule gets its precendence from last terminal symbol. pasted below: https://www.gnu.org/software/bison/manual/html_node/How-Precedence.html#How-Precedence
The first effect of the precedence declarations is to assign precedence levels to the terminal symbols declared. The second effect is to assign precedence levels to certain rules: each rule gets its precedence from the last terminal symbol mentioned in the components.
For below example, how the parsing will happen if above is the case:
nonterm1 : nonterm2
| nonterm1 term1 nonterm3 nonterm4
| nonterm1 term2 nonterm5 nonterm6
One more example:
nonterm1 : nonterm2 nonterm3
| nonterm1 term1 nonterm2 nonterm3
| nonterm1 term2 nonterm2 nonterm3
Consider first example. First parser will consume nonterm2
then for term1
rule, nonterm1
will hold nonterm2
value and term2
higher precedence than term1
, parser have to pass value to nonterm1
in term2
rule but it is invalid if we provide term1
precedence to term1
rule because as the statement suggests, last terminal token gives rule precedence. Assigning value of nonterm3 nonterm4
to nonterm1
in term2
rule is not possible or is it?
Value is for term or nonterm but not partial set of symbols in rule or is it possible?
Does the info doc says last terminal symbol meant to be "last but one" symbol which if it is terminal?
解决方案
默认情况下,产生式的优先级是产生式右侧最后一个终端的声明优先级,无论后面有多少(或几个)非终端。
如果该终端没有声明的优先级,则生产也没有声明的优先级。
在序言中使用 、 和 声明来%left
声明%right
优先%precedence
级。%nonassoc
优先级仅用于解决歧义。如果语法完全确定解析操作,则不参考优先级。
推荐阅读
- ios - 使用 NEKit 在 iOS NEPacketTunnelProvider 中代理 websocket 连接
- hadoop - 将文件放入 HDFS
- python - Python。在 Seaborn Facetgrid 上使用两个 y 轴绘制线图和条形图
- java - RuntimeException:无法在片段中使用 Fotoapparat 连接到相机服务
- asp.net-mvc - Asp.net Odata Web API Cross Origin Put and Pacth Request Browser Preflight Error
- android - 将字符串 url 数据放入位图
- sql - 带选择的 SQL INSERT
- jquery - 如何使用fabric js在画布上自动缩放具有纵横比的大图像
- mongodb - Mongo Db 在 Json 过滤器中查询 Json
- javascript - Vue JS Spinner 用于同一页面中的多个组件(单个应用程序)