Parse::FSM::Driver - Run-time engine for Parse::FSM parser
use MyParser; # isa Parse::FSM::Driver $parser = MyParser->new; $parser->input( \&lexer ); $parser->user( $user_pointer ); $result = $parser->parse( $start_rule ); $result = $parser->parse_start_rule; $token = $parser->peek_token; $token = $parser->get_token; $parser->unget_token(@tokens);
This module implements a deterministic top-down parser based on a pre-computed Finite State Machine (FSM).
The FSM is generated by Parse::FSM, by reading a BNF-type grammar file and generating a run-time module that includes the state tables. The module also include the run-time parsing routine that follows the state tables to obtain a parse of the input.
This module is not intended to be used stand alone. It is used as a base class by the modules generated by Parse::FSM.
Creates a new object.
Get/set of the parser user pointer. The user pointer is not used by the parser, and is available for communication between the parser actions and the calling module.
It can for example point to a data structure that describes the objects already identified in the parse.
Get/set the parser input lexer iterator. The iterator is a code reference of a function that returns the next token to be parsed as an array ref, with token type and token value [$type, $value]. It returns undef on end of input. E.g. for a simple expression lexer:
[$type, $value]
undef
sub make_lexer { my($line) = @_; return sub { for ($line) { /\G\s+/gc; return [NUM => $1] if /\G(\d+)/gc; return [NAME => $1] if /\G([a-z]\w*)/gci; return [$1 => $1] if /\G(.)/gc; return; } }; } $parser->input(make_lexer("2+3*4"));
Returns the next token to be retrieved by the lexer, but keeps it in the input queue. Can be used by a rule action to decide based on the input that follows.
Extracts the next token from the lexer stream. Can be used by a rule action to discard the following tokens.
Pushes back the given list of tokens to the lexer input stream, to be retrieved on the next calls to get_token.
get_token
This function receives an optional start rule name, and uses the default rule of the grammar if not supplied.
It parses the input stream, leaving the stream at the first unparsed token, and returns the parse value - the result of the action function for the start rule.
The function dies with an error message indicating the input that cannot be parsed in case of a parse error.
For each rule XXX in the grammar, Parse::FSM creates a corresponding parse_XXX to start the parse at that rule. This is a short-cut to parse('XXX').
XXX
parse_XXX
parse('XXX')
See Parse::FSM
To install Parse::FSM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::FSM
CPAN shell
perl -MCPAN -e shell install Parse::FSM
For more information on module installation, please visit the detailed CPAN module installation guide.