Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

RFC: Parsing with perl - Regexes and beyond

by moritz (Cardinal)
on Apr 03, 2008 at 08:31 UTC ( #678119=perlmeditation: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    term        -> atom operator term
    term        -> '(' term ')'
    term        -> atom
    operator    -> '*' | '/'| '+' | '-' 
    atom        -> \d+
    
  2. or download this
    term    ->
    atom operator term
    ...
    '2'  '*'      ('3'  '+'      term)
    '2'  '*'      ('3'  '+'      (atom))
    '2'  '*'      ('3'  '+'      ('4'))
    
  3. or download this
    expression  -> (term add_op)* term
    term        -> (factor mul_op)* factor
    ...
    add_op      -> '+' | '-'
    mul_op      -> '*' | '/'
    atom        -> \d+
    
  4. or download this
    expression
    term                   add_op term
    ...
    (factor  mulop factor) '+'    ('4')
    ((atom)  mulop (atom)) '+'    ('4')
    (('2')   '*'   ('3') ) '+'    ('4')
    
  5. or download this
    #!/usr/bin/perl
    use strict;
    ...
                '4'
              ]
            ];
    
  6. or download this
    sub match {
        my $expected_token = shift;
    ...
        }
        return 1;
    }
    
  7. or download this
    sub expression {
        my @res = (term());
    ...
    sub atom {
        match('Number');
    }
    
  8. or download this
    my $parse_tree = expression();
    print Dumper $parse_tree;
    ...
                '4'
              ]
            ];
    
  9. or download this
    return @res > 1 ? \@res :
    $res[0];
    
  10. or download this
    # parse tree for '2 * (3+4)':
    [
    ...
        '*',
        [ '3', '+', '4' ],
    ];
    
  11. or download this
    sub execute {
        my $tree = shift;
    ...
    }
    
    print execute($parse_tree), "\n";
    
  12. or download this
    sub make_rule {
        my ($operator, $next) = @_;
    ...
    
    *term       = make_rule('MulOp', \&factor);
    *expression = make_rule('AddOp', \&term);
    
  13. or download this
    grammar Arithmetic {
    
    ...
    
    # match it:
    $input ~~ Grammar.expression;
    
  14. or download this
        rule factor {
            | <atom>
            | '(' <expression> [ ')' || <panic: Expected ')'> ]
        }
    
  15. or download this
    # this example mixes official Perl 6 syntax an PGE syntax,
    # I don't really know if they are 100% compatible
    ...
    rule expression is optable { ... }; # yes, '...' is valid Perl 6 ;-)
    proto 'term:' is tighter('infix:*')
                  is parsed(&factor) { ... };
    
  16. or download this
    ModernArithmetic is Arithmetc {
        token infix:</> { '' } # different symbol, same rule name
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://678119]
Approved by Corion
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-09-01 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (300 votes), past polls