Lexer.jflex 4.26 KB
Newer Older
petter's avatar
petter committed
1
package java_cup;
petter's avatar
petter committed
2
3
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.Location;
petter's avatar
petter committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java_cup.runtime.Symbol;
import java.lang.Error;
import java.io.InputStreamReader;

%%

%class Lexer
%implements sym
%public
%unicode
%line
%column
%cup
%{
petter's avatar
petter committed
18
    public Lexer(ComplexSymbolFactory sf){
petter's avatar
petter committed
19
	this(new InputStreamReader(System.in));
petter's avatar
petter committed
20
        symbolFactory = sf;
petter's avatar
petter committed
21
22
    }
    private StringBuffer sb;
petter's avatar
petter committed
23
    private ComplexSymbolFactory symbolFactory;
petter's avatar
petter committed
24
    private int csline,cscolumn;
petter's avatar
petter committed
25
26
    public Symbol symbol(String name, int code){
	return symbolFactory.newSymbol(name, code,new Location(yyline+1,yycolumn+1-yylength()),new Location(yyline+1,yycolumn+1));
petter's avatar
petter committed
27
    }
petter's avatar
petter committed
28
29
    public Symbol symbol(String name, int code, String lexem){
	return symbolFactory.newSymbol(name, code, new Location(yyline+1, yycolumn +1), new Location(yyline+1,yycolumn+yylength()), lexem);
petter's avatar
petter committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    }
    protected void emit_warning(String message){
	ErrorManager.getManager().emit_warning("Scanner at " + (yyline+1) + "(" + (yycolumn+1) + "): " + message);
    }
    protected void emit_error(String message){
	ErrorManager.getManager().emit_error("Scanner at " + (yyline+1) + "(" + (yycolumn+1) +  "): " + message);
    }
%}

Newline = \r | \n | \r\n
Whitespace = [ \t\f] | {Newline}

/* comments */
Comment = {TraditionalComment} | {EndOfLineComment}
TraditionalComment = "/*" {CommentContent} \*+ "/"
EndOfLineComment = "//" [^\r\n]* {Newline}
CommentContent = ( [^*] | \*+[^*/] )*

ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*


%eofval{
petter's avatar
petter committed
52
    return symbolFactory.newSymbol("EOF",sym.EOF);
petter's avatar
petter committed
53
54
55
56
57
58
59
60
%eofval}

%state CODESEG

%%  

<YYINITIAL> {

petter's avatar
petter committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  {Whitespace}  {                                              }
  "?"           { return symbol("QESTION",QUESTION);           }
  ";"           { return symbol("SEMI",SEMI);                  }
  ","           { return symbol("COMMA",COMMA);                }
  "*"           { return symbol("STAR",STAR);                  }
  "."           { return symbol("DOT",DOT);                    }
  "|"           { return symbol("BAR",BAR);                    }
  "["           { return symbol("LBRACK",LBRACK);              }
  "]"           { return symbol("RBRACK",RBRACK);              }
  ":"           { return symbol("COLON",COLON);                }
  "::="         { return symbol("COLON_COLON_EQUALS",COLON_COLON_EQUALS);   }
  "%prec"       { return symbol("PERCENT_PREC",PERCENT_PREC);  }
  ">"           { return symbol("GT",GT);                      }
  "<"           { return symbol("LT",LT);                      }
  {Comment}     {                                              }
petter's avatar
petter committed
76
  "{:"          { sb = new StringBuffer(); csline=yyline+1; cscolumn=yycolumn+1; yybegin(CODESEG);    }
petter's avatar
petter committed
77
78
  "package"     { return symbol("PACKAGE",PACKAGE);            } 
  "import"      { return symbol("IMPORT",IMPORT);	       }
79
  "static"      { return symbol("STATIC",STATIC);	       }
80
  "class"       { return symbol("CLASS",CLASS); 	       }
petter's avatar
petter committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  "code"        { return symbol("CODE",CODE);		       }
  "action"      { return symbol("ACTION",ACTION);	       }
  "parser"      { return symbol("PARSER",PARSER);	       }
  "terminal"    { return symbol("PARSER",TERMINAL);	       }
  "non"         { return symbol("NON",NON);		       }
  "nonterminal" { return symbol("NONTERMINAL",NONTERMINAL);    }
  "init"        { return symbol("INIT",INIT);		       }
  "scan"        { return symbol("SCAN",SCAN);		       }
  "with"        { return symbol("WITH",WITH);		       }
  "start"       { return symbol("START",START);		       }
  "precedence"  { return symbol("PRECEDENCE",PRECEDENCE);      }
  "left"        { return symbol("LEFT",LEFT);		       }
  "right"       { return symbol("RIGHT",RIGHT);		       }
  "nonassoc"    { return symbol("NONASSOC",NONASSOC);          }
  "extends"     { return symbol("EXTENDS",EXTENDS);            }
  "super"       { return symbol("SUPER",SUPER);                }
  {ident}       { return symbol("ID",ID,yytext());             }
petter's avatar
petter committed
98
99
100
101
  
}

<CODESEG> {
petter's avatar
petter committed
102
  ":}"         { yybegin(YYINITIAL); return symbolFactory.newSymbol("CODE_STRING",CODE_STRING, new Location(csline, cscolumn),new Location(yyline+1,yycolumn+1+yylength()), sb.toString()); }
petter's avatar
petter committed
103
104
105
106
107
  .|\n            { sb.append(yytext()); }
}

// error fallback
.|\n          { emit_warning("Unrecognized character '" +yytext()+"' -- ignored"); }