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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:36:07+00:00 2026-06-06T20:36:07+00:00

I’ve been trying to find examples everywhere but it’s been in vain. I am

  • 0

I’ve been trying to find examples everywhere but it’s been in vain.

I am trying to write a basic Ruby interpreter. For this, I wrote a flex lexical file, containing token recognition sentences, and a grammar file.

I wish for my grammar to contain semantic type checking.

My grammar file contains, for example:

arg : arg '+' arg 

This should be a valid rule for integers and floats.

According to what I’ve read, I can specify type for a non terminal such as arg, like so:

%type <intval> arg

where “intval” is in the type union and corresponds to the int C type.

But this is only for integers, I am not sure how to make the rule valid for, say, floats.
I thought about having two different rules, one for ints and one for floats, like:

argint : argint '+' argint
argfloat : argfloat '+' argfloat

but I am sure there is a much, much better way of doing so, since this atrocity would require me to have rules to allow additions between floats and ints.

All examples I’ve found have only one type (usually integers in calculator-like examples).

How can I achieve specifying that a rule such as an addition can have ints and floats as arguments?

Thank you very much.

  • 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-06T20:36:09+00:00Added an answer on June 6, 2026 at 8:36 pm

    This isn’t the answer you’re hoping for. I think the reason that you haven’t seen examples of what you want is that it’s impractical to enforce typing rules in the grammar file (the .y); rather, developers accomplish this in procedural .c or .cpp code. Generally, you will have do some analysis of the parsed input anyway, so it’s a byproduct to enforce the semantic rules as you do so.

    As an aside, I don’t quite understand how you are parsing expressions, given the fragment of your grammar that you reproduce in your question.

    Here’s why I claim that it’s impractical. (1) Your type information has to percolate all through the non-terminals of the grammar. (2) Worse, it has to be reflected in variable names.

    Consider this toy example of parsing simple assignment statements that can use identifiers, numeric constants, and the four desk calculator operators. The NUMBER token can be an integer like 42 or a float like 3.14. And let’s say that an IDENTIFIER is one letter, A-Z.

    %token IDENTIFIER NUMBER
    
    %%
    
    stmt : IDENTIFIER '=' expr
         ;
    
    expr : expr '+' term
         | expr '-' term
         | term
         ;
    
    term : term '*' factor
         | term '/' factor
         | factor
         ;
    
    factor : '(' expr ')'
           | '-' factor
           | NUMBER
           | IDENTIFIER
           ;
    

    Now let’s try to introduce typing rules. We’ll separate the NUMBER token into FLT_NUMBER and INT_NUMBER. Our expr, term, and factor non-terminals split into two as well:

    %token IDENTIFIER FLT_NUMBER INT_NUMBER
    
    stmt : IDENTIFIER '=' int_expr
         | IDENTIFIER '=' flt_expr
         ;
    
    int_expr : int_expr '+' int_term
             | int_expr '-' int_term
             | int_term
             ;
    
    flt_expr : flt_expr '+' flt_term
             | flt_expr '-' flt_term
             | flt_term
             ;
    
    int_term : int_term '*' int_factor
             | int_term '/' int_factor
             | int_factor
             ;
    
    flt_term : flt_term '*' flt_factor
             | flt_term '/' flt_factor
             | flt_factor
             ;
    
    int_factor : '(' int_expr ')'
               | '-' int_factor
               | INT_NUMBER
               | int_identifier
               ;
    
    flt_factor : '(' flt_expr ')'
               | '-' flt_factor
               | FLT_NUMBER
               | flt_identifier
               ;
    
    int_identifier : IDENTIFIER ;
    
    flt_identifier : IDENTIFIER ;
    

    As our grammar stands at this point, there’s a conflict: the parser can’t tell whether to recognize an IDENTIFIER as a int_identifier or a flt_identifier. So it doesn’t know whether to reduce A = B as IDENTIFIER = int_expr or IDENTIFIER = flt_expr.

    (Here’s where my understanding of Ruby is a little soft:) Ruby (like most languages) doesn’t provide a way at the lexical level to determine the numeric type of an identifier. Contrast this with old school BASIC, where A denotes a number and A$ denotes a string. In other words, if you invented a language where, say, A# denotes an integer and A@ denotes a float, then you could make this work.

    If you wanted to permit limited mixed-type expressions, like an int_term '*' flt_factor, then your grammar would get even more complicated.

    There might be ways to work around these issues. A parser built from technology other than yacc/bison might make it easier. At the least, perhaps my sketch will give you some ideas to pursue further.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.