Skip to content

Advanced rules #3

Open
Open
@barsan-md

Description

@barsan-md

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.

val digits by token("\\d+")
val plus by token("+")
val minus
// val ...
val operator = plus or minus or ...
// this won't work because it will match addition before others
val operation: Parser<Tree> = digits and operator and digits
// it must match an operation that has the maximum value in the chain
val operation: Parser<Tree> = not(operator with higher precedence) and digits and operator and digits and not(operator with higher precedence)
// or you must associate the digits with the operator having higher precedence
val assignLeft: Parser<Tree> = (operator with higher precedence) and digits and (operator with lower precedence)
val rightAssign: Parser<Tree> = (operator with lower precedence) and digits and (operator with higher precedence)

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions