简体   繁体   English

我正在尝试在 C 中设计一个词法分析器

[英]I'm trying to design a lexical Analyzer in C

My Code:我的代码:

#include <stdio.h>

#include <ctype.h>

#include <string.h>


int main()

{

    int identifier_counter=0,constants_counter=0,operators_counter=0,delimeters_counter=0,i,j,x;
    char expr[50],operators[50],identifiers[50],constants[50],delimeters[50];
    printf("Enter the Expression: ");
    scanf("%[^\n]s",expr);
    for(i=0;i<strlen(expr);i++)
    {
        if(isspace(expr[i])){
            continue;
        }
        else if(isalpha(expr[i]))
        {
            identifiers[identifier_counter]=expr[i];
            identifier_counter++;
        }
        else if(isdigit(expr[i]))
        {
            x=(expr[i]-'0');
            i=i+1;
            while(isdigit(expr[i]))
            {
                x=x*10+(expr[i]-'0');
                i++;
            }
            i=i-1;
            constants[constants_counter]=x;
            constants_counter++;
        }
        else if(expr[i]==','||expr[i]==';'||expr[i]=='{'||expr[i]=='}')
        {
            if(expr[i]==',')
            {
                delimeters[delimeters_counter]=',';
                delimeters_counter++;
            }
            else if(expr[i]==';')
            {
                delimeters[delimeters_counter]=';';
                delimeters_counter++;
            }
            else if(expr[i]=='{')
            {
                delimeters[delimeters_counter]='{';
                delimeters_counter++;
            }
            else if(expr[i]=='}')
            {
                delimeters[delimeters_counter]='}';
                delimeters_counter++;
            }
            
        }
        else
        {
            if(expr[i]=='*')
            {
                operators[operators_counter]='*';
                operators_counter++;
            }
            else if(expr[i]=='-')
            {
                operators[operators_counter]='-';
                operators_counter++;
            }
            else if(expr[i]=='+')
            {
                operators[operators_counter]='+';
                operators_counter++;
            }
            else if(expr[i]=='=')
            {
                operators[operators_counter]='=';
                operators_counter++;
            }
            else if(expr[i]=='/')
            {
                
                operators[operators_counter]='/';
                operators_counter++;
            }
            else if(expr[i]=='%')
            {
                
                operators[operators_counter]='%';
                operators_counter++;
            }
        }
    }
    printf("\nIdentifiers are: ");
    for(j=0;j<identifier_counter;j++)
    {
        printf("%c ",identifiers[j]);
    }
    printf("\nConstants are: ");
    for(j=0;j<constants_counter;j++)
    {
        printf("%d ",constants[j]);
    }
    printf("\nDelimeters are: ");
    for(j=0;j<delimeters_counter;j++)
    {
        printf("%c ",delimeters[j]);
    }
    printf("\nOperators are: ");
    for(j=0;j<operators_counter;j++)
    {
        printf("%c ",operators[j]);
    }
    return 0;
}

My Queries:我的查询:

  1. I want to consider numbers or underscores after the variable's name as "identifiers".我想将变量名称后的数字或下划线视为“标识符”。 for example: user input is "a4".例如:用户输入是“a4”。 I want the program to print "identifiers are: a4".我希望程序打印“标识符是:a4”。 In my code 'a' is considered as an identifier and '4' as a constant.在我的代码中,“a”被视为标识符,“4”被视为常量。 If the user input is: "a 4" then it's ok if it prints 'a' as identifier and '4' as constant, since there's a space between.如果用户输入是:“a 4”,则可以将“a”作为标识符打印,将“4”打印为常量,因为两者之间有空格。

  2. I want to generate a token for data types.我想为数据类型生成一个令牌。 If the user types 'int' it should return that "datatypes are: int".如果用户键入“int”,它应该返回“数据类型为:int”。 I know one procedure but it's lengthy and time taking, I've tried to search if there are any built-in functions so that they can return the data type, but I couldn't find any.我知道一个过程,但它既冗长又耗时,我试图搜索是否有任何内置函数以便它们可以返回数据类型,但我找不到任何东西。

Can you help me with these two queries and how I should update my code accordingly.你能帮我解决这两个查询以及我应该如何相应地更新我的代码。 Thank you.谢谢你。

You should split your problem into little pieces.你应该把你的问题分成小块。 First, how do you recognize a identifier?首先,如何识别标识符? Write a function, that takes a string and returns the true, if it starts with an identifier.编写一个 function,它接受一个字符串并返回 true,如果它以标识符开头。 If you have done this, make the function return the length of the identifier.如果你已经这样做了,让 function 返回标识符的长度。 Now do the same thing with numbers and everything you want.现在对数字和你想要的一切做同样的事情。 Now you have build a tokenizer/lexer.现在你已经构建了一个分词器/词法分析器。 I have an implementation (but a bigger one) here .我在这里有一个实现(但更大的一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM