Sha256: 2908e3c8d594c5c9f01a0bedeb7fbd3c9ab87d9a87e1187d9e9318bc6bde5b5a

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# Purpose: to demonstrate how to build and render a parse tree for JSON
# language
require 'rley'  # Load the gem
require_relative 'json_lexer'

# Steps to render a parse tree (of a valid parsed input):
# 1. Define a grammar
# 2. Create a parser for that grammar
# 3. Tokenize the input
# 4. Let the parser process the input
# 5. Generate a parse tree from the parse result
# 6. Render the parse tree (in JSON)

########################################
# Step 1. Load a grammar for JSON
require_relative 'JSON_grammar'

# A JSON parser derived from our general Earley parser.
class JSONParser < Rley::Parser::GFGEarleyParser
  attr_reader(:source_file)

  # Constructor
  def initialize()
    # Builder the Earley parser with the JSON grammar
    super(GrammarJSON)
  end
  
  def parse_file(aFilename)
    tokens = tokenize_file(aFilename)
    result = parse(tokens)

    return result
  end
  
  private
  
  def tokenize_file(aFilename)
    input_source = nil
    File.open(aFilename, 'r') { |f| input_source = f.read }

    lexer = JSONLexer.new(input_source, GrammarJSON)
    return lexer.tokens
  end
end # class

# End of file

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rley-0.4.03 examples/data_formats/JSON/JSON_parser.rb
rley-0.4.02 examples/data_formats/JSON/JSON_parser.rb
rley-0.4.01 examples/data_formats/JSON/JSON_parser.rb
rley-0.4.00 examples/data_formats/JSON/JSON_parser.rb
rley-0.3.12 examples/data_formats/JSON/JSON_parser.rb