Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Significant optimisation to LALR(1) parser generation.

  • Loading branch information...
commit 0ba5868bfd9312bc96dabfcbba366222bc86885f 1 parent ccf4d1e
@beelsebob authored
View
16 CoreParse.xcodeproj/project.pbxproj
@@ -128,6 +128,10 @@
1F9F83A613B732AC006E939D /* Term.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F9F83A413B732AC006E939D /* Term.h */; };
1F9F83AC13B7CABA006E939D /* CPRHSItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F9F83AA13B7CAB9006E939D /* CPRHSItem.h */; };
1F9F83AD13B7CABA006E939D /* CPRHSItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F9F83AB13B7CABA006E939D /* CPRHSItem.m */; };
+ 1FA6430215E2B9790004FCD3 /* NSObject+IsCoreParseObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA6430015E2B9790004FCD3 /* NSObject+IsCoreParseObject.h */; };
+ 1FA6430315E2B9790004FCD3 /* NSObject+IsCoreParseObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6430115E2B9790004FCD3 /* NSObject+IsCoreParseObject.m */; };
+ 1FA6430715E2C5130004FCD3 /* NSArray+Functional.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA6430515E2C5130004FCD3 /* NSArray+Functional.h */; };
+ 1FA6430815E2C5130004FCD3 /* NSArray+Functional.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA6430615E2C5130004FCD3 /* NSArray+Functional.m */; };
1FA68DA014DE98C4005519B9 /* CPErrorToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA68D9E14DE98C4005519B9 /* CPErrorToken.h */; settings = {ATTRIBUTES = (Public, ); }; };
1FA68DA114DE98C4005519B9 /* CPErrorToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA68D9F14DE98C4005519B9 /* CPErrorToken.m */; };
1FA68DA514DE9D3D005519B9 /* CPTestErrorHandlingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA68DA314DE9D3D005519B9 /* CPTestErrorHandlingDelegate.h */; };
@@ -270,6 +274,10 @@
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>"; };
+ 1FA6430015E2B9790004FCD3 /* NSObject+IsCoreParseObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+IsCoreParseObject.h"; sourceTree = "<group>"; };
+ 1FA6430115E2B9790004FCD3 /* NSObject+IsCoreParseObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+IsCoreParseObject.m"; sourceTree = "<group>"; };
+ 1FA6430515E2C5130004FCD3 /* NSArray+Functional.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Functional.h"; sourceTree = "<group>"; };
+ 1FA6430615E2C5130004FCD3 /* NSArray+Functional.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Functional.m"; sourceTree = "<group>"; };
1FA68D9E14DE98C4005519B9 /* CPErrorToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPErrorToken.h; sourceTree = "<group>"; };
1FA68D9F14DE98C4005519B9 /* CPErrorToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPErrorToken.m; sourceTree = "<group>"; };
1FA68DA314DE9D3D005519B9 /* CPTestErrorHandlingDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPTestErrorHandlingDelegate.h; sourceTree = "<group>"; };
@@ -400,6 +408,10 @@
DA2B1DF41566B311002FDBD7 /* CPSenTestKitAssertions.h */,
1F38820B132432A1000C8876 /* NSSetFunctional.h */,
1F38820C132432AA000C8876 /* NSSetFunctional.m */,
+ 1FA6430515E2C5130004FCD3 /* NSArray+Functional.h */,
+ 1FA6430615E2C5130004FCD3 /* NSArray+Functional.m */,
+ 1FA6430015E2B9790004FCD3 /* NSObject+IsCoreParseObject.h */,
+ 1FA6430115E2B9790004FCD3 /* NSObject+IsCoreParseObject.m */,
);
path = CoreParse;
sourceTree = "<group>";
@@ -657,6 +669,8 @@
1F893A2314DEF40D00316FF7 /* CPTestErrorEvaluatorDelegate.h in Headers */,
1FA866B715DFAEBC005350EE /* CPRule+Internal.h in Headers */,
1FA866C015E18F68005350EE /* CPRHSItem+Private.h in Headers */,
+ 1FA6430215E2B9790004FCD3 /* NSObject+IsCoreParseObject.h in Headers */,
+ 1FA6430715E2C5130004FCD3 /* NSArray+Functional.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -906,6 +920,8 @@
1FA68DA614DE9D3D005519B9 /* CPTestErrorHandlingDelegate.m in Sources */,
1F893A2014DEEBFC00316FF7 /* CPRecoveryAction.m in Sources */,
1F893A2414DEF40D00316FF7 /* CPTestErrorEvaluatorDelegate.m in Sources */,
+ 1FA6430315E2B9790004FCD3 /* NSObject+IsCoreParseObject.m in Sources */,
+ 1FA6430815E2C5130004FCD3 /* NSArray+Functional.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
15 CoreParse/NSArray+Functional.h
@@ -0,0 +1,15 @@
+//
+// NSArray+Functional.h
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSArray (Functional)
+
+- (NSArray *)map:(id(^)(id obj))block;
+
+@end
View
34 CoreParse/NSArray+Functional.m
@@ -0,0 +1,34 @@
+//
+// NSArray+Functional.m
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "NSArray+Functional.h"
+
+@implementation NSArray (Functional)
+
+- (NSArray *)map:(id(^)(id obj))block
+{
+ NSUInteger c = [self count];
+ id *resultingObjects = malloc(c * sizeof(id));
+
+ NSUInteger nonNilCount = 0;
+ for (id obj in self)
+ {
+ id r = block(obj);
+ if (nil != r)
+ {
+ resultingObjects[nonNilCount] = r;
+ nonNilCount++;
+ }
+ }
+
+ NSArray *a = [NSArray arrayWithObjects:resultingObjects count:nonNilCount];
+ free(resultingObjects);
+ return a;
+}
+
+@end
View
15 CoreParse/NSObject+IsCoreParseObject.h
@@ -0,0 +1,15 @@
+//
+// NSObject+IsCoreParseObject.h
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSObject (IsCoreParseObject)
+
+- (BOOL)isCPItem;
+
+@end
View
18 CoreParse/NSObject+IsCoreParseObject.m
@@ -0,0 +1,18 @@
+//
+// NSObject+IsCoreParseObject.m
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "NSObject+IsCoreParseObject.h"
+
+@implementation NSObject (IsCoreParseObject)
+
+- (BOOL)isCPItem
+{
+ return NO;
+}
+
+@end
View
11 CoreParse/NSSetFunctional.m
@@ -13,18 +13,23 @@ @implementation NSSet(Functional)
- (NSSet *)map:(id(^)(id obj))block
{
- NSMutableSet *newSet = [NSMutableSet setWithCapacity:[self count]];
+ NSUInteger c = [self count];
+ id *resultingObjects = malloc(c * sizeof(id));
+ NSUInteger nonNilCount = 0;
for (id obj in self)
{
id r = block(obj);
if (nil != r)
{
- [newSet addObject:r];
+ resultingObjects[nonNilCount] = r;
+ nonNilCount++;
}
}
- return [[newSet copy] autorelease];
+ NSSet *s = [NSSet setWithObjects:resultingObjects count:nonNilCount];
+ free(resultingObjects);
+ return s;
}
@end
View
20 CoreParse/Parsers/CPShiftReduceParsers/CPItem.m
@@ -8,6 +8,8 @@
#import "CPItem.h"
+#import "NSObject+IsCoreParseObject.h"
+
@interface CPItem ()
@property (readwrite,retain) CPRule *rule;
@@ -31,8 +33,8 @@ - (id)initWithRule:(CPRule *)initRule position:(NSUInteger)initPosition
if (nil != self)
{
- [self setRule:initRule];
- [self setPosition:initPosition];
+ rule = [initRule retain];
+ position = initPosition;
}
return self;
@@ -40,7 +42,7 @@ - (id)initWithRule:(CPRule *)initRule position:(NSUInteger)initPosition
- (id)copyWithZone:(NSZone *)zone
{
- return [[CPItem allocWithZone:zone] initWithRule:[self rule] position:[self position]];
+ return [[CPItem allocWithZone:zone] initWithRule:rule position:position];
}
- (void)dealloc
@@ -76,14 +78,14 @@ - (id)itemByMovingDotRight
return [c autorelease];
}
+- (BOOL)isCPItem
+{
+ return YES;
+}
+
- (BOOL)isEqual:(id)object
{
- if ([object isKindOfClass:[CPItem class]])
- {
- CPItem *other = (CPItem *)object;
- return [other position] == position && [other rule] == rule;
- }
- return NO;
+ return [object isCPItem] && ((CPItem *)object)->position == position && ((CPItem *)object)->rule == rule;
}
- (NSUInteger)hash
View
10 CoreParse/Parsers/CPShiftReduceParsers/CPLALR1Parser.m
@@ -12,6 +12,7 @@
#import "CPLR1Item.h"
#import "NSSetFunctional.h"
+#import "NSArray+Functional.h"
#import "CPShiftReduceAction.h"
@@ -29,6 +30,7 @@ - (BOOL)constructShiftReduceTables
{
CPGrammar *aug = [[self grammar] augmentedGrammar];
NSArray *kernels = [self kernelsForGrammar:aug];
+ NSArray *lr0Kernels = [kernels map:^ NSSet * (NSSet *s) { return [s map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }]; }];
NSUInteger itemCount = [kernels count];
NSArray *allNonTerminalNames = [[self grammar] allNonTerminalNames];
NSString *startSymbol = [aug start];
@@ -70,9 +72,9 @@ - (BOOL)constructShiftReduceTables
NSSet *lr0G = [g map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }];
NSUInteger indx = 0;
NSUInteger ix = NSNotFound;
- for (NSSet *lr1Kernel in kernels)
+ for (NSSet *lr0Kernel in lr0Kernels)
{
- if ([[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G])
+ if ([lr0Kernel isEqualToSet:lr0G])
{
ix = indx;
break;
@@ -94,9 +96,9 @@ - (BOOL)constructShiftReduceTables
NSSet *lr0G = [g map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }];
NSUInteger indx = 0;
NSUInteger gotoIndex = NSNotFound;
- for (NSSet *lr1Kernel in kernels)
+ for (NSSet *lr0Kernel in lr0Kernels)
{
- if ([[lr1Kernel map:^ id (CPLR1Item *i) { return [CPItem itemWithRule:[i rule] position:[i position]]; }] isEqualToSet:lr0G])
+ if ([lr0Kernel isEqualToSet:lr0G])
{
gotoIndex = indx;
break;
View
13 CoreParse/Parsers/CPShiftReduceParsers/CPShiftAction.h
@@ -0,0 +1,13 @@
+//
+// CPShiftAction.h
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "CPShiftReduceAction.h"
+
+@interface CPShiftAction : CPShiftReduceAction
+
+@end
View
13 CoreParse/Parsers/CPShiftReduceParsers/CPShiftAction.m
@@ -0,0 +1,13 @@
+//
+// CPShiftAction.m
+// CoreParse
+//
+// Created by Tom Davie on 20/08/2012.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "CPShiftAction.h"
+
+@implementation CPShiftAction
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.