Parse
From ESEwiki
←Older revision | Newer revision→
The Parse library is dedicated to providing a high quality parser with minimum code to write. In fact the user almost has one thing only to write: the grammar. The classes are written in such a way that even that is simplified, leveraging efficiently the SmartEiffel "manifest notation".
The parser is a simple LL(n) (i.e. top-down) parser. The library only manages the parsing; everything else is left to the client:
- the form of the tokens (they must inherit from PARSER_IMAGE which is an empty class);
- the actions to perform at each production reduction (typically creating an abstract syntactic tree).
For an example, see the tutorial.
Also note that yepp uses this library for its own internal source parsing (making it a good study example), and to implement its core functionality (indeed YEPP is an Eiffel Parser Producer).
PARSE_NODE
The parse nodes represent the grammar. They can be decorated with agents to be called when the node is reduced.
Non-terminal nodes are kept in a tree that reduces the backtracking (because common prefixes are kept common).
Terminal nodes use an agent to shift the token. Such an agent is given the MINI_PARSER_BUFFER source and is expected to return either Void if the token cannot be shifted, or a PARSER_IMAGE if the token was successfully shifted.
PARSER_IMAGE is a deferred empty class. Its actual implementation must be provided and will be used by the provided agents.
ESE_PARSER
ESE_PARSER is the main class to use.
Its parse function accepts
- a PARSE_TABLE which represents the grammar;
- a MINI_PARSER_BUFFER as the source to be parsed;
- an array of actions to be performed if the source is successfully parsed. Those actions are the decorated agents of the grammar (see above). For non-terminal nodes, the actions are simple no-arg agents; for terminal nodes, the actions accept a PARSER_IMAGE built by the (also provided) shift action.
The parse function returns an error message, a STRING, that is Void if the source was successfully parsed.

