class CLTK::Parsers::InfixCalc
Overview
A parser for a simple infix calculator.
Defined in:
cltk/parsers/infix_calc.crConstant Summary
-
CLAUSES =
[{lhs: :e, expression: "NUM", precedence: nil, arg_type: :splat, cb: 0}, {lhs: :e, expression: "LPAREN e RPAREN", precedence: nil, arg_type: :splat, cb: 1}, {lhs: :e, expression: "e PLS e", precedence: nil, arg_type: :splat, cb: 2}, {lhs: :e, expression: "e SUB e", precedence: nil, arg_type: :splat, cb: 3}, {lhs: :e, expression: "e MUL e", precedence: nil, arg_type: :splat, cb: 4}, {lhs: :e, expression: "e DIV e", precedence: nil, arg_type: :splat, cb: 5}] of CLAUSE | LIST_PROD
-
A parser for a simple infix calculator.
-
DEFAULT_ARG_TYPE =
[:splat]
-
A parser for a simple infix calculator.
-
PREC_COUNT =
{:left => 1, :right => 1, :non => 0}
-
A parser for a simple infix calculator.
-
PROCS =
[] of ProdProc
-
A parser for a simple infix calculator.
-
TOKEN_PRECS =
{"PLS" => {"left", 1}, "SUB" => {"left", 1}, "MUL" => {"right", 1}, "DIV" => {"right", 1}} of String => Tuple(String, Int32)
-
A parser for a simple infix calculator.
Class Method Summary
-
.build_list_production_(symbol, list_elements, separator, empty)
Adds productions and actions for parsing (non)?empty lists.
-
.build_up_productions
A parser for a simple infix calculator.
-
.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.
-
.finalize_from_serialized_parser(path)
A parser for a simple infix calculator.
-
.new
Instantiates a new parser and creates an environment to be used for subsequent calls.
-
.prune(do_lookahead, do_precedence)
This method uses lookahead sets and precedence information to resolve conflicts and remove unnecessary reduce actions.
-
.serialize_to_file(path)
A parser for a simple infix calculator.
-
.to_parser
A parser for a simple infix calculator.
Macro Summary
-
build_list_production(symbol, list_elements, separator = "")
Adds productions and actions for parsing empty lists.
-
build_nonempty_list_production(symbol, list_elements, separator = "")
Adds productions and actions for parsing nonempty lists.
-
clause(expression, precedence = nil, arg_type = nil)
Declares a new clause inside of a production.
-
default_arg_type(type)
Set the default argument type for the actions associated with clauses.
-
left(*symbols)
This method is used to specify that the symbols in symbols are left-associative.
- list(symbol, list_elements, separator = "")
-
nonassoc(*symbols)
This method is used to specify that the symbols in symbols are non-associative.
- nonempty_list(symbol, list_elements, separator = "")
- prec(direction, *symbols)
-
production(lhs, expression = nil, precedence = nil, arg_type = nil)
Adds a new production to the parser with a left-hand value of symbol.
-
right(*symbols)
This method is used to specify that the symbols in symbols are right associative.
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
Adds productions and actions for parsing (non)?empty lists.
@see CFG#(non)?empty_list_production
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]
Instantiates a new parser and creates an environment to be used for subsequent calls.
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]
Macro Detail
Adds productions and actions for parsing empty lists.
@see CFG#empty_list_production
Adds productions and actions for parsing nonempty lists.
@see CFG#nonempty_list_production
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]
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]
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]
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]
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]
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]