spurperl has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I want to use Parse::RecDescent to parse a certain assembly language. In assembly, data is "line oriented", that is newline is the statement separator. Simplified demonstration:

command1 arg1 \n
command2 arg2 \n
Defines two statements. More than one statement on a line is illegal, a statement broken to two lines is also illegal.

Now, P::RD has an issue with newlines. It swallows them by default. There is a workaround using the "skip" directive, but it became tedious and my grammar code is full of <skip: qr/ \t*/> directives.

Is there an easier way ?

What I want, for starters, is a trivial "command arg" pairs for statements:

line -> command arg "\n"

Replies are listed 'Best First'.
•Re: Handling newlines in P::RD
by merlyn (Sage) on Nov 15, 2004 at 17:34 UTC
      Just to clarify for the OP, "It stays set through all invoked rules as well" means if you place it in your start rule, it'll affect all your rules.
        Thanks, this is the approach I've eventually taken. Still, looking at my grammar and at the attemps I did trying stuff, it's clear that P::RD has an issue with line-oriented input. It's possible, but adds difficulties, mainly because its default "skippable" chars include a newline.

        This can be trivially solved with a tiny bit of preprocessing, but I tried a pure P::RD approach.

        To conclude - it works, it even looks clean enough and not *too* complicated.

        Thanks for the feedback !

Re: Handling newlines in P::RD
by Boots111 (Hermit) on Nov 16, 2004 at 04:25 UTC
    spurperl~

    I cannot suggest a good method, but you could always cheat by doing a s/\n/MONKEY/g on your text before hand and have Parse::RecDescent use "MONKEY" as its statement delimiter...

    Boots
    ---
    Computer science is merely the post-Turing decline of formal systems theory.
    --???
Re: Handling newlines in P::RD
by metaperl (Curate) on Dec 03, 2004 at 02:13 UTC