I have defined simple grammar for parsing string and number using Treetop as below.
grammar Simple
rule value
number / string
end
rule string
word space string
/
word
end
rule word
[0-9a-zA-Z]+
end
rule number
[1-9] [0-9]*
end
rule space
' '+
end
end
Ruby:
parser = SimpleParser.new
parser.parse('123abc wer') # => nil
I expect the parser to return string node but look like the parser could not understand the input. Any idea would be appreciated.
In Treetop (and PEGs in general, actually) the choice operator is ordered, unlike most other parsing formalisms.
So, in
you are telling Treetop that you prefer
numberoverstring.Your input starts with
1, which matches bothnumberandstring(throughword), but you told Treetop to prefer thenumberinterpretation, so it parses it as anumber. When it comes to theain the input, it has no more rules to apply, and thus it returns nothing (nil), because in Treetop it is an error to not consume the entire input stream.If you simply reverse the order of the choice, the entire input will interpreted as a
stringinstead of anumber:Or, you could keep the order as it is, but allow the
valuerule to be matched multiple times. Either insert a new top-level rule like this:or modify the
valuerule like this:Which will give you an AST roughly like this: