Principles of Compilation | Course Design—Use of Lex and Yacc

Level 1 - Calculation of the proportion of specific bases in the base sequence

1. Task description

In a double-stranded DNA molecule, the higher the proportion of G and C base pairs, the stronger its stability. Write a lex description file to calculate the ratio of G and C bases in the specified base sequence.

2. Programming requirements

After completing the above programming tasks, copy and paste the C language source program to the code editor on the right, click the "Evaluate" button, run the program, and the system will automatically compare the results.

3. Test instructions

The platform will test the code you write:

Test Input: ACGTTGATCGGAATCTTCGTExpected Output:0.450

Test Input: TTACGGTACCAATGCTAATGCCTAExpected Output:0.417

4. Code

#include <stdio.h>
#include <string.h>

int main()
{
    char dna[100];
    scanf("%s", dna);

    int length = strlen(dna); 
    int gcCount = 0; 
    for(int i = 0; i < length; i++)
    {
        if(dna[i] == 'G' || dna[i] == 'C') 
            gcCount++;
    }

    double ratio = (double)gcCount / length; 
    printf("%.3f", ratio);
    return 0;
}

Level 2 — Recognition of words, numbers and symbols in text strings

1. Task description

Write a lex description file to identify the words, numbers and symbols in the specified text string (spaces are not processed).

2. Programming requirements

After completing the above programming tasks, copy and paste the C language source program to the code editor on the right, click the "Evaluate" button, run the program, and the system will automatically compare the results.

3. Test instructions

The platform will test the code you write:

Test input:

using namespace std;
int main()
{
    int year = 2022;
    cout << "hello" << endl;
    return 0;
}

Expected output:

using 单词
namespace 单词
std 单词
; 符号
int 单词
main 单词
( 符号
) 符号
{ 符号
int 单词
year 单词
= 符号
2022 数字
; 符号
cout 单词
< 符号
< 符号
" 符号
hello 单词
" 符号
< 符号
< 符号
endl 单词
; 符号
return 单词
0 数字
; 符号
} 符号

4. Code

#include<stdio.h>
#include<string.h>
#define   maxn 10000

char str[maxn];
void fun(char str[]);
int main() {
	int i = 0;
	while ((str[i] = getchar()) != EOF) {
		i++;
	}
	str[i] = '\0';
	fun(str);
	return 0;
}

void fun(char str[]) {
	for (int j = 0; str[j] != '\0'; j++) {
	
		if (str[j] >= '0' && str[j] <= '9') {
			while (str[j] >= '0' && str[j] <= '9') {
				putchar(str[j]);
				j++;
			}
			j--;
			printf(" 数字\n");
			continue;
		} 
		if ((str[j] >= 'a' && str[j] <= 'z') || (str[j] >= 'A' && str[j] <= 'Z')){
			while ((str[j] >= 'a' && str[j] <= 'z') || (str[j] >= 'A' && str[j] <= 'Z')){
				putchar(str[j]);
				j++;
			}
			j--;
			printf(" 单词\n");
			continue;
		} 
		if (str[j] == ' '||str[j] == '\n'||str[j]=='	'){
			continue;
		} 
		putchar(str[j]);
		printf(" 符号\n");
					
	}

}

Level 3 - Implementation of a Simple Calculator

1. Task description

Write a yacc description file to implement a calculator with addition and multiplication functions.

2. Programming requirements

After completing the above programming tasks, copy and paste the C language source program to the code editor on the right, click the "Evaluate" button, run the program, and the system will automatically compare the results.

3. Test instructions

The platform will test the code you write:

Test Input: 5*7+2Expected Output:37

Test Input: 9+3*6Expected Output:27

4. Code

#include "task1-3.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int calculate(int a, int b, char op) {
    switch(op) {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            if (b != 0) return a / b;
            else return 0;
        default:
            return 0;
    }
}

int calculate_expression(char** s) {
    char op[2];
    int num, stack[100], top = -1;

    while (**s && **s != ')') {
        if (isdigit(**s)) {
            sscanf(*s, "%d", &num);
            stack[++top] = num;

            while (isdigit(**s))
                (*s)++;
        }

        if (**s == '+' || **s == '-' || **s == '*' || **s == '/') {
            sscanf(*s, "%s", op);

            if ((*op == '*' || *op == '/') && isdigit(*(*s+1))) {
                int nextNum;
                sscanf(*s+1, "%d", &nextNum);
                stack[top] = calculate(stack[top], nextNum, *op);

                while (isdigit(*(*s+1)))
                    (*s)++;
            }
            else if (top > 0) {
                int right = stack[top--];
                int left = stack[top--];
                stack[++top] = calculate(left, right, *op);
            }
        }

        if (**s == '(') {
            (*s)++;
            stack[++top] = calculate_expression(s);
            (*s)++;
        }

        (*s)++;
    }

    while (top > 0) {
        int right = stack[top--];
        int left = stack[top--];
        stack[++top] = calculate(left, right, '+');
    }

    return stack[0];
}

int calculate_expression_with_parentheses(const char* expression) {
    char* s = (char*)expression;
    return calculate_expression(&s);
}

int main() {
    char expression[100];
    fgets(expression, 100, stdin);
    printf("%d\n", calculate_expression_with_parentheses(expression));
    return 0;
}

Guess you like

Origin blog.csdn.net/sun80760/article/details/131078490
Recommended