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 6731191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:29:55+00:00 2026-05-26T10:29:55+00:00

My grammar is something like this decl: attributes; {/*create an object here with the

  • 0

My grammar is something like this

decl:

   attributes;  {/*create an object here with the attributes $$?*/ }


attributes:



  |

  att1 attributes {$$ = $1;}

  |

  att2 attributes {$$ = $1;}

  |

  attn attributes {$$ = $1;};

I want to get all the attributes entered by the user, some of them are optional and the order doesn’t matter, types are different

  • 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-05-26T10:29:56+00:00Added an answer on May 26, 2026 at 10:29 am

    You need to return a structure in your $$ variable; assign to its members as you need to. Here’s an example from some code I have handy:

    struct value_list {
        char *value;
        struct value_list *next;
    };
    
    # ...
    
    valuelist:  TOK_VALUE
        {
            struct value_list *new = calloc(1, sizeof(struct value_list));
            if (!new)
                yyerror(_("Memory allocation error."));
            PDEBUG("Matched: value (%s)\n", $1);
    
            new->value = $1;
            new->next = NULL;
            $$ = new;
        }
    
    valuelist:  valuelist TOK_VALUE
        {
            struct value_list *new = calloc(1, sizeof(struct value_list));
            if (!new)
                yyerror(_("Memory allocation error."));
            PDEBUG("Matched: value (%s)\n", $1);
    
            new->value = $2;
            new->next = $1;
            $$ = new;
        }
    

    Another example from the same parser, which puts more effort into customizing a struct based on an entry than the above simpler rules; the downside is that this is getting quite complex, but the upside is that this is a better demonstration of attributes of a single object:

    /* from a header file */
    struct codomain {
        char *namespace;
        char *name;             /* codomain name */
        char *attachment;
        struct alt_name *altnames;
        void *xmatch;
        size_t xmatch_size;
        int xmatch_len;
        /* ... and it goes on like this ... */
    }
    
    # from the grammar:
    
    profile_base: TOK_ID opt_id flags TOK_OPEN rules TOK_CLOSE
        {
            struct codomain *cod = $5;
    
            if (!cod) {
                yyerror(_("Memory allocation error."));
            }
    
            cod->name = $1;
            cod->attachment = $2;
            if ($2 && $2[0] != '/')
                /* we don't support variables as part of the profile
                 * name or attachment atm
                 */
                yyerror(_("Profile attachment must begin with a '/'."));
            cod->flags = $3;
            if (force_complain)
                cod->flags.complain = 1;
    
            post_process_nt_entries(cod);
            PDEBUG("%s: flags='%s%s'\n",
                   $3,
                   cod->flags.complain ? "complain, " : "",
                   cod->flags.audit ? "audit" : "");
    
            $$ = cod;
    
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing my own DSL in XText. I want do something like this:
I have an Xtext grammar that does something like this: Model: (names += Name)*
With ANTLR2, you could define something like this in grammar definition file: options {
Let's say I have a grammar defined to something like: some_rule := a b
I have this simple grammar for a C# like syntax. I can't figure out
Say I have a Boost Spirit grammar like this, where a parent rule passes
How do you do something like this with ANTLR? Example input: title: hello world
perhaps I'm asking the wrong question. I have code like this: class ExpressionGrammar(Grammar): def
I came across something like ArgProcessor argProcessor = runWebApp.new ArgProcessor(options); This line is from
I have this grammar and need to modify it to allow parenthesis like: (-1)

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.