Code Monkey home page Code Monkey logo

Comments (5)

erezsh avatar erezsh commented on June 11, 2024 2

from lark.

MegaIng avatar MegaIng commented on June 11, 2024

Can you provide a full python script that results n this error message? The error message indicates that the file is empty, which doesn't seem correct.

from lark.

EliotBD03 avatar EliotBD03 commented on June 11, 2024

Sure !
the test set (test.spf, since I decided to call my language to spf):

entier var = 10;
entier var = 10 + 10 * 2;
10 / 10;
10 < 11;
11 < 10;
10 <= 10;
10 >= 10;
faux vaut vrai;
faux != vrai;
faux;
vrai;
faux et vrai;
vrai et vrai;
non vrai et faux;
"test";
"test"[0];
"tu" + "es";
taille "test";
[];
[1];
[1 , [2, 3]];
[1:3];
[0, 1, 1];
taille [0, 1, 1];
[1, 2][0];
[1,2,[3,4]];
entier x = 3;
booléen z = x < 4;
ajouter 2 dans [1];
ajouter 3 dans [1, 2];
ajouter [3, 4] dans [1, 2];
entier a = 3;
entier a = a + 1;
#ceci est un commentaire
entier a = a + 1;

the python code only takes this file to parse it

from sys import platlibdir, argv

from lark import Lark, Token, Transformer, Tree, v_args

from modules.statement import Statement
from modules.variable import global_context

debug = False

class Interpreter(Transformer):

    @v_args()
    def interpret(self, args, statement_type=Statement()):
        if isinstance(args, Tree):
            type = args.data.upper()
            return statement_type[type](self.interpret(args.children))
        elif isinstance(args, Token):
            return args.value
        elif len(args) == 1 and isinstance(args[0], Token):
            return args[0].value
        else:
            return [self.interpret(node) for node in args]
        
def memory():
    print("------MEMOIRE---------")
    for variable in global_context.keys():
        print(global_context[variable])


if __name__ == "__main__":
    print(argv)
    with open("spf.lark", "r") as grammar:
        interpreter = Interpreter()
        parser = Lark(
            grammar, start="statement", parser="lalr", transformer=interpreter
        )
        """
        while True:
            tree = parser.parse(input(">"))
            result = interpreter.interpret(tree)
            print(result)
            print(global_context)
        """    
        if "--debug" in argv[1:] or "-d" in argv[1:]:
            debug = True
        with open("modules/test.spf", "r") as file:
            for line in file:
                print("----------------------------------")
                print(f"TEST : " + line)
                tree = parser.parse(line)
                result = interpreter.interpret(tree)
        if "--memory" in argv[1:] or "-m" in argv[1:]:
            memory()        

and the full log

----------------------------------
TEST : entier var = 10;

----------------------------------
TEST : entier var = 10 + 10 * 2;

----------------------------------
TEST : 10 / 10;

----------------------------------
TEST : 10 < 11;

----------------------------------
TEST : 11 < 10;

----------------------------------
TEST : 10 <= 10;

----------------------------------
TEST : 10 >= 10;

----------------------------------
TEST : faux vaut vrai;

----------------------------------
TEST : faux != vrai;

----------------------------------
TEST : faux;

----------------------------------
TEST : vrai;

----------------------------------
TEST : faux et vrai;

----------------------------------
TEST : vrai et vrai;

----------------------------------
TEST : non vrai et faux;

----------------------------------
TEST : "test";

----------------------------------
TEST : "test"[0];

----------------------------------
TEST : "U" + " Sussy";

----------------------------------
TEST : taille "test";

----------------------------------
TEST : [];

----------------------------------
TEST : [1];

----------------------------------
TEST : [1 , [2, 3]];

----------------------------------
TEST : [1:3];

----------------------------------
TEST : [0, 1, 1];

----------------------------------
TEST : taille [0, 1, 1];

----------------------------------
TEST : [1, 2][0];

----------------------------------
TEST : [1,2,[3,4]];

----------------------------------
TEST : entier x = 3;

----------------------------------
TEST : booléen z = x < 4;

----------------------------------
TEST : ajouter 2 dans [1];

----------------------------------
TEST : ajouter 3 dans [1, 2];

----------------------------------
TEST : ajouter [3, 4] dans [1, 2];

----------------------------------
TEST : entier a = 3;

----------------------------------
TEST : entier a = a + 1;

----------------------------------
TEST : #ceci est un commentaire

