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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:18:47+00:00 2026-06-06T05:18:47+00:00

I was working with a polyhedral compiler that produce quite unreadable code, here is

  • 0

I was working with a polyhedral compiler that produce quite unreadable code, here is a small sample:

for (t2=2*t1;t2<=min(floord(2*T+N-3,32),floord(64*t1+N+61,32));t2++) {
  for (t3=max(ceild(32*t2-N-27,32),2*t1);t3<=min(min(floord(2*T+N-3,32),floord(64*t1+N+61,32)),floord(32*t2+N+27,32));t3++) {
    if ((t1 <= floord(32*t3-N+1,64)) && (t2 <= t3-1)) {
      if ((N+1)%2 == 0) {
        for (t5=max(32*t2,32*t3-N+4);t5<=32*t2+31;t5++) {
          a[-32*t3+t5+N-2][N-2]=b[-32*t3+t5+N-2][N-2];;
        }
      }
    }
    if ((t1 <= floord(32*t2-N+1,64)) && (t2 >= t3)) {
      if ((N+1)%2 == 0) {
        for (t6=max(32*t3,32*t2-N+4);t6<=min(32*t2,32*t3+31);t6++) {
          a[N-2][-32*t2+t6+N-2]=b[N-2][-32*t2+t6+N-2];;
        }
      }
    }

I was trying to debug a part of the compiler by translating arithmetic expression in the produced code with a printf of the array indices, e.g., this expression:

a[-32*t3+t5+N-2][N-2]=b[-32*t3+t5+N-2][N-2];;

should become this printf:

printf("a[%d][%d] = b[%d][%d]\n",-32*t3+t5+N-2,N-2,-32*t3+t5+N-2,N-2);

I started trying with awk and produced this simple program that identifies strings to be modified and let the rest of the program unchanged:

awk '{if ($0 ~ "^[ ]*[a,b]") print "printf("; else print $0;}'

However, I do not know how to parse the arithmetic expression in order to leave its structure while removing the indexes of the array accesses.
I tried with a while loop but I am stuck at the moment.
awk should be ok to do such substitions but any suggestions in other languages are welcome!

update The arithmetic expression can be any arithmetic expression, such as:

b[t3][t4]=0.2*(a[t3][t4]+a[t3][t4-1]+a[t3][1+t4]+a[1+t3][t4]+a[t3-1][t4]);;
  • 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-06T05:18:50+00:00Added an answer on June 6, 2026 at 5:18 am

    Code:

    awk '/\[/ { \
        sub(/^ */, ""); \
        sub(/;*$/, ""); \
        print \
            "printf(\"" \
            gensub("\\[[^]]*\\]", "[%d]", "g") \
            "\"" \
            gensub("[^\\[]*\\[([^]]*)\\][^\\[]*", ",\\1", "g") \
            ");" \
        ; \
        next; \
    }1'
    

    Explanations:

    • awk '/\[/ { ........; next; }1' will execute ........ on any line where it finds a [ character, and will print the line untouched otherwise.
    • sub(/^ */, ""); trims leading space characters off the current line
    • sub(/;*$/, ""); trims tailing semi-colon characters off the current line
    • This is followed by a print statement concatenating 5 bits
    • gensub("\\[[^]]*\\]", "[%d]", "g") returns a copy of the current line, where any square-bracketed statements are replaced with with [%d]. Note that nested square-brackets would break this. Also note, that as opposed to sub, the gensub command does not actually modify the current line.
    • gensub("[^\\[]*\\[([^]]*)\\][^\\[]*", ",\\1", "g") also takes a copy of the current line, and for each square-bracketed expression it finds, it does 3 things:
      • [^\\[]* removes leading non-[ characters
      • \\[([^]]*)\\] matches the square-bracketed expression, and ",\\1" replaces it with a comma character followed by what was inside the square-brackets
      • [^\\[]* removes trailing non-[ characters (until the next square-bracketed expression or the end of the line)
    • Note that removing both leading and trailing non-[ characters is redundant between 2 square-bracketed expression, but is useful at the very beginning and the very end of the line.

    Input:

    for (t2=2*t1;t2<=min(floord(2*T+N-3,32),floord(64*t1+N+61,32));t2++) {
      for (t3=max(ceild(32*t2-N-27,32),2*t1);t3<=min(min(floord(2*T+N-3,32),floord(64*t1+N+61,32)),floord(32*t2+N+27,32));t3++) {
        if ((t1 <= floord(32*t3-N+1,64)) && (t2 <= t3-1)) {
          if ((N+1)%2 == 0) {
            for (t5=max(32*t2,32*t3-N+4);t5<=32*t2+31;t5++) {
              a[-32*t3+t5+N-2][N-2]=b[-32*t3+t5+N-2][N-2];;
            }
          }
        }
        if ((t1 <= floord(32*t2-N+1,64)) && (t2 >= t3)) {
          if ((N+1)%2 == 0) {
            for (t6=max(32*t3,32*t2-N+4);t6<=min(32*t2,32*t3+31);t6++) {
              a[N-2][-32*t2+t6+N-2]=b[N-2][-32*t2+t6+N-2];;
            }
          }
        }
    
    
        b[t3][t4]=0.2*(a[t3][t4]+a[t3][t4-1]+a[t3][1+t4]+a[1+t3][t4]+a[t3-1][t4]);;
    

    Output:

    for (t2=2*t1;t2<=min(floord(2*T+N-3,32),floord(64*t1+N+61,32));t2++) {
      for (t3=max(ceild(32*t2-N-27,32),2*t1);t3<=min(min(floord(2*T+N-3,32),floord(64*t1+N+61,32)),floord(32*t2+N+27,32));t3++) {
        if ((t1 <= floord(32*t3-N+1,64)) && (t2 <= t3-1)) {
          if ((N+1)%2 == 0) {
            for (t5=max(32*t2,32*t3-N+4);t5<=32*t2+31;t5++) {
    printf("a[%d][%d]=b[%d][%d]",-32*t3+t5+N-2,N-2,-32*t3+t5+N-2,N-2);
            }
          }
        }
        if ((t1 <= floord(32*t2-N+1,64)) && (t2 >= t3)) {
          if ((N+1)%2 == 0) {
            for (t6=max(32*t3,32*t2-N+4);t6<=min(32*t2,32*t3+31);t6++) {
    printf("a[%d][%d]=b[%d][%d]",N-2,-32*t2+t6+N-2,N-2,-32*t2+t6+N-2);
            }
          }
        }
    
    printf("b[%d][%d]=0.2*(a[%d][%d]+a[%d][%d]+a[%d][%d]+a[%d][%d]+a[%d][%d])",t3,t4,t3,t4,t3,t4-1,t3,1+t4,1+t3,t4,t3-1,t4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

working on an old translation sample code for windows phone 7. Recently, I have
Working on a SQL query here, I have an ID column that I created
Working on a bunch of forms at the moment and I'm finding that I
Working on a website that has Employee and Branch entities, using a database table
Working on a rather small, and simple layout, I decided to use Meyer's CSS
Working on a movie website and would love to find an API that I
Working on an extension that use the new experimental devtools apis. How do you
Working sample using one Table SELECT t.* FROM ( SELECT TITLE.name, (TITLE.value-TITLE.msp) AS Lower,
Working with a Lucene index, I have a standard document format that looks something
Working with Json, how can I NSlog only the title in this code: NSDictionary

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.