I’ve created a grammar to parse simple ldap query syntax. The grammer is:
expression : LEFT_PAREN! ('&' | '||' | '!')^ (atom | expression)* RIGHT_PAREN! EOF ;
atom : LEFT_PAREN! left '='^ right RIGHT_PAREN! ;
left : ITEM;
right : ITEM;
ITEM : ALPHANUMERIC+;
LEFT_PAREN : '(';
RIGHT_PAREN : ')';
fragment ALPHANUMERIC
: ('a'..'z' | 'A'..'Z' | '0'..'9');
WHITESPACE : (' ' | '\t' | '\r' | '\n') { skip(); };
Now this grammar works fine for:
(!(attr=hello2))
(&(attr=hello2)(attr2=12))
(||(attr=hello2)(attr2=12))
However, when I try and run:
(||(attr=hello2)(!(attr2=12)))
It fails with: line 1:29 extraneous input ‘)’ expecting EOF
If I remove the EOF off the expression grammar, everything passes, but then wrong numbers of brackets are not caught as being a syntax error. (This is being parsed into a tree, hence the ^ and ! after tokens) What have I missed?
As already mentioned by others, your expression has to end with a
EOF, but a nested expression cannot end with anEOF, of course.Remove the
EOFfromexpression, and create a proper “entry point” for your parser that ends with theEOF.file: T.g
file: Main.java
To run the demo, do:
*nix/MacOS:
Windows:
which produces the DOT code representing the following AST:
image created using graphviz-dev.appspot.com