ANTLR "Hello" Lexer and Parser Error

Q

How to test the "Hello" lexer and parser with invalid source code? I have created the "Hello" grammar and generated the "Hello" lexer and parser.

✍: FYIcenter

A

To test the "Hello" lexer and parser with invalid source code, we need to review the "Hello" grammar again.

\fyicenter>more hello.g4

// Define a grammar called Hello
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

The "Hello" grammar file contains:

  • One rule called "r". It starts with the keyword "hello" and is followed by an "ID" token.
  • The "ID" token must be one or more lower case characters.
  • All white space characters are ignored.

So the following source code lines should be valid:

hello you
hello there
hello hello

And the following source code lines are invalid:

Hello you
hello you!
hello Joe
hello 123

If you run the TestRig with invalid source code lines, you will get syntax errors from the parser:

\fyicenter>java -cp .;antlr-4.7.1-complete.jar \
   org.antlr.v4.gui.TestRig Hello r -tree

Hello you
^Z
line 1:0 token recognition error at: 'H'
line 1:1 missing 'hello' at 'ello'
(r <missing 'hello'> ello)


\fyicenter>java -cp .;antlr-4.7.1-complete.jar \
   org.antlr.v4.gui.TestRig Hello r -tree
   
hello you!
^Z
line 1:9 token recognition error at: '!'
(r hello you)


\fyicenter>java -cp .;antlr-4.7.1-complete.jar \
   org.antlr.v4.gui.TestRig Hello r -tree

hello Joe
^Z
line 1:6 token recognition error at: 'J'
(r hello oe)


\fyicenter>java -cp .;antlr-4.7.1-complete.jar \
   org.antlr.v4.gui.TestRig Hello r -tree

hello 123
^Z
line 1:6 token recognition error at: '1'
line 1:7 token recognition error at: '2'
line 1:8 token recognition error at: '3'
line 2:0 missing ID at '<EOF>'
(r hello <missing ID>)

 

ANTLR TestRig Graphical Output

ANTLR "Hello" Grammar File and Test

ANTLR Commands and Examples

⇑⇑ FAQ for ANTLR Parser Generator

2020-12-02, 6454👍, 0💬