Basic declaration adn assignation recognition
This commit is contained in:
parent
f082c3f7d8
commit
efa1776155
@ -1 +1,2 @@
|
|||||||
|
interegular==0.3.3
|
||||||
lark==1.2.2
|
lark==1.2.2
|
||||||
|
80
spf.lark
80
spf.lark
@ -1,16 +1,22 @@
|
|||||||
start: (instruction)* TERMINAL
|
start: (instruction)*
|
||||||
|
|
||||||
instruction: KEYWORD TERMINAL
|
instruction: keyword expression TERMINAL
|
||||||
| declaration TERMINAL
|
| type declaration TERMINAL
|
||||||
|
| assignation TERMINAL
|
||||||
|
| test
|
||||||
|
| loop
|
||||||
|
|
||||||
declaration: TYPE VARIABLE
|
declaration: VARIABLE (EQUAL_SIGN expression)?
|
||||||
| TYPE VARIABLE "=" expression
|
|
||||||
|
assignation: VARIABLE EQUAL_SIGN expression
|
||||||
|
|
||||||
expression: literal
|
expression: literal
|
||||||
| list
|
| list
|
||||||
| range
|
| range
|
||||||
| expression operator expression
|
//| expression operator expression
|
||||||
| operator expression
|
//| operator expression
|
||||||
|
|
||||||
|
type: "booléen" | "entier" | "texte" | "liste"
|
||||||
|
|
||||||
literal: ENTIER
|
literal: ENTIER
|
||||||
| BOOLEEN
|
| BOOLEEN
|
||||||
@ -25,35 +31,49 @@ range: "[" ENTIER? ":" ENTIER? "]"
|
|||||||
controls: test
|
controls: test
|
||||||
| loop
|
| loop
|
||||||
|
|
||||||
operator: PLUS
|
operator: PLUS_SIGN
|
||||||
| MINUS
|
| MINUS_SIGN
|
||||||
| TIMES
|
| TIMES_SIGN
|
||||||
| DIVIDE // TODO: not complete
|
| DIVIDE_SIGN // TODO: not complete
|
||||||
|
|
||||||
PLUS: "+"
|
PLUS_SIGN: "+"
|
||||||
MINUS: "-"
|
MINUS_SIGN: "-"
|
||||||
TIMES: "*"
|
TIMES_SIGN: "*"
|
||||||
DIVIDE: "/"
|
DIVIDE_SIGN: "/"
|
||||||
|
|
||||||
|
EQUAL_SIGN: "="
|
||||||
|
|
||||||
test: "si" expression "alors" "{" instruction* "}"
|
test: "si" expression "alors" "{" instruction* "}"
|
||||||
|
| "si" expression "alors" "{" instruction* "}" "sinon" "{" instruction* "}"
|
||||||
|
|
||||||
loop: "si" expression "alors" "{" (instruction)* "}"
|
loop: WHILES_KW expression WHILEE_KW "{" (instruction)* "}"
|
||||||
|
|
||||||
COMMENT: "#" /[^\n]/
|
|
||||||
%ignore COMMENT
|
|
||||||
|
|
||||||
TERMINAL: ";"
|
TERMINAL: ";"
|
||||||
|
|
||||||
VARIABLE: /([a-z]|[A-Z]|_)+([a-z]|[A-Z]|[0-9]|_)+/ // TODO: ajouter accents
|
VAR_CHARS: /[a-zA-Zçâêîôûéàèìòùëïü_]/
|
||||||
|
VARIABLE: VAR_CHARS (VAR_CHARS | /[0-9]/)*
|
||||||
TYPE: "booléen" | "entier" | "texte" | "liste"
|
|
||||||
|
|
||||||
KEYWORD: "ajouter" | "afficher"
|
|
||||||
|
|
||||||
ENTIER: /[1-9][0-9]*/
|
|
||||||
|
|
||||||
BOOLEEN: "vrai" | "faux"
|
|
||||||
|
|
||||||
STRING: /\w+/
|
|
||||||
|
|
||||||
|
|
||||||
|
ADD_KW: "ajouter"
|
||||||
|
SHOW_KW: "afficher"
|
||||||
|
WHILES_KW: "tant que"
|
||||||
|
WHILEE_KW: "faire"
|
||||||
|
|
||||||
|
keyword: ADD_KW
|
||||||
|
| SHOW_KW
|
||||||
|
|
||||||
|
%import common.INT -> ENTIER
|
||||||
|
|
||||||
|
TRUE_KW: "vrai"
|
||||||
|
FALSE_KW: "faux"
|
||||||
|
|
||||||
|
BOOLEEN: TRUE_KW
|
||||||
|
| FALSE_KW
|
||||||
|
|
||||||
|
%import common.ESCAPED_STRING -> STRING
|
||||||
|
|
||||||
|
COMMENT: /#[^\n]*/
|
||||||
|
%ignore COMMENT
|
||||||
|
|
||||||
|
%import common.WS
|
||||||
|
%ignore WS
|
||||||
|
6
spf.py
6
spf.py
@ -29,11 +29,13 @@ def main():
|
|||||||
print("Trace activated (TODO)", file=sys.stderr)
|
print("Trace activated (TODO)", file=sys.stderr)
|
||||||
|
|
||||||
with open("spf.lark") as grammar:
|
with open("spf.lark") as grammar:
|
||||||
spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer)
|
spf_parser = lark.Lark(grammar, parser="lalr", strict=True, debug=True)
|
||||||
|
|
||||||
with open(args.spf_file) as spf_input:
|
with open(args.spf_file) as spf_input:
|
||||||
program = spf_input.read()
|
program = spf_input.read()
|
||||||
spf_parser.parse(program)
|
parsed = spf_parser.parse(program)
|
||||||
|
|
||||||
|
print(parsed.pretty())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user