class CLTK::Parser
- CLTK::Parser
- Reference
- Object
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.crcltk/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
-
.add_state(state)
If state (or its equivalent) is not in the state list it is added and it's ID is returned.
-
.build_list_production(symbol, list_elements, separator = "")
Adds productions and actions for parsing empty lists.
-
.build_nonempty_list_production(symbol : String | Symbol, list_elements, separator = "")
Adds productions and actions for parsing nonempty lists.
- .c(expression, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _)
-
.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.
-
.check_sanity
This method is used to (surprise) check the sanity of the constructed parser.
-
.clean
Removes resources that were needed to generate the parser but aren't needed when actually parsing input.
- .dat(type)
-
.default_arg_type(type)
Set the default argument type for the actions associated with clauses.
-
.each_state(&)
Iterate over the parser's states.
-
.env
make these class attributes accessible from outside (for the Visitor to work on them)
- .finalize(opts : Opts = {explain: false, lookahead: true, precedence: true})
-
.get_io(o, mode = "w")
Converts an object into an IO object as appropriate.
-
.grammar
@return [CFG] The grammar that can be parsed by this Parser.
-
.grammar_prime
This method generates and memoizes the G' grammar used to calculate the LALR(1) lookahead sets.
-
.inform_conflict(state_id, type, sym)
Inform the parser core that a conflict has been detected.
-
.left(*symbols)
This method is used to specify that the symbols in symbols are left-associative.
-
.lh_sides
make these class attributes accessible from outside (for the Visitor to work on them)
- .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 = "")
- .p(symbol, expression = nil, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _)
- .parse(tokens, opts : NamedTuple | Nil = nil)
-
.parse(tokens, opts = nil)
This function is where actual parsing takes place.
-
.procs
make these class attributes accessible from outside (for the Visitor to work on them)
-
.prune(do_lookahead, do_precedence)
This method uses lookahead sets and precedence information to resolve conflicts and remove unnecessary reduce actions.
-
.right(*symbols)
This method is used to specify that the symbols in symbols are right associative.
- .setenv(env)
-
.start(symbol)
Changes the starting symbol of the parser.
-
.states
make these class attributes accessible from outside (for the Visitor to work on them)
-
.symbols
make these class attributes accessible from outside (for the Visitor to work on them)
-
.token_hook(sym, &proc : Proc(Environment, Nil))
Add a hook that is executed whenever sym is seen.
-
.token_hooks
make these class attributes accessible from outside (for the Visitor to work on them)
- .tupelize(name : Symbol)
- .tupelize(name : String = {{ @type.stringify }})
Instance Method Summary
-
#env
@return [Environment] Environment used by the instantiated parser.
-
#parse(tokens)
Parses the given token stream using the encapsulated environment.
Macro Summary
-
clause(expression, precedence = nil, arg_type = nil, &action)
Declares a new clause inside of a production.
-
production(symbol, expression = nil, precedence = nil, arg_type = nil, &action)
Adds a new production to the parser with a left-hand value of symbol.
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
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.
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
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
@return [Boolean] If the destination symbol is reachable from the start symbol after reading symbols.
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]
Removes resources that were needed to generate the parser but aren't needed when actually parsing input.
@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]
Iterate over the parser's states.
@yieldparam [State] state One of the parser automaton's state objects
@return [void]
make these class attributes accessible from outside (for the Visitor to work on them)
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.
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]
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]
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
@return [void]
make these class attributes accessible from outside (for the Visitor to work on them)
This method is used to specify that the symbols in symbols are non-associative.
@param [Array
@return [void]
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
@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