-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLex.g
More file actions
82 lines (58 loc) · 1.77 KB
/
Lex.g
File metadata and controls
82 lines (58 loc) · 1.77 KB
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
// COMS22201: Lexical analyser
lexer grammar Lex;
@members {
public void displayRecognitionError(String[] tokenNames, RecognitionException e){
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
String errorType = e.toString();
int syntaxErrorNumber = getNumberOfSyntaxErrors();
System.err.println("Error "+syntaxErrorNumber+":");
System.err.println("Error type: "+errorType);
System.err.println("Error occured at : "+hdr);
System.err.println("The error message is : "+msg);
}
}
//---------------------------------------------------------------------------
// KEYWORDS
//---------------------------------------------------------------------------
WRITE : 'write' ;
WRITELN : 'writeln' ;
DO : 'do' ;
ELSE : 'else' ;
FALSE : 'false' ;
IF : 'if' ;
READ : 'read' ;
SKIP : 'skip' ;
THEN : 'then' ;
TRUE : 'true' ;
WHILE : 'while' ;
//---------------------------------------------------------------------------
// OPERATORS
//---------------------------------------------------------------------------
SEMICOLON : ';' ;
OPENPAREN : '(' ;
CLOSEPAREN : ')' ;
INTNUM : ('0'..'9')+ ;
FLOAT : ('0'..'9')+'.'('0'..'9')+ ;
STRING : '\'' ('\'' '\'' | ~'\'')* '\'';
COMMENT : '{' (~'}')* '}' {skip();} ;
WS : (' ' | '\t' | '\r' | '\n' )+ {skip();} ;
IDENTIFIER : ('a'..'z'|'A'..'Z')(SUBID (SUBID (SUBID (SUBID (SUBID (SUBID SUBID?)?)?)?)?)?)?; //The length of an IDENTIFIER should not larger than 8
fragment
SUBID : ('a'..'z'|'A'..'Z'|'0'..'9'|'_');
ASSIGN : ':=';
EQ : '=' ;
LEQ : '<=' ;
AND : '&' ;
NOT : '!' ;
ADD : '+' ;
SUB : '-' ;
MULTIPLY : '*' ;
// Extra Language Features
GRT : '>' ;
LES : '<' ;
GEQ : '>=' ;
OR : '|' ;
NEQ : '!=' ;
DIVIDE : '/' ;
BITXOR : '^';