bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: succinct awk code to check for paired parantheses?


From: Jakub Martisko
Subject: Re: succinct awk code to check for paired parantheses?
Date: Thu, 25 Mar 2021 14:47:25 +0100

Indeed, you need context free grammers to parse parentheses properly.
Basic regexps are weaker than that. The easiest way to parse them (I am
not providing awk code, because it would be a mess imho) is to use
stack structure and then parse the input string symbol after symbol. If
the current symbol is '(' push it to the stack, if it is ')', check that
the stack contains matching '(' and pop it from the stack, if you get to
the end of the string and the stack is empty at that point,
the string passed.

For this, yacc would be even better option than lex.

Alternative approach is using a counter, increase it when the input is
'(', decrease is when the input is ')', if you ever get to negatives, or
parse the whole string and the counter ends with non-zero value, the
string fails the test.


On Thu, Mar 25, 2021 at 06:26:23AM -0700, david kerns wrote:
> I submit the following test cases that fail the proposed awk solutions:
> 
> #1
>    if ((a>b) && (b>c) && // )
>      ((c>d)) {
>       a = 0;
>    }
> 
> #2
>    if ) 5 ( b = 0;
> 
> I stand by my original post, this is a job for lex
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]