CPGrammar Class Reference
Inherits from | NSObject |
Conforms to | NSCoding |
Declared in | CPGrammar.h |
Overview
The CPGrammar class represents a context free grammar. Grammars can be used later to construct parsers.
Tasks
Creating and Initialising a Grammar
-
+ grammarWithStart:rules:
Creates a grammar based on a starting non-terminal and a list of rules.
-
+ grammarWithStart:backusNaurForm:
Creates a grammar based on a starting non-terminal and some backus naur form.
-
+ grammarWithStart:backusNaurForm:error:
Creates a grammar based on a starting non-terminal and some backus naur form.
-
– initWithStart:rules:
Initialises a grammar based on a starting non-terminal and a list of rules.
-
– initWithStart:backusNaurForm:
Initialises a grammar based on a starting non-terminal and some backus naur form.
-
– initWithStart:backusNaurForm:error:
Initialises a grammar based on a starting non-terminal and some backus naur form.
Configuring a Grammar
-
– allRules
The set of rules in the grammar.
-
– addRule:
Adds a rule to the grammar.
Retreiving Grammar Rules
-
– allNonTerminalNames
All the non-terminals that the grammar can expand.
-
– rulesForNonTerminalWithName:
The rules relevant when attempting to match a non-terminal.
-
start
The starting symbol for the grammar.
property
Class Methods
grammarWithStart:backusNaurForm:
Creates a grammar based on a starting non-terminal and some backus naur form.
+ (id)grammarWithStart:(NSString *)start backusNaurForm:(NSString *)bnf
Parameters
- start
The non-terminal that all parses must reduce to.
- bnf
BNF for the grammar.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and some backus naur form.
see initWithStart:backusNaurForm: for a description of the syntax used for BNF.
Bug: Warning this method is deprecated, use grammarWithStart:backusNaurForm:error: instead.
Declared In
CPGrammar.h
grammarWithStart:backusNaurForm:error:
Creates a grammar based on a starting non-terminal and some backus naur form.
+ (id)grammarWithStart:(NSString *)start backusNaurForm:(NSString *)bnf error:(NSError **)error
Parameters
- start
The non-terminal that all parses must reduce to.
- bnf
BNF for the grammar.
- error
A pointer to an error object which will be filled if the method returns nil.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and some backus naur form.
see initWithStart:backusNaurForm: for a description of the syntax used for BNF.
See Also
Declared In
CPGrammar.h
grammarWithStart:rules:
Creates a grammar based on a starting non-terminal and a list of rules.
+ (id)grammarWithStart:(NSString *)start rules:(NSArray *)rules
Parameters
- start
The non-terminal that all parses must reduce to.
- rules
An array of CPRules to describe the grammar.
Return Value
Returns a CPGrammar based on the rules and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and a list of rules.
Declared In
CPGrammar.h
Instance Methods
addRule:
Adds a rule to the grammar.
- (void)addRule:(CPRule *)rule
Parameters
- rule
The rule to add.
Discussion
Adds a rule to the grammar.
Declared In
CPGrammar.h
allNonTerminalNames
All the non-terminals that the grammar can expand.
- (NSArray *)allNonTerminalNames
Return Value
An array of non-terminal names that are explained by the grammar.
Discussion
All the non-terminals that the grammar can expand.
Declared In
CPGrammar.h
allRules
The set of rules in the grammar.
- (NSSet *)allRules
Return Value
Returns the set of rules used to describe the grammar.
Discussion
The set of rules in the grammar.
Declared In
CPGrammar.h
initWithStart:backusNaurForm:
Initialises a grammar based on a starting non-terminal and some backus naur form.
- (id)initWithStart:(NSString *)start backusNaurForm:(NSString *)bnf
Parameters
- start
The non-terminal that all parses must reduce to.
- bnf
BNF for the grammar.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and some backus naur form.
The BNF is expressed using rules in the form nonTerminal ::= <subNonTerminal> "subTerminal" <subNonTerminal>;
. Rules may optionally be prefixed with a number indicating their tag.
This allows you to quickly construct grammars in a readable form.
You may also use EBNF to construct grammars. This allows you to use the symbols “*”, “+”, and “?” to indicate that a construction may appear 0 or more; 1 or more; and 0 or 1 times respectively.
You may also parenthesise subrules.
When you use any of the above EBNF constructs or parentheses, the parser will return the contents in an NSArray.
You may use tags to identify sections of rules to be extracted in your result classes or parser delegate. To do this, use the syntax nonTerminal ::= foo@<subNonTerminal> bar@"subTerminal";
. You may use multiple tags to identify the same section of a rule. For example range ::= min@'Number' '-' max@'Number' | min@'Number' '-' | '-' max@'Number' | min@max@'Number'
.
The grammar used for parsing the BNF can be expressed as follows:
ruleset ::= <rule>+; rule ::= "Number"? <unNumbered>; unNumbered ::= "Identifier" "::=" <rightHandSide> ";"; rightHandSide ::= (<sumset> "|")* <sumset>?; sumset ::= <taggedRightHandSideItem>+; taggedRightHandSideItem ::= ("Identifier" "@")? <rightHandSideItem>; rightHandSideItem ::= <unit> <repeatSymbol>?; unit ::= <gramarSymbol> | "(" <rightHandSide> ")"; repeatSymbol ::= "*" | "+" | "?"; grammarSymbol ::= <nonTerminal> | <terminal>; nonTerminal ::= "<" "Identifier" ">"; terminal ::= "String";
Bug: Warning this method is deprecated, use initWithStart:backusNaurForm:error: instead.
Declared In
CPGrammar.h
initWithStart:backusNaurForm:error:
Initialises a grammar based on a starting non-terminal and some backus naur form.
- (id)initWithStart:(NSString *)start backusNaurForm:(NSString *)bnf error:(NSError **)error
Parameters
- start
The non-terminal that all parses must reduce to.
- bnf
BNF for the grammar.
- error
A pointer to an error object which will be filled if the method returns nil.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and some backus naur form.
The BNF is expressed using rules in the form nonTerminal ::= <subNonTerminal> "subTerminal" <subNonTerminal>;
. Rules may optionally be prefixed with a number indicating their tag.
This allows you to quickly construct grammars in a readable form.
You may also use EBNF to construct grammars. This allows you to use the symbols “*”, “+”, and “?” to indicate that a construction may appear 0 or more; 1 or more; and 0 or 1 times respectively.
You may also parenthesise subrules.
When you use any of the above EBNF constructs or parentheses, the parser will return the contents in an NSArray.
You may use tags to identify sections of rules to be extracted in your result classes or parser delegate. To do this, use the syntax nonTerminal ::= foo@<subNonTerminal> bar@"subTerminal";
. You may use multiple tags to identify the same section of a rule. For example range ::= min@'Number' '-' max@'Number' | min@'Number' '-' | '-' max@'Number' | min@max@'Number'
.
The grammar used for parsing the BNF can be expressed as follows:
ruleset ::= <rule>+; rule ::= "Number"? <unNumbered>; unNumbered ::= "Identifier" "::=" <rightHandSide> ";"; rightHandSide ::= (<sumset> "|")* <sumset>?; sumset ::= <taggedRightHandSideItem>+; taggedRightHandSideItem ::= ("Identifier" "@")? <rightHandSideItem>; rightHandSideItem ::= <unit> <repeatSymbol>?; unit ::= <gramarSymbol> | "(" <rightHandSide> ")"; repeatSymbol ::= "*" | "+" | "?"; grammarSymbol ::= <nonTerminal> | <terminal>; nonTerminal ::= "<" "Identifier" ">"; terminal ::= "String";
Declared In
CPGrammar.h
initWithStart:rules:
Initialises a grammar based on a starting non-terminal and a list of rules.
- (id)initWithStart:(NSString *)start rules:(NSArray *)rules
Parameters
- start
The non-terminal that all parses must reduce to.
- rules
An array of CPRules to describe the grammar.
Return Value
Returns a CPGrammar based on the rules and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and a list of rules.
Declared In
CPGrammar.h
rulesForNonTerminalWithName:
The rules relevant when attempting to match a non-terminal.
- (NSArray *)rulesForNonTerminalWithName:(NSString *)nonTerminalName
Parameters
- nonTerminalName
The name of the non-terminal to find rules to match.
Return Value
Returns all rules that match a particular non-terminal.
Discussion
The rules relevant when attempting to match a non-terminal.
Declared In
CPGrammar.h