Al Phaba
Al Phaba

Reputation: 6765

Why is ending parenthesis not recognizes as valid in my antlr4 grammar?

I am writing a DSL with ANTLR4, now I have a problem with ending right parenthesis. Why is this command not valid

This is the command:

set(buffer,variableX|"foo");

the parse tree with the error

enter image description here

Here is my grammar

        grammar Expr;
        
        prog: expr+ EOF;
        expr:
                     statement                     #StatementExpr
                     |NOT expr                         #NotExpr
                     | expr AND expr                #AndExpr
                     | expr (OR | XOR) expr         #OrExpr
                     | function                             #FunctionExpr
                     | LPAREN expr RPAREN       #ParenExpr
                     | writeCommand              #WriteExpr
         ;
        
        writeCommand: setCommand | setIfCommand;
        statement: ID '=' getCommand  NEWLINE         #Assign;
        setCommand: 'set' LPAREN variableType '|' parameter RPAREN SEMI;
        setIfCommand: 'setIf' LPAREN variableType '|' expr '?' parameter ':' parameter RPAREN SEMI;
        
        getCommand: getFieldValue | getInstanceAttribValue|getInstanceAttribValue|getFormAttribValue|getMandatorAttribValue;
        getFieldValue: 'getFieldValue' LPAREN instanceID=ID COMMA fieldname=ID RPAREN;
        getInstanceAttribValue: 'getInstanceAttrib' LPAREN instanceId=ID COMMA moduleId=ID COMMA attribname=ID RPAREN;
        getFormAttribValue: 'getFormAttrib' LPAREN formId=ID COMMA moduleId=ID COMMA attribname=ID RPAREN;
        getMandatorAttribValue: 'getMandatorAttrib' LPAREN mandator=ID COMMA moduleId=ID COMMA attribname=ID RPAREN;
        
        twoParameterList: parameter '|' parameter;
        parameter:variableType | constType;
        pdixFuncton:ID;
        constType:
                        ID
                        | '"'  ID '"';
        variableType:
                            valueType
                            |instanceType
                            |formType
                            |bufferType
                            |instanceAttribType
                            |formAttribType
                            |mandatorAttribType
                            ;
        valueType:'value' COMMA parameter (COMMA functionParameter)?;
        instanceType: 'instance' COMMA instanceParameter;
        formType: 'form' COMMA formParameter;
        bufferType: 'buffer' COMMA ID '|' parameter;
        instanceParameter: 'instanceId'
                                        | 'instanceKey'
                                        | 'firstpenId'
                                        | 'lastpenId'
                                        | 'lastUpdate'
                                        | 'started'
                                        ;
        formParameter: 'formId'
                                    |'formKey'
                                    |'lastUpdate'
                                    ;
        functionParameter: 'lastPen'
                                        | 'fieldGroup'
                                        | ' fieldType'
                                        | 'fieldSource'
                                        | 'updateId'
                                        | 'sessionId'
                                        | 'icrConfidence'
                                        | 'icrRecognition'
                                        |  'lastUpdate';
        
        instanceAttribType:('instattrib' | 'instanceattrib') COMMA attributeType;
        formAttribType:'formattrib' COMMA attributeType;
        mandatorAttribType: 'mandatorattrib' COMMA attributeType;
        attributeType:ID '#' ID;
        
         function:
                        commandIsSet            #IsSet
                        | commandIsEmpty    #IsEmpty
                        | commandIsEqual  #IsEqual
                        ;
        commandIsSet: IS_SET LPAREN parameter RPAREN;
        commandIsEmpty: IS_EMPTY LPAREN parameter RPAREN;
        commandIsEqual: IS_EQUAL LPAREN twoParameterList RPAREN;
        
        LPAREN : '(';
        RPAREN : ')';
        LBRACE : '{';
        RBRACE : '}';
        LBRACK : '[';
        RBRACK : ']';
        SEMI : ';';
        COMMA : ',';
        DOT : '.';
        ASSIGN : '=';
        GT : '>';
        LT : '<';
        BANG : '!';
        TILDE : '~';
        QUESTION : '?';
        COLON : ':';
        EQUAL : '==';
        LE : '<=';
        GE : '>=';
        NOTEQUAL : '!=';
        AND : 'and';
        OR : 'or';
        XOR :'xor';
        NOT :'not'  ;
        INC : '++';
        DEC : '--';
        ADD : '+';
        SUB : '-';
        MUL : '*';
        DIV : '/';
        
        INT: [0-9]+;
        NEWLINE: '\r'? '\n';
        IS_SET:'isSet';
        IS_EMPTY:'isEmpty';
        IS_EQUAL:'isEqual';
        WS: (' '|'\t' | '\n' | '\r' )+ -> skip;
        
        ID
        :   JavaLetter JavaLetterOrDigit*
        ;
        
        fragment
        JavaLetter
        :   [a-zA-Z$_] // these are the "java letters" below 0xFF
        |   // covers all characters above 0xFF which are not a surrogate
            ~[\u0000-\u00FF\uD800-\uDBFF]
            {Character.isJavaIdentifierStart(_input.LA(-1))}?
        |   // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
            [\uD800-\uDBFF] [\uDC00-\uDFFF]
            {Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
        ;
        
        fragment
        JavaLetterOrDigit
        :   [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
        |   // covers all characters above 0xFF which are not a surrogate
            ~[\u0000-\u00FF\uD800-\uDBFF]
            {Character.isJavaIdentifierPart(_input.LA(-1))}?
        |   // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
            [\uD800-\uDBFF] [\uDC00-\uDFFF]
            {Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
        ;
        fragment DoubleQuote: '"' ;   // Hard to read otherwise.

Upvotes: 1

Views: 35

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170178

The input set(buffer,variableX|"foo"); cannot be parsed by:

setCommand: 'set' LPAREN variableType '|' parameter RPAREN SEMI;

because buffer,variableX|"foo" is being matched by:

bufferType: 'buffer' COMMA ID '|' parameter;

which causes '|' parameter from the setCommand to not be able to match anything. If the input was something like set(buffer,variableX|"foo"|"bar");, it would parse successfully. Or remove '|' parameter from either the setCommand rule or from the bufferType rule.

Upvotes: 2

Related Questions