python - How to define grammar for minimum 1 of foo and maximum 1 of bar - Stack Overflow
In parsimonious, I'm trying to define a grammar where there has to be one occurrence of foo and zero to 1 occurrence of bar and in any order. How do I get parsimonious to throw an error ?
from parsimonious.grammar import Grammar
g = Grammar(r"""
text = ws* "section" ws* (foo / bar)+ ws*
foo = ws* "foo" ws*
bar = ws* "bar" ws*
ws = ~"\s*"
""")
print(g.parse("""section foo"""))
print(g.parse("""section foo bar bar""")) # How to throw error as more than one bar
print(g.parse("""section bar""")) # How to throw error as foo is required
print(g.parse("""section bar foo"""))
print(g.parse("""section foo bar"""))
In parsimonious, I'm trying to define a grammar where there has to be one occurrence of foo and zero to 1 occurrence of bar and in any order. How do I get parsimonious to throw an error ?
from parsimonious.grammar import Grammar
g = Grammar(r"""
text = ws* "section" ws* (foo / bar)+ ws*
foo = ws* "foo" ws*
bar = ws* "bar" ws*
ws = ~"\s*"
""")
print(g.parse("""section foo"""))
print(g.parse("""section foo bar bar""")) # How to throw error as more than one bar
print(g.parse("""section bar""")) # How to throw error as foo is required
print(g.parse("""section bar foo"""))
print(g.parse("""section foo bar"""))
Share
Improve this question
asked Dec 16, 2024 at 2:35
hm-distrohm-distro
112 bronze badges
1
|
1 Answer
Reset to default 0Your grammar isn't correct.
You are looking for
text = ws* "section" ws* foo* bar foo* ws*
最新文章
- 谷歌继续封死华为后路,Mate 30无法安装谷歌服务
- 谷歌跟微软干上了:公布更多Windows安全漏洞
- 台北电脑展10大杀手级电脑硬件
- 为何政府机关被禁止采购Win 8系统?
- 谷歌收购移动软件商Quickoffice 整合Apps
- 台北电脑展周二开幕:Windows 8成焦点
- How to efficiently read HDF5 file using NetCDF-java - Stack Overflow
- caching - Django: Slow Dashboard with Aggregated Data and Related Models - Stack Overflow
- python - How to send multiple images to azure custom vision api without going over the transaction limit of azure? - Stack Overf
- Unable to Access Event Data via Eventbrite API - 404 NOT_FOUND Error - Stack Overflow
- Issue with Ruby in CICD environment on Linux: usrbinenv: 'ruby.exe': No such file or directory - Stack Overflow
- How to optimize query performance in a large fact table with billions of rows? - Stack Overflow
- typescript - How to fix GitHub building "unknown token" error in yarn.lock for GitHub Pages? - Stack Overflow
- android - How to reduce height of a composable to wrap content without padding - Stack Overflow
- typescript - How to keep autosuggestion with generics when working with keyof? - Stack Overflow
- typescript - Declare object and internally refer to other values - Stack Overflow
- Composing dependent functions in Lean - Stack Overflow
foo
,foo bar
, orbar foo
. (Or should that be "at least one" occurrence offoo
, matching the title?) – chepner Commented Dec 16, 2024 at 14:01