The LangSec Journey by Prashant Anantharaman and Dr. Sergey Bratus
Oct 24, 2016 • 2 min read

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.

require 'hammer-parser'

Basic hammer methods

@hammer = Hammer::Parser
parser = @hammer.ch('h')

The above parser ‘parser’ would be used to recognize the character ‘h’.

@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.

@hammer = Hammer::Parser
character = @hammer.ch_range('a','z')
parser = @hammer.many(character)

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.