Skip to content

How to parse expression with an operator followed by two parameters #69

Answered by renggli
DXie123 asked this question in Q&A
Discussion options

You must be logged in to vote

To deal with nested grammars you need to recursively call yourself:

final frac = undefined();
final expr = frac | digit();
final arg = char('{') & expr & char('}');
frac.set(string('\\frac') & arg.star());

print(frac.parse('\\frac{\\frac{1}{2}}{3}')); 
// ==> Success[1:22]: [\frac, [[{, [\frac, [[{, 1, }], [{, 2, }]]], }], [{, 3, }]]]

This kind of recursive code is easier to write when using a GrammarDefinition.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #69 on December 08, 2020 19:56.