-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentLexer.fsl
More file actions
59 lines (57 loc) · 2.01 KB
/
AssignmentLexer.fsl
File metadata and controls
59 lines (57 loc) · 2.01 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
// MADE BY: Kevin Moore s204462, Aryan Mirzazadeh s204489, Jakob Jacobsen s204502, Bjørn Laursen s204451
// The generated lexer module will start with this code
{
module AssignmentLexer
open FSharp.Text.Lexing
open System
// open the module that defines the tokens
open AssignmentParser
// Set the language to English such that 4.0 is parsed as 4 and not 40.
System.Globalization.CultureInfo.CurrentCulture <- new System.Globalization.CultureInfo("en-US")
}
// We define macros for some regular expressions we will use later
let digit = ['0'-'9']
let char = ['a'-'z' 'A'-'Z']
let num = digit+ ( '.' digit+)? ('E' ('+'|'-')? digit+ )?
let whitespace = [' ' '\t']
let newline = "\n\r" | '\n' | '\r'
let var = ['a'-'z' 'A'-'Z']['a'-'z' 'A'-'Z' '0'-'9']*
// We define now the rules for recognising and building tokens
// for each of the tokens of our language we need a rule
// NOTE: rules are applied in order top-down.
// This is important when tokens overlap (not in this example)
rule tokenize = parse
// deal with tokens that need to be ignored (skip them)
| whitespace { tokenize lexbuf }
| newline { lexbuf.EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf; }
// deal with tokens that need to be built
| num { NUM(Double.Parse(LexBuffer<_>.LexemeString lexbuf)) }
| '*' { TIMES }
| '/' { DIV }
| '+' { PLUS }
| '-' { MINUS }
| '^' { POW }
| '(' { LPAR }
| ')' { RPAR }
// Asserting tokens
| ":=" { ASSIGN }
| ';' { SEMICOLON }
| "->" { ASSERTS }
| '[' { LBRACKET }
| ']' { RBRACKET }
| "if" { IF }
| "fi" { FI }
| "do" { DO }
| "od" { OD }
| "skip" { SKIP }
// Boolean tokens
| '&' {AND}
| '!' {NOT}
| '|' {OR}
| '=' {EQ}
| '<' {LE}
| '>' {GR}
| "true" {TRUE}
| "false" {FALSE}
| var { VAR(LexBuffer<_>.LexemeString lexbuf) }
| eof { EOF }