Getting the separation that you want is easiest if you break up the start
rule. Here's an example (without writing COMMA
s to the AST):
start : prepphrase //one prepphrase is required.
(COMMA! prepphrase)* //"COMMA!" means "match a COMMA but don't write it to the AST"
;
prepphrase: noun1=n //You can use "noun1=n" instead of "noun1+=n" when you're only using it to store one value
(prep noun2=n)?
-> ^(NOUN $noun1) ^(PREP prep)? ^(NOUN $noun2)?
;
A prepphrase
is a noun that may be followed by a preposition with another noun. The start
rule looks for comma-separated prepphrase
s.
The output appears like the parse tree image, but without the commas.
If you prefer explicitly writing out ASTs with ->
or if you don't like syntax like COMMA!
, you can write the start
rule like this instead. The two different forms are functionally equivalent.
start : prepphrase //one prepphrase is required.
(COMMA prepphrase)*
-> prepphrase+ //write each prepphrase, which doesn't include commas
;