[antlr-interest] Help on error handling in antlr grammar...

sudha_vara vamsikp406 at kiwibox.com
Thu Jan 22 22:54:57 PST 2004


Hi,
I'm using ANTLR for validating the user entered commands in command
line interface (CLI).

One sample of the command is "edit logsettings logid [{emaildest}]".
{} means that it can be an array list seperated with ',' (comma) & []
means that 
the parameter is optional.
'logid' will take any of the digit & 'emaildest' will take an email id
as its value.

I need to handle the error situations & have to show appropriate error
message to the user.
Here are the different kind of errors that can be produced as a result
of the command.

Success Cases:

1. I/P: edit logsettings logid = 123

2. I/P: edit logsettings logid = 123 emaildest = 
vamsikp406 at kiwibox.com


Failure Cases:

1. I/P: edit logsettings
   O/P: logid parameter missing.

2. I/P: edit logsettings logid = 2a
   O/P: logid value invalid.

3. I/P: edit logsettings abcd logid = 123
   O/P: abcd parameter unknown.

4. I/P: edit logsettings logid = 123 emaildest = .vamsikp at kiwibox.com
   O/P: emaildest value invalid.

5. I/P: edit logsettings logid = 123 emaildest = vamsikp at kiwibox.com
logid = 234
   O/P: logid parameter repeated.

The grammar file that I had written for the command is given below.

With this, I'm getting non-determinism errors due to the last rule
(ID). So error handling is not complete.
And for some commands, the value of a parameter can contain the white
spaces also that needs to be taken for value.
Can anyone help me out what could be the better way to handle the 
above mentioned errors. It's very urgent for me.

Thanks in advance.

Regards,
Vamsi.

=================================
header{
package test;

import antlr.TokenStreamRecognitionException;
import antlr.NoViableAltException;

}

class EditLogsettingsParser extends Parser;
options
    {
        k = 2;
        buildAST = true; // uses CommonAST by default
        defaultErrorHandler = false;
    }
    
    // Define some variables to be used in the parser
    {
        int logIdCount;
        int emailDestCount;

        String logid = null;
        String emaildest = null;
    }

    // match one-or-more 'function name followed by params' pairs
    startRule
    :
    editLogsettings EOF
    ;

editLogsettings
    :
    EDIT WS LOGSETTINGS (WS editLogSettingsParams)* NEWLINE
    {
        System.out.println("command = edit logsettings");
        
        if (logIdCount == 0)
        {
            System.out.println("logid parameter missing.");
        }
        else
        {
            System.out.println("logid = " + logid);
        }

        if (emaildest != null)
        {
            System.out.println("emailDest = " + emaildest);
        }
    }
    ;

editLogSettingsParams
    : setLogId
    | setEmailDest
    | setError
    ;

setLogId
    :
    LOGID (WS)? a:ID
    {
        if (logIdCount == 1)
        {
            System.out.println("logid parameter repeated.");
        }

        String logIdValue = a.getText().trim();

        if(logIdValue.equals("="))
        {
            System.out.println("logid parameter missing.");
        }
        else
        {
            if (isLogIdValid(logIdValue))
            {
                logid = logIdValue;
            }
            else
            {
                System.out.println("logid value invalid.");
            }

            logIdCount++;
        }
    }

    exception
    catch [NoViableAltException nvae]
    {
        System.out.println("logid parameter missing.");
    }
    ;

setEmailDest
    :
    EMAILDEST (WS)? a:ID
    {
        if (emailDestCount == 1)
        {
            System.out.println("emaildest parameter repeated.");
        }

        String emailDestValue = a.getText().trim();

        if(emailDestValue.equals("="))
        {
            System.out.println("emaildest parameter missing.");
        }
        else
        {
            if (isEmailDestValid(emailDestValue))
            {
                emaildest = emailDestValue;
            }
            else
            {
                System.out.println("emaildest value invalid.");
            }

            emailDestCount++;
        }
    }

    exception
    catch [NoViableAltException nvae]
    {
        System.out.println("emaildest parameter missing.");
    }
    ;

setError
    :
    a:ID
    {
        if (true)
        {
            System.out.println(a.getText().trim() + " parameter 
unknown.");
        }
    }
    ;

isLogIdValid[String strInputString] returns[boolean bRetVal = true]
    :
    {
        int strLength = strInputString.length();

        for (int i = 0; i < strLength; i++)
        {
            char c = strInputString.charAt(i);

            if (Character.isDigit(c))
            {
                continue;
            }
            else
            {
                bRetVal = false;
                break;
            }
        }
    }
    ;

isEmailDestValid[String strInputString] returns[boolean bRetVal = 
true]
    :
    {
        String patternMatch = "[a-zA-Z\\d\\.\@_]*";

        if (!(inputStr.matches(patternMatch)))
        {
            bRetVal = false;
        }

        if ((strInputString.startsWith(".")) || 
            (strInputString.startsWith("_")) || 
            (strInputString.startsWith("@")))
        {
            bRetVal = false;
        }
    }
    ;


// Lexer framework (rule contents are missing here)
class EditLogsettingsLexer extends Lexer;
options
{ 
    k = 4;
    caseSensitiveLiterals = false;
    caseSensitive = false;
    charVocabulary = ' '..'~' | '¥';
}

EDIT        : "edit";
LOGSETTINGS : "logsettings";
LOGID       : "logi" ('d')?;
EMAILDEST   : "em" ('a' ('i' ('l' ('d' ('e' ('s' ('t')?)?)?)?)?)?)?;

WS      : (' ' | '\t')+;

NEWLINE : '\n';

ID options { testLiterals = false; }
    : ('=') => '='! (WS)? (~(' ' | '\t' | '\n'))+
    | (WS)? (~(' ' | '\t' | '\n'))+
    ;
==============================


 

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/antlr-interest/

To unsubscribe from this group, send an email to:
 antlr-interest-unsubscribe at yahoogroups.com

Your use of Yahoo! Groups is subject to:
 http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list