Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:34:53+00:00 2026-06-18T06:34:53+00:00

I have the following grammar: cmds : cmd+ ; cmd : include_cmd | other_cmd

  • 0

I have the following grammar:

cmds
    : cmd+
    ;

cmd
    : include_cmd  |  other_cmd
    ;

include_cmd
    : INCLUDE  DOUBLE_QUOTE  FILE_NAME  DOUBLE_QUOTE
    ;

other_cmd
    : CMD_NAME  ARG+
    ;


INCLUDE
    : '#include'
    ;

DOUBLE_QUOTE
    : '"'
    ;

CMD_NAME
    : ('a'..'z')*
    ;

ARG
    : ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')+
    ;

FILE_NAME
    : ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '.')+
    ;

So the difference between CMD_NAME, ARG and FILE_NAME is not large, CMD_NAME must be lower case letters, ARG can have upper case letter and “_” and FILE_NAME yet can have “.”.

But this has a problem, when I test the rule with – #include “abc”, ‘abc’ is interpreted as CMD_NAME instead of FILE_NAME, I think it is because CMD_NAME is before FILE_NAME in the grammar file, this leads to parsing error.

Do I have to rely on such technique as predict to deal with this? Is there a pure EBNF solution other than relying on host programming language?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:34:54+00:00Added an answer on June 18, 2026 at 6:34 am

    But this has a problem, when I test the rule with – #include “abc”, ‘abc’ is interpreted as CMD_NAME instead of FILE_NAME, I think it is because CMD_NAME is before FILE_NAME in the grammar file, this leads to parsing error.

    The set of all valid CMD_NAMEs intersects with the set of all valid FILE_NAMEs. Input abc qualifies as both. The lexer matches the input with the first rule listed (as you suspected) because it’s the first one matched.

    Do I have to rely on such technique as [predicate] to deal with this? Is there a pure EBNF solution other than relying on host programming language?

    It depends on what you’re willing accept in your grammar. Consider changing your include_cmd rule to something more conventional, like this:

    include_cmd : INCLUDE STRING;
    
    STRING 
        : '"' ~('"'|'\r'|'\n')* '"' {String text = getText(); setText(text.substring(1, text.length() - 1));}
        ;
    

    Now input #include "abc" turns into tokens [INCLUDE : #include] [STRING : abc].

    I don’t think the grammar should be responsible for determining whether a file name is valid or not: a valid file name doesn’t imply a valid file, and the grammar has to understand OS file naming conventions (valid characters, paths, etc) that probably have no bearing on the grammar itself. I think you’ll be fine if you’re willing to drop rule FILE_NAME for something like the rules the above.

    Also worth noting, your CMD_NAME rule matches zero-length input. Consider changing ('a'..'z')* to ('a'..'z')+ unless a CMD_NAME really can be empty.


    Keep in mind, too, that you’ll have the same problem with ARG that you did with FILE_NAME. It’s listed after CMD_NAME, so any input that qualifies for both rules (like abc again) will hit CMD_NAME. Consider breaking these rules up into more conventional ones like so:

    other_cmd : ID (ID | NUMBER)+ SEMI;   //instead of CMD_NAME ARG+
    ID        : ('a'..'z'|'A'..'Z'|'_')+; //instead of CMD_NAME, "id" part of ARG
    NUMBER    : ('0'..'9')+;              //"number" part of ARG
    SEMI      : ';';
    

    I added rule SEMI to mark the end of a command. Otherwise the parser won’t know if input a b c d is supposed to be one command with three arguments (a(b,c,d)) or two commands with one argument each (a(b), c(d)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following grammar: grammar bxk; options { language=CSharp3; } // start rule
I have the following parser grammar (this is a small sample): expr: ident assignop
i have an problem with antlr. I have the following simple grammar: grammar bxk;
I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup . How can I
I have following div in a page (I can not modify). <div id=:0.control>Click me</div>
I have the following grammar: grammar ru.focusmedia.fire.idl.IDL with org.eclipse.xtext.xbase.Xbase generate idl http://www.focusmedia.ru/fire/idl/IDL Model: 'package'
I'm at my first experience with SableCC and grammar definition. I have the following
I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using
I have the following Prolog definite clause grammar: s-->[a],s,[b]. s-->[]. This will result in
I have the following Antlr grammar: grammar MyGrammar; doc : intro planet; intro :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.