Self-management PetitParser PPCompositeParsers

I have a grammar of a programming language that I would like to explode in several subclasses of PPCompositeParser (for example, one class will process instructions, another class will process expressions, another class with the structure of the handle structure). I want to do this to avoid getting a large class with dozens of instance variables.

My problem is that these subgrams have a cyclical dependence: the structural grammar refers to the statement statement rule of the operator grammar, which refers to the expression rule of the expression grammar of the expression, which refers to the "subroutine name" structural grammar (closing the dependent word). I tried a simple approach to have, for example, the #subroutineName method in the grammar of an expression that looks like this:

MyExpressionGrammar>>subroutineName ^ N2TJStructureParser newStartingAt: #subroutineName 

but this fails during initialization due to infinite recursion (obviously).

To solve this problem, I created PPDeferedParser:

 PPParser subclass: #PPDeferedParser instanceVariableNames: 'creationBlock' classVariableNames: '' poolDictionaries: '' category: 'PetitParser-Tools' PPDeferedParser>>parseOn: aStream ^ creationBlock value parseOn: aStream 

which makes the previous #subroutineName look like this:

 MyExpressionGrammar>>subroutineName ^ PPDederedParser creationBlock: [N2TJStructureParser newStartingAt: #subroutineName] 

This seems to work, but I wonder if there is another solution.

+4
source share
1 answer

Currently, splitting a composite parser into several subclasses of PPCompositeParser is not directly supported by PetitParser.

Keep in mind that if you use the PetitParser browser, you do not need to worry about instance variables, they are automatically managed for you. In addition, an instance variable is not necessary for each product. For example, terminals may be in methods that you call directly.

You probably also work, but this is not very pleasant, because it requires you to pay close attention to how you want to connect your grammars. Also, in your implementation, you should lazily cache the result, because otherwise your code will create new compound parsers during parsing. It is very expensive.

All this aside, it may be possible to improve PPCompositeParser to support dependencies between several subclasses, for example, by declaring dependent other parsers that the constructor will prepare, initialize, and ultimately resolve.

+3
source

Source: https://habr.com/ru/post/1469021/


All Articles