class Parser (View source)

Parser class.

Rules can be defined on the fly. Once finalized, a Parle\Lexer instance is required to deliver the token stream.

Constants

ACTION_ERROR

ACTION_SHIFT

ACTION_REDUCE

ACTION_GOTO

ACTION_ACCEPT

ERROR_SYNTAX

ERROR_NON_ASSOCIATIVE

ERROR_UNKNOWN_TOKEN

Properties

int $action
int $reduceId

Methods

void
advance()

Process next parser rule.

void
build()

Finalize the grammar rules

void
consume(string $data, Lexer $lexer)

Consume the data for parsing.

void
dump()

Dump the current grammar to stdout.

errorInfo()

Retrieve the error information in case Parle\Parser::action() returned the error action.

void
left(string $token)

Declare a terminal with left associativity.

void
nonassoc(string $token)

Declare a terminal, that cannot appear more than once in the row.

void
precedence(string $token)

Declares a precedence rule for a fictitious terminal symbol.

int
push(string $name, string $rule)

Push a grammar rule.

void
reset(int $tokenId)

Reset parser state using the given token id.

void
right(string $token)

Declare a token with right-associativity

string
sigil(int $idx)

Retrieve a part of the match by a rule.

void
token(string $token)

Declare a terminal to be used in the grammar.

int
tokenId(string $token)

Retrieve the id of the named token.

string
trace()

Retrieve the current parser operation description.

bool
validate(string $data, Lexer $lexer)

Validate an input string.

Details

void advance()

Process next parser rule.

Return Value

void

void build()

Finalize the grammar rules

Any tokens and grammar rules previously added are finalized. The rule set becomes readonly and the parser is ready to start.

Return Value

void

void consume(string $data, Lexer $lexer)

Consume the data for parsing.

Parameters

string $data

Data to be parsed.

Lexer $lexer

A lexer object containing the lexing rules prepared for the particular grammar.

Return Value

void

void dump()

Dump the current grammar to stdout.

Return Value

void

ErrorInfo errorInfo()

Retrieve the error information in case Parle\Parser::action() returned the error action.

Return Value

ErrorInfo

void left(string $token)

Declare a terminal with left associativity.

Parameters

string $token

Token name.

Return Value

void

void nonassoc(string $token)

Declare a terminal, that cannot appear more than once in the row.

Parameters

string $token

Token name.

Return Value

void

void precedence(string $token)

Declares a precedence rule for a fictitious terminal symbol.

This rule can be later used in the specific grammar rules.

Parameters

string $token

Return Value

void

int push(string $name, string $rule)

Push a grammar rule.

The production id returned can be used later in the parsing process to identify the rule matched.

Parameters

string $name

Rule name.

string $rule

The rule to be added. The syntax is Bison compatible.

Return Value

int

Returns integer representing the rule index.

void reset(int $tokenId)

Reset parser state using the given token id.

Parameters

int $tokenId

Token id.

Return Value

void

void right(string $token)

Declare a token with right-associativity

Parameters

string $token

Token name.

Return Value

void

string sigil(int $idx)

Retrieve a part of the match by a rule.

This method is equivalent to the pseudo variable functionality in Bison.

Parameters

int $idx

Match index, zero based.

Return Value

string

Returns a string with the matched part.

void token(string $token)

Declare a terminal to be used in the grammar.

Parameters

string $token

Token name.

Return Value

void

int tokenId(string $token)

Retrieve the id of the named token.

Parameters

string $token

Name of the token as used in Parle\Parser::token().

Return Value

int

Returns integer representing the token id.

See also

Parser::token

string trace()

Retrieve the current parser operation description.

This can be especially useful for studying the parser and to optimize the grammar.

Return Value

string

Returns a string with the trace information.

bool validate(string $data, Lexer $lexer)

Validate an input string.

The string is parsed internally, thus this method is useful for the quick input validation.

Parameters

string $data

String to be validated.

Lexer $lexer

A lexer object containing the lexing rules prepared for the particular grammar.

Return Value

bool

Returns boolean witnessing whether the input chimes or not with the defined rules.