Getting symbols with Lark parsing

2024/11/16 21:21:31

I'm trying to parse a little pseudo-code I'm writing and having some trouble getting values for symbols. It parses successfully, but it won't return a value the same as it would with "regular" characters. Here's an example:

>>> from lark import Lark
>>> parser = Lark('operator: "<" | ">" | "=" | ">=" | "<=" | "!="', start="operator")
>>> parsed = parser.parse(">")
>>> parsed
Tree(operator, [])
>>> parsed.data
'operator'
>>> parsed.value
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'Tree' object has no attribute 'value'

Why wouldn't there be a value? Is there another way to get the exact operator that was used?

Answer

Author of Lark here. Mike's answer is accurate, but a better way to get the same result is by using the "!" prefix on the rule:

>>> from lark import Lark
>>> parser = Lark('!operator: "<" | ">" | "=" | ">=" | "<=" | "!="', start="operator")
>>> parser.parse(">")
Tree(operator, [Token(__MORETHAN, '>')])
https://en.xdnf.cn/q/71625.html

Related Q&A

Why cant I connect to my localhost django dev server?

Im creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000…

How to use L2 pooling in Tensorflow?

I am trying to implement one CNN architecture that uses L2 pooling. The reference paper particularly argues that L2 pooling was better than max pooling, so I would like to try L2 pooling after the acti…

Abstract base class is not enforcing function implementation

from abc import abstractmethod, ABCMetaclass AbstractBase(object):__metaclass__ = ABCMeta@abstractmethoddef must_implement_this_method(self):raise NotImplementedError()class ConcreteClass(AbstractBase)…

Create dataframe from dictionary of list with variable length

I have a dictionary of list which is like - from collections import defaultdict defaultdict(list,{row1: [Affinity],row2: [Ahmc,Garfield,Medical Center],row3: [Alamance,Macbeth],row4: [],row5: [Mayday]}…

How to standardize ONE column in Spark using StandardScaler?

I am trying to standardize (mean = 0, std = 1) one column (age) in my data frame. Below is my code in Spark (Python):from pyspark.ml.feature import StandardScaler from pyspark.ml.feature import VectorA…

Pandas Dataframe - select columns with a specific value in a specific row

I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe

PermissionError: [Errno 13] Permission denied in Django

I have encountered a very strange problem.Im working with django, I create a directory on server, and try to save pickle file into it, this way:with open(path, wb) as output: pickle.dump(obj, output, p…

Evaluating Jacobian at specific points using sympy

I am trying to evaluate the Jacobian at (x,y)=(0,0) but unable to do so. import sympy as sp from sympy import * import numpy as np x,y=sp.symbols(x,y, real=True) J = Function(J)(x,y) f1=-y f2=x - 3*y*(…

PyAudio cannot find any output devices

When I run:import pyaudio pa = pyaudio.PyAudio() pa.get_default_output_device_info()I get:IOError: No Default Output Device AvailableWhen I say:pa.get_device_count()It returns 0L.And of course if I lis…

How do I write a Hybrid Property that depends on a column in children relationship?

Lets say I have two tables (using SQLAlchemy) for parents and children:class Child(Base):__tablename__ = Childid = Column(Integer, primary_key=True) is_boy = Column(Boolean, default=False)parent_id = C…