Traceback (most recent call last):
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser_state.py", line 77, in feed_token
    action, arg = states[state][token.type]
                  ~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: '$END'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/CroissantCompiler/spf.py", line 50, in <module>
    tree = parser.parse(line)
           ^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/lark.py", line 658, in parse
    return self.parser.parse(text, start=start, on_error=on_error)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parser_frontends.py", line 104, in parse
    return self.parser.parse(stream, chosen_start, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser.py", line 42, in parse
    return self.parser.parse(lexer, start)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser.py", line 88, in parse
    return self.parse_from_state(parser_state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser.py", line 111, in parse_from_state
    raise e
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser.py", line 105, in parse_from_state
    return state.feed_token(end_token, True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/CroissantCompiler/lib/python3.12/site-packages/lark/parsers/lalr_parser_state.py", line 80, in feed_token
    raise UnexpectedToken(token, expected, state=self, interactive_parser=None)
lark.exceptions.UnexpectedToken: Unexpected token Token('$END', '') at line 1, column 1.
Expected one of: 
	* INTEGER_VALUE
	* PRINT
	* POUR
	* NON
	* IDENTIFIER
	* TEXT_VALUE
	* TAILLE
	* BOOLEAN_VALUE
	* TANT
	* TYPE
	* __ANON_5
	* AJOUTER
	* LSQB
	* SI
	* MINUS

from lark.

erezsh avatar erezsh commented on June 11, 2024

You didn't include spf.lark, which is the most important bit 🤷

But probably your grammar just doesn't support an empty program. (comments don't count as syntax)

from lark.

kazawai avatar kazawai commented on June 11, 2024

Hey, thanks a lot for your help !
Here's our grammar (sorry to not have included it beforehand) :

?start: statement+ -> interpret

    ?statement: assignment ";"
        | condition ";" -> condition
        | loop ";" -> loop
        | expression ";"
        | PRINT expression ";" -> print

    ?assignment: TYPE IDENTIFIER "=" expression -> initialization
        | TYPE IDENTIFIER -> declaration

    ?expression: comparison
        | arithmetic_op
        | string_e
        | boolean_e
        | list_e
        | variable

    ?block: "{" statement* "}"

    ?condition: "si" boolean_e "alors" block ("sinon" block)? -> if_else

    ?loop: "tant" "que" boolean_e "faire" block -> while
        | "pour" "chaque" TYPE IDENTIFIER "dans" expression "faire" block -> for

    ?comparison: expression ("vaut" | "==") expression -> equals
        | expression ("ne vaut pas" | "!=") expression -> not_equal
        | expression "<=" expression -> less_equal
        | expression ">=" expression -> greater_equal
        | expression "<" expression -> less
        | expression ">" expression -> greater

    ?arithmetic_op: INTEGER_VALUE -> int_atomic_value
        | "-" expression -> int_negation
        | expression "+" expression -> addition
        | expression "-" expression -> subtraction
        | expression "*" expression -> multiplication
        | expression "/" expression -> division

    ?boolean_e: BOOLEAN_VALUE -> bool_atomic_value
        | "non" boolean_e -> bool_negation
        | boolean_e "et" boolean_e -> conjunction
        | boolean_e "ou" boolean_e -> disjunction

    ?string_e: TEXT_VALUE -> string_atomic_value
        | string_e "+" string_e -> concat
        | "taille" string_e -> size
        | string_e "[" arithmetic_op "]" -> index

    ?list_e:  "[]" -> list_bc //basis case
        | "[" sequence "]" -> list_gc //general case
        | "[" arithmetic_op ":" arithmetic_op "]" -> range_list
        | list_e "+" list_e -> concat_list
        | list_e "["arithmetic_op"]" -> index_list
        | "taille" list_e -> size_list
        | "ajouter" expression "dans" list_e -> add

    ?sequence: expression ("," expression)*

  //  ?boolean: expression ("<" | ">" | "<=" | ">=") expression -> comparison
  //      | "non" boolean -> not

    ?variable: IDENTIFIER


// Tokens (type has a priority over identifier)
TYPE.1: /entier|texte|liste|booléen/

PRINT: "afficher"

IDENTIFIER: /[_a-zA-Zé][\wé_]*/
TEXT_VALUE.1: /"[^"]*"/
INTEGER_VALUE.1: /(0)|((-)?[1-9][0-9]*)/
BOOLEAN_VALUE.1: /faux|vrai/

DELIMITER: ";"

COMMENT: /^#.*\n$/
%ignore COMMENT
%ignore "\n"
%ignore " "

How could we define a rule to handle empty programs as you said since it, in fact, probably doesn't support it ?
We already tried to add things like "$" and "$END" but to no luck..
Thanks again for your help ^^

from lark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.