Comments (5)
from lark.
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.
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.
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.
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)
- Issue of installing lark in Python HOT 1
- Pipe in terminal regex not working as expected HOT 1
- Transformer Not Applying Expected Transformations in Lark Parser HOT 3
- Deprecation Warning HOT 6
- accepts() vs choices() in InteractiveParser HOT 10
- No such file or directory: 'COMMON.lark' HOT 4
- Grammar Syntax For Unordered Groups HOT 2
- Is it possible to parse parts of the input? HOT 12
- Forgiving syntax HOT 3
- Post 1388 changes HOT 4
- Dynamic Earley: Incorrect value for SymbolNode.end
- Inconsistent parse results from simple ambiguous grammar HOT 4
- Superfluous identical ambiguities in Earley HOT 3
- Porting from pyparsing match_previous_literal HOT 4
- _TERMINAL appears in tree HOT 1
- Lexer matches shorter literals before longer literals HOT 1
- Priorities not working within recursive rules
- Error in parsing datetime strings HOT 1
- Ambiguities and Priorities in 1.2.1 earley HOT 6
- tests/test_cache.py and Python >= 3.12: SyntaxWarning: invalid escape sequence '\w'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lark.