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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:04:38+00:00 2026-06-05T01:04:38+00:00

I’m interested in adding semi-colon insertion ala Google Go to my flex file. From

  • 0

I’m interested in adding semi-colon insertion ala Google Go to my flex file.

From the Go documentation:

Semicolons

Like C, Go’s formal grammar uses semicolons to terminate statements;
unlike C, those semicolons do not appear in the source. Instead the
lexer uses a simple rule to insert semicolons automatically as it
scans, so the input text is mostly free of them.

The rule is this. If the last token before a newline is an identifier
(which includes words like int and float64), a basic literal such as a
number or string constant, or one of the tokens

break continue fallthrough return ++ -- ) }

the lexer always inserts a semicolon after the token. This could be
summarized as, “if the newline comes after a token that could end a
statement, insert a semicolon”.

A semicolon can also be omitted immediately before a closing brace, so
a statement such as

go func() { for { dst <- <-src } }()

needs no semicolons. Idiomatic Go programs have semicolons only in
places such as for loop clauses, to separate the initializer,
condition, and continuation elements. They are also necessary to
separate multiple statements on a line, should you write code that
way.

One caveat. You should never put the opening brace of a control
structure (if, for, switch, or select) on the next line. If you do, a
semicolon will be inserted before the brace, which could cause
unwanted effects. Write them like this

if i < f() {
    g()
}

not like this

if i < f()  // wrong! 
{           // wrong!
    g()     // wrong!
}           // wrong!

How would I go about doing this (how can I insert tokens in the stream, how can I see the last token that was matched to see if it is a good idea, etc etc etc)?

I am using bison too, but Go seems to just use their lexer for semicolon insertion.

  • 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-05T01:04:41+00:00Added an answer on June 5, 2026 at 1:04 am

    You could pass lexer result tokens through a function that inserts semicolons where necessary. Upon detection of the need to insert, the next token can be put back to the input stream, basically lexing it again in the next turn.

    Below is an example that inserts a SEMICOLON before a newline, when it follows a WORD. The bison file “insert.y” is this:

    %{
    #include <stdio.h>
    
    void yyerror(const char *str) {
      printf("ERROR: %s\n", str);
    }
    
    int main() {
      yyparse();
      return 0;
    }
    %} 
    %union {
      char *string;
    }
    %token <string> WORD
    %token SEMICOLON NEWLINE
    %%
    input: 
         | input WORD          {printf("WORD: %s\n", $2); free($2);}
         | input SEMICOLON     {printf("SEMICOLON\n");}
         ;
    %%
    

    and the lexer is generated by flex from this:

    %{
    #include <string.h>
    #include "insert.tab.h"
    int f(int token);
    %}
    %option noyywrap
    %%
    [ \t]          ;
    [^ \t\n;]+     {yylval.string = strdup(yytext); return f(WORD);}
    ;              {return f(SEMICOLON);}
    \n             {int token = f(NEWLINE); if (token != NEWLINE) return token;}
    %%
    int insert = 0;
    
    int f(int token) {
      if (insert && token == NEWLINE) {
        unput('\n');
        insert = 0;
        return SEMICOLON;
      } else {
        insert = token == WORD;
        return token;
      }
    }
    

    For input

    abc def
    ghi
    jkl;
    

    it prints

    WORD: abc
    WORD: def
    SEMICOLON
    WORD: ghi
    SEMICOLON
    WORD: jkl
    SEMICOLON
    

    Unputting a non-constant token requires a little extra work – I have tried to keep the example simple, just to give the idea.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words

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.