Skip to content

Relevance of refX? #130

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

You must be logged in to vote

You are right, in the example above you do not need ref0, because there are no cycles in your grammar.

Consider the following example that parses balanced parenthesis:

class ParensGrammar extends GrammarDefinition {
  Parser start() => char('(') & ref0(start) & char(')') 
                  | epsilon();
}

If you replaced ref0(start) with start() you would end up with an infinite recursion when building the grammar. The ref0 functionality breaks such cycles apart and makes sure that repeatedly referenced parsers are only instantiated once (see documentation).

Replies: 1 comment

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 #126 on February 12, 2022 11:04.