Chapter 1 - Building the basic parser combinators
In this post, we shall build some basic parsers to understand the functioning of parser combinators.
If you haven’t installed the hammer-parser ruby gem, please go back to the previous post and install it first.
- Fire your
irb
and include the hammer-parser gem.
require 'hammer-parser'
Basic hammer methods
-
The hammer class
Hammer::Parser
has all the following methods that we need to make use of in our journey. We will see how the class is used in the individual examples below. -
ch()
- This parser would be used to recognize a single character. For example,
@hammer = Hammer::Parser
parser = @hammer.ch('h')
The above parser ‘parser’ would be used to recognize the character ‘h’.
ch_change()
- This parser would be used to have characters in specific ranges.
@hammer = Hammer::Parser
parser = @hammer.ch_range('a','z')
The above parser would be used to recognize a single character in the range a-z.
many()
- This parser would be used for 0 or more occurences of the parser included as an argument.
@hammer = Hammer::Parser
character = @hammer.ch_range('a','z')
parser = @hammer.many(character)
-
many1()
- Same asmany()
but includes 1 or more occurences instead of 0 or more. -
end_p()
- To denote the end of the parser.
sequence()
token()
optional()
middle()
choice()
not()
int_range()
middle()
Recognizing the input with the help of the parsers
We make use of the same parser as in the example of many1()
.
@hammer = Hammer::Parser
character = @hammer.ch_range('a','z')
parser = @hammer.many1(character)
if !parser.parse("abcd").nil?
true
else
false
end
If the input is recognized, then the parse()
method would return an object of HParseResult
. If the input is not recognized, then nil
is returned.