Thursday, 15 April 2010

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


I am writing a parser which generates an abstract syntax tree with Boolean expression.

I have the following Peg.js grammar which supports ^ and , as and and < Code> | respectively

  start = operation // Alternate white location _ = [\ t \ r \ n] * operation "operation" = "("? _ Left: (operand / operation ) _ Operator: operator _ right: (operand / operation) _ ")"? {Return {operation: operator, middle: [left, right]}; } Operator "operator" = operator: ["& amp; | "|"] {Return operator; } Operand "Operand" = Operand: [A-z] {Return {Operand: Operand}; }  

This is successfully expressed in expressions such as a & amp; B and a and amp; (Bc) , although it fails if the expression starts with an operation:

  (A | B) & amp; C line 1, column 8: the expected end of the input is but found.  

Expression is read correctly if I surround it with brackets:

  (a | b) & c)  

My guess is that Peg.js is only taking (a | b) as an operation, instead of the operand of parent operation < Code> & amp; C .

What am I missing?

Your operation rule states that the brackets are optional but the one is not applied to the other when there is one . For example, (a & amp; b is parsed.)

You need to break it in small pieces. Allow the operator to give priority to your move. For more or different rules.

Try it:

  start = sentence sentence = or centence or centenus = LHS: and the centenary __ '|' __ rhs: or can {Return {Operation: '|', Between: [LHS, RAS]};} / and Sentence and Status = LHS: Primary Sentence __ '& amp;' __ CRS and Sentence {Return {Operation: 'End', Beach: [LHS, RAS]}} / Primary Centence Primitive = ('_ Sentence: Sentence _') '{Return Sentence;} / Operand Operand = Operand: [Ed] {Return {Operand: Operand};} _ "Alternative Whitespace" = Whitespace * __ "Mandatory Whitespace" = Whitespace + Whitespace = [\ t \ n \ r] +  

No comments:

Post a Comment