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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:44:31+00:00 2026-06-02T19:44:31+00:00

Hi I’m working on a small bison to learn how it works. The bison

  • 0

Hi I’m working on a small bison to learn how it works. The bison is supposed to parse a sentence.
The sentence is made of expressions and expressions are made of words.

Following is my code:

%{
#include <stdio.h>
#include <string.h>


void yyerror(const char *str)
{
    fprintf(stderr,"error: %s\n",str);
}

int yywrap()
{
    return 1;
}

main()
{
    yyparse();
}

%}

%token ASSIGN RANGE OR AND WHITESPACE QUOTE LPAREN RPAREN NOT GREATER LESS

%union 
{
        int number;
        char *string;
}

%token <number> VALUE
%token <string> WORD

%type <string> term
%type <string> expression
%%

query:   /* empty */
    | query expression 
    {
        printf("WOrd:%s",$2);
    }
    ;

expression:
     term
    |expression term
    |expression AND term
        {
            printf("AND");
        }
    ;

term:
    WORD
    {
        $$=$1;
    }
    ;

So, when user enters a word, it supposed to print out the word.
User should be able to type in:
word,word word,word and word

I’m not sure how to pass a word using $$ and print it out from the “query expression” rule.
How do I do this?

this is my flex:

%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
%}
%%
[0-9]+                  yylval.number=atoi(yytext);return VALUE;
[a-zA-Z][a-zA-Z]*       yylval.string=strdup(yytext);return WORD;
":"                     return ASSIGN;
"and"|"&"|"&&"          return AND; 
".."                    return RANGE;
"-"                     return NOT;
"|"                     return OR;
"\""                    return QUOTE;
">"                     return GREATER;
"<"                     return LESS;
\n                      /* ignore end of line */;
\t                      /* ignore end of line */;

%%

THanks so much in advance.
Sarah

  • 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-02T19:44:33+00:00Added an answer on June 2, 2026 at 7:44 pm

    Usually, the point of writing a parser is so that you end up with a data structure that represents the input. You then transform the structure in some way, or, in your case, just print it out.

    At each expression production, you want to construct a node in that structure that represents what you have recognized so far.

    I’m a little rusty, but it would be something like this:

    query:   /* empty */
         | query expression { printNode($2); /* printf()s are in here */ }
    ;
    
    expression: term { $$ = makeTermNode($1); }
              | expression OR term { $$ = makeOrNode($1, $3); }
              | expression AND term  { $$ = makeAndNode($1, $3); }
    ;
    

    The data structure to hold your nodes:

    struct Node {
        int nodeType;          /* WORD or operator token like AND, OR */
        node* leftOperand;
        node* rightOperand;    /* will be null if the node is a term */
    }
    
    %union 
    {
        int number;
        char *string;
        Node *node;
    }
    

    Update:

    It’s been a while since I coded in C, so I will have to resort to pseudocode. There is no code here to reclaim memory once we’re done with it. Apologies for any other blunders.

    struct Node *makeTermNode(int word) {
        Node *node = malloc(sizeof struct Node);
        node->nodeType = word;
        node->rightOperand = null;
        node->leftOperand = null;
        return node;
    }
    

    Notice that your WORD token just denotes that a string of letters of some sort was scanned; the specific sequence of letters is discarded. (If you want to know the sequence, have your lexer return a copy of yytext instead of the WORD token.)

    struct Node *makeAndNode(struct Node* leftOperand, struct Node *rightOperand) {
        Node *node = malloc(sizeof struct Node);
        node->nodeType = AND;
        node->leftOperand = leftOperand;
        node->rightOperand = rightOperand;
        return node;
    }
    

    And likewise for makeOrNode(). Alternatively, you could write just makeNodeWithOperator(int operator, struct Node* leftOperand, struct Node *rightOperand) to handle the “and” and “or” cases.

    I changed printAllNodes() to printNode(). It starts at the root of the expression tree structure we have built, recursively visiting the left side of each subexpression first, then the right. It goes something like this:

    void printNode (struct Node* node) {
        switch (node->nodeType) {
        case WORD:
            printf("%i", node->nodeType);
            return;
        case AND:
        case OR:
            printf("(");
            printNode(node->leftOperand);
            printf("%i", node->nodeType);
            printfNode(node->rightOperand);
            printf(")");
            return;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.