From f2a82451ac8e3cc6f1ffaaa32f8867049d62ac72 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 16 Mar 2025 11:50:54 +0100 Subject: [PATCH] adding a bit of lexer --- spf.lark | 36 +++++++++++++++++++++++++++++++++--- spf.py | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/spf.lark b/spf.lark index d542453..3aa6a56 100644 --- a/spf.lark +++ b/spf.lark @@ -3,8 +3,8 @@ start: (instruction)* TERMINAL instruction: KEYWORD TERMINAL | declaration TERMINAL -declaration: TYPE NOM - | TYPE NOM "=" expression +declaration: TYPE VARIABLE + | TYPE VARIABLE "=" expression expression: literal | list @@ -12,9 +12,29 @@ expression: literal | expression operator expression | operator expression +literal: ENTIER + | BOOLEEN + | texte + +texte: "\"" STRING "\"" + +list: "[" expression? ("," expression)* "]" + +range: "[" ENTIER? ":" ENTIER? "]" + controls: test | loop +operator: PLUS + | MINUS + | TIMES + | DIVIDE // TODO: not complete + +PLUS: "+" +MINUS: "-" +TIMES: "*" +DIVIDE: "/" + test: "si" expression "alors" "{" instruction* "}" loop: "si" expression "alors" "{" (instruction)* "}" @@ -24,6 +44,16 @@ COMMENT: "#" /[^\n]/ 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" + +KEYWORD: "ajouter" | "afficher" + +ENTIER: /[1-9][0-9]*/ + +BOOLEEN: "vrai" | "faux" + +STRING: /\w+/ + + diff --git a/spf.py b/spf.py index f0c006c..0951032 100755 --- a/spf.py +++ b/spf.py @@ -32,7 +32,7 @@ def main(): spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer) with open(args.spf_file) as spf_input: - program = spf_input.readall() + program = spf_input.read() spf_parser.parse(program)