parsing - How to change code using Scala Parser Combinators to take operator precedence into account? -


consider part of grammar:

  def expression = simpleexpression ~ opt(relation ~ simpleexpression)   def relation = "=" | "#" | "<=" | "<" | ">=" | ">" | "in" | "is"   def simpleexpression = opt("+" | "-") ~ rep1sep (term, addoperator)   def addoperator = "+" | "-" | "or"   def term = factor ~ rep(muloperator ~ factor)   def muloperator = "*" | "/" | "div" | "mod" | "&"   def factor: parser[any] = number | "(" ~ expression ~ ")" | "~" ~ factor 

is necessary rewrite parts of create new rules, or there method (like | vs. ||| first vs. longest rule matching) i'm missing necessary thing?

operator precedence natural result of way rules written. example, in grammar simpleexpression composed of addition, subtraction , logical-or of term, , term composed of multiplication, division, modulus , logical-and of factor.

so if have this:

1 + 2 * 3 

you'll following (roughly speaking, clarity):

list(1, (2 ~ list(* ~ 3))) 

and if have this:

1 * 2 + 3 

you'll (roughly speaking):

list((1 ~ list(* ~ 2)), 3) 

you lose addition operators because of rep1sep -- separators discarded.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -