对于C编程分配,我试图写几个头文件来检查所谓的" X编程语言"的语法。我最近刚开始,我正在编写第一个头文件。这是我写的代码:
#ifndef _DeclarationsChecker_h_
#define _DeclarationsChecker_h_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LINE_LENGTH_LIMIT 200
#define CODE_LINE_LIMIT 1000
void checkDeclarations(char **code, int num_lines) {
char *currentLine;
for (int currentLineNum = 0; currentLineNum < num_lines; currentLineNum++) {
if (code[currentLineNum] != NULL) {
currentLine = code[currentLineNum];
char (**tokenized)[LINE_LENGTH_LIMIT];
for (int i = 0; i < strlen(currentLine); i++) {
tokenized[i] = strtok(currentLine, " ");
if (tokenized[i] == NULL)
break;
}
char *currentToken;
for (int i = 0; i < LINE_LENGTH_LIMIT; i++) {
currentToken = tokenized[i];
if (strcmp("***", currentToken))
break;
char (*nextToken) = tokenized[i + 1];
if (strcmp("global", currentToken)) {
if (!strcmp("character", nextToken) && !strcmp("integer", nextToken) && !strcmp("double", nextToken) && !strcmp("string", nextToken)) {
printf("Declarations: unknown data type %s at line %d", nextToken, currentLineNum);
}
}
if (strcmp("character", currentToken) || strcmp("integer", currentToken) || strcmp("double", currentToken) || strcmp("string", currentToken)) {
char *functionName = strtok(nextToken, '(');
if (strcmp("character", functionName) || strcmp("integer", functionName) || strcmp("double", functionName) || strcmp("string", functionName) || strcmp("while", functionName) || strcmp("if", functionName) || strcmp("else", functionName) || strcmp("global", functionName) || strcmp("equal", functionName) || strcmp("nequal", functionName) || strcmp("return", functionName)) {
printf("Declarations: naming violation of %s at line %d", functionName, currentLineNum);
}
for (int i = 0; i < strlen(functionName); i++) {
if (!isalnum(functionName[i]) && (functionName[i] != '_') && (functionName[i] != '?')) {
printf("Declarations: naming violation of %s at line %d", functionName, currentLineNum);
}
}
}
}
}
}
}
#endif
我收到了几个编译警告,我会在最后添加警告,当我尝试启动程序时,它会立即给出一个&#34;程序崩溃&#34;错误,但我认为可能是因为尚未编写的头文件。我能做些什么来摆脱我得到的错误?感谢您的回答,任何帮助将不胜感激。 (请注意,我是C的新手,我并没有完全理解数组与指针和双指针之间的可互换性概念(例如:** ptr))
...\declarationschecker.h(30): warning C4018: '<': signed/unsigned mismatch
...\declarationschecker.h(32): warning C4047: '=': 'char (*)[200]' differs in levels of indirection from 'char *'
...\declarationschecker.h(42): warning C4047: '=': 'char *' differs in levels of indirection from 'char (*)[200]'
...\declarationschecker.h(59): warning C4047: 'function': 'const char *' differs in levels of indirection from 'int'
...\declarationschecker.h(59): warning C4024: 'strtok': different types for formal and actual parameter 2
...\declarationschecker.h(66): warning C4018: '<': signed/unsigned mismatch
...\declarationschecker.h(47): warning C4047: 'initializing': 'char *' differs in levels of indirection from 'char (*)[200]'
需要标题的主要c文件发布在下面:
#include "CodeReader.h"
#include "BracketsChecker.h"
#include "DeclarationsChecker.h"
#include "StatementsChecker.h"
#include "SSAChecker.h"
int main(int argc, char * argv[]) {
if (argc < 2) {
printf("Please provide X code file name\n");
exit(1);
}
char **code = readCode(argv[1]);
int num_lines = getCodeNumLines();
checkBrackets(code, num_lines);
checkDeclarations(code, num_lines);
checkProgramStatements(code, num_lines);
checkSSA(code, num_lines);
cleanMemory(code);
int terminalHung; scanf("%d", &terminalHung);
return 0;
}
答案 0 :(得分:2)
首先,无法访问项目的其余部分(我假设还有其他一些文件包含您在帖子中引用的一些功能),因此无法知道您是如何创建代码的生成器,限制对语法问题的建议。
以下两条评论中对您帖子中的以下错误的说明,以及底部代码中的内嵌评论:
首先,在您编辑的部分中,我看不到函数readCode()
是什么,因为您没有包含它,但如果它不创建内存则无法使用变量code
。
声明char (**tokenized)[LINE_LENGTH_LIMIT];
后,您尝试使用char **
的第0个数组元素而不先创建内存。充其量,你的程序会在运行时崩溃,更糟糕的是,它似乎工作。这称为 Undefined 或 Unspecified behavior 。 (阅读如何使用 malloc )。因为您正在为字符串集合准备存储,所以您只需要两个diminsions,而不是三个。 char *[]
或char **
可行。在任何一种情况下,必须初始化这些并在使用前创建内存。但是,由于您已经知道最大行数和行的最大长度,因此只需声明并使用:char tokenized[CODE_LINE_LIMIT][LINE_LENGTH_LIMIT];
。
另外,声明char *token = 0;
与strtok一起使用。 (见原因评论)
另外,声明一次使用多次使用的变量(例如i
。看原因评论)
对于其余部分,请再次查看内嵌评论,了解您的代码中之前的错误/警告是如何解决的:
static void checkDeclarations(char **code, int num_lines)
{
char *token = 0;//use with strtok
char *currentLine;
char tokenized[CODE_LINE_LIMIT][LINE_LENGTH_LIMIT] = {{0}};
int i, len; // declare multiply used variables once
for (int currentLineNum = 0; currentLineNum < num_lines; currentLineNum++) {
if (code[currentLineNum] != NULL) {
currentLine = code[currentLineNum];
//char (*tokenized)[LINE_LENGTH_LIMIT] = {0};
len = strlen(currentLine);
for( i = 0; i< len; i++ ) // corrected
//for (int i = 0; i < strlen(currentLine); i++) // don't do string comparison's in a loop
{ // and avoid comparisons of different types
// return of strlen() is an unsigned int
token = strtok(currentLine, " ");
if (token == NULL) break;
else strcpy(tokenized[i], token);
}
char *currentToken;
//for (int i = 0; i < LINE_LENGTH_LIMIT; i++) { // shadow declaration of previously declared variable
for ( i = 0; i < LINE_LENGTH_LIMIT; i++) { // corrected
currentToken = tokenized[i];
if (strcmp("***", currentToken))
break;
char (*nextToken) = tokenized[i + 1];
if (strcmp("global", currentToken)) {
if (!strcmp("character", nextToken) && !strcmp("integer", nextToken) && !strcmp("double", nextToken) && !strcmp("string", nextToken)) {
printf("Declarations: unknown data type %s at line %d", nextToken, currentLineNum);
}
}
if (strcmp("character", currentToken) || strcmp("integer", currentToken) || strcmp("double", currentToken) || strcmp("string", currentToken)) {
//char *functionName = strtok(nextToken, '('); // strtok 2nd argument requires a string, not an integer
char *functionName = strtok(nextToken, "("); // corrected
// note: calling this in a loop will be a problem. either Declare 'functionName' at top of function
// or use 'token', already declared
if (strcmp("character", functionName) || strcmp("integer", functionName) || strcmp("double", functionName) || strcmp("string", functionName) || strcmp("while", functionName) || strcmp("if", functionName) || strcmp("else", functionName) || strcmp("global", functionName) || strcmp("equal", functionName) || strcmp("nequal", functionName) || strcmp("return", functionName)) {
printf("Declarations: naming violation of %s at line %d", functionName, currentLineNum);
}
//for (int i = 0; i < strlen(functionName); i++) { // "i" has already been declared above
for ( i = 0; i < len; i++) { // corrected
if (!isalnum(functionName[i]) && (functionName[i] != '_') && (functionName[i] != '?')) {
printf("Declarations: naming violation of %s at line %d", functionName, currentLineNum);
}
}
}
}
}
}
}
编辑以解决评论中的问题...
您可能已经知道以下内容,但您的帖子没有给出任何指示,因此我提供以下内容以防万一:
在将字符串分配给char *str
之前(例如,通过使用strcpy
或strcat
等),您必须创建内存:
int desiredStrLen = 80;
char *str = calloc(desiredStrLen + 1, 1);
if(str)// test return of calloc before trusting it worked
{
//use str
...
free(str); // always, when finished with any dynamically allocated memory, free it.
对于字符串集合(例如,在读取文件行时需要),您可以为一组字符串创建内存。确定文件中的行数和最长行后,您可以创建足以将从文件中读取的每一行复制到字符串中的内存:
char **currentLine = Create2DStr(numLines, longestLine);
if(strings)
{
/// use currentLine (in your loop)
...
strcpy(currentLine[i], code[currentLineNum]);
...
// when finished with string collection, free it.
free2DStr(&strings, numLines);
我上面使用的功能可以通过多种方式实现。我使用以下内容:
char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen)
{
int i;
char **str = {0};
str = calloc(numStrings, sizeof(char *));
for(i=0;i<numStrings; i++)
{
str[i] = calloc(maxStrLen + 1, 1);
}
return str;
}
void free2DStr(char *** str, ssize_t numStrings)
{
int i;
if(!(*str)) return;
for(i=0;i<numStrings; i++)
{
free((*str)[i]);
(*str)[i] = NULL;
}
free((*str));
(*str) = NULL;
}