GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SI3-Compiler/analyse_lexicale.py at main · Other-Project/SI3-Compiler

This repository was archived by the owner on Jun 15, 2024. It is now read-only.

Expand file tree

/

analyse_lexicale.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
87 lines (72 loc) · 2.41 KB
/

analyse_lexicale.py

Copy path

File metadata and controls

87 lines (72 loc) · 2.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import sys
from sly import Lexer
class FloLexer(Lexer):
# Noms des lexèmes (sauf les litéraux). En majuscule. Ordre non important
tokens = {
IDENTIFIANT,
TYPE,
INTEGER,
BOOLEAN,
IF,
ELSE,
WHILE,
RETURN,
AND,
OR,
NOT,
EQ,
LE,
GE,
NE,
}
# Les caractères litéraux sont des caractères uniques qui sont retournés tel quel quand rencontré par l'analyse lexicale.
# Les litéraux sont vérifiés en dernier, après toutes les autres règles définies par des expressions régulières.
# Donc, si une règle commence par un de ces littérals (comme INFERIEUR_OU_EGAL), cette règle aura la priorité.
literals = {"+", "-", "*", "/", "%", "(", ")", ";", ",", "{", "}", "=", ">", "<"}
# chaines contenant les caractère à ignorer. Ici espace et tabulation
ignore = " \t"
# Expressions régulières correspondant au différents Lexèmes par ordre de priorité
EQ = r"=="
LE = r"<="
GE = r">="
NE = r"!="
@_(r"0|[1-9][0-9]*")
def INTEGER(self, t):
t.value = int(t.value)
return t
@_(r"Vrai|Faux")
def BOOLEAN(self, t):
t.value = t.value == "Vrai"
return t
# cas général
IDENTIFIANT = r"[a-zA-Z][a-zA-Z0-9_]*" # en général, variable.flo ou nom de fonction
# cas spéciaux:
IDENTIFIANT["booleen"] = TYPE
IDENTIFIANT["entier"] = TYPE
IDENTIFIANT["si"] = IF
IDENTIFIANT["sinon"] = ELSE
IDENTIFIANT["tantque"] = WHILE
IDENTIFIANT["retourner"] = RETURN
IDENTIFIANT["et"] = AND
IDENTIFIANT["ou"] = OR
IDENTIFIANT["non"] = NOT
# Syntaxe des commentaires à ignorer
ignore_comment = r"\#.*"
# Permet de conserver les numéros de ligne. Utile pour les messages d'erreurs
@_(r"\n+")
def ignore_newline(self, t):
self.lineno += t.value.count("\n")
# En cas d'erreur, indique où elle se trouve
def error(self, t):
print(f'Ligne{self.lineno}: caractère inattendu "{t.value[0]}"', file=sys.stderr)
self.index += 1
exit(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: python3 analyse_lexicale.py NOM_FICHIER_SOURCE.flo")
else:
with open(sys.argv[1], "r") as f:
data = f.read()
lexer = FloLexer()
for tok in lexer.tokenize(data):
print(tok)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.