Question

Problem:
Don't know how to Translate the Contents of an Antlr4 ".g4" Grammar File to match the Grammar for ParseKit.


Specifically:
How to Translate -> Channel(HIDDEN); from Antlr4 Grammar to ParseKit Grammar.


Context:
I'm creating a .Framework in Objective-C/Xcode for iOS. I need to Generate a Parser that mimics the basic functionality of an Antlr4 Generated Parser in Java. The way to go about designing the functionality of the Parser isn't a problem; I am interested in learning more about the Grammar Syntax for ParseKit.


Grammar Code:

Antlr3:
WHITESPACE : [ \t\r\n]+ {$channel=HIDDEN;}

Antlr4:
WHITESPACE : [ \t\r\n]+ -> channel(HIDDEN);

ParseKit:
whitespace = /[\s\t\r\n]+/ __________________; How to ignore???


Questions:
- Does ParseKit support Channels?
- What is the Grammar that ParseKit uses to send to a Hidden Channel?
- Alternatively is there anything similar in nature to Channels that would work in ParseKit?

Was it helpful?

Solution

Creator of ParseKit here. ParseKit does not have a Channels feature, nor does it have a -> tree rewrite feature as ANTLR does. (In general, ParseKit is not as fully-featured as ANTLR).

However, you can probably achieve the effect you are looking for using ParseKit's discard feature.

When designing your ParseKit grammar, there are often tokens in your language which are pure syntax. These tokens are not important for computing a result nor for building an abstract syntax tree (for further processing). You'll usually want to automatically discard these tokens using the ParseKit ! directive.

For example, say you have a rule in your ParseKit grammar called primaryExpr like:

primaryExpr = atom
            | '(' expr ')'!
            ;

Notice the ! after the close parenthesis ')'. This is the discard directive which instructs the ParseKit tokenizer to discard the ) token from the token stream reported to your ParseKit assembler (delegate) callbacks.

So in your -parser:didMatchPrimaryExpr: callback, the assembly parameter's stack will not contain the ) token. The ) token will have already been discarded. Discarding is desirable in this case, as the ) is pure syntax which is not important for processing the relevant parenthesized expression.

So to discard an unimportant token in your grammar, place a ! directive immediately after it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top