Grako - How to do error handling?

2024/10/13 17:17:42

How do I do error handling with Grako?

EBNF (MyGrammar.ebnf):

pattern   = { tag | function }* ;
tag       = tag:( "%" name:id "%" );
function  = function:("$" name:id "()" );
id        = ?/([^\\%$,()=])+/? ;

I'm generating the parser with

python -m grako --whitespace '' MyGrammar.ebnf > my_parser.py

Parsing an empty string and a "bad" string (that could not be matched by the grammar) both results to an empty AST Closure.

parser = MyGrammarParser()
ast = parser.parse(u"%test%", rule_name='pattern')     #ast contains something
ast = parser.parse(u"", rule_name='pattern')           #ast = []
ast = parser.parse(u"$bad $test", rule_name='pattern') #ast = []

And additionally: Is there any error message like 'expected foo at position 123'?

Answer

For starters, the first rule does match the empty string. Perhaps you'd want to try something like:

pattern   = { tag | function }+ $ ;

Yes, the generated parser will raise an exception if it can't parse the input string; note the $in the above rule: it tells the parser it should see the end of the input in that position. Without it, the parser is happy to succeed having parsed only part of the input.

Then, I don't think that named elements within named elements will produce the desired results.

This is a version of the grammar that may produce what you want:

pattern   = { tag | function }+ $ ;
tag       = ( "%" tag:id "%" );
function  = ("$" function:id "()" );
id        = ?/([^\\%$,()=])+/? ;
https://en.xdnf.cn/q/118057.html

Related Q&A

How get the softlayer storage credendials?

Im trying to get the Username,password and host IQN of a authorized Softlayer Network Storage. I used this python script, but the shell returns []import SoftLayerAPI_USERNAME = xxxAPI_KEY = yyyystorage…

dopy.manager.DoError: Unable to authenticate you

Im trying to configure a Virtual Machine(with Vagrant and Ansible), that needs a file.py to the full correct configuration of this machine (according to the book that Im studying),Im was using the Digi…

subprocess.Popen: OSError: [Errno 2] No such file or directory only on Linux

This is not a duplicate of subprocess.Popen: OSError: [Errno 13] Permission denied only on Linux as that problem occurred due to wrong permissions. That has been fixed and this is an entirely different…

Windows Theano Keras - lazylinker_ext\mod.cpp: No such file or directory

I am installing Theano and Keras follwing the How do I install Keras and Theano in Anaconda Python on Windows?, which worked fine for me with an older release before. Now I have upgraded to the latest…

Python Threading: Making the thread function return from an external signal

Could anyone please point out whats wrong with this code. I am trying to return the thread through a variable flag, which I want to control in my main thread. test27.pyimport threading import timelock …

Python join data lines together

Hello i have dataset a few thousand lines which is split in even and odd number lines and i cant find a way to join them together again in the same line. Reading the file and overwriting it is fine or …

How to undraw plot with Zelle graphics?

This is a code problem for Python 3.5.2 using John Zelles graphics.py:I have spent a good amount of time looking for the answer here, but just can not figure it out. The function undraw() exists just l…

/bin/sh: line 62: to: command not found

I have a python code in which I am calling a shell command. The part of the code where I did the shell command is:try:def parse(text_list):text = \n.join(text_list)cwd = os.getcwd()os.chdir("/var/…

Using PyMySQL with MariaDB

I have read this article about switching from MySQL to MariaDB on Ubuntu. The article claims that it would be not problem to switch between the two. Currently, I am using PyMySQL to connect to my MySQL…

Overloading str in Python

I have code that looks like the following:class C(str):def __init__(self, a, b):print(init was called!)super().__init__(b)self.a = ac = C(12, c)When I try to run it, it gives me the following error:Tra…