Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Parsing with Regexes and Beyond

by moritz (Cardinal)
on Jun 06, 2008 at 07:02 UTC ( #690624=perltutorial: 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;
    ...
              [ 'AddOp', '+'  ],
              [ 'Number', '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
    # parse tree for '2 * (3+4)':
    [
    ...
        '*',
        [ '3', '+', '4' ],
    ];
    
  10. or download this
    my $parse_tree = expression();
    match('EOF');
    
  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: perltutorial [id://690624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-11-28 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls