Advertisement
hakonhagland

regexp-grammar-c

Apr 9th, 2021
1,786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.09 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Data::Printer;
  4. use Regexp::Grammars;
  5. {
  6.     my $parser = qr {
  7.        <nocontext:>
  8.        <debug: off>
  9.  
  10.        <C>
  11.        <rule:  C>                      <FunctionWithoutParams>
  12.        <rule:  FunctionWithoutParams>  <ReturnType> <FunctionName> \(\) <CodeBlock>
  13.  
  14.        <token: ReturnType>             \w+
  15.        <token: FunctionName>           \w+
  16.  
  17.        <rule:  CodeBlock>              \{ <Blocks> \}
  18.        <rule:  Blocks>                 <[Block]>+
  19.        <rule:  Block>                  <IF> | <Statement>
  20.  
  21.        <rule:  IF>                     if \( <BoolExpr> \) <CodeBlock>
  22.        <token: BoolExpr>               .*?
  23.  
  24.        <token: Statement>              .*?;
  25.     }xms;
  26.     my $data = '
  27. void func() {
  28.    int x = 10;
  29.    if( x == 10 ) {
  30.        printf("print statement");
  31.        int y = 20;
  32.        if( y == 20 ) {
  33.            printf("y equals twenty");
  34.        }
  35.    }
  36. }';
  37.  
  38.     if ($data =~ $parser) {
  39.         my %res = %/;
  40.         p \%res;
  41.         #print Dumper \%res;
  42.     }
  43.     else {
  44.         warn "No match";
  45.     }
  46. }
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement