ANTLR Lab, please give advice

38 views
Skip to first unread message

Rolf Blum

unread,
Nov 17, 2023, 4:21:42 PM11/17/23
to antlr-discussion
hallo, 
the grammar is:
grammar TextBlock;

textBlock: sentence+ EOF;

sentence: word+ FullStop;

word: LETTER+;

LETTER: [a-zA-Z];

FullStop: '.';

This is the input:
This is a text.
It consists of several lines.
Each line consists of one or more Words and is terminated with a fullstop.
And here is the response:
1:14 token recognition error at: '.'
2:28 token recognition error at: '.'
3:73 token recognition error at: '.'
1:0 mismatched input 'This' expecting 'and'
3:29 extraneous input 'more' expecting {, 'and'}
3:44 extraneous input 'is' expecting {'and', '.'}

The following modification did not help: sentence: word+ FullStop {getText().length() > 1};

Jeffrey Coffield

unread,
Nov 17, 2023, 4:40:00 PM11/17/23
to antlr-di...@googlegroups.com
I don't see where you are discarding white space. Try adding:
WS : [ \t]+ -> skip ;


--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/4f2f84ff-2b13-4383-ad99-20a3296f7402n%40googlegroups.com.

Rolf Blum

unread,
Nov 18, 2023, 2:03:48 AM11/18/23
to antlr-discussion
Jeffrey, you are right, it is missing, after including  the WS rule the output was somehow better, but not right. The errormessage still is:
1:14 token recognition error at: '.'
2:28 token recognition error at: '.'
3:73 token recognition error at: '.'
1:0 mismatched input 'This' expecting 'or'
3:29 extraneous input 'more' expecting {'or', '.'}

Jeffrey Coffield

unread,
Nov 18, 2023, 11:33:40 AM11/18/23
to antlr-di...@googlegroups.com
I tried this grammar:
-----------------------------
grammar TextBlock;

textBlock: sentence+ EOF;

sentence: word+ FullStop;

word: LETTER+;

LETTER: [a-zA-Z];

FullStop: '.';

WS : [ \t\r\n]+ -> channel(1) ;
--------------------------------
With this a input:
--------------------------------
Line a.
Line b.
--------------------------------
Note the WS is also ignoring line feeds now.
Dump of the token stream:
------------------------------------
  0: type =   1, name = LETTER, text = `L`  
  1: type =   1, name = LETTER, text = `i`  
  2: type =   1, name = LETTER, text = `n`  
  3: type =   1, name = LETTER, text = `e`  
  4: type =   3, name = WS, text = ` `  (HIDDEN)
  5: type =   1, name = LETTER, text = `a`  
  6: type =   2, name = FullStop, text = `.`  
  7: type =   3, name = WS, text = `NEWLINE`  (HIDDEN)
  8: type =   1, name = LETTER, text = `L`  
  9: type =   1, name = LETTER, text = `i`  
 10: type =   1, name = LETTER, text = `n`  
 11: type =   1, name = LETTER, text = `e`  
 12: type =   3, name = WS, text = ` `  (HIDDEN)
 13: type =   1, name = LETTER, text = `b`  
 14: type =   2, name = FullStop, text = `.`  
 15: type =   3, name = WS, text = `NEWLINE`  (HIDDEN)
 16: type =  -1, name = EOF, text = `<EOF>` 
-------------------------------------------------------
Attached is the parsed tree as shown by one of our development tools.


TextBlock.png
Reply all
Reply to author
Forward
0 new messages