Permalink
Browse files

Performance improvements to constructing LALR1 parsers.

  • Loading branch information...
1 parent c0f37ab commit 096d50397f21671d2bc4885e4d04e0bfdad18acf @beelsebob beelsebob committed Oct 28, 2011
View
4 CoreParse/Grammar/CPRule.m
@@ -131,15 +131,15 @@ - (NSString *)description
- (NSUInteger)hash
{
- return [self tag] << 16 + [[self name] hash];
+ return [name hash] << 16 + [self tag] ;
}
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:[CPRule class]])
{
CPRule *other = (CPRule *)object;
- return [other tag] == [self tag] && [[other name] isEqualToString:[self name]] && [[other rightHandSideElements] isEqualToArray:[self rightHandSideElements]];
+ return [other tag] == tag && [[other name] isEqualToString:name] && [[other rightHandSideElements] isEqualToArray:rightHandSide];
}
return NO;
View
4 CoreParse/Parsers/CPShiftReduceParsers/CPItem.m
@@ -81,14 +81,14 @@ - (BOOL)isEqual:(id)object
if ([object isKindOfClass:[CPItem class]])
{
CPItem *other = (CPItem *)object;
- return [other rule] == [self rule] && [other position] == [self position];
+ return [other position] == position && [other rule] == rule;
}
return NO;
}
- (NSUInteger)hash
{
- return [[self rule] hash] + position;
+ return ((NSUInteger)rule << 4) + position;
}
- (NSString *)description
View
30 CoreParse/Parsers/CPShiftReduceParsers/CPLALR1Parser.m
@@ -66,10 +66,17 @@ - (BOOL)constructShiftReduceTables
{
NSSet *g = [aug lr0GotoKernelWithItems:itemsSet symbol:next];
NSSet *lr0G = [g map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }];
- NSUInteger ix = [kernels indexOfObjectPassingTest:^ BOOL (NSSet *lr1Kernel, NSUInteger indx, BOOL *stop)
- {
- return [[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G];
- }];
+ NSUInteger indx = 0;
+ NSUInteger ix = NSNotFound;
+ for (NSSet *lr1Kernel in kernels)
+ {
+ if ([[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G])
+ {
+ ix = indx;
+ break;
+ }
+ indx++;
+ }
BOOL success = [[self actionTable] setAction:[CPShiftReduceAction shiftAction:ix] forState:idx name:[next name]];
if (!success)
{
@@ -82,10 +89,17 @@ - (BOOL)constructShiftReduceTables
{
NSSet *g = [aug lr0GotoKernelWithItems:itemsSet symbol:[CPGrammarSymbol nonTerminalWithName:nonTerminalName]];
NSSet *lr0G = [g map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }];
- NSUInteger gotoIndex = [kernels indexOfObjectPassingTest:^ BOOL (NSSet *lr1Kernel, NSUInteger indx, BOOL *stop)
- {
- return [[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G];
- }];
+ NSUInteger indx = 0;
+ NSUInteger gotoIndex = NSNotFound;
+ for (NSSet *lr1Kernel in kernels)
+ {
+ if ([[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G])
+ {
+ gotoIndex = indx;
+ break;
+ }
+ indx++;
+ }
BOOL success = [[self gotoTable] setGoto:gotoIndex forState:idx nonTerminalNamed:nonTerminalName];
if (!success)
{
View
37 CoreParseTests/CoreParseTests.m
@@ -29,10 +29,8 @@ @implementation CoreParseTests
CPTokeniser *mapCssTokeniser;
}
-- (void)setUp
+- (void)setUpMapCSS
{
- [super setUp];
-
NSCharacterSet *identifierCharacters = [NSCharacterSet characterSetWithCharactersInString:
@"abcdefghijklmnopqrstuvwxyz"
@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -81,15 +79,15 @@ - (void)setUp
[mapCssTokeniser setDelegate:[[[CPTestMapCSSTokenisingDelegate alloc] init] autorelease]];
mapCssInput = @"node[highway=\"trunk\"]"
- @"{"
- @" line-width: 5.0;"
- @" label: jam;"
- @"} // Zomg boobs!\n"
- @"/* Haha, fooled you */"
- @"way relation[type=\"multipolygon\"]"
- @"{"
- @" line-width: 0.0;"
- @"}";
+ @"{"
+ @" line-width: 5.0;"
+ @" label: jam;"
+ @"} // Zomg boobs!\n"
+ @"/* Haha, fooled you */"
+ @"way relation[type=\"multipolygon\"]"
+ @"{"
+ @" line-width: 0.0;"
+ @"}";
CPGrammar *grammar = [CPGrammar grammarWithStart:@"ruleset"
backusNaurForm:
@@ -116,12 +114,10 @@ - (void)setUp
mapCssParser = [[CPLALR1Parser alloc] initWithGrammar:grammar];
}
-- (void)tearDown
+- (void)tearDownMapCSS
{
[mapCssParser release];
[mapCssTokeniser release];
-
- [super tearDown];
}
- (void)testKeywordTokeniser
@@ -319,6 +315,7 @@ - (void)testQuotedTokeniser
- (void)testMapCSSTokenisation
{
+ [self setUpMapCSS];
if (![[mapCssTokeniser tokenise:mapCssInput] isEqualTo:[CPTokenStream tokenStreamWithTokens:[NSArray arrayWithObjects:
[CPKeywordToken tokenWithKeyword:@"node"],
[CPKeywordToken tokenWithKeyword:@"["],
@@ -355,6 +352,7 @@ - (void)testMapCSSTokenisation
{
STFail(@"Tokenisation of MapCSS failed", nil);
}
+ [self tearDownMapCSS];
}
- (void)testSLR
@@ -566,16 +564,20 @@ - (void)testBNFGrammarGeneration
- (void)testMapCSSParsing
{
+ sleep(20.0);
+ [self setUpMapCSS];
CPSyntaxTree *tree = [mapCssParser parse:[mapCssTokeniser tokenise:mapCssInput]];
if (nil == tree)
{
STFail(@"Failed to parse MapCSS", nil);
}
+ [self tearDownMapCSS];
}
- (void)testParallelParsing
{
+ [self setUpMapCSS];
CPTokenStream *stream = [[[CPTokenStream alloc] init] autorelease];
[NSThread detachNewThreadSelector:@selector(runMapCSSTokeniser:) toTarget:self withObject:stream];
CPSyntaxTree *tree1 = [mapCssParser parse:stream];
@@ -585,6 +587,7 @@ - (void)testParallelParsing
{
STFail(@"Parallel parse of MapCSS failed", nil);
}
+ [self tearDownMapCSS];
}
- (void)testParseResultParsing
@@ -708,14 +711,16 @@ - (void)testEBNF
- (void)testEncodingAndDecodingOfParsers
{
+ [self setUpMapCSS];
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:mapCssParser];
CPParser *mapCssParser2 = [NSKeyedUnarchiver unarchiveObjectWithData:d];
CPSyntaxTree *tree = [mapCssParser2 parse:[mapCssTokeniser tokenise:mapCssInput]];
if (nil == tree)
{
- STFail(@"Failed to parse MapCSS", nil);
+ STFail(@"Failed to encode and decode MapCSSParser", nil);
}
+ [self tearDownMapCSS];
}
@end

0 comments on commit 096d503

Please sign in to comment.