Xarpeg Kotlin PEG Parser
Online Parser Sample
Enter mathematical expressions in the input field below and click "Evaluate" to see the results. This interactive demo lets you test arithmetic operations, variables, lambda functions, and more. Try the examples on the right to get started, or write your own expressions!
Tip: Press Ctrl+Enter (or Cmd+Enter on Mac) to evaluate
The parser evaluates expressions with arithmetic operations, comparison operators, ternary conditionals, variables, lambda expressions, and function calls. Stack traces are shown for runtime errors.
- Enter expressions using
+,-,*,/, and parentheses. - Compare values:
5 > 3,10 == 10,x != y,a <= b - Use ternary operator:
condition ? valueIfTrue : valueIfFalse - Assign variables:
x = 10 - Define lambdas:
add = (a, b) -> a + b - Call functions:
add(5, 3) - Identifiers can use letters, digits, and underscore (but cannot start with a digit).
- Comparison operators return boolean values (
true/false) with strict type checking. - See the evaluated result or read error messages with stack traces.
This grammar includes arithmetic operations, comparison operators, ternary conditionals, variables, lambda expressions, and function calls.
Expression <- _ Assignment _
Assignment <- (Identifier _ '=' _ Assignment) / Ternary
Ternary <- (Equality _ '?' _ Equality _ ':' _ Equality) / Equality
Equality <- Ordering (_ ('==' / '!=') _ Ordering)*
Ordering <- Sum (_ ('<=' / '>=' / '<' / '>') _ Sum)*
Sum <- Product (_ ('+' / '-') _ Product)*
Product <- Factor (_ ('*' / '/') _ Factor)*
Factor <- Lambda / FunctionCall / Variable / Number / '(' _ Expression _ ')'
Lambda <- '(' _ (Identifier _ (',' _ Identifier _)*)? ')' _ '->' _ Expression
FunctionCall <- Identifier _ '(' _ (Expression _ (',' _ Expression _)*)? ')'
Variable <- Identifier
Identifier <- [a-zA-Z_][a-zA-Z0-9_]*
Number <- [0-9]+ ('.' [0-9]+)?
_ <- [ \t\r\n]*