LabCommScanner.flex 2.81 KB
Newer Older
1
package se.lth.control.labcomm2006.compiler;
Anders Nilsson's avatar
Anders Nilsson committed
2
3
4

import beaver.Symbol;
import beaver.Scanner;
5
import se.lth.control.labcomm2006.compiler.LabCommParser.Terminals;
Anders Nilsson's avatar
Anders Nilsson committed
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

%%

%public 
%final 
%class LabCommScanner
%extends Scanner

%type Symbol 
%function nextToken 
%yylexthrow Scanner.Exception

%unicode
%line %column

%{
  StringBuffer strbuf = new StringBuffer(128);

  private Symbol sym(short id) {
    return new Symbol(id, yyline + 1, yycolumn + 1, len(), str());
  }

  private Symbol sym(short id, String value) {
    return new Symbol(id, yyline + 1, yycolumn + 1, len(), value);
  }

  private String str() { return yytext(); }
  private int len() { return yylength(); }
%}

LineTerminator = \n|\r|\r\n
InputCharacter = [^\r\n]

WhiteSpace = [ ] | \t | \f | {LineTerminator}

Comment = {TraditionalComment}
        | {EndOfLineComment}

TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" | "/*" "*"+ [^/*] ~"*/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?

Identifier = [:jletter:][:jletterdigit:]*

DecimalNumeral = 0 | {NonZeroDigit} {Digits}? 
Digits = {Digit}+
Digit = 0 | {NonZeroDigit}
NonZeroDigit = [1-9]

%%

<YYINITIAL> {
  {WhiteSpace}                   { }
  {Comment}                      { }

  "sample"                       { return sym(Terminals.SAMPLE); }
  "typedef"                      { return sym(Terminals.TYPEDEF); }
  "struct"                       { return sym(Terminals.STRUCT); }
  "void"                         { return sym(Terminals.VOID); }
  "boolean"                      { return sym(Terminals.BOOLEAN); }
  "byte"                         { return sym(Terminals.BYTE); }
  "short"                        { return sym(Terminals.SHORT); }
  "int"                          { return sym(Terminals.INT); }
  "long"                         { return sym(Terminals.LONG); }
  "float"                        { return sym(Terminals.FLOAT); }
  "double"                       { return sym(Terminals.DOUBLE); }
  "string"                       { return sym(Terminals.STRING); }

  {DecimalNumeral}               { return sym(Terminals.INTEGER_LITERAL); }
  "_"                            { return sym(Terminals.UNDERSCORE); }
  "{"                            { return sym(Terminals.LBRACE); }
  "}"                            { return sym(Terminals.RBRACE); }
  "["                            { return sym(Terminals.LBRACK); }
  "]"                            { return sym(Terminals.RBRACK); }
  ";"                            { return sym(Terminals.SEMICOLON); }
  ","                            { return sym(Terminals.COMMA); }
  
  {Identifier}                   { return sym(Terminals.IDENTIFIER); }
}

// fall through errors
.|\n                             { throw new RuntimeException("Illegal character \""+str()+ "\" at line "+yyline+", column "+yycolumn); }
<<EOF>>                          { return sym(Terminals.EOF); }