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

  • SEARCH
  • Home
  • 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 6936435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:14:00+00:00 2026-05-27T12:14:00+00:00

I’m trying to create a LALR(1) parser in Yacc/Bison that can accept commands with

  • 0

I’m trying to create a LALR(1) parser in Yacc/Bison that can accept commands with flexible grammar. An example is controlling the environment in a house by adjusting room temperatures (floats), window positions (ints), and ceiling fans (enum). Example (desired) grammar would be:

set kitchen 22.5, den 0 24.0, bathroom fast 25, kitchen slow

I’m stuck on how to handle the stacked data for what I’ve called a “room_arg” in the rules. I could add a room_arg arg type to the %union statement, set %type <arg> room_arg and make the “room_arg” rule:

room_arg:  TOK_CIELING_FAN { $$.fan = $1; }
         | TOK_WINDOW      { $$.window_position = $1; }
         | TOK_THERMOSTAT  { $$.temperature = $1; }

But when it comes to doing reduction of the “room_arg” rule to “room_args” – I’ve got to figure out which stack items are which. Add to that that the “room_cmd” for a particular room (“kitchen” in the example) doesn’t require grouping in the command adds to my challenge.

Changing the format of the command would be lovely, but, alas not possible.

Could someone suggest a strategy for implementation, or perhaps improvement to the parser logic to make things simpler?

The grammar to criticize is below.

%{
typedef enum fan_setting_tag { OFF, SLOW, MEDIUM, FAST } fan_setting;

struct room_arg_tag {
   int          window_position;
   fan_setting  fan;
   float    temperature;
   char *   room_name;
} room_arg;
%}
%token TOK_ROOM
%token TOK_CIELING_FAN
%token TOK_WINDOW
%token TOK_THERMOSTAT
%token TOK_SET

%type <fan>     TOK_CIELING_FAN
%type <window>  TOK_WINDOW
%type <tempr>   TOK_THERMOSTAT
%type <str>     TOK_ROOM

%union {
    int         window;
    float       tempr;
    fan_setting fan;
}

%%

command: /* empty */ | cmd command

cmd: TOK_SET room_cmds { execute_command( ... ); }

room_cmds: room_cmd
         | room_cmd ',' room_cmds

room_cmd:  TOK_ROOM room_args

room_args: room_arg
         | room_arg room_args

room_arg:  TOK_CIELING_FAN
         | TOK_WINDOW
         | TOK_THERMOSTAT
  • 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-27T12:14:01+00:00Added an answer on May 27, 2026 at 12:14 pm

    This is a case where you really want inherited attributes. You can simulate them with extra actions, but unfortunately yacc and bison don’t give you a way to do them more directly (btyacc provides syntacic sugar to make this much easier.)

    Basically what you do is use embedded actions to setup the inherited attributes and use $0 to access them within the lower level rules.

    With btyacc you would do:

    %union {
        int         window;
        float       tempr;
        fan_setting fan;
        room_arg    *room;
    }
    
    %token<fan>     TOK_CIELING_FAN
    %token<window>  TOK_WINDOW
    %token<tempr>   TOK_THERMOSTAT
    %token<str>     TOK_ROOM
    
    %type<room> room_cmds(<room>)
    %type<>     room_cmd(<room>), room_args(<room>), room_arg(<room>)
    
    %%
    
    command: /* empty */ | cmd command ;
    
    cmd: TOK_SET room_cmds(new_room_arg()) { execute_command( ... ); } ;
    
    room_cmds($room):
               room_cmd($room) { $$ = $room; }
             | room_cmd($room) ',' room_cmds($room) { $$ = $room; }
    ;
    
    room_cmd($room):  TOK_ROOM room_args($room) { $room->room_name = $1; } ;
    
    room_args($room): 
               room_arg($room)
             | room_arg($room) room_args($room)
    ;
    
    room_arg($room):
               TOK_CIELING_FAN { $room->fan = $1; }
             | TOK_WINDOW      { $room->window_position = $1; }
             | TOK_THERMOSTAT  { $room->temperature = $1; }
    ;
    

    In yacc or bison, this becomes something like:

    cmd: TOK_SET { $$ = new_room_arg(); } room_cmds { execute_command(...)' } '
    room_cmds:
          room_cmd { $$ = $<room>0; }
        | room_cmd ',' { $$ = $<room>0; } room_cmds { $$ = $<room>0; }
    ;
    room_cmd: TOK_ROOM { $$ = $<room>0; } room_args { $<room>0->room_name = $1; }
    

    and so forth, which is tricky to get right (get all the right types in the right places) and somewhat error prone.

    In this specific case, you could just use a global variable (as there’s no recursion involved), but that doesn’t work too well in more complex cases.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.