I am doing some hands-on with antlr4 for parsing a file and seem to be stuck with an issue that has keep me awake for few hours now.. Following is the simple grammar i have defined in ESQLGrammar.g4 file that is placed in my projects src/main/antlr4.
grammar ESQLGrammar;
esqlCode:
declBrokerSchema? esqlContents;
declBrokerSchema
: BROKER SCHEMA schemaName (PATH schemaPathList ';')?;
schemaName
: IDENTIFIER;
schemaPathList
: IDENTIFIER (',' IDENTIFIER)*;
esqlContents
: (declareVariable)*?;
declareVariable
: DECLARE variableNames esqlDataType ';';
variableNames
: variableName (',' variableName)*;
variableName
: IDENTIFIER;
esqlDataType
: (BLOB|CHARACTER|BOOLEAN|NAMESPACE);
WS : [ \r\t\n]+ -> skip ;
IDENTIFIER : [a-zA-Z_][a-zA-Z0-9_.]*;
BROKER : 'BROKER';
SCHEMA : 'SCHEMA';
PATH : 'PATH';
DECLARE : 'DECLARE';
BLOB : 'BLOB';
CHARACTER : 'CHARACTER';
BOOLEAN : 'BOOLEAN';
NAMESPACE : 'NAMESPACE';
My Input is a file
BROKER SCHEMA nameOfSchema PATH pathVal1,pathVal2;
DECLARE iSharedVar CHARACTER;
However, when i change the grammar lines as below to use fixed keywords with extra spaces in them
declBrokerSchema
: 'BROKER SCHEMA ' schemaName ('PATH ' schemaPathList ';')?;
// Notice the keywords in ' ' with extra space at end.
declareVariable
: 'DECLARE ' variableNames esqlDataType ';';
then it seems to parse the lines and throws the error provided below:
line 2:19 mismatched input 'CHARACTER' expecting {'BLOB', 'CHARACTER', 'BOOLEAN', 'NAMESPACE'}
DeclBrokerSchema(schemaName=nameOfSchema, schemaPathList=pathVal1,pathVal2)
[DeclareVariable(varibleNames=iSharedVar, dataType=CHARACTER, defultValue=null, modifier=null, isConstant=false, aliasType=null, initialValueExpression=null)]
which appears to recognise it but with an error. So need your expert view on these:
- I can't find any of the rules that could have matched 'CHARACTER' during the lexer phase.. then why does it throw an error ?
- Also, why does it require me to use the tokens in ' ' that too with an extra space? If i remove the space it fails to parse it..
Am i missing something here .. pls help..!