Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#4 Escaped '/' in regexp throws exception

open
nobody
None
5
2013-06-06
2008-01-16
Anonymous
No

To match a '/' in a regexp in AWK you have to escape it as follows:
\/

But doing so causes Jawk to throw an exception.

eg the following AWK script (which is looking for /cgi):

/\/cgi/ { print }

causes this:

Exception in thread "main" org.jawk.frontend.AwkParser$ParserException: Expecting an ID. Got (299): { (line 0)
at org.jawk.frontend.AwkParser.SYMBOL(AwkParser.java:1173)
at org.jawk.frontend.AwkParser.FACTOR(AwkParser.java:1097)
at org.jawk.frontend.AwkParser.FACTOR_FOR_INCDEC(AwkParser.java:1016)
at org.jawk.frontend.AwkParser.FACTOR_FOR_IN(AwkParser.java:983)
at org.jawk.frontend.AwkParser.FACTOR_FOR_GETLINE(AwkParser.java:967)
at org.jawk.frontend.AwkParser.TERM(AwkParser.java:939)
at org.jawk.frontend.AwkParser.TERM(AwkParser.java:947)
at org.jawk.frontend.AwkParser.EXPRESSION(AwkParser.java:871)
at org.jawk.frontend.AwkParser.COMPARISON_EXPRESSION(AwkParser.java:853)
at org.jawk.frontend.AwkParser.LE2_EXPRESSION(AwkParser.java:833)
at org.jawk.frontend.AwkParser.LE1_EXPRESSION(AwkParser.java:816)
at org.jawk.frontend.AwkParser.TERTIARY_EXPRESSION(AwkParser.java:802)
at org.jawk.frontend.AwkParser.CONCAT_EXPRESSION(AwkParser.java:785)
at org.jawk.frontend.AwkParser.CONCAT_EXPRESSION(AwkParser.java:788)
at org.jawk.frontend.AwkParser.COMMA_EXPRESSION(AwkParser.java:768)
at org.jawk.frontend.AwkParser.ASSIGNMENT_EXPRESSION(AwkParser.java:749)
at org.jawk.frontend.AwkParser.RULE(AwkParser.java:683)
at org.jawk.frontend.AwkParser.RULE_LIST(AwkParser.java:615)
at org.jawk.frontend.AwkParser.SCRIPT(AwkParser.java:599)
at org.jawk.frontend.AwkParser.parse(AwkParser.java:237)
at org.jawk.Awk.invoke(Awk.java:114)
at org.jawk.Awk.main(Awk.java:83)

Regards,

Stephen Summerfield
stephen@netality.com

Discussion

  • Hi
    I have also experimented this issue.
    Please find here after the description of the "ugly" patch I did on my side:
    In readRegexp() from AwkParser.java I just add a check to make sure an escaped / is taken into account (lines FIXME: are the one I added)

    private void readRegexp()
       throws IOException {
    
    // should only contain nothing or =, depending on whether
    // starting with /... or /=...
    assert regexp.length()==0 || regexp.length()==1;
    while(c >= 0 && c != '/' && c != '\n') {
        /* FIXME:Just to be able to to include / in the pattern 
         * FIXME:In case of \ force to read the one character more:
         * FIXME:- if second is not /, append the two characters to regexp buffer
         * FIXME:- if a / is read, only the / is append. */
        if (c== '\\') {                          /* FIXME: */
            c = reader.read();               /* FIXME: */
            if (c!='/') regexp.append('\\'); /* FIXME: */
        }                                        /* FIXME: */
        regexp.append((char)c);
        c = reader.read();
        // completely bypass \r's
        while(c == '\r') c = reader.read();
    }
    if (c < 0 || c == '\n')
        throw new LexerException("Unterminated regular expression: "+regexp);
    c = reader.read();
    // completely bypass \r's
    while(c == '\r') c = reader.read();
      }
    

    Regards

     
    Last edit: Terrot Francois 2013-06-06