How to have Variable as Recursive Regex in Perl? -


i'm writing simple translator john tromp's binary lambda calculus on de bruijn notation lambda calculus can understand how lambda files working in 2012 "most functional" international obfuscated c code winner

here example of language before translation primes.blc:

00010001100110010100011010000000010110000010010001010111110111101001000110100001110011010000000000101101110011100111111101111000000001111100110111000000101100000110110 

i'm having trouble nested regex in commented line before primes.txt file save section of bruijn.pl:

#!/usr/bin/env perl #use strict; use warnings; use io::file; use cwd; $originalcwd = getcwd()."/"; #primes.blc argument test conversion #______________________________________________________________________open file ($name) = @argv; $file = new io::file; $file->open("< ".$originalcwd."primes.blc") || die("could not open file!"); #$file->open("< ".$name) || die("could not open file!"); while (<$file>){ $field .= $_; } $file->close; #______________________________________________________________________translate $field =~ s/(00|01|(1+0))/$1 /gsm; $field =~ s/00 /\\ /gsm; $field =~ s/01 /(a /gsm; $field =~ s/(1+)0 /length($1)." "/gsme;  $recursparenthesesregex = m/\(([^()]+|(??{$recursparenthesesregex}))*\)/; #$field =~ 1 while s/(\(a){1}(([\s\\]+?(\d+|$recursparenthesesregex)){2})/\($2\)/sm; #______________________________________________________________________save file #$fh = new io::file "> ".$name; $fh = new io::file "> ".$originalcwd."primes.txt"; if (defined $fh) { print $fh $field; $fh->close; } 

an translated file primes.txt should be:

\ (\ (1 (1 ((\ (1 1) \ \ \ ((1 \ \ 1) (\ (((4 4) 1) (\ (1 1) \ (2 (1 1)))) \ \ \ \ ((1 3) (2 (6 4)))))) \ \ \ (4 (1 3))))) \ \ ((1 \ \ 2) 2)) 

currently line commented out translates readable format looks like:

\ (a \ (a 1 (a 1 (a (a \ (a 1 1 \ \ \ (a (a 1 \ \ 1 (a \ (a (a (a 4 4 1 (a \ (a 1 1 \ (a 2 (a 1 1 \ \ \ \ (a (a 1 3 (a 2 (a 6 4 \ \ \ (a 4 (a 1 3 \ \ (a (a 1 \ \ 2 2  

which needs find innermost abstractions of (a , 2 of either number or matching parentheses , contents , insert trailing ) , remove a way outermost application.

you need regex

 # (\(a)(([\s\\]*?(?:\d+|(?&recursparens))){2})(?(define)(?<recursparens>(?>\((?>(?>[^()]+)|(?:(?=.)(?&recursparens)|))+\))))   ( \(a )                       # (1)  (                             # (2 start)       (                             # (3 start)            [\s\\]*?             (?:                 \d+               |                   (?&recursparens)             )       ){2}                          # (3 end)  )                             # (2 end)   (?(define)        (?<recursparens>              # (4 start)            (?>                 \(                 (?>                      (?> [^()]+ )                   |  (?:                           (?= . )                           (?&recursparens)                         |                        )                 )+                 \)            )       )                             # (4 end)  ) 

with perl code

use strict; use warnings; use feature qw{say};  $field = "00010001100110010100011010000000010110000010010001010111110111101001000110100001110011010000000000101101110011100111111101111000000001111100110111000000101100000110110";  $field =~ s/(00|01|(1+0))/$1 /g; $field =~ s/00 /\\ /g; $field =~ s/01 /(a /g; $field =~ s/(1+)0 /length($1)." "/ge;  1 while $field =~ s/(\(a)(([\s\\]*?(?:\d+|(?&recursparens))){2})(?(define)(?<recursparens>(?>\((?>(?>[^()]+)|(?:(?=.)(?&recursparens)|))+\))))/\($2\)/g;  $field =~ s/\( /\(/g;  $field; 

that give output

\ (\ (1 (1 ((\ (1 1) \ \ \ ((1 \ \ 1) (\ (((4 4) 1) (\ (1 1) \ (2 (1 1)))) \ \ \ \ ((1 3) (2 (6 4)))))) \ \ \ (4 (1 3))))) \ \ ((1 \ \ 2) 2)) 

that can formatted

 \   (                             # (1 start)       \        (                             # (2 start)            1             (                             # (3 start)                 1                  (                             # (4 start)                      (                             # (5 start)                           \                            ( 1 1 )                       # (6)                           \ \ \                            (                             # (7 start)                                ( 1 \ \ 1 )                   # (8)                                (                             # (9 start)                                     \                                      (                             # (10 start)                                          (                             # (11 start)                                               ( 4 4 )                       # (12)                                               1                                          )                             # (11 end)                                          (                             # (13 start)                                               \                                                ( 1 1 )                       # (14)                                               \                                                (                             # (15 start)                                                    2                                                     ( 1 1 )                       # (16)                                               )                             # (15 end)                                          )                             # (13 end)                                     )                             # (10 end)                                     \ \ \ \                                      (                             # (17 start)                                          ( 1 3 )                       # (18)                                          (                             # (19 start)                                               2                                                ( 6 4 )                       # (20)                                          )                             # (19 end)                                     )                             # (17 end)                                )                             # (9 end)                           )                             # (7 end)                      )                             # (5 end)                      \ \ \                       (                             # (21 start)                           4                            ( 1 3 )                       # (22)                      )                             # (21 end)                 )                             # (4 end)            )                             # (3 end)       )                             # (2 end)       \ \        (                             # (23 start)            ( 1 \ \ 2 )                   # (24)            2       )                             # (23 end)  )                             # (1 end) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -