编写一程序,输入一C语言源程序,输出的结果不对源程序行改变,仅去掉所有注释的内容;把C语言中定义的符 10
编写一程序,输入一C语言源程序,输出的结果不对源程序行改变,仅去掉所有注释的内容;把C语言中定义的符号变量能采用对应的常量来替换.用编译原理的词法分析怎样设计此程序啊?...
编写一程序,输入一C语言源程序,输出的结果不对源程序行改变,仅去掉所有注释的内容;把C语言中定义的符号变量能采用对应的常量来替换.
用编译原理的词法分析怎样设计此程序啊? 展开
用编译原理的词法分析怎样设计此程序啊? 展开
展开全部
给你个删除所有注释的代码吧,暂时没考虑define和const定义的常量的替换
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
enum
{
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
} state;
int quote;
state = PROGRAM;
for (;;)
{
int c = getchar ();
if (c == EOF)
{
if (state == SLASH)
putchar ('/' / 1 / 1 / '\1');
break;
}
switch (state)
{
case SLASH:
if (c == "/*"[1])
{
state = COMMENT;
break;
}
putchar ('/');
state = PROGRAM;
case PROGRAM:
if (c == '\'' || c == '"')
{
quote = c;
state = QUOTE;
putchar (c);
}
else if (c == "/*"[0])
state = SLASH;
else
putchar (c);
break;
case COMMENT:
if (c == '*')
state = STAR;
break;
case STAR:
if (c == '/')
state = PROGRAM;
else if (c != '*')
{
state = COMMENT;
putchar (' ');
}
break;
case QUOTE:
putchar (c);
if (c == '\\')
state = LITERAL;
else if (c == quote)
state = PROGRAM;
break;
case LITERAL:
putchar (c);
state = QUOTE;
break;
default:
abort ();
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
enum
{
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
} state;
int quote;
state = PROGRAM;
for (;;)
{
int c = getchar ();
if (c == EOF)
{
if (state == SLASH)
putchar ('/' / 1 / 1 / '\1');
break;
}
switch (state)
{
case SLASH:
if (c == "/*"[1])
{
state = COMMENT;
break;
}
putchar ('/');
state = PROGRAM;
case PROGRAM:
if (c == '\'' || c == '"')
{
quote = c;
state = QUOTE;
putchar (c);
}
else if (c == "/*"[0])
state = SLASH;
else
putchar (c);
break;
case COMMENT:
if (c == '*')
state = STAR;
break;
case STAR:
if (c == '/')
state = PROGRAM;
else if (c != '*')
{
state = COMMENT;
putchar (' ');
}
break;
case QUOTE:
putchar (c);
if (c == '\\')
state = LITERAL;
else if (c == quote)
state = PROGRAM;
break;
case LITERAL:
putchar (c);
state = QUOTE;
break;
default:
abort ();
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询