Last active
August 29, 2015 14:04
-
-
Save cocodrips/5ad0d9b4612e09771ce5 to your computer and use it in GitHub Desktop.
lex入門
This file contains hidden or 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
%{ | |
enum { | |
INT = 1, | |
FLOAT, | |
ID, | |
NUM, | |
REAL, | |
COMMA, | |
EQ, | |
EX, | |
QU, | |
SEMI, | |
ADD, | |
SUB, | |
MUL, | |
DIV, | |
LPAR, | |
RPAR, | |
ERROR | |
}; | |
%} | |
%% | |
int {return INT;} | |
float {return FLOAT;} | |
[a-z][a-z0-9]* {return ID;} | |
0|[1-9][0-9]* {return NUM;} | |
([0-9]+"."[0-9]*)|([0-9]*"."[0-9]+) {return REAL;} | |
"," {return COMMA;} | |
"=" {return EQ;} | |
"!" {return EX;} | |
"?" {return QU;} | |
";" {return SEMI;} | |
"+" {return ADD;} | |
"-" {return SUB;} | |
"*" {return MUL;} | |
"/" {return DIV;} | |
"(" {return LPAR;} | |
")" {return RPAR;} | |
"\n"|" "|"\t" { } | |
"/*"[a-z0-9 ]*"*/" { } | |
. {return ERROR;} | |
%% | |
int yywrap(void){ return 1;} | |
int main(void){ | |
int t; | |
while((t=yylex()) != 0){ | |
printf("number = %d, string = '%s'\n", t, yytext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
('-')/ Compiler flex sample.lex 1:55:55
('-')/ Compiler cc lex.yy.c 1:55:56
('-')/ Compiler ./a.out 1:55:58
ab = .2/**/
number = 3, string = 'ab'
number = 7, string = '='
number = 5, string = '.2'