[erlang-questions] leex and yecc spotting double newline

jm <>
Mon Mar 23 07:12:06 CET 2009


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?

2) When parsing the example at the above URL (minus the postfix version 
comments) it seems to have problems with the "sender=". Why?

   code used to parse example (based on code from 
http://hopper.squarespace.com/blog/2008/5/29/leex-and-yecc.html ):

  parse(Filename) ->
      {ok, InFile} = file:open(Filename, [read]),
      Acc = loop(InFile, []),
      file:close(InFile),
      postfix_policy_parser:parse(Acc).

  loop(InFile, Acc) ->
      case io:request(InFile, {get_until, prompt, postfix_policy_lexer, 
token, [1]}) of
          {ok, Token, EndLine} ->
              io:format("Token: ~p EndLine: ~p~n", [Token, EndLine]),
              loop(InFile, Acc ++ [Token]);
          {error, token} ->
              exit(scanning_error);
          {eof,_} ->
              Acc
      end.


Of course any other pointers are welcomed.


Jeff.




More information about the erlang-questions mailing list