[erlang-questions] leex and yecc spotting double newline

Robert Virding <>
Wed Mar 25 22:16:31 CET 2009


Hi Jeff,

2009/3/23 jm <>

> Say the lexer definition contains, among other things,
>
> Definitions.
> W  = [^=\s\r\n]
> WS = \s
> CR = \r
> NL = \n
> EQ = =
>
> Rules.
> {W}+  : {token, {word, TokenLine, TokenChars}}.
> {EQ}  : {token, {eq, TokenLine, TokenChars}}.
> {WS}+ : {token, {ws, TokenLine, TokenChars}}.
> ({CR}|{NL}|{CR}{NL}) : {token, {nl, TokenLine, TokenChars}}.
>
>
> At the moment, the yecc definition file contains,
>
>  Nonterminals
>  request lines line lhs rhs words.
>
>  Terminals
>  eq word ws nl.
>
>  Rootsymbol request.
>  request -> lines : '$1'.
>
>  lines -> line nl : ['$1'].
>  lines -> line lines nl : ['$1'] ++ '$3'.
>
>  line -> lhs eq rhs nl : {line, '$1', '$3'}.
>
>  lhs -> word : {lhs, '$1'}.
>
>  rhs -> words : {rhs, '$1'}.
>
>  words -> word : ['$1'].
>  words -> word ws words : ['$1', '$2'] ++ '$3'.
>
>
> and is designed to parse the postfix policy request (see
> http://www.postfix.org/SMTPD_POLICY_README.html)
>
> A couple of questions:
>
> 1) What do I put in the yecc file so the it can spot a double newline
> which is used to terminate a request?


Just of the top of my head I think you could do it by defining:

request -> lines nl : {request,'$1'}.

lines -> line : ['$1'].
lines -> lines line : '$1' ++ ['$2'].

line -> lhs eq rhs nl : {line, '$1', '$3'}.

I think this will do it assuming you only allow one blank line between
requests and you always have one blank line after a request, even after the
last one. Sitting at a conference now so I will try it later tonight.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090325/34052761/attachment.html>


More information about the erlang-questions mailing list