Small Basic Compiler Problem

Small Basic Compiler Problem
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

Write an interpreter in TinyBasic, and for any given TinyBasic program, your program can run it and get the correct result. So, what is the correct TinyBasic program called?

(1) Comply with the grammar rules of TinyBasic language;
(2) One or more outputs will be generated when the program is executed, which can be interrupted (that is, the program will not enter an infinite loop state).

Grammar rules of TinyBasic language:

(1) The TinyBasic program in each line is in the following form (all letters appearing in uppercase)
[<space>]<line number><space><statement>
Among them, there can be any number of [<space>] There is no space, of course;
there must be a line number in <line number>, starting from 1 and increasing by 1;
<space> should contain at least one space;
<statement> should be one of the following statements:
LET<space> <variable>=<expression>
PRINT<space><variable>
GOTO<space><expression>
IF<space><expression>
STOP
(2) The rules for defining variables and expressions are that
<variable> must be single Uppercase English letters, store an integer value;
<expression> is
<constant>, an integer constant in the range of [-10000…10000], such as 0, -5, 34
<variable>+<variable> represented by two variables It is a signed integer
<variable>><variable> greater than sign, true is 1, false is 0
(3) There are no spaces on both sides of the = (equal sign) in the expression and the LET statement;
(4) TinyBasic program has only 100 lines at most .

Rules for executing TinyBasic language programs:

(1) Execute from the first line in the program;
(2) The initial value of all variables used in the program is 0;
(3) Statements are executed continuously unless an IF or GOTO statement is encountered;
(4) 5 kinds of statements The definition of
LET assigns a value to a variable. If two variables are added, the result of the addition is within [-10000…10000].
Print the value of the variable in the format PRINT <variable name>=<value>. Left-aligned and on a separate line; no extra spaces in the line.
GOTO Jumps to the line whose line number is the value of <expression>. <expression> need not be a constant; the value of <expression> is a valid line number in the program.
IF If the value of the expression is not 0, continue to the next line; if the value of the expression is 0, skip the next line and execute the next line of the next line. There should be at least two more statements below the IF statement.
STOP terminates execution. A TinyBasic program will always execute to a STOP statement (if your interpreter is correct); a TinyBasic program may contain more than one STOP statement; the last sentence of the program is not necessarily a STOP statement.
Input

There is only one set of input data, including a program, no extra blank lines, each line is a statement, and the specific requirements are as explained above. The program you write must run the TinyBasic program correctly.
Output

Output the running result of the program without extra blank lines at the beginning and end of the file.
Sample Input

1 LET A=10
2 LET I=0
3 LET X=I+I
4 LET T=1
5 LET X=X+T
6 PRINT X
7 LET T=1
8 LET I=I+T
9 IF A>I
10 GOTO 3
11 STOP
Sample Output

X=1
X=3
X=5
X=7
X=9
X=11
X=13
X=15
X=17
X=19

#include<stdio.h>
#include<string.h>
struct node
{
    char key[50];
    char var[50];
}str[110];
int a[30];
int main()
{
  memset(a, 0, sizeof(a));
  int l=0,n;
  while(scanf("%d",&n)!=EOF)
  {
      char s[50],s1[50];
    scanf("%s",s);
    strcpy(str[n].key,s);
    l++;
    if(strcmp(s,"STOP")!=0)
    {
        scanf("%s",s1);
    strcpy(str[n].var,s1);
    }
    //if(strcmp(s,"STOP")==0)
    //{
    //    break;
    //}

  }

  int j;
  for(j=1;j<=l;)
    {
        if(strcmp(str[j].key,"LET")==0)
    {
        int i;
        int m=(int)(str[j].var[0]-'A');
        if((str[j].var[2]>='0'&&str[j].var[2]<='9')||str[j].var[2]=='-')
        {
            int k=strlen(str[j].var);
            int l1=0,p=1;
            for(i=k-1;str[j].var[i]!='='&&str[j].var[i]!='-';i--)
            {
                l1+=(str[j].var[i]-'0')*p;
                p*=10;
            }
            if(str[j].var[2]=='-')
            a[m]=-l1;
            else
            a[m]=l1;
            //printf("shu %c=%d\n",str[j].var[0],a[m]);
        }
        else if(str[j].var[3]=='+')
        {
           int m1=(int)(str[j].var[2]-'A');
           int m2=(int)(str[j].var[4]-'A');
           a[m]=a[m1]+a[m2];

        }
        else if(str[j].var[3]=='>')
        {
            int m1=(int)(str[j].var[2]-'A');
           int m2=(int)(str[j].var[4]-'A');
           if(a[m1]>a[2])
           a[m]=1;
           else
           a[m]=0;
           //printf("> %c=%d\n",str[j].var[0],a[m]);
        }
        j++;
    }
    else if(strcmp(str[j].key,"PRINT")==0)
    {
        int m=(int)(str[j].var[0]-'A');
        printf("%c=%d\n",str[j].var[0],a[m]);
        j++;
    }
    else if(strcmp(str[j].key,"IF")==0)
    {
        int m1=(int)(str[j].var[0]-'A');
            int m2=(int)(str[j].var[2]-'A');
            if(a[m1]<=a[m2])
            j++;
            j++;
    }
    else if(strcmp(str[j].key,"GOTO")==0)
    {int i,l1=0,k,p=1;
    k=strlen(str[j].var);
        for(i=k-1;i>=0;i--)
        {
            l1+=(int)(str[j].var[i]-'0')*p;
            p*=10;
        }
        j=l1;
    }
    else if(strcmp(str[j].key,"STOP")==0)
    {
        break;
    }
    }
    return 0;
  }


“`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324792563&siteId=291194637