-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava_lang.l
133 lines (119 loc) · 2.67 KB
/
java_lang.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%option stack
%option yylineno
%option nounput
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "java_lang_parse.h"
#define YY_NO_UNPUT
struct java_keyword { const char * name; int tokenType; };
const struct java_keyword *key_token;
extern struct java_keyword *JavaLangGetKeywordToken (const char * str, unsigned int len);
%}
digital [[:digit:]]
/*--- Ident ---*/
name [[:alpha:]_][[:alnum:]_$]*
%x CCOMMENT
%x LCOMMENT
%x CSTRING
%x CBODY
%%
/* The Line comments */
"//" { yy_push_state(LCOMMENT); }
<LCOMMENT>.* { yymore(); }
<LCOMMENT>\n { yy_pop_state (); }
/* The Content comments */
"/*" { yy_push_state(CCOMMENT); }
<CCOMMENT>. { yymore(); }
<CCOMMENT>\n { yymore(); }
<CCOMMENT>"*/" { yy_pop_state (); }
/* String */
\" { BEGIN(CSTRING); }
<CSTRING>\\\" { yymore(); }
<CSTRING>\" {
BEGIN(INITIAL);
#ifdef DEBUG
fprintf (stdout,"%s",yytext);
#endif
#ifdef HAVE_YACC
javayylval.pval = strdup(yytext);
javayylval.pval[strlen(yytext)-1] = 0;
return STRING;
#endif
}
<CSTRING>[^\"] { yymore(); }
/* Program Body */
\{ { yy_push_state(CBODY);}
<CBODY>\} { yy_pop_state (); }
<CBODY>\{ { yy_push_state(CBODY); }
<CBODY>\n { yymore(); }
<CBODY>"/*" { yy_push_state(CCOMMENT); }
<CBODY>"//" { yy_push_state(LCOMMENT); }
<CBODY>[^\}\{]+ { yymore(); }
/* Eat up the new line */
\n {
#ifdef DEBUG
fprintf (stdout,"%s",yytext);
#endif
}
/* Eat up the space */
[ \t]+ {
#ifdef DEBUG
fprintf (stdout,"%s",yytext);
#endif
}
/* Ident */
{name} {
#ifdef DEBUG
fprintf (stdout,"(ID: %s)",yytext);
#endif
#ifdef HAVE_YACC
key_token = JavaLangGetKeywordToken(yytext, strlen(yytext));
if (key_token == 0) {
javayylval.pval = strdup(yytext);
return NAME_ID ;
}
else {
#ifdef DEBUG
fprintf (stdout,"KeyWD");
#endif
return key_token->tokenType;
}
#endif
}
/* Integer Digital/Number */
{digital}+ {
#ifdef DEBUG
fprintf (stdout,"(Num: %s)",yytext);
#endif
#ifdef HAVE_YACC
javayylval.ival = atoi(yytext);
return NUMBER;
#endif
}
/* Misc */
[\<\>\[\]\(\),] {
#ifdef DEBUG
fprintf (stdout,"(Misc: %s)",yytext);
#endif
#ifdef HAVE_YACC
return yytext[0];
#endif
}
. {
fprintf (stderr,"L:%d:Syntax error near \'%s\'\n",yylineno, yytext);
exit (-1);
}
%%
#ifndef HAVE_YACC
int main()
{
yyin = fopen("test.java", "r");
yylex();
return 0;
}
#endif
int yywrap() {
return 1;
}