From 4ee05452c08b4cb248a0bc22e5ed5f13f737ab9b Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 16 Mar 2025 11:34:15 +0100 Subject: [PATCH] adding parser code --- spf.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spf.py b/spf.py index 6780ae8..f0c006c 100755 --- a/spf.py +++ b/spf.py @@ -7,17 +7,20 @@ import argparse import lark import sys +class SPFTransformer(lark.Transformer): + pass + def main(): - parser = argparse.ArgumentParser() - parser.add_argument("spf_file", help="Fichier source à interpréter") - parser.add_argument("-d", "--dump", + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument("spf_file", help="Fichier source à interpréter") + arg_parser.add_argument("-d", "--dump", help="affichage de la mémoire du programme", action="store_true") - parser.add_argument("-t", "--trace", + arg_parser.add_argument("-t", "--trace", help="affichage de la mémoire au cours du programme", action="store_true") - args = parser.parse_args() + args = arg_parser.parse_args() if args.dump: print("Dump activated (TODO)", file=sys.stderr) @@ -25,6 +28,13 @@ def main(): if args.trace: print("Trace activated (TODO)", file=sys.stderr) + with open("spf.lark") as grammar: + spf_parser = lark.Lark(grammar, parser="lalr", transformer=SPFTransformer) + + with open(args.spf_file) as spf_input: + program = spf_input.readall() + spf_parser.parse(program) + if __name__ == "__main__": main()