Questions tagged [sly]

This tag is for the Python parser generator Sly, a proposed redesign by the same author of the Ply parser. On October 11, 2022, the author indicated that Sly has been retired and work on it will not continue, although the github repository will be maintained. Do not use this tag for questions related to the Emacs Lisp IDE with the same name. For those, use the emacs tag or post on http://emacs.stackexchange.com.

Sly was a LALR(1) parser generator written in Python by David Beazley, creator of the Ply parser generator, which Sly was intended to replace. On October 11, 2022, Beazley officially retired Sly, although he indicated that the Github repository will continue to exist. The version on that repository requires Python v3.6 or above.

Documentation was kept on Read the Docs, but it was not updated to reflect the last changes to Sly.

It is important that questions identify the version number (sly.__version__) or the Github repository date of the Sly installation.

20 questions
8
votes
1 answer

When can you use a name before it's defined?

In SLY there is an example for writing a calculator (reproduced from calc.py here): from sly import Lexer class CalcLexer(Lexer): tokens = { NAME, NUMBER } ignore = ' \t' literals = { '=', '+', '-', '*', '/', '(', ')' } # Tokens …
COVFEFE-19
  • 293
  • 1
  • 8
4
votes
1 answer

Trying to solve dangling else for a Mini-C grammar

I have this grammar: translation_unit ::= external_declaration | translation_unit external_declaration external_declaration ::= function_definition | declaration function_definition ::= type_specifier declarator…
Juan_2054
  • 51
  • 3
2
votes
2 answers

how to do multi-line parsing with python's sly.Parser class?

I am trying to create my own programming language with a library called sly. I have created a lexer to tokenize my program but I am stuck on getting the parser to successfully parse multiple instructions. When I didn't take this into account and…
error 1044
  • 111
  • 1
  • 6
2
votes
2 answers

Sly parser: condition rule not recalled

Hello I am building my first programming language with sly module in python. I was following some tutorials and I have a question about how the parser behaves. This is the code: from sly import Lexer from sly import Parser class BasicLexer(Lexer): …
Mazzola
  • 75
  • 7
1
vote
1 answer

Trying to make assembly-level sly programming language

I'm trying to write a low-level parser using sly and python. However, when I run my code I get an error: syntax error: "1", at line 1 The code in question is: #!/bin/python3 from sly import Lexer, Parser class LowLexer(Lexer): tokens = {DAT,…
1
vote
1 answer

SLY can't produce Empty Productions rule

I am trying write a parser code for following production rule. Statement: Variable+ | epsilon My initial implementation: ''' Statement: Variables | epsilon Variables: Variable Variables | Variable (one or more variable) epsilon: null ('') '''…
Tom
  • 21
  • 5
1
vote
2 answers

How to parse multiple statements in sly?

I am trying to parse some code using sly. I would like to separate the statements with a semicolon. I have defined a token called SEMI which represents a semicolon: class MyLexer(Lexer): tokens = { ..., SEMI } SEMI =…
macic
  • 21
  • 1
  • 4
1
vote
1 answer

SLY python can't parse simple tokens

I'm working on making a simple interpreted programming language using SLY to generate a AST which I will interpret without using SLY. Currently I have been able to generate all my tokens and giving them to the parser, but it can't recognize any…
Ivan Lo Greco
  • 315
  • 1
  • 9
1
vote
2 answers

Is it possible to easily eliminate this reduce/reduce conflict?

This is my code for if statements that might have elses or elsifs for the scripting language I'm writing a parser for. I'm actually rather proud of how compact I got it: @_( 'IF LPAREN expr_comp RPAREN THEN NEWLINE statement_set { elsif_statement }…
Sean Duggan
  • 1,105
  • 2
  • 18
  • 48
1
vote
1 answer

Sly: How can I implement if and while statements and commands

I'm new in sly and I'm trying to write simple language with sly python. I want to implement if-else, while loop and print command. I tried to search a lot, but there aren't many tutorials about sly. I'm totally confused how can I make it. I want…
Jurakin
  • 832
  • 1
  • 5
  • 19
1
vote
1 answer

How to avoid printing a variable without using the say function in python sly?

So I am using a python package sly which has a lexer and parser class. I am making my own programming language called NoobPy. So currently, the code will open test.noob and read each line and parse it. Now, if I were to define a variable, let's say…
Flampt
  • 23
  • 3
1
vote
1 answer

how to differentiate between 'int' and 'float' datatypes while using SLY in Python

I am using Sly package in Python to make a dummy Lexer. here is the documentation which only states that tokens which are numbers can only be identified as NUMBER as we, the programmer defines it: @_(r'\d+') def NUMBER(self, t): t.value =…
0
votes
1 answer

How to parse multiple tokens using python Sly Parser?

I have a lexer: from sly import Lexer class BasicLexer(Lexer): tokens = {OBJECT, FUNCTON} ignore = '.' OBJECT = r'object\(\"(.*?)\"\)' FUNCTION = r'function\(\"(.*?)\"\)' def OBJECT(self, t): match =…
sshussain270
  • 1,785
  • 4
  • 25
  • 49
0
votes
1 answer

How can I indicate an error during a parse operation?

Within the scripting language I am implementing, valid IDs can consist of a sequence of numbers, which means I have an ambiguous situation where "345" could be an integer, or could be an ID, and that's not known until runtime. Up until now, I've…
Sean Duggan
  • 1,105
  • 2
  • 18
  • 48
0
votes
1 answer

How do I iterate through a dictionary/set in SLY?

So, I'm trying to transition my code from my earlier PLY implementation to SLY. Previously, I had some code that loaded a binary file with a wide range of reserved words scraped from documentation of the scripting language I'm trying to implement.…
Sean Duggan
  • 1,105
  • 2
  • 18
  • 48
1
2