[erlang-questions] yecc subpattern issue

Robert Virding <>
Mon Dec 29 11:46:17 CET 2008


2008/12/28 Phil Pirozhkov <>

> Thank you for helping me out again, Robert!
>
> The point was that adding elements to matched list doesn't work.
>
> This one works fine:
> blocks -> blocks block : '$1' ++ ['$2'].
>
> And this one (i used before):
> blocks -> block blocks : ['$1' | '$2'].
> (yes, blocks and block reverted here compared to the first example,
> because of $2 (blocks) should be a list, and $1 (block) - a value).
>
> This is odd, because [H|T] seems to be more erlang'ish (and more optimal i
> suggest?)
> than ++.


These two are basically equivalent. If I remember correctly then the first
one is more efficient from a yecc/LALR(1) point of view , while the second
is more erlangy. I would personally use the second style.

Just rolled back TokenLine changes, they doesn't affect parser at all, i.e.
> it works perfectly fine with leex provided tuples of the following format:
>
> [{'{'},{forc},{identifier,a},{in},{identifier,b},{'}'},{'{'},
>     {identifier,a},{'}'},{text," kg"},{'{'},{endc},{'}'}].


Yes, this will work as long as you don't get any parse errors. Yecc assumes
all tokens are tuples where the first element is the type and the second
element is the line number. It will access the the second element to get the
line number when it detects an error. Yecc ignores other elements in the
token tuple so you are free to put data in extra elements which can then be
accessed in the productions. That is the reason for giving the format of
tokens as

{Type,TokenLine} or {Type,TokenLine,TokenValue}

You can of course have more values if necessary. It is not wise not to
follow the "standard" token formats.


> Just to notice, '$end' is not required too, works fine without it.
> Maybe line numbers and '$end' should be marked as optional in yecc docs.
> I agree that line numbers are very helpful when it comes to seek for issues
> in the data provided to parser, but this only eats up memory when is not
> required.


Yecc is kind and adds a '$end' token, so it is optional. As I explained
above the line numbers are not optional and yecc assumes that they are
there.

The amount of extra memory needed for the line numbers is small and I
wouldn't worry about. Also they are so easy to generate from leex or a
hand-written scanner that there is really no reason not to put them in. Even
in LFE where I can't use yecc I still have the line numbers to give better
error reporting.

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


More information about the erlang-questions mailing list