|
|
发表于 2003-12-4 15:53:56| 字数 4,452| - 中国–上海–上海 鹏博士宽带
|
显示全部楼层
package Parse;
%%
%implements Lexer
%function nextToken
%type java_cup.runtime.Symbol
%char
%state COMMENT
ALPHA=[A-Za-z]
DIGIT=[0-9]
NEW_LINE=(\n\r|\r\n|\n)
NONNEWLINE_WHITE_SPACE_CHAR=[\ \t\b]
STRING_TEXT=(\\\"|[^\n\r\"])*
COMMENT_TEXT=([^/*\n\r]|[^*\n\r]"/"[^*\n\r]|[^/\n\r]"*"[^/\n\r]|"*"[^/\n\r]|"/"[^*\n\r])*
%{
private int comment_count = 0;
private void newline() {
errorMsg.newline(yychar);
}
private void err(int pos, String s) {
errorMsg.error(pos,s);
}
private void err(String s) {
err(yychar,s);
}
private java_cup.runtime.Symbol tok(int kind, Object value) {
return new java_cup.runtime.Symbol(kind, yychar, yychar+yylength(), value);
}
private ErrorMsg.ErrorMsg errorMsg;
Yylex(java.io.InputStream s, ErrorMsg.ErrorMsg e) {
this(s);
errorMsg=e;
}
%}
%eofval{
{
if(comment_count!=0) err(yychar, "comment block end '*/' missing at end of file");
return tok(sym.EOF, null);
}
%eofval}
%%
<YYINITIAL> "," { return tok(sym.COMMA, null); /* punctuation symbols.... */ }
<YYINITIAL> ":" { return tok(sym.COLON, null); }
<YYINITIAL> ";" { return tok(sym.SEMICOLON, null); }
<YYINITIAL> "(" { return tok(sym.LPAREN, null); }
<YYINITIAL> ")" { return tok(sym.RPAREN, null); }
<YYINITIAL> "[" { return tok(sym.LBRACK, null); }
<YYINITIAL> "]" { return tok(sym.RBRACK, null); }
<YYINITIAL> "{" { return tok(sym.LBRACE, null); }
<YYINITIAL> "}" { return tok(sym.RBRACE, null); }
<YYINITIAL> "." { return tok(sym.DOT, null); }
<YYINITIAL> "+" { return tok(sym.PLUS, null); }
<YYINITIAL> "-" { return tok(sym.MINUS, null); }
<YYINITIAL> "*" { return tok(sym.TIMES, null); }
<YYINITIAL> "/" { return tok(sym.DIVIDE, null); }
<YYINITIAL> "=" { return tok(sym.EQ, null); }
<YYINITIAL> "<>" { return tok(sym.NEQ, null); }
<YYINITIAL> "<" { return tok(sym.LT, null); }
<YYINITIAL> "<=" { return tok(sym.LE, null); }
<YYINITIAL> ">" { return tok(sym.GT, null); }
<YYINITIAL> ">=" { return tok(sym.GE, null); }
<YYINITIAL> "&" { return tok(sym.AND, null); }
<YYINITIAL> "|" { return tok(sym.OR, null); }
<YYINITIAL> ":=" { return tok(sym.ASSIGN, null); }
<YYINITIAL> "while" { return tok(sym.WHILE, null); /* reserved words... */ }
<YYINITIAL> "for" { return tok(sym.FOR, null); }
<YYINITIAL> "to" { return tok(sym.TO, null); }
<YYINITIAL> "break" { return tok(sym.BREAK, null); }
<YYINITIAL> "let" { return tok(sym.LET, null); }
<YYINITIAL> "in" { return tok(sym.IN, null); }
<YYINITIAL> "end" { return tok(sym.END, null); }
<YYINITIAL> "function" { return tok(sym.FUNCTION, null); }
<YYINITIAL> "var" { return tok(sym.VAR, null); }
<YYINITIAL> "type" { return tok(sym.TYPE, null); }
<YYINITIAL> "array" { return tok(sym.ARRAY, null); }
<YYINITIAL> "if" { return tok(sym.IF, null); }
<YYINITIAL> "then" { return tok(sym.THEN, null); }
<YYINITIAL> "else" { return tok(sym.ELSE, null); }
<YYINITIAL> "do" { return tok(sym.DO, null); }
<YYINITIAL> "of" { return tok(sym.OF, null); }
<YYINITIAL> "nil" { return tok(sym.NIL, null); }
<YYINITIAL> {NONNEWLINE_WHITE_SPACE_CHAR}+ { /* whitespace characters are ignored */ }
<YYINITIAL,COMMENT> {NEW_LINE} { newline(); /* newlines call newline() to know the line number if an error occurs */ }
<YYINITIAL> "/*" { yybegin(COMMENT); comment_count = comment_count + 1; }
<COMMENT> "/*" { comment_count = comment_count + 1; }
<COMMENT> "*/" {
comment_count = comment_count - 1;
if(comment_count < 0) err(yychar, "comment block end without comment block start");
if (comment_count == 0) {
yybegin(YYINITIAL);
}
}
<YYINITIAL> "*/" {
err(yychar, "comment block end '*/' without comment block start");
}
<COMMENT> {COMMENT_TEXT} { }
<YYINITIAL> \"{STRING_TEXT}\" {
return tok(sym.STRING, new String(yytext().substring(1,yytext().length() - 1)));
}
<YYINITIAL> \"{STRING_TEXT} {
String str = yytext().substring(1,yytext().length());
err(yychar, "unclosed string");
}
<YYINITIAL> {DIGIT}+ {
return tok(sym.INT, Integer.valueOf(yytext()));
}
<YYINITIAL> {ALPHA}({ALPHA}|{DIGIT}|_)* {
return tok(sym.ID, new String(yytext()));
}
<YYINITIAL,COMMENT> . {
err(yychar, "unmatched input character: "+yytext()+", ASCII: "+ ((int) yytext().charAt(0)) );
} |
|