Sokmesa Khiev
Sokmesa Khiev

Reputation: 2920

PEG grammar to parse optional content

Can anyone tell me how to write syntax on Treetop that supports both:

system u AAA1 car=5, motor=4

and

system u car=5, motor=4

Upvotes: 0

Views: 321

Answers (1)

Phrogz
Phrogz

Reputation: 303224

You want to use an "Optional Expression" (see about 60% down on this page):

grammar Sokmesa
  rule line
    "system" wsp+ marker wsp+ (category wsp+)? attributelist
  end
  rule category
    "AAA1" # Or whatever
  end
  # Add rules for 'marker', 'wsp', and 'attributelist' here
end

Upvotes: 2

Related Questions