gpt4 book ai didi

c - 如何向 C 词法分析器添加浮点解析功能?

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:24 25 4
gpt4 key购买 nike

我正在尝试将浮点功能添加到这个我用 C 语言编写的简单词法分析器中,用于 C(除其他外)。我对此有一些想法,但它们都是不完整的解决方案,主要涉及将 if 语句添加到 Parse 整数文字中,但由于 while 语句,它仍然会停止并将句点计为句点。我考虑过在那个 while 语句中添加一个 OR,但我不完全确定如何指定它只是一个句点。这是代码:

    /* front.c */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
/*Global Declarations */
/*variables*/
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *in_fp, *fopen();

/*function declarations*/
void addChar();
void getChar();
void getNonBlank();
int lex();

/*Character classes */
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99

/*token codes*/
#define INT_LIT 10
#define FLOAT
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26
#define MOD_OP 27
#define SEMICOL 28
#define COMMA 29
#define EXCLAMATION_MARK 30
#define AT_SIGN 31
#define POUND_SIGN 32
#define DOLLAR_SIGN 33
#define CARAT_SIGN 34
#define AMPERSAND 35
#define PERIOD_MARK 36
#define LESSTHAN_SIGN 37
#define GREATERTHAN_SIGN 38

#define QUESTION_MARK 39
#define LEFT_SQUAREBRACKET 40
#define RIGHT_SQUAREBRACKET 41
#define LEFT_CURLYBRACKET 42
#define RIGHT_CURLYBRACKET 43
#define BACKSLASH 44
#define VERTICALBAR 45

#define SINGLE_QUOTE 46

#define DOUBLE_QUOTE 47
#define COLON 48

#define UNDERSCORE 49
#define TILDE 50
#define GRAVE_ACCENT 51




/*********************/
/*main driver */
main()
{
/*Open the input data file and process its contents*/
if ((in_fp = fopen("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else
{
getChar();
do
{
lex();
} while (nextToken != EOF);
}
}

/***************************/
/*lookup - a function to lookup operators and parentheses
and return the token */
int lookup(char ch)
{
switch (ch)
{
case '=':
addChar();
nextToken = ASSIGN_OP
break;

case '(':
addChar();
nextToken = LEFT_PAREN;
break;

case ')':
addChar();
nextToken = RIGHT_PAREN;
break;

case '+':
addChar();
nextToken = ADD_OP;
break;

case '-':
addChar();
nextToken = SUB_OP;
break;

case '*':
addChar();
nextToken = MULT_OP;
break;

case '/':
addChar();
nextToken = DIV_OP;
break;

case '%':
addChar();
nextToken = MOD_OP;
break;

case ';':
addChar();
nextToken = SEMICOL;
break;

case ':':
addChar();
nextToken = COLON;
break;

case '"':
addChar();
nextToken = DOUBLE_QUOTE;
break;

case ',':
addChar();
nextToken = COMMA;
break;

case '.':
addChar();
nextToken = PERIOD_MARK;
break;

case '!':
addChar();
nextToken = EXCLAMATION_MARK;
break;

case '@':
addChar();
nextToken = AT_SIGN;
break;

case '#':
addChar();
nextToken = POUND_SIGN;
break;

case '$':
addChar();
nextToken = DOLLAR_SIGN;
break;

case '^':
addChar();
nextToken = CARAT_SIGN;
break;

case '&':
addChar();
nextToken = AMPERSAND;
break;

case '<':
addChar();
nextToken = LESSTHAN_SIGN;
break;

case '>':
addChar();
nextToken = GREATERTHAN_SIGN;
break;

case '?':
addChar();
nextToken = QUESTION_MARK;
break;

case '[':
addChar();
nextToken = LEFT_SQUAREBRACKET;
break;

case ']':
addChar();
nextToken = RIGHT_SQUAREBRACKET;
break;

case '{':
addChar();
nextToken = LEFT_CURLYBRACKET;
break;

case '}':
addChar();
nextToken = RIGHT_CURLYBRACKET;
break;

case '\'':
addChar();
nextToken = SINGLE_QUOTE;
break;*

case '|':
addChar();
nextToken = VERTICALBAR;
break;

case '_':
addChar();
nextToken = UNDERSCORE;
break;

case '~':
addChar();
nextToken = TILDE;
break;

case '`':
addChar();
nextToken = GRAVE_ACCENT;
break;

case '\\':
addChar();
nextToken = BACKSLASH;
break;

default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}

/*****************************/
/* addChar = a function to add nextChar to lexeme */
void addChar()
{
if (lexLen <= 98)
{
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else
printf("Error - lexeme is too long \n");
}

/**********************************/
/* getChar- a function to get the next character of
input and determine its character class */
void getChar()
{
if ((nextChar = getc(in_fp)) != EOF)
{
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else charClass = UNKNOWN;
}
else
charClass = EOF;
}

/********************************************/
/* getNonBlank - a function to call getChar until it
returns a non-whitespace character */
void getNonBlank()
{
while (isspace(nextChar))
getChar();
}

/*******************************/
/* lex - a simple lexical analyzer for arithmetic
expressions */
int lex()
{
lexLen = 0;
getNonBlank();
switch (charClass)
{
/*Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = IDENT;
break;


/*Parse integer literals and ?Floats?*/
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = INT_LIT;
break;

/*Parentheses and operators*/
case UNKNOWN:
lookup(nextChar);
getChar();
break;

/*EOF*/
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
}/*End of switch*/
printf("Next token is: %d, Next lexeme is %s\n",
nextToken, lexeme);
return nextToken;
} /*End of function lex*/

我在想,也许如果我在 while 语句中使用像“charClass.ch == '.'”这样的东西作为“charClass == DIGIT”的扩展,通过 || (或),但我认为我可能会与另一种语言混淆或做错了。我可能不是,但目前很难正确测试这个程序。

这是我认为需要更改以获得 float 的具体部分:

    /*Parse integer literals and ?Floats?*/
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = INT_LIT;
break;

最佳答案

/*Parse integer literals and ?Floats?*/
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT)
{
addChar();
getChar();
}

此时您已经知道nextChar 是什么了。如果它是一个点,请编写更多代码来使用它和所有后续数字并将 nextToken 设置为 FLOAT_LIT。 否则会落入此:

        nextToken = INT_LIT;
break;

关于c - 如何向 C 词法分析器添加浮点解析功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com