我正在使用 sableCC 并尝试构建一个接受 C 中的条件的语法。但是,当我尝试在条件上添加括号时出现错误,因为与表达式的括号冲突。例如,它接受 4 = 3 或 x = 95 mod 5 和 5 = 5 但没有任何括号。我也觉得“不”语法规则不正确。
Helpers
tab = 9;
cr = 13;
lf = 10;
sign = '+' | '-';
digit = ['0'..'9'];
lowercase = ['a'..'z'];
uppercase = ['A'..'Z'];
letter = lowercase | uppercase;
idletter = letter | '_';
idchar = letter | '_' | digit;
Tokens
number = [digit - '0'] digit*;
id = idletter idchar*;
plus = '+';
minus = '-';
times = '*';
div = 'div';
mod = 'mod';
equal = '=';
hash = '#';
greater = '>';
greateq = '>=';
less = '<';
lesseq = '<=';
not = 'not';
and = 'and';
or = 'or';
lparen = '(';
rparen = ')';
eol = cr | lf | cr lf;
blank = ' ' | tab;
Ignored Tokens
eol, blank;
Productions
program = compare*;
expr = {term} term
| {add} expr plus term
| {sub} expr minus term
;
term = {factor} factor
| {times} term times factor
| {div} term div factor
| {mod} term mod factor
;
factor = {number} number
| {id} id
| {expr} lparen expr rparen
;
compare = {cond} cond |
{gr}compare greater cond |
{gq}compare greateq cond |
{ls}compare less cond |
{lq}compare lesseq cond |
{eq}compare equal cond |
{not} not cond
;
cond = {expr} expr |
{and} cond and expr |
{or} cond or expr
;