Regex help

Simon Wistow simon at thegestalt.org
Fri Apr 20 22:23:36 BST 2012


On Fri, Apr 20, 2012 at 08:37:42PM +0000, Anthony Lucas said:
> 
> How about a parser using Regexp? Parser::MGC?
> 
> You can implement a proper parser, or just a few staged regexp, or anything in-between.

I ended up just doing this

    my ($label, $op, $operands) = $line =~ m!
        ^
        (?::(\w+)      \s+)? # optional label
        ([A-Za-z]+)    \s+   # opcode
        (.+)           \s*   # operands
        $
    !x;
    # TODO this won't cope with commas inside quotes 
    # e.g DAT 0x20, "hello, goodbye", 0x10
    my @operands = split /\s*,\s*/, ($operands || "");

at some point I should properly parse the assembler file using a grammar 
but since the spec is still in flux I'll go with my bodged parser for 
now.

If any of you care you can see the code here

http://search.cpan.org/~simonw/CPU-Emulator-DCPU16-0.3/
https://github.com/simonwistow/CPU-Emulator-DCPU16

Simon




More information about the london.pm mailing list