Matching strict whole word using flex, lex, leex

665 views Asked by At

I've just started using leex and yecc to do the lexer/parser stuff for a really simple control shell.

My lexer definition :

Definitions.

COMMAND =  STATUS|START|RELOAD|EXIT|STOP|RESTART
NAME       = [a-z_][a-z_0-9]*
WHITESPACE = [\s\t\n\r]

Rules.

{COMMAND}     : {token, {command,  TokenLine, list_to_atom(TokenChars)}}.
{NAME}        : {token, {name, TokenLine, TokenChars}}.
{WHITESPACE}+ : skip_token.

Erlang code.

Nothing much really. Simple case are working fine:

iex(0)> :lexer.string('START')
{:ok, [{:command, 1, :START}], 1}
iex(1)> :lexer.string('START STOP')
{:ok, [{:command, 1, :START}, {:command, 1, :STOP}], 1}
iex(2)> :lexer.string('START foobar')
{:ok, [{:command, 1, :START}, {:name, 1, 'foobar'}], 1}

But these have been bothering me:

iex(3)> :lexer.string('STARTo')
{:ok, [{:command, 1, :START}, {:name, 1, 'o'}], 1}

Instead i want something like:

iex(3)> :lexer.string('STARTo')
{:ok, [{:illegal, 'STARTo'}], 1}

But i can't figure how to make match at word boundary using these kind of tools (which use a slim impl. for regex btw)

Ps: In case, I know that this isn't the right way to do it, but just wanting to know if possible and how

EDIT Removing lex and flex tag since this seems to be limitation from leex and not from these *tools

0

There are 0 answers