class CLTK::Parser

Overview

The Parser class may be sub-classed to produce new parsers. These parsers have a lot of features, and are described in the main documentation.

Included Modules

Direct Known Subclasses

Defined in:

cltk/parser.cr
cltk/parser/actions.cr
cltk/parser/environment.cr
cltk/parser/parse_stack.cr
cltk/parser/parser.cr
cltk/parser/prod_proc.cr
cltk/parser/state.cr
cltk/parser/tupelize.cr:2
cltk/parser/tupelize.cr:34

Class Method Summary

Instance Method Summary

Macro Summary

Class methods inherited from module CLTK::Parser::Explain

explain(io : IO) explain

Instance methods inherited from class Object

in?(collection : Array | Set) in?

Class Method Detail

def self.add_state(state) #

If state (or its equivalent) is not in the state list it is added and it's ID is returned. If there is already a state with the same items as state in the state list its ID is returned and state is discarded.

@param [State] state State to add to the parser.

@return [Integer] The ID of the state.


[View source]
def self.build_list_production(symbol, list_elements, separator = "") #

Adds productions and actions for parsing empty lists.

@see CFG#empty_list_production


[View source]
def self.build_nonempty_list_production(symbol : String | Symbol, list_elements, separator = "") #

Adds productions and actions for parsing nonempty lists.

@see CFG#nonempty_list_production


[View source]
def self.c(expression, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _) #

[View source]
def self.check_reachability(start, dest, symbols) #

This method checks to see if the parser would be in parse state dest after starting in state start and reading symbols.

@param [Symbol] start Symbol representing a CFG production. @param [Symbol] dest Symbol representing a CFG production. @param [Array] symbols Grammar symbols.

@return [Boolean] If the destination symbol is reachable from the start symbol after reading symbols.


[View source]
def self.check_sanity #

This method is used to (surprise) check the sanity of the constructed parser. It checks to make sure all non-terminals used in the grammar definition appear on the left-hand side of one or more productions, and that none of the parser's states have invalid actions. If a problem is encountered a ParserConstructionException is raised.

@return [void]


[View source]
def self.clean #

Removes resources that were needed to generate the parser but aren't needed when actually parsing input.

@return [void]


[View source]
def self.dat(type) #

[View source]
def self.default_arg_type(type) #

Set the default argument type for the actions associated with clauses. All actions defined after this call will be passed arguments in the way specified here, unless overridden in the call to {Parser.clause}.

@param [:array, :splat] type The default argument type.

@return [void]


[View source]
def self.each_state(&) #

Iterate over the parser's states.

@yieldparam [State] state One of the parser automaton's state objects

@return [void]


[View source]
def self.env #

make these class attributes accessible from outside (for the Visitor to work on them)


[View source]
def self.finalize(opts : Opts = {explain: false, lookahead: true, precedence: true}) #

[View source]
def self.get_io(o, mode = "w") #

Converts an object into an IO object as appropriate.

@param [Object] o Object to be converted into an IO object. @param [String] mode String representing the mode to open the IO object in.

@return [IO, false] The IO object or false if a conversion wasn't possible.


[View source]
def self.grammar #

@return [CFG] The grammar that can be parsed by this Parser.


[View source]
def self.grammar_prime #

This method generates and memoizes the G' grammar used to calculate the LALR(1) lookahead sets. Information about this grammar and its use can be found in the following paper:

Simple Computation of LALR(1) Lookahead Sets Manuel E. Bermudez and George Logothetis Information Processing Letters 31 - 1989

@return [CFG]


[View source]
def self.inform_conflict(state_id, type, sym) #

Inform the parser core that a conflict has been detected.

@param [Integer] state_id ID of the state where the conflict was encountered. @param [:RR, :SR] type Reduce/Reduce or Shift/Reduce conflict. @param [Symbol] sym Symbol that caused the conflict.

@return [void]


[View source]
def self.left(*symbols) #

This method is used to specify that the symbols in symbols are left-associative. Subsequent calls to this method will give their arguments higher precedence.

@param [Array] symbols Symbols that are left associative.

@return [void]


[View source]
def self.lh_sides #

make these class attributes accessible from outside (for the Visitor to work on them)


[View source]
def self.list(symbol, list_elements, separator = "") #

[View source]
def self.nonassoc(*symbols) #

This method is used to specify that the symbols in symbols are non-associative.

@param [Array] symbols Symbols that are non-associative.

@return [void]


[View source]
def self.nonempty_list(symbol, list_elements, separator = "") #

[View source]
def self.p(symbol, expression = nil, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _) #

[View source]
def self.parse(tokens, opts : NamedTuple | Nil = nil) #

[View source]
def self.parse(tokens, opts = nil) #

This function is where actual parsing takes place. The tokens argument must be an array of Token objects, the last of which has type EOS. By default this method will return the value computed by the first successful parse tree found.

Additional information about the parsing options can be found in the main documentation.

@param [Array] tokens Tokens to be parsed. @param [Hash] opts Options to use when parsing input.

@option opts [:first, :all] :accept Either :first or :all. @option opts [Object] :env The environment in which to evaluate the production action. @option opts [Boolean,String,IO] :parse_tree To print parse trees in the DOT language or not. @option opts [Boolean,String,IO] :verbose To be verbose or not.

@return [Object, Array] Result or results of parsing the given tokens.


def self.procs #

make these class attributes accessible from outside (for the Visitor to work on them)


def self.prune(do_lookahead, do_precedence) #

This method uses lookahead sets and precedence information to resolve conflicts and remove unnecessary reduce actions.

@param [Boolean] do_lookahead Prune based on lookahead sets or not. @param [Boolean] do_precedence Prune based on precedence or not.

@return [void]


def self.right(*symbols) #

This method is used to specify that the symbols in symbols are right associative. Subsequent calls to this method will give their arguments higher precedence.

@param [Array] symbols Symbols that are right-associative.

@return [void]


def self.setenv(env) #

def self.start(symbol) #

Changes the starting symbol of the parser.

@param [Symbol] symbol The starting symbol of the grammar.

@return [void]


def self.states #

make these class attributes accessible from outside (for the Visitor to work on them)


def self.symbols #

make these class attributes accessible from outside (for the Visitor to work on them)


def self.token_hook(sym, &proc : Proc(Environment, Nil)) #

Add a hook that is executed whenever sym is seen.

The sym must be a terminal symbol.

@param [Symbol] sym Symbol to hook into @param [Proc] proc Code to execute when the block is seen

@return [void]


def self.token_hooks #

make these class attributes accessible from outside (for the Visitor to work on them)


def self.tupelize(name : Symbol) #

def self.tupelize(name : String = {{ @type.stringify }}) #

Instance Method Detail

def env #

@return [Environment] Environment used by the instantiated parser.


def parse(tokens) #

Parses the given token stream using the encapsulated environment.

@see .parse


Macro Detail

macro clause(expression, precedence = nil, arg_type = nil, &action) #

Declares a new clause inside of a production. The right-hand side is specified by expression and the precedence of this production can be changed by setting the precedence argument to some terminal symbol.

@param [String, Symbol] expression Right-hand side of a production. @param [Symbol] precedence Symbol representing the precedence of this production. @param [:array, :splat] arg_type Method to use when passing arguments to the action. @param [Proc] action Action to be taken when the production is reduced.

@return [void]


macro production(symbol, expression = nil, precedence = nil, arg_type = nil, &action) #

Adds a new production to the parser with a left-hand value of symbol. If expression is specified it is taken as the right-hand side of the production and action is associated with the production. If expression is nil then action is evaluated and expected to make one or more calls to Parser.clause. A precedence can be associate with this production by setting precedence to a terminal symbol.

@param [Symbol] symbol Left-hand side of the production. @param [String, Symbol, nil] expression Right-hand side of the production. @param [Symbol, nil] precedence Symbol representing the precedence of this produciton. @param [:array, :splat] arg_type Method to use when passing arguments to the action. @param [Proc] action Action associated with this production.

@return [void]