정규식 재귀JSL은 재귀 적 정규식을 지원합니까?
나는 노력했다? R과 \ g <0> 둘 다 작동하지 않는 것 같습니다.
regex("test(1, (2, 3));", "\(([^()]|(?R))*\");재 : regex 재귀몇 분 남았 어. 위키피디아 왼쪽 재귀없이 규칙 집합을 제공합니다 ... 빠르게 실행됩니다.
text = "(1+2)*(3+4+5)*6*7*8*21*(0+(1+(2*3))+4*(5+6)*7*8+(((12+13+14)*15+16+17)*18+19+20))*22"; // plus, star, paren, integer. pattern does not handle spaces.
stack = {}; // the "working" stack... holds the RPN numbers
// the following add and mul routines pop two numbers and push the result...
addstack = Function( {},
{num1 = Num( Remove From( stack )[1] ), num2 = Num( Remove From( stack )[1] )},
Insert Into( stack, Char( num1 + num2 ) )
);
mulstack = Function( {},
{num1 = Num( Remove From( stack )[1] ), num2 = Num( Remove From( stack )[1] )},
Insert Into( stack, Char( num1 * num2 ) )
);
dummy = {}; // dummy variable with a subscript that will evaluate as needed
// BNF-like grammar of Expression, Term, Factor, Constant
// >? is the conditional assignment from LHS to RHS. The >>
// immediate assignment will not work because of back-up and retry
// which would push incorrect items to stack. The RHS of the >? is either
// the dummy[] list or working stack. The dummy stack reference uses the
// calculation of its subscript to manipulate the working stack.
// As the grammar/pattern runs, the expression is evaluated.
/*
E = (Expr( T ) + "+" + Expr( E )) >? dummy[addstack(); 1] | Expr( T );
T = (Expr( T ) + "*" + Expr( F )) >? dummy[mulstack(); 1] | Expr( F );
F = ("(" + Expr( E ) + ")") | Expr( C );
C = Pat Span( "0123456789" ) >? stack[N Items( stack ) + 1];
*/
// re-written without left recursion
// https://en.wikipedia.org/wiki/Left_recursion#Removing_left_recursion
//
E = Expr( T ) + Expr( EP );
EP = ("+" + Expr( T ) >? dummy[addstack(); 1] + Expr( EP )) | "";
T = Expr( F ) + Expr( TP );
TP = ("*" + Expr( F ) >? dummy[mulstack(); 1] + Expr( TP )) | "";
F = ("(" + Expr( E ) + ")") | Expr( C );
C = Pat Span( "0123456789" ) >? stack[N Items( stack ) + 1];
rc = Pat Match( text, Pat Pos( 0 ) + E + Pat R Pos( 0 ), NULL, FULLSCAN );
If( rc,
Show( rc, Num( stack[1] ), Eval( Parse( text ) ) ); // use JMP's parse to verify the answer
,
Show( rc )
);
면밀히 따라 가면 첫 번째 버전이 왼쪽 재귀와 함께 어떻게 실행되는지 궁금해 할 것입니다. 왜 재귀가 사용 가능한 메모리를 다 쓸 때까지 계속 반복되지 않는가? 기본적으로 패턴 매처는 각 expr (...)이 적어도 하나의 문자와 일치해야한다고 가정합니다. 어떤 시점에서 재귀는 충분히 깊어서 충분한 텍스트가 남아 있지 않고 일치하는 부분이 백업됩니다. 백업하고 다시 시도하는 모든 작업에는 긴 문자열이 필요합니다. 두 번째 예에서 왼쪽 재귀없이 patmatch ()는 expr ()이 이제 ""(영숫자)와 일치 할 수 있으므로 FULLSCAN 옵션을 사용하여 가정을 해제해야합니다.
재 : regex 재귀아니요, 패턴 매치러는 재귀를 좋아합니다. 다음은 표현 인터프리터의 전체 예제입니다. 나는 문법 전문가가 아니야. 나는 이것이 재귀 하강 파서 , 나는 약 10-20 명의 연산자를 가진 표현식이 눈에 띄게 느려지도록 적당히 긴 표현식을 추적하는 것을 많이 생각한다고 생각합니다.
PatMatch는 패턴이 일치하면 1을 반환하고 실패하면 0을 반환합니다 (불균형 괄호, 유효하지 않은 연산자, 잘못된 숫자).
text = "3+4*((5+7)*(9+11*2+2)+13)"; // plus, star, paren, integer. pattern does not handle spaces.
stack = {}; // the "working" stack... holds the RPN numbers
// the following add and mul routines pop two numbers and push the result...
addstack = Function( {},
{num1 = Num( Remove From( stack )[1] ), num2 = Num( Remove From( stack )[1] )},
Insert Into( stack, Char( num1 + num2 ) )
);
mulstack = Function( {},
{num1 = Num( Remove From( stack )[1] ), num2 = Num( Remove From( stack )[1] )},
Insert Into( stack, Char( num1 * num2 ) )
);
dummy = {}; // dummy variable with a subscript that will evaluate as needed
// BNF-like grammar of Expression, Term, Factor, Constant
// >? is the conditional assignment from LHS to RHS. The >>
// immediate assignment will not work because of back-up and retry
// which would push incorrect items to stack. The RHS of the >? is either
// the dummy[] list or working stack. The dummy stack reference uses the
// calculation of its subscript to manipulate the working stack.
// As the grammar/pattern runs, the expression is evaluated.
E = (Expr( T ) + "+" + Expr( E )) >? dummy[addstack(); 1] | Expr( T );
T = (Expr( T ) + "*" + Expr( F )) >? dummy[mulstack(); 1] | Expr( F );
F = ("(" + Expr( E ) + ")") | Expr( C );
C = Pat Span( "0123456789" ) >? stack[N Items( stack ) + 1];
rc = Pat Match( text, Pat Pos( 0 ) + E + Pat R Pos( 0 ) );
If( rc,
Show( rc, Num( stack[1] ), Eval( Parse( text ) ) ); // use JMP's parse to verify the answer
,
Show( rc )
);