javascript - Ambiguous grammar when parsing boolean expressions with Peg.js -


i'm writing parser generates abstract syntax tree boolean expressions.

i have following peg.js grammar supports ^ , , & , | respectively:

start   = operation  // optional whitespace _  = [ \t\r\n]*  operation "operation"   = "("? _ left:(operand / operation) _ operator:operator _ right:(operand / operation) _  ")"?   {     return {       operation: operator,       between: [ left, right ]     };   }  operator "operator"   = operator:["&"|"|"]     {     return operator;   }  operand "operand"   = operand:[a-z]   {      return { operand: operand };   } 

it parses expressions a & b , a & (b | c), fails if expression starts operation:

(a | b) & c line 1, column 8: expected end of input " " found. 

the expression gets parsed correctly if surround parenthesis:

((a | b) & c) 

my guess peg.js taking (a | b) operation, instead of operand of parent operation, failing when seeing & c.

what missing?

your operation rule says brackets optional having 1 not enforce other there. e.g., (a & b parsed successfully.

you need break smaller parts. separate rules and , or allow operator precedence trick.

try this:

start   = sentence  sentence   = orsentence  orsentence   = lhs:andsentence __ '|' __ rhs:orsentence { return { operation: '|', between: [lhs, rhs] }; }   / andsentence  andsentence   = lhs:primarysentence __ '&' __ rhs:andsentence { return { operation: '&', between: [lhs, rhs] }; }   / primarysentence  primarysentence   = '(' _ sentence:sentence _ ')' { return sentence; }   / operand  operand   = operand:[a-z] { return { operand: operand }; }  _ "optionalwhitespace"   = whitespace *  __ "mandatorywhitespace"   = whitespace +  whitespace   = [ \t\n\r]+ 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -