扩展Backus–Naur形式的操作顺序 [英] Extended Backus–Naur Form order of operations

查看:77
本文介绍了扩展Backus–Naur形式的操作顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一种非常简单的规则语言创建一个正式规范. 我想使用EBNF,因为这是一个标准,但是我不知道如何指定操作顺序.这是到目前为止的规格.

I am creating a formal spec for a very simple rule language, very simple. I want to use EBNF as this is a standard but I can't figure out how to specify order of operations. Here is the specification so far.

rule = statement, { (‘AND’|’OR’), statement};

variable = ‘$’,alphabetic character, {alphabetic character | digit};

statement = variable, [ ‘count’,[white space ],’>’,[white space],number ];

alphabetic character = "A" | "B" | "C" | "D" | "E" | "F" | "G"
                     | "H" | "I" | "J" | "K" | "L" | "M" | "N"
                     | "O" | "P" | "Q" | "R" | "S" | "T" | "U"
                     | "V" | "W" | "X" | "Y" | "Z" ;

number = [ "-" ] , digit , { digit } ;

digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;

white space = ? white space characters ? ; 

我的问题是如何显示方括号中的内容应首先进行评估. 像这样

The question I have is how do I show that things in brackets should be evaluated first. So something like this

$strap AND ($greenSticker count > 5 OR ($greenSticker AND $redSticker))

对于大多数语言来说,这似乎是一个共同的功能,但是我的Google技能使我感到失望,而且我似乎找不到一个示例.

It seems like a common feature to most languages, but my Google skills are failing me and I can't seem to find an example.

推荐答案

将其作为LL语法的简化示例:

Given this as a simplified example LL grammar:

expression -> (+|-|ε) term ((+|-) term)*
term -> factor ((*|/) factor)*
factor -> var | number | (expression)

如您所见,优先级较低的运算符(+-)比较高优先级的运算符(*/)具有更通用的规则.这一切都与产生正确的解析树有关.但是根据经验,外部"或更通用的规则的优先级较低,这就是为什么加法和减法运算符放在term旁边的原因,因为必须进一步导出term.如果您查看更复杂的语法,您会发现这是极端的,它具有适当的优先级.

As you can see, the operators with lower precedence (+ and -) are in a more general rule than the higher precedence operators (* and /). It is all about producing the correct parse tree. But as a rule of thumb, "outer" or more general rules have less precedence, which is why the addition and subtraction operators are placed beside term, because term must be further derived. If you look at more complicated grammars you will see that this is taken into an extreme to have proper precedence.

这篇关于扩展Backus–Naur形式的操作顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