[英]Changing associativity schema in a grammar
I'm trying to use SableCC to generate a Parser for models, which I call LAM. 我正在尝试使用SableCC为模型生成一个解析器,我称之为LAM。 LAM in itself are simple, and a simple grammar (where I omit a lot of things) for these is:
LAM本身很简单,并且这些的简单语法(我省略了很多东西)是:
L := 0 | (x,y) | F(x1,...,xn) | L || L | L ; L
I wrote this grammar: 我写了这个语法:
Helpers
number = ['0' .. '9'] ;
letter = ['a' .. 'z'] ;
uletter = ['A' .. 'Z'] ;
Tokens
zero = '0' ;
comma = ',' ;
parallel = '||' ;
point = ';' ;
lpar = '(' ;
rpar = ')' ;
identifier = letter+ number* ;
uidentifier = uletter+ number* ;
Productions
expr = {term} term |
{parallel} expr parallel term |
{point} expr point term;
term = {parenthesis} lpar expr rpar |
{zero} zero |
{invk} uidentifier lpar paramlist rpar |
{pair} lpar [left]:identifier comma [right]:identifier rpar ;
paramlist = {list} list |
{empty} ;
list = {var} identifier |
{com} identifier comma list ;
This basically works, but there is a side effect: it is left associative. 这基本上是可行的,但有一个副作用:它是关联的。 For example, if I have
例如,如果我有
L = L1 || L2 ; L3 || L4
Then it is parsed like: 然后将其解析为:
L = ((L1 || L2) ; L3) || L4
I want to give all precedence to the ";" 我要优先考虑“;” operator, and so have L parsed like
运算符,因此将L解析为
L = (L1 || L2) ; (L3 || L4)
(other things, like "||", could remains left-associative) (其他事物,例如“ ||”,可能仍保持左联想)
My questions are: 我的问题是:
It is accepted also "RTFM link" :-D Thank you all 也被认为是“ RTFM链接” :-D谢谢大家
You need to create a hierarchy of rules that matches the desired operator precedence. 您需要创建与所需的运算符优先级匹配的规则层次结构。
expr = {subexp} subexp |
{parallel} subexp parallel expr ;
subexp = {term} term |
{point} term point subexp;
Note that I also changed the associativity. 请注意,我还更改了关联性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.