-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (94 loc) · 3.05 KB
/
index.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
const fs = require('fs')
const RESERVED_WORDS = ['int', 'float', 'char', 'if', 'else']
const RELATIONAL_OPERATORS = ['>', '<', '>=', '<=', '!=', '==']
const FINAL_SYMBOLS = ['=', ',', ';', '(', ')', '{', '}', ' ']
const NUMBERS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
const isNumber = n => /^-?[\d.]+(?:e-?\d+)?$/.test(n)
const checkIsNotAnySymbol = letter => {
return (RELATIONAL_OPERATORS.includes(letter) || FINAL_SYMBOLS.includes(letter) || letter === ' ')
}
const finalSymbolMap = new Map()
const identifiersMap = new Map()
const reserverdWordsMap = new Map()
const numbersMap = new Map()
const relationalOpertorsMap = new Map()
const checkAndAddReservedWord = word => {
if (reserverdWordsMap.has(word)) {
return
}
reserverdWordsMap.set(word, 'palavra reservada')
}
const checkAndAddFinalSymbolMap = symbol => {
if (finalSymbolMap.has(symbol)) {
return
}
finalSymbolMap.set(symbol, 'terminal')
}
const checkAndAddIdentifiersMap = identifier => {
if (identifiersMap.has(identifier) || identifier == '' || NUMBERS.includes(identifier[0])) {
return
}
identifiersMap.set(identifier, 'idenficador')
}
const checkAndAddRelationalOperatorsMap = operator => {
if (relationalOpertorsMap.has(operator)) {
return
}
relationalOpertorsMap.set(operator, 'operador relacional')
}
const lexicalAnaliser = line => {
const lineSplit = line.split('')
let nextWord = ''
let word = ''
let initial = 0
let final = 0
for (let idx = 0; idx <= lineSplit.length; idx ++) {
if (FINAL_SYMBOLS.includes(line[idx])) {
checkAndAddFinalSymbolMap(line[idx])
}
if (RELATIONAL_OPERATORS.includes(line[idx])) {
checkAndAddRelationalOperatorsMap(line[idx])
}
word += line[idx]
if (RESERVED_WORDS.includes(word)) {
checkAndAddReservedWord(word)
word = ''
}
if (NUMBERS.includes(word)) {
checkAndAddNumbersMap(word)
word = ''
}
nextWord = line[(idx + 1)]
if (FINAL_SYMBOLS.includes(nextWord) || RELATIONAL_OPERATORS.includes(nextWord)) {
FINAL_SYMBOLS.forEach(b => {
const index = word.indexOf(b)
if (index >= 0) {
word.split(word[index]).forEach(c => {
c = c.trim()
if (!RESERVED_WORDS.includes(c) && (!isNumber(c))) {
checkAndAddIdentifiersMap(c)
}
})
}
})
RELATIONAL_OPERATORS.forEach(b => { const index = word.indexOf(b)
if (index >= 0) {
word.split(word[index]).forEach(c => {
c = c.trim()
if (!RESERVED_WORDS.includes(c) && (!isNumber(c))) {
checkAndAddIdentifiersMap(c)
}
})
}
})
nextWord = ''
word = ''
}
}
}
const file = fs.readFileSync('./Prod.anderson', 'UTF-8').split('\n').map(e => e.trim()).filter(e => e.length > 0);
file.forEach(lexicalAnaliser)
console.log('identificadores', identifiersMap)
console.log('palavras reservadas', reserverdWordsMap)
console.log('simbolos finais', finalSymbolMap)
console.log('operadores relacionais', relationalOpertorsMap)