class CLTK::Parsers::PrefixCalc

Overview

A parser for a simple prefix calculator.

Defined in:

cltk/parsers/prefix_calc.cr

Constant Summary

CLAUSES = [{lhs: :e, expression: "NUM", precedence: nil, arg_type: :splat, cb: 0}, {lhs: :e, expression: "PLS e e", precedence: nil, arg_type: :splat, cb: 1}, {lhs: :e, expression: "SUB e e", precedence: nil, arg_type: :splat, cb: 2}, {lhs: :e, expression: "MUL e e", precedence: nil, arg_type: :splat, cb: 3}, {lhs: :e, expression: "DIV e e", precedence: nil, arg_type: :splat, cb: 4}] of CLAUSE | LIST_PROD

A parser for a simple prefix calculator.

DEFAULT_ARG_TYPE = [:splat]

A parser for a simple prefix calculator.

PREC_COUNT = {:left => 0, :right => 0, :non => 0}

A parser for a simple prefix calculator.

PROCS = [] of ProdProc

A parser for a simple prefix calculator.

TOKEN_PRECS = {} of String => Tuple(String, Int32)

A parser for a simple prefix calculator.

Class Method Summary

Macro Summary

Instance methods inherited from class CLTK::Parser

env env, parse(tokens) parse

Class methods inherited from class CLTK::Parser

_parse(procs, lh_sides, symbols, states, token_hooks, tokens, opts : NamedTuple? = nil) _parse, add_state(state) add_state, c(expression, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _) c, crystalize(name : Symbol | String = self.name) crystalize, each_state(&block) each_state, env env, grammar grammar, grammar_prime grammar_prime, inform_conflict(state_id, type, sym) inform_conflict, lh_sides lh_sides, p(symbol, expression = nil, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _) p, parse(tokens, opts = nil)
parse(tokens : Array, opts : NamedTuple? = nil)
parse
, procs procs, setenv(env) setenv, start(symbol) start, states states, symbols symbols, token_hook(sym, &proc : Proc(Environment, Nil)) token_hook, token_hooks token_hooks

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.build_list_production_(symbol, list_elements, separator, empty) #

Adds productions and actions for parsing (non)?empty lists.

@see CFG#(non)?empty_list_production


[View source]
def self.build_up_productions #

A parser for a simple prefix calculator.


[View source]
def self.finalize(explain = false, lookahead = true, precedence = true, use = nil) #

This method will finalize the parser causing the construction of states and their actions, and the resolution of conflicts using lookahead and precedence information.

No calls to {Parser.production} may appear after the call to Parser.finalize.

@param [Hash] opts Options describing how to finalize the parser.

@option opts [Boolean,String,IO] :explain To explain the parser or not. @option opts [Boolean] :lookahead To use lookahead info for conflict resolution. @option opts [Boolean] :precedence To use precedence info for conflict resolution. @option opts [String,IO] :use A file name or object that is used to load/save the parser.

@return [void]


[View source]
def self.finalize_from_serialized_parser(path) #

A parser for a simple prefix calculator.


[View source]
def self.new #

Instantiates a new parser and creates an environment to be used for subsequent calls.


[View source]
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]


[View source]
def self.serialize_to_file(path) #

A parser for a simple prefix calculator.


[View source]
def self.to_parser #

A parser for a simple prefix calculator.


[View source]

Macro Detail

macro build_list_production(symbol, list_elements, separator = "") #

Adds productions and actions for parsing empty lists.

@see CFG#empty_list_production


[View source]
macro build_nonempty_list_production(symbol, list_elements, separator = "") #

Adds productions and actions for parsing nonempty lists.

@see CFG#nonempty_list_production


[View source]
macro clause(expression, precedence = nil, arg_type = nil) #

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]


[View source]
macro 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]
macro 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<Symbol>] symbols Symbols that are left associative.

@return [void]


[View source]
macro list(symbol, list_elements, separator = "") #

[View source]
macro nonassoc(*symbols) #

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

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

@return [void]


[View source]
macro nonempty_list(symbol, list_elements, separator = "") #

[View source]
macro prec(direction, *symbols) #

[View source]
macro production(lhs, expression = nil, precedence = nil, arg_type = nil) #

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]


[View source]
macro 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<Symbol>] symbols Symbols that are right-associative.

@return [void]


[View source]