adding a bit of lexer

This commit is contained in:
Debucquoy Anthony 2025-03-16 11:50:54 +01:00
parent 5aaaf574d1
commit f2a82451ac
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
2 changed files with 34 additions and 4 deletions

View File

@ -3,8 +3,8 @@ start: (instruction)* TERMINAL
instruction: KEYWORD TERMINAL instruction: KEYWORD TERMINAL
| declaration TERMINAL | declaration TERMINAL
declaration: TYPE NOM declaration: TYPE VARIABLE
| TYPE NOM "=" expression | TYPE VARIABLE "=" expression
expression: literal expression: literal
| list | list
@ -12,9 +12,29 @@ expression: literal
| expression operator expression | expression operator expression
| operator expression | operator expression
literal: ENTIER
| BOOLEEN
| texte
texte: "\"" STRING "\""
list: "[" expression? ("," expression)* "]"
range: "[" ENTIER? ":" ENTIER? "]"
controls: test controls: test
| loop | loop
operator: PLUS
| MINUS
| TIMES
| DIVIDE // TODO: not complete
PLUS: "+"
MINUS: "-"
TIMES: "*"
DIVIDE: "/"
test: "si" expression "alors" "{" instruction* "}" test: "si" expression "alors" "{" instruction* "}"
loop: "si" expression "alors" "{" (instruction)* "}" loop: "si" expression "alors" "{" (instruction)* "}"
@ -24,6 +44,16 @@ COMMENT: "#" /[^\n]/
TERMINAL: ";" TERMINAL: ";"
VARIABLE: /([a-z]|[A-Z]|_)+[a-z]|[A-Z]|[0-9]_)+/ // TODO: ajouter accents VARIABLE: /([a-z]|[A-Z]|_)+([a-z]|[A-Z]|[0-9]|_)+/ // TODO: ajouter accents
TYPE: "booléen" | "entier" | "texte" | "liste" TYPE: "booléen" | "entier" | "texte" | "liste"
KEYWORD: "ajouter" | "afficher"
ENTIER: /[1-9][0-9]*/
BOOLEEN: "vrai" | "faux"
STRING: /\w+/

2
spf.py
View File

@ -32,7 +32,7 @@ def main():
spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer) spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer)
with open(args.spf_file) as spf_input: with open(args.spf_file) as spf_input:
program = spf_input.readall() program = spf_input.read()
spf_parser.parse(program) spf_parser.parse(program)