# Lalrpop regex precedence problem

**URL:** <https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726>\
**Category:** help\
**Created:** [April 9, 2018, 1:54am UTC](https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726 "2018-04-09T01:54:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dre](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@dre](https://users.rust-lang.org/u/dre)\
**Post date:** [April 9, 2018, 1:54am UTC](https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726/1 "2018-04-09T01:54:00Z")

</div>

I'm trying to build a simple lalrpop parser for a simple expr language that skips the contents of comments (if it sees "`/*`" it matches everything until "'\*/").

I'm probably doing something stupid, but when I try to use lalrpop's regex precedence method, I'm getting weird results. Simple example:

```
use std::str::FromStr;
grammar;

pub Term = { Num, "(" <Term> ")" };
Num: i32 = r"[0-9]+" => i32::from_str(<>).unwrap();

match { 
	"(",
	")",
	r"[0-9]+",
} else {
	r"[a0-9].+" 
}

```

here there's two (very) overlapping regexs, which I am trying to prioritize. However, even though the lower-ranked regex `r"[a0-9].+"` is never used, it seems to derail matches that succeed when I don't have it:

```
	println!("result={:?}", TermParser::new().parse("(22)").unwrap());
	assert!(TermParser::new().parse("(22)").is_ok());

```

gives a panic with the output:  
` result=Err(UnrecognizedToken { token: Some((1, Token(0, "22)"), 4)), expected: ["\"(\"", "r#\"[0-9]+\"#"] })`

Any insight into the right way to do this? Or if there's a better approach?

Thanks!

---

<div class="post-metadata">

**Author:** ![dylan.dpc](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@dylan.dpc](https://users.rust-lang.org/u/dylan.dpc)\
**Post date:** [April 9, 2018, 5:38am UTC](https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726/2 "2018-04-09T05:38:48Z")

</div>

Hey

You can also join our [gitter](https://gitter.im/lalrpop/Lobby) channel and post your question there for a faster way of getting an answer.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/matklad/32/41370_2.png) [@matklad](https://users.rust-lang.org/u/matklad)\
**Post date:** [April 9, 2018, 9:48am UTC](https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726/3 "2018-04-09T09:48:54Z")

</div>

> [@dre](#):
>
> However, even though the lower-ranked regex r"[a0-9].+" is never used,

In lalrpop, lexing and parsing are separate phases. That is, the input is first lexed using all regexes from the grammar and the match section, and only then parsed, so, for lexing, it doesn't matter if you actually use a regex.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:47am UTC](https://users.rust-lang.org/t/lalrpop-regex-precedence-problem/16726/4 "2023-01-12T08:47:23Z")

</div>


