Open barsan-md opened 7 years ago
@barsan-ds I'm not quite sure I understand your use case. Is it similar to the operator table in jparsec
? If not, could you please provide a more concrete example with operators and maybe the input that should be parsed?
From what I could understand from the example, that seems to be the case. Does anything like that exist in better-parse?
Not yet. There's no built-in features that allow defining operator rules like this. Though I considered adding something like that (and especially with support for left recursion removal), and I might add this among other features in future.
You can define combinators in your project according to your needs, and I'll be grateful if you come up with an example implementation of the operator table.
hey there, this sounds like a nice feature and I prototyped a lightweight solution for this. With it the ArithmeticEvaluator could be simplified to this:
// ... tokens as usual
val number by num use { text.toInt() }
val term: Parser<Int> by number or
(skip(minus) and parser(::term) map { -it }) or
(skip(lpar) and parser(::rootParser) and skip(rpar))
// subSumchain, divMulChain, powChain can be merged to this:
override val rootParser: Parser<Int> by operatorTable(term)
.infixl(pow) { a, _, b -> a.toDouble().pow(b.toDouble()).toInt() } // highest priority first
.infixl(div or mul use {type }) { a, op, b -> if (op == div) a / b else a * b }
.infixl(plus or minus use { type }) { a, op, b -> if (op == plus) a + b else a - b } // lowest priority last
the operator table provides these helpers:
infixl
leftAssociative infix operationinfixr
rightAssociative infix operationprefix
prefix operationpostfix
postfix operationI can create a PullRequest if you think this might be a good fit for your lib
I wonder if there is it possible to implement advanced rules, something like if(condition), then(this rule can be applied).
I'm new to both kotlin and parser combinators so maybe my understanding of the problem is completely wrong.
As a concrete example what I want to do is very similar to the aritmetic evaluator example: https://github.com/h0tk3y/better-parse/blob/master/demo/src/main/kotlin/com/example/ArithmeticsEvaluator.kt except I have many different levels of precedece on operators (not just 3: sum, sub < mul, div < pow). (Small note here: exponentiation is right associative, not left (a^b^c = a^(b^c))) Suppose, instead of collectiong the same level operations into the same rule (like the example), a general single rule is the only way possible, to be more general and open to the addition of new operator.
Is it possible or does this approach make sense? PS: this library is awesome and the infix notation makes the rules very readable when compared with other libraries and languages. Good job, thank you!