Replies: 2 comments
-
I tried to have something like $.RULE("topLevelExpression", (i, n) => {
let array = [ ]
while (i < n) {
$.SUBRULE($.additionExpression)
i++
}
return array
}) It doesn't work because |
Beta Was this translation helpful? Give feedback.
-
Hello @ekal. My opinion is that once that computation become less than trivial I would prefer to build an intermediate data structure during parsing and perform the computations afterwards instead of during the parsing. The code snippet you included above seems to indicate you are mixing those up and using an embedded actions parser to attempt to compute the result "on the fly" I suggest you try build an in memory tree structure representing the calculation (left operand, right operand, operator) This should simplify your problem as in the first step you would only have to collect the operands and operators. |
Beta Was this translation helpful? Give feedback.
-
I am building a calculator with Chevrotain parser and I have played with the calculator example
Chevrotain Playground: https://chevrotain.io/playground/
Parser Grammar: Calculator embedded semantics
Input Sample: Parenthesis precedence
The example above uses integer as input/output. I would like to support integers AND arrays.
Basic example:
2 * ( 3 + 7 )
Example with arrays:
2 * ( {{array1}} + {{array2}} )
I know how to lex and parse
{{array}}
but I don't know how to visit them or loop over the arrays to achieve the following resultEnd result of the parsing of
2 * ( {{array1}} + {{array2}} )
should be
[22, 44, 66, 88, 110]
Once the AST has been created (including the arrays on some leaf node), how can I reduce it to the end result which is an array.
One approach is to have some sort of state and run the visitor as many time as
array.length
. But it is unclear to me how it can be implemented with ChevretainBeta Was this translation helpful? Give feedback.
All reactions