Permalink
Browse files

Added support for EBNF grammars.

  • Loading branch information...
1 parent 773a846 commit a73a8f68a12cc60635188edaf63d5b8d54ed2153 @beelsebob beelsebob committed Oct 23, 2011
@@ -77,6 +77,8 @@
1F9F83A213B731F3006E939D /* Expression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F9F83A013B731F3006E939D /* Expression.m */; };
1F9F83A613B732AC006E939D /* Term.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F9F83A413B732AC006E939D /* Term.h */; };
1F9F83A713B732AC006E939D /* Term.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F9F83A513B732AC006E939D /* Term.m */; };
+ 1F9F83AC13B7CABA006E939D /* CPRHSItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F9F83AA13B7CAB9006E939D /* CPRHSItem.h */; };
+ 1F9F83AD13B7CABA006E939D /* CPRHSItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F9F83AB13B7CABA006E939D /* CPRHSItem.m */; };
1FB3EB24132BA02C00ACC453 /* CPTestEvaluatorDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FB3EB22132BA02C00ACC453 /* CPTestEvaluatorDelegate.h */; };
1FB3EB25132BA02C00ACC453 /* CPTestEvaluatorDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FB3EB23132BA02C00ACC453 /* CPTestEvaluatorDelegate.m */; };
1FB3EB2C132BB2F200ACC453 /* CPLR1Parser.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FB3EB2A132BB2E600ACC453 /* CPLR1Parser.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -89,6 +91,8 @@
1FB81323132FEDAF0095982D /* CPTestWhiteSpaceIgnoringDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FB81321132FEDAF0095982D /* CPTestWhiteSpaceIgnoringDelegate.m */; };
1FB81326132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FB81324132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.h */; };
1FB81327132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FB81325132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.m */; };
+ 1FC00D5014544EDC00DC8D35 /* CPRHSItemResult.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FC00D4E14544EDC00DC8D35 /* CPRHSItemResult.h */; };
+ 1FC00D5114544EDC00DC8D35 /* CPRHSItemResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FC00D4F14544EDC00DC8D35 /* CPRHSItemResult.m */; };
1FC1827D139ADC800027F597 /* CPGrammarPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FC1827B139ADC800027F597 /* CPGrammarPrivate.h */; };
1FC1827E139ADC800027F597 /* CPGrammarPrivate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FC1827C139ADC800027F597 /* CPGrammarPrivate.m */; };
1FC18282139AE3810027F597 /* CPGrammarInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FC18280139AE3800027F597 /* CPGrammarInternal.h */; };
@@ -184,6 +188,8 @@
1F9F83A013B731F3006E939D /* Expression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Expression.m; sourceTree = "<group>"; };
1F9F83A413B732AC006E939D /* Term.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Term.h; sourceTree = "<group>"; };
1F9F83A513B732AC006E939D /* Term.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Term.m; sourceTree = "<group>"; };
+ 1F9F83AA13B7CAB9006E939D /* CPRHSItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPRHSItem.h; sourceTree = "<group>"; };
+ 1F9F83AB13B7CABA006E939D /* CPRHSItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPRHSItem.m; sourceTree = "<group>"; };
1FB3EB22132BA02C00ACC453 /* CPTestEvaluatorDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPTestEvaluatorDelegate.h; sourceTree = "<group>"; };
1FB3EB23132BA02C00ACC453 /* CPTestEvaluatorDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPTestEvaluatorDelegate.m; sourceTree = "<group>"; };
1FB3EB2A132BB2E600ACC453 /* CPLR1Parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPLR1Parser.h; sourceTree = "<group>"; };
@@ -196,6 +202,8 @@
1FB81321132FEDAF0095982D /* CPTestWhiteSpaceIgnoringDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPTestWhiteSpaceIgnoringDelegate.m; sourceTree = "<group>"; };
1FB81324132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPTestMapCSSTokenisingDelegate.h; sourceTree = "<group>"; };
1FB81325132FF16E0095982D /* CPTestMapCSSTokenisingDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPTestMapCSSTokenisingDelegate.m; sourceTree = "<group>"; };
+ 1FC00D4E14544EDC00DC8D35 /* CPRHSItemResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPRHSItemResult.h; sourceTree = "<group>"; };
+ 1FC00D4F14544EDC00DC8D35 /* CPRHSItemResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPRHSItemResult.m; sourceTree = "<group>"; };
1FC1827B139ADC800027F597 /* CPGrammarPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPGrammarPrivate.h; sourceTree = "<group>"; };
1FC1827C139ADC800027F597 /* CPGrammarPrivate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPGrammarPrivate.m; sourceTree = "<group>"; };
1FC18280139AE3800027F597 /* CPGrammarInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPGrammarInternal.h; sourceTree = "<group>"; };
@@ -425,6 +433,10 @@
1F530B901322813F00F52EB5 /* CPRule.m */,
1FB3EB38132D096700ACC453 /* CPGrammarSymbol.h */,
1FB3EB39132D096800ACC453 /* CPGrammarSymbol.m */,
+ 1F9F83AA13B7CAB9006E939D /* CPRHSItem.h */,
+ 1F9F83AB13B7CABA006E939D /* CPRHSItem.m */,
+ 1FC00D4E14544EDC00DC8D35 /* CPRHSItemResult.h */,
+ 1FC00D4F14544EDC00DC8D35 /* CPRHSItemResult.m */,
);
path = Grammar;
sourceTree = "<group>";
@@ -504,6 +516,8 @@
1FC18282139AE3810027F597 /* CPGrammarInternal.h in Headers */,
1F9F83A113B731F3006E939D /* Expression.h in Headers */,
1F9F83A613B732AC006E939D /* Term.h in Headers */,
+ 1F9F83AC13B7CABA006E939D /* CPRHSItem.h in Headers */,
+ 1FC00D5014544EDC00DC8D35 /* CPRHSItemResult.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -568,6 +582,7 @@
1F0E88EC130462F300537D04 /* Project object */ = {
isa = PBXProject;
attributes = {
+ LastUpgradeCheck = 0420;
ORGANIZATIONNAME = "In The Beginning...";
};
buildConfigurationList = 1F0E88EF130462F300537D04 /* Build configuration list for PBXProject "CoreParse" */;
@@ -683,6 +698,8 @@
1FC18283139AE3810027F597 /* CPGrammarInternal.m in Sources */,
1F9F83A213B731F3006E939D /* Expression.m in Sources */,
1F9F83A713B732AC006E939D /* Term.m in Sources */,
+ 1F9F83AD13B7CABA006E939D /* CPRHSItem.m in Sources */,
+ 1FC00D5114544EDC00DC8D35 /* CPRHSItemResult.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -8,6 +8,7 @@
#import "CPGrammar.h"
#import "CPGrammarPrivate.h"
+#import "CPGrammarInternal.h"
#import "CPTokeniser.h"
#import "CPTokenStream.h"
@@ -17,14 +18,16 @@
#import "CPWhiteSpaceToken.h"
#import "CPQuotedRecogniser.h"
#import "CPIdentifierRecogniser.h"
-#import "CPSLRParser.h"
+#import "CPLALR1Parser.h"
#import "CPIdentifierToken.h"
#import "CPQuotedToken.h"
#import "CPNumberToken.h"
#import "CPItem.h"
#import "CPLR1Item.h"
+#import "CPRHSItem.h"
+
#import "NSSetFunctional.h"
#import <objc/runtime.h>
@@ -94,11 +97,49 @@ - (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree
case 9:
return [NSMutableArray arrayWithObject:[children objectAtIndex:0]];
case 10:
- case 11:
return [children objectAtIndex:0];
+ case 11:
+ {
+ CPRHSItem *i = [[[CPRHSItem alloc] init] autorelease];
+ [i setContents:[NSArray arrayWithObject:[children objectAtIndex:0]]];
+ NSString *symbol = [(CPKeywordToken *)[children objectAtIndex:1] keyword];
+ if ([symbol isEqualToString:@"*"])
+ {
+ [i setRepeats:YES];
+ [i setMayNotExist:YES];
+ }
+ else if ([symbol isEqualToString:@"+"])
+ {
+ [i setRepeats:YES];
+ [i setMayNotExist:NO];
+ }
+ else
+ {
+ [i setRepeats:NO];
+ [i setMayNotExist:YES];
+ }
+ return i;
+ }
case 12:
- return [CPGrammarSymbol nonTerminalWithName:[(CPIdentifierToken *)[children objectAtIndex:1] identifier]];
+ return [children objectAtIndex:0];
case 13:
+ {
+ CPRHSItem *i = [[[CPRHSItem alloc] init] autorelease];
+ [i setContents:[children objectAtIndex:1]];
+ [i setRepeats:NO];
+ [i setMayNotExist:NO];
+ return i;
+ }
+ case 14:
+ case 15:
+ case 16:
+ case 17:
+ case 18:
+ return [children objectAtIndex:0];
+ return [children objectAtIndex:0];
+ case 19:
+ return [CPGrammarSymbol nonTerminalWithName:[(CPIdentifierToken *)[children objectAtIndex:1] identifier]];
+ case 20:
return [CPGrammarSymbol terminalWithName:[(CPQuotedToken *)[children objectAtIndex:0] content]];
default:
return syntaxTree;
@@ -156,6 +197,11 @@ - (id)initWithStart:(NSString *)initStart backusNaurForm:(NSString *)bnf
[tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"::="]];
[tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"<"]];
[tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@">"]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"("]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@")"]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"*"]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"+"]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"?"]];
[tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"|"]];
[tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@";"]];
[tokeniser addTokenRecogniser:[CPNumberRecogniser integerRecogniser]];
@@ -177,22 +223,33 @@ - (id)initWithStart:(NSString *)initStart backusNaurForm:(NSString *)bnf
CPRule *rightHandSide2 = [CPRule ruleWithName:@"rightHandSide" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"rightHandSide"], [CPGrammarSymbol terminalWithName:@"|"], nil] tag:6];
CPRule *rightHandSide3 = [CPRule ruleWithName:@"rightHandSide" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"sumset"], nil] tag:7];
- CPRule *sumset1 = [CPRule ruleWithName:@"sumset" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"sumset"], [CPGrammarSymbol nonTerminalWithName:@"grammarSymbol"], nil] tag:8];
- CPRule *sumset2 = [CPRule ruleWithName:@"sumset" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"grammarSymbol"], nil] tag:9];
+ CPRule *sumset1 = [CPRule ruleWithName:@"sumset" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"sumset"], [CPGrammarSymbol nonTerminalWithName:@"rightHandSideItem"], nil] tag:8];
+ CPRule *sumset2 = [CPRule ruleWithName:@"sumset" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"rightHandSideItem"], nil] tag:9];
+
+ CPRule *rightHandSideItem1 = [CPRule ruleWithName:@"rightHandSideItem" rightHandSideElements:[NSArray arrayWithObject:[CPGrammarSymbol nonTerminalWithName:@"unit"]] tag:10];
+ CPRule *rightHandSideItem2 = [CPRule ruleWithName:@"rightHandSideItem" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"unit"], [CPGrammarSymbol nonTerminalWithName:@"repeatSymbol"], nil] tag:11];
+
+ CPRule *unit1 = [CPRule ruleWithName:@"unit" rightHandSideElements:[NSArray arrayWithObject:[CPGrammarSymbol nonTerminalWithName:@"grammarSymbol"]] tag:12];
+ CPRule *unit2 = [CPRule ruleWithName:@"unit" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol terminalWithName:@"("], [CPGrammarSymbol nonTerminalWithName:@"sumset"], [CPGrammarSymbol terminalWithName:@")"], nil] tag:13];
+
+ CPRule *repeatSymbol1 = [CPRule ruleWithName:@"repeatSymbol" rightHandSideElements:[NSArray arrayWithObject:[CPGrammarSymbol terminalWithName:@"*"]] tag:14];
+ CPRule *repeatSymbol2 = [CPRule ruleWithName:@"repeatSymbol" rightHandSideElements:[NSArray arrayWithObject:[CPGrammarSymbol terminalWithName:@"+"]] tag:15];
+ CPRule *repeatSymbol3 = [CPRule ruleWithName:@"repeatSymbol" rightHandSideElements:[NSArray arrayWithObject:[CPGrammarSymbol terminalWithName:@"?"]] tag:16];
- CPRule *grammarSymbol1 = [CPRule ruleWithName:@"grammarSymbol" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"nonterminal"], nil] tag:10];
- CPRule *grammarSymbol2 = [CPRule ruleWithName:@"grammarSymbol" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"terminal"], nil] tag:11];
+ CPRule *grammarSymbol1 = [CPRule ruleWithName:@"grammarSymbol" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"nonterminal"], nil] tag:17];
+ CPRule *grammarSymbol2 = [CPRule ruleWithName:@"grammarSymbol" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol nonTerminalWithName:@"terminal"], nil] tag:18];
- CPRule *nonterminal = [CPRule ruleWithName:@"nonterminal" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol terminalWithName:@"<"], [CPGrammarSymbol terminalWithName:@"Identifier"], [CPGrammarSymbol terminalWithName:@">"], nil] tag:12];
+ CPRule *nonterminal = [CPRule ruleWithName:@"nonterminal" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol terminalWithName:@"<"], [CPGrammarSymbol terminalWithName:@"Identifier"], [CPGrammarSymbol terminalWithName:@">"], nil] tag:19];
- CPRule *terminal = [CPRule ruleWithName:@"terminal" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol terminalWithName:@"String"], nil] tag:13];
+ CPRule *terminal = [CPRule ruleWithName:@"terminal" rightHandSideElements:[NSArray arrayWithObjects:[CPGrammarSymbol terminalWithName:@"String"], nil] tag:20];
- CPGrammar *bnfGrammar = [CPGrammar grammarWithStart:@"ruleset" rules:[NSArray arrayWithObjects:ruleset1, ruleset2, rule1, rule2, unNumbered, rightHandSide1, rightHandSide2, rightHandSide3, sumset1, sumset2, grammarSymbol1, grammarSymbol2, nonterminal, terminal, nil]];
- CPParser *parser = [CPSLRParser parserWithGrammar:bnfGrammar];
+ CPGrammar *bnfGrammar = [CPGrammar grammarWithStart:@"ruleset" rules:[NSArray arrayWithObjects:ruleset1, ruleset2, rule1, rule2, unNumbered, rightHandSide1, rightHandSide2, rightHandSide3, sumset1, sumset2, rightHandSideItem1, rightHandSideItem2, unit1, unit2, repeatSymbol1, repeatSymbol2, repeatSymbol3, grammarSymbol1, grammarSymbol2, nonterminal, terminal, nil]];
+ CPParser *parser = [CPLALR1Parser parserWithGrammar:bnfGrammar];
[parser setDelegate:del];
- NSArray *initRules = [parser parse:tokenStream];
- return [self initWithStart:initStart rules:initRules];
+ NSMutableArray *initRules = [parser parse:tokenStream];
+
+ return [self initWithStart:initStart rules:[self tidyRightHandSides:initRules]];
}
- (id)init
@@ -27,5 +27,11 @@
- (NSSet *)first:(NSArray *)obj;
- (NSString *)uniqueSymbolNameBasedOnName:(NSString *)name;
+- (NSString *)symbolNameNotInSet:(NSSet *)symbols basedOnName:(NSString *)name;
+
+- (NSArray *)tidyRightHandSides:(NSArray *)oldRules;
+- (NSSet *)collectRHSElementsForNewRules:(NSArray *)rightHandSide;
+- (NSDictionary *)nameNewRules:(NSSet *)rhsElements withRules:(NSArray *)oldRules;
+- (NSArray *)addRHSRules:(NSDictionary *)rules toRules:(NSArray *)oldRules;
@end
Oops, something went wrong.

0 comments on commit a73a8f6

Please sign in to comment.