# Lexer - How to handle conflicts? (Menhir)

**URL:** <https://discuss.ocaml.org/t/lexer-how-to-handle-conflicts-menhir/13717>\
**Category:** Learning\
**Tags:** parsing, lexer\
**Created:** [December 26, 2023, 3:17pm UTC](https://discuss.ocaml.org/t/lexer-how-to-handle-conflicts-menhir/13717 "2023-12-26T15:17:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [December 26, 2023, 3:17pm UTC](https://discuss.ocaml.org/t/lexer-how-to-handle-conflicts-menhir/13717/1 "2023-12-26T15:17:59Z")

</div>

Hey! I am setting up a tiny DSL, with the structure:

```auto
user: id uuidv4, name name

```

The idea is to generate CSV files based on this, and I am trying to figure out how to handle conflicting lexer rules. I have the following:

```auto
let white = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
let id = ['a'-'z' 'A'-'Z' '_' '-']*
rule read = 
  parse 
  | id { IDENTIFIER (Lexing.lexeme lexbuf) }
  | "uuidv4" { UUIDV4 }
  | "name" { NAME }

```

And then

```auto
expr:
  | tbl = IDENTIFIER; COLON; r = row; NEWLINE; e = expr { Table (tbl,r, e) }
  | tbl = IDENTIFIER; COLON; r = row; SEMICOLON; e = expr { Table (tbl,r, e) }
  | tbl = IDENTIFIER; COLON; r = row { Table (tbl,r, End) }
  ;

row: 
  | row_title = IDENTIFIER; NAME; COMMA; r = row { Name (row_title, r) }
  | row_title = IDENTIFIER; NAME { Name (row_title, End) }
  ;

```

And the challenge is that the lexer never identifier name because it has a conflict with id, is there any way to make it use a different lexer depending on the situation? Or how do I best resolve this?

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [December 26, 2023, 4:32pm UTC](https://discuss.ocaml.org/t/lexer-how-to-handle-conflicts-menhir/13717/2 "2023-12-26T16:32:08Z")

</div>

In the parser, you can define a production that recognizes both an identifier and name, and then you can use it in the other productions:

```yacc
id_or_name:
  | IDENTIFIER { $1 }
  | NAME { "name" }

expr:
  | tbl = id_or_name; ...

```

---

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [December 26, 2023, 6:14pm UTC](https://discuss.ocaml.org/t/lexer-how-to-handle-conflicts-menhir/13717/4 "2023-12-26T18:14:46Z")

</div>

Thanks! This solved the issue.
