我正在尝试匹配此
f(some_thing) == 'something else'
所以布尔表达式应该是
expression operator expression
问题是我无法弄清楚如何在没有左递归的情况下做到这一点 这些是我的规则
expression
=
bool_expression
/ function_call
/ string
/ real_number
/ integer
/ identifier
bool_expression
= l:expression space* op:bool_operator space* r:expression
{ return ... }
使用语法符号,我有
O := ==|<=|>=|<|>|!= // operators
E := B|.... // expression, many non terminals
B := EOE
因为我的语法是EOE,我不知道如何使用左手算法
A := Ab|B
transforms into
A := BA'
A':= e|bA
e为空且b为终端
答案 0 :(得分:5)
这样的事情应该这样做:
expression
= bool_expression
bool_expression
= add_expression "==" bool_expression
/ add_expression "!=" bool_expression
/ add_expression
add_expression
= mult_expression "+" add_expression
/ mult_expression "-" add_expression
/ mult_expression
mult_expression
= atom "*" mult_expression
/ atom "/" mult_expression
/ atom
atom
= function_call
/ string
/ real_number
/ integer
/ identifier
function_call
= identifier "(" (expression ("," expression)*)? ")"
string
= "'" [^']* "'"
identifier
= [a-zA-Z_]+
integer
= [0-9]+
real_number
= integer "." integer?
/ "." integer