Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Syntax and Semantic Analysis

Tony Bargnesi edited this page Mar 9, 2017 · 7 revisions

Overview

bel_parser takes a layered approach to syntax and semantic analysis given a BEL specification.

At a glance here are the layers:

  • Execute all BEL parsers, parse input into Abstract Syntax Trees ("ASTs").
  • Filter for the ASTs that you are interested in.
  • Validate syntax and semantics of Term or Statement AST using BELParser::Language::ExpressionValidator.

Detail

Parsers

Each parser turn text input (usually a line ending with \n) into an AST.

Parsers are concerned with recognizing BEL expression structures. They don't care about the content beyond matching simple character patterns. Content is checked using syntax and semantic functions relative to the BEL specification.

There are three groups of parsers:

  • Common
    • Blank Line
    • Comment Line
    • Function
    • Identifer
    • String
    • List
  • Expression
    • Parameter
    • Term
    • Relationship
    • Observed Term (type of Statement)
    • Simple Statement
    • Nested Statement
    • Statement Comment
  • BEL Script
    • Define Annotation
    • Define Namespace
    • Set Document
    • Set
    • Unset

Note the following:

  • Parsers use composition to build larger structures (e.g. Parameter uses Identifier and String).
  • Multiple parsers can be executed on the input to determine the best match (see BELParser::ASTGenerator).
  • You can filter for an AST of interest using BELParser::ASTFilter wrapped around ASTGenerator.

Parsing for a specific node type

Screencast

Annotated code example:

require 'bel_parser'

# Generate all AST that can be recognized from the input.
ast_results =
  BELParser::ASTGenerator.new(
    "p(HGNC:AKT1) => bp(MESHPP:Apoptosis)\n"
  ).each.to_a

# Now let's parse and filter for just simple_statement AST node types.
statement_results =
  BELParser::ASTFilter.new(
    BELParser::ASTGenerator.new("p(HGNC:AKT1) => bp(MESHPP:Apoptosis)\n"),
    :simple_statement
  ).each.to_a
statement_ast = statement_results.last.last
puts statement_ast

#(simple-statement
#  (statement
#    (subject
#      (term
#        (function
#          (identifier "p"))
#        (argument
#          (parameter
#            (prefix
#              (identifier "HGNC"))
#            (value
#              (identifier "AKT1"))))))
#    (relationship "=>")
#    (object
#      (term
#        (function
#          (identifier "bp"))
#        (argument
#          (parameter
#            (prefix
#              (identifier "MESHPP"))
#            (value
#              (identifier "Apoptosis")))))) nil))
Clone this wiki locally