Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time



BracketNotation is a parser for generating syntax trees from sentences annotated with the kind of bracket notation that is commonly used by linguists. The result is a tree structure with nodes that describe the phrases and constituents of the sentence.

BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree, and small portions of his code have been incorporated in the parser.


Cody Brimhall (


Copyright © 2010-2011 Cody Brimhall


Distributed under the terms of the GNU General Public License, v. 3

Using the Parser

To use the BracketNotation parser, simply initialize a Parser instance with the string you want to parse, and call the #parse method. Parser performs some basic validation before attempting to evaluate the string. If validation fails, Parser raises a ValidationError. If the evaluator encounters a syntax error in the string, it raises an EvaluationError.

require 'bracket_notation'
include BracketNotation

input = "[S [NP colorless green ideas] [VP [V sleep] [Adv furiously]]]"

  parser =
  tree = parser.parse

  puts tree.pretty_print # => S
                         #    -- NP
                         #    ---- colorless
                         #    ---- green
                         #    ---- ideas
                         #    -- VP
                         #    ---- V
                         #    ------ sleep
                         #    ---- Adv
                         #    ------ furiously
rescue ValidationError
  puts "Validation failed: #{$!}"
rescue EvaluationError
  puts "Evaluation failed: #{$!}"

Using the Geometry Classes

The geometry classes are a set of simple constructions to make working with layouts in a cartesian plane a little easier. The three classes are Point, Size and Rect; they are immutable, and their use is straightforward:

require 'bracket_notation'
include BracketNotation::Geometry

point1 =, 0)                   # => {x: 0, y: 0}
point2 = point1.point_by_adding_to_x(42)   # => {x: 42, y: 0}
size1 =, 0)                     # => {width: 0, height: 0}
size2 = size1.size_by_adding_to_height(42) # => {width: 0, height: 42}
rect1 =, size2)            # => {origin: {x: 0, y: 0}, size: {width: 0, height: 42}}
rect2 =, 42, 42, 42)           # => {origin: {x: 42, y: 42}, size: {width: 42, height: 42}}

Using the View Classes

In order to make it easier to represent a tree visually, BracketNotation includes some basic view classes: Node (and its subclasses Branch and Leaf) and Tree. These classes implement a basic n-ary tree, with methods and attributes for laying the nodes out on a cartesian plane and tracking their locations and dimensions. See the documentation for Tree and Node for details.

Bugs, Feature Requests, Et Cetera

If you have any bugs, feature requests, or glowing praise, you can find this project on GitHub.


Generate a representation of a syntax tree using a string of bracket notation.




No releases published


No packages published


You can’t perform that action at this time.