Created
March 14, 2012 00:11
-
-
Save meric/2032871 to your computer and use it in GitHub Desktop.
Sample lex file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#import <Foundation/Foundation.h> | |
#import <stdlib.h> | |
#import "SVGBase.h" | |
#import "SVGLength.h" | |
#import "SVGAngle.h" | |
#import "y.tab.h" | |
void yyerror(char *); | |
%} | |
%% | |
^"length" { return LENGTH; } | |
^"angle" { return ANGLE; } | |
^"color" { return COLOR; } | |
[:] { return SEMICOLON; } | |
[#][0-9A-Fa-f]{3}([0-9A-Fa-f]{3})? { /* Hexadecimal */ | |
size_t length = strlen(yytext); | |
char hexadecimal[7]="000000"; | |
int i = 0; | |
for (;i < 6;i++) hexadecimal[i]=yytext[(length ==4 ? i/2 : i) + 1]; | |
yylval.integerValue = strtol(hexadecimal, NULL, 16); | |
return HEXADECIMAL; } | |
[+-]?[0-9]+ { yylval.integerValue = atoi(yytext); return INTEGER;} | |
[+-]?[0-9]*[.][0-9]+ { yylval.integerValue = atof(yytext); return FLOAT; } | |
em|ex|px|cm|mm|in|pt|pc { | |
NSString *key = [NSString stringWithCString:yytext | |
encoding:NSUTF8StringEncoding]; | |
NSNumber *lengthType = [SVGLengthTypeDict objectForKey:key]; | |
yylval.lengthTypeValue = [lengthType intValue]; | |
return LENGTHTYPE; | |
} | |
deg|rad|grad { | |
NSString *key = [NSString stringWithCString:yytext | |
encoding:NSUTF8StringEncoding]; | |
NSNumber *angleType = [SVGAngleTypeDict objectForKey:key]; | |
yylval.angleTypeValue = [angleType intValue]; | |
return ANGLETYPE; | |
} | |
[\n] { return *yytext; } | |
. yyerror("Unknown character"); | |
%% | |
int yywrap(void) { | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#import <Foundation/Foundation.h> | |
#import <stdio.h> | |
#import "SVGBase.h" | |
#import "SVGAngle.h" | |
#import "SVGLength.h" | |
int yylex(void); | |
void yyerror(char *); | |
void* yy_scan_string(const char*); | |
NSObject *yyvalue; | |
%} | |
%union { | |
SVGFloat floatValue; | |
SVGInteger integerValue; | |
SVGLengthType lengthTypeValue; | |
SVGAngleType angleTypeValue; | |
char* stringPtr; | |
char charValue; | |
NSObject* objectPtr; | |
}; | |
%token SEMICOLON LENGTH ANGLE COLOR | |
%token <integerValue> INTEGER | |
%token <floatValue> FLOAT | |
%token <lengthTypeValue> LENGTHTYPE | |
%token <angleTypeValue> ANGLETYPE | |
%token <integerValue> HEXADECIMAL | |
%type <objectPtr> length | |
%type <lengthTypeValue> lengthType | |
%type <objectPtr> angle | |
%type <angleTypeValue> angleType | |
%type <objectPtr> value | |
%type <floatValue> number | |
%type <integerValue> color | |
%% | |
//program: | |
// program value '\n' { NSLog(AT"%@\n", $2); } | |
// | /* empty */ | |
// ; | |
value: length {yyvalue=$1;} | |
| angle {yyvalue=$1;} | |
| color {yyvalue=[NSNumber numberWithLong:$1];} | |
; | |
number: INTEGER {$$=(float)$1;} | |
| FLOAT {$$=(float)$1;} | |
; | |
lengthType: LENGTHTYPE | |
| /* empty */ {$$=SVGLengthTypeNumber;} | |
; | |
length: LENGTH | |
SEMICOLON | |
number lengthType {$$=[SVGLength lengthWithValue:$3 unit:$4];} | |
; | |
angleType: ANGLETYPE | |
| /* empty */ {$$=SVGAngleTypeUnspecified;} | |
; | |
angle: ANGLE | |
SEMICOLON | |
number angleType {$$=[SVGAngle angleWithValue:$3 unit:$4];} | |
; | |
color: COLOR | |
SEMICOLON | |
HEXADECIMAL {$$=$3;} | |
; | |
%% | |
void yyerror(char *s) { | |
fprintf(stderr, "%s\n", s); | |
} | |
int main(int argc, char**argv) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
//yy_scan_string("color(#AABBCC)"); | |
yy_scan_string(argv[1]); | |
yyparse(); | |
NSLog(@"%@\n", yyvalue); | |
return 0; | |
[pool release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment