optional operator matching

25 views
Skip to first unread message

kang joni

unread,
Aug 4, 2018, 10:17:18 AM8/4/18
to antlr-di...@googlegroups.com
I have small grammar like the following


grammar OptOperator; 
 
prog:  CONST? NUMBER EOF; 

CONST : [Cc] [Oo] [Nn] [Ss] [Tt];

NUMBER : [0-9]+ ;


I want to make sure that, the user can input one of either "const 1" or "12". But I can't seem to do that. How to achieve this?

I'm using c++ target.

thanks

Steve Vinoski

unread,
Aug 4, 2018, 10:32:26 AM8/4/18
to antlr-di...@googlegroups.com
On Sat, Aug 4, 2018 at 10:17 AM, kang joni <kangj...@gmail.com> wrote:
I have small grammar like the following

grammar OptOperator; 
 
prog:  CONST? NUMBER EOF; 

CONST : [Cc] [Oo] [Nn] [Ss] [Tt];

NUMBER : [0-9]+ ;


I want to make sure that, the user can input one of either "const 1" or "12". But I can't seems to do that. How to achieve this?

I'm using c++ target.

You need to skip whitespace by adding the following:

WS : [ \t\r\n]+ -> skip;

--steve

kang joni

unread,
Aug 4, 2018, 11:08:03 AM8/4/18
to antlr-di...@googlegroups.com
yeah that sounds right, but I get this output 

line 1:5 token recognition error at: ' '
line 1:0 mismatched input 'const' expecting {CONST, NUMBER}


My code so far is like the following one :

  std::string buffer="const 1"; // or "124"
  antlr4::ANTLRInputStream input(buffer);
  OptOperator::OptOperatorLexer lexer(&input);
  antlr4::CommonTokenStream tokens(&lexer);
  OptOperator::OptOperatorParser parser(&tokens);
  parser.setBuildParseTree(true);
  tokens.fill();
  antlr4::tree::ParseTree *tree = parser.prog();


Any idea?

Mike Cargal

unread,
Aug 4, 2018, 3:41:13 PM8/4/18
to ANTLR List
can you share  the new grammar?  It still looks like it's not ignoring =
whitespace

On Aug 4, 2018, at 11:08 AM, kang joni <kangj...@gmail.com> wrote:

yeah that sounds right, but I get this output 

line 1:5 token recognition error at: ' '
line 1:0 mismatched input 'const' expecting {CONST, NUMBER}


My code so far is like the following one :

  antlr4::ANTLRInputStream input(buffer);
  OptOperator::OptOperatorLexer lexer(&input);
  antlr4::CommonTokenStream tokens(&lexer);
  OptOperator::OptOperatorParser parser(&tokens);
  parser.setBuildParseTree(true);
  tokens.fill();
  antlr4::tree::ParseTree *tree = parser.prog();


Any idea?

On Saturday, August 4, 2018 at 9:32:26 PM UTC+7, Steve Vinoski wrote:


On Sat, Aug 4, 2018 at 10:17 AM, kang joni <kangj...@gmail.com> wrote:
I have small grammar like the following


grammar OptOperator; 
 
prog:  CONST? NUMBER EOF; 

CONST : [Cc] [Oo] [Nn] [Ss] [Tt];

NUMBER : [0-9]+ ;


I want to make sure that, the user can input one of either "const 1" or "12". But I can't seems to do that. How to achieve this?

I'm using c++ target.

You need to skip whitespace by adding the following:

WS : [ \t\r\n]+ -> skip;

--steve

--
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.
For more options, visit https://groups.google.com/d/optout.

Yanxin Guan

unread,
Aug 5, 2018, 9:40:46 PM8/5/18
to antlr-di...@googlegroups.com
you should add the following  to your grammar

grammar OptOperator; 
 
prog:  CONST? NUMBER EOF; 

CONST : [Cc] [Oo] [Nn] [Ss] [Tt];

NUMBER : [0-9]+ ;

WS : (' ' | '\t' | '\r' | '\n') ->channel(HIDDEN);

Mike Cargal <mikec...@gmail.com> 于2018年8月5日周日 上午3:41写道:

kang joni

unread,
Aug 5, 2018, 11:35:31 PM8/5/18
to antlr-di...@googlegroups.com
Apologize for late reply, turn out.. this is about miscompilation problem. I have custom (my own by rewriting them from scratch) antlr4 cmake static build instead of using offical cmake repo build. But still couldn't find at which compilation flags that trigger this. Using offical cmake build repo is indeed recommended.
Reply all
Reply to author
Forward
0 new messages