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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:50:34+00:00 2026-05-12T15:50:34+00:00

I wounder how to implement indentation as block delimiters in bison + flex. Just

  • 0

I wounder how to implement indentation as block delimiters in bison + flex. Just like in python. I’m writing my own programming language ( mostly for fun, but I intend to use it together with a game engine ), I’ll try to come up with something special that minimizes boilerplate and maximizes dev speed.

I have already written an compiler ( actually a `langToy’ to Nasm translator ) in C, but failed. By some reason it was only able to handle one string in the whole source file ( well, I had been awake for more than 48 hours – so… You know, brain meltdown ).

I don’t know if curly brackets and/or begin -> end are easier to implement ( I don’t have problem doing that ) or if it’s just my brain that locks up.

Thanks in advance!


Update: Okay, I have no clue about how to do it with flex. I have problems with returning multiple DEDENTs to the parser. Flex/Bison are relatively new to me.


Update 2:
This is the flex-file I’ve come up with so far; it does not quite get it:

%x t
%option noyywrap

%{
  int lineno = 0, ntab = 0, ltab = 0, dedent = 0;
%}

%%

<*>\n  { ntab = 0; BEGIN(t); }
<t>\t  { ++ntab; }
<t>.   { int i; /* my compiler complains not c99 if i use for( int i=0... */
         if( ntab > ltab )
           printf("> indent >\n");
         else if( ntab < ltab )
           for( i = 0; i < ltab - ntab; i++ )
             printf("< dedent <\n");
         else
           printf("=        =\n");

         ltab = ntab; ntab = 0;
         BEGIN(INITIAL);
         /* move to next rule */
         REJECT;}
.    /* ignore everything else for now */

%%

main()
{
  yyin = fopen( "test", "r" );
  yylex();
}

You can try to play around with it, maybe you sees what I’m missing. returning multiple dedents would be an ease in Haxe ( return t_dedent( num ); ).

This code doesn’t always match the indents/dedents correctly.


Update 3: I think that I will give up hope on flex and do it my own way, If anyone knows how to do it in flex I would be happy to hear it anyways.

  • 1 1 Answer
  • 1 View
  • 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-12T15:50:35+00:00Added an answer on May 12, 2026 at 3:50 pm

    What you need to do is have flex count the amount of whitespace at the beginning of every line and insert an appropriate number of INDENT/UNINDENT tokens for the parser to use to group things. One question is what you want to do about tabs vs spaces — do you just want to have them be equivalent with fixed tab stops, or do you want to require indenting to be consistent (so if one line begins with a tab and the next with a space, you signal an error, which is probably a little harder).

    Assuming you want fixed 8-column tabstops, you can use something like

    %{
    /* globals to track current indentation */
    int current_line_indent = 0;   /* indentation of the current line */
    int indent_level = 0;          /* indentation level passed to the parser */
    %}
    
    %x indent /* start state for parsing the indentation */
    %s normal /* normal start state for everything else */
    
    %%
    <indent>" "      { current_line_indent++; }
    <indent>"\t"     { current_line_indent = (current_line_indent + 8) & ~7; }
    <indent>"\n"     { current_line_indent = 0; /*ignoring blank line */ }
    <indent>.        {
                       unput(*yytext);
                       if (current_line_indent > indent_level) {
                           indent_level++;
                           return INDENT;
                       } else if (current_line_indent < indent_level) {
                           indent_level--;
                           return UNINDENT;
                       } else {
                           BEGIN normal;
                       }
                     }
    
    <normal>"\n"     { current_line_indent = 0; BEGIN indent; }
    ... other flex rules ...
    

    You do have to make sure you start the parse in indent mode (to get the indentation on the first line).

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

Sidebar

Related Questions

I'd like implement a pagination. So I just wonder whether it's possible to select
I just wonder how to implement notification system for asp.net mvc3 application like basically
I'm trying to implement Window socket using Python. Mostly, everything has been so far
I am a beginner to Haskell. I just wonder how to implement a function
I'd like to create a class called Object to be able to implement toString(),
Edit : This is a mostly Rails question I'm trying to implement instant payment
I'd like to build a caching proxy as a Python WSGI middleware and wonder
Just wonder is there a way to implement heapify operation in a functional style
I would like to implement some facebook action in my app and I wonder:
I wonder how is generated the grammar of the Python language and how it

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.