-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLex.g
94 lines (69 loc) · 2.06 KB
/
Lex.g
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
// COMS22303: Lexical analyser
lexer grammar Lex;
@members {
static int lexErrors = 0;
public void displayRecognitionError(String[] tokenNames,
RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
lexErrors++;
if(e instanceof FailedPredicateException)
{
System.out.println("Lex error: var name > 9 characters");
}
else if(e instanceof Exception)
{
System.out.println("Lex error: " + hdr + " " + msg);
}
}
public int getLexErrors()
{
return lexErrors;
}
}
//---------------------------------------------------------------------------
// KEYWORDS
//---------------------------------------------------------------------------
BEGIN : 'begin' ;
END : 'end' ;
WRITE : 'write' ;
WRITELN : 'writeln' ;
ELSE : 'else' ;
IF : 'if' ;
READ : 'read' ;
REPEAT : 'repeat' ;
UNTIL : 'until' ;
WHILE : 'while' ;
//---------------------------------------------------------------------------
// OPERATORS
//---------------------------------------------------------------------------
SEMICOLON : ';' ;
OPENPAREN : '(' ;
CLOSEPAREN : ')' ;
GREATER : '>' ;
LESSER : '<' ;
EQUALS : '=' ;
DEQUALS : '!=' ;
GEQUALS : '>=' ;
LEQUALS : '<=' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIVIDE : '/' ;
ASSIGN : ':=' ;
REALNUM : INT '.' INT (EXPONENT)?;
fragment
EXPONENT : 'e' ('-')? INT ;
fragment
INT : ('0'..'9')+ ;
//IDENT : CHAR ( INT | CHAR )* ;
fragment
CHAR : ('A'..'Z' | 'a'..'z') ;
STRING : '\'' ('\'' '\'' | ~'\'')* '\'';
COMMENT : '{' (~'}')* '}' {skip();} ;
WS : (' ' | '\t' | '\r' | '\n' )+ {skip();} ;
IDENT
: CHAR CHARORINT?;
CHARORINT
@init {int N = 0;}
: ((CHAR | ('0'..'9')) {N++;} )+{N<=8}?;