C语言中的标记化程序
在我的计算机科学课上,我必须想出一个程序,使用最大munch算法对给定输入文件的字符串进行标记。这就是我和我的朋友到目前为止所做的C语言中的标记化程序,c,C,在我的计算机科学课上,我必须想出一个程序,使用最大munch算法对给定输入文件的字符串进行标记。这就是我和我的朋友到目前为止所做的 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> FILE * input_from_args(int argc, const char *argv[]) { if(argc = 1) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
FILE *
input_from_args(int argc, const char *argv[])
{
if(argc = 1)
{
return stdin;
}
else
{
return fopen(argv[1], "r");
}
}
int main(int argc, const char* argv[])
{
FILE *src = input_from_args(argc, argv);
if(src == NULL)
{
fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
int c, nc;
while((c = getchar()) != EOF || (c = getchar()) != '\n')
{
if(c == ' ' || c == '\t')
{
printf("Blank Space");
}
else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
{
if(c == '+')
{
printf("%s \t addition operator\n", c);
}
else if(c == '-')
{
printf("%s \t subtraction operator\n", c);
}
else if(c == '*')
{
printf("%s \t multiplication operator\n", c);
}
if(c == '/')
{
printf("%s \t division operator\n", c);
}
else if(c == '^')
{
printf("%s \t exponentiation\n", c);
}
}
else if(c == '(' || c == ')')
{
if(c == '(')
{
printf("%s \t open parentheses\n", c);
}
else if(c == ')')
{
printf("%s \t close parentheses\n", c);
}
}
else if(c == '<' || c == '>' || c == '=')
{
if(c == '<' && (nc = getchar()) == '=')
{
printf("%s%s \t comparison operator\n", c, nc);
}
else if(c == '>' && (nc = getchar()) == '=')
{
printf("%s%s \t comparison operator\n", c, nc);
}
else if(c == '>' || c == '<')
{
printf("%s \t comparison operator\n", c);
}
else if(c == '=')
{
printf("%s \t equals operator\n", c);
}
}
else if(isdigit(c))
{
printf("%s \t simple numeral \n", c);
}
else if(isalpha(c))
{
printf("%s \t invalid token\n", c);
}
}
}
#包括
#包括
#包括
#包括
文件*
从参数输入参数(int argc,const char*argv[])
{
如果(argc=1)
{
返回stdin;
}
其他的
{
返回fopen(argv[1],“r”);
}
}
int main(int argc,const char*argv[]
{
FILE*src=从参数(argc,argv)输入参数;
如果(src==NULL)
{
fprintf(stderr,“%s:无法打开%s\n”,argv[0],argv[1]);
退出(退出失败);
}
INTC,nc;
而((c=getchar())!=EOF | |(c=getchar())!='\n')
{
如果(c=''| | c='\t')
{
printf(“空白区域”);
}
else如果(c='+'| | c='-'| | c='*'| | c='/'| | c='^')
{
如果(c=='+')
{
printf(“%s\t加法运算符\n”,c);
}
else如果(c=='-')
{
printf(“%s\t减法运算符\n”,c);
}
else如果(c=='*')
{
printf(“%s\t乘法运算符\n”,c);
}
如果(c=='/'))
{
printf(“%s\t除法运算符\n”,c);
}
else if(c=='^')
{
printf(“%s\t求幂\n”,c);
}
}
else如果(c==”(“| | c==”))
{
如果(c=='(')
{
printf(“%s\t开括号\n”,c);
}
如果(c=='),则为else)
{
printf(“%s\t右括号\n”,c);
}
}
else如果(c=''| c='='')
{
如果(c='&&(nc=getchar())='=')
{
printf(“%s%s\t比较运算符\n”,c,nc);
}
否则,如果(c='>'| c=='一个非常聪明的技巧可以更容易地发现(argc=1)而不是(argc==1)类错误,那就是实际习惯于将常数放在左边。
如果您编写了If(1=argc),编译器会抱怨您试图给常量赋值,您会立即发现这一点。
顺便说一句,这来自gnu编码风格。继前面的评论之后,您的代码块比较
和=
容易出错,并且永远不会与所写的=
匹配。作为例程的一部分,您在循环开始时会读取c
两次。强制读取两个字符,您将永远不会在
>比较运算符
输入:=比较等价运算符
输入:6
6简单数字
输入:a
无效的令牌
输入:q
收到“q”,退出。
输出-文件
$ ./bin/getchar_token_calc dat/tokens.txt
Blank Space
Blank Space
+ addition operator
- subtraction operator
* multiplication operator
/ division operator
^ exponentiation
( open parentheses
) close parentheses
= equals operator
< comparison operator
> comparison operator
<= comparison equivalence operator
>= comparison equivalence operator
6 simple numeral
'q' received, quitting.
$。/bin/getchar\u token\u calc dat/tokens.txt
空白处
空白处
+加法运算符
-减法运算符
*乘法运算符
/除法运算符
^指数化
(开括号)
)右括号
=等于运算符
<比较运算符
>比较运算符
=比较等价运算符
6简单数字
收到“q”,退出。
0)argc=1
-->argc==1
1)printf(“%s\t addition operator\n”,c);
-->printf(“%c\t addition operator\n”,c);
请注意,如果用户编写if(i>j),读取nc=getchar()
的代码会给您带来麻烦
因为nc
读取j
,然后它就完全丢失了。就我个人而言,我不喜欢这种编写代码的方式,但我承认它可以在你学习语言时帮助你,并且容易犯类似的错误。我使用编译器警告,警告你在条件下的赋值。如果我真的想要赋值,我会D编写代码>如果((ARCC=1)=0)(这要强调的是,在这种情况下,测试是没有意义的)。有些人更喜欢<代码>((ARCC=1))< /C> >;我认为这太微妙了。我编译代码的方式,它(<代码>(m=1)返回2;)不会编译——它会产生错误。(我将GCC与-Wall-Wextra-Werror
和一些额外的选项一起使用;-Wall-Werror
就足够了)。我确保编译器报告这些问题,因为我使用的编译器能够这样做。哦,我明白了,但这不是标准的,不是吗?不是(不是每个编译器都必须有如此挑剔的选项),但我用于开发的编译器被设置为异常复杂,因此开发机器上的代码中没有此类问题。当代码复制到其他机器上时,问题也不在那里的代码中。因此,这不是问题。如果您的编译器没有可用的警告,您可能会决定您需要我想用笨拙的逻辑编写代码,但它读起来最特别。我不是在比较1;我是在比较m
和某个东西(碰巧是1
)。我确信。事实上,墙会产生这样的警告。
while (1)
{
c = fgetc (src);
if (c == 'q' || c == EOF) /* just `q` for quit :) */
break;
...
else if(c == '>' || c == '<')
else if(c == '<' || c == '>' || c == '=')
{
...
}
else if ( c == '=')
{
printf("\t %c \t equals operator\n", c);
}
else if(c == '<' || c == '>')
{
if ((nc = fgetc (src)) == '=')
{
if(c == '<')
{
printf("\t %c= \t comparison equivalence operator\n", c);
}
else if(c == '>')
{
printf("\t %c= \t comparison equivalence operator\n", c);
}
}
else
{
printf("\t %c \t comparison operator\n", c);
ungetc (nc, src); /* the next character may be important */
}
}
getchar()
fgetc (src)
if (stdf) printf (" input : "); /* stdf being the stdinflag */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
FILE *
input_from_args (int argc, const char *argv[], size_t *stdinflag)
{
if (argc == 1)
{
*stdinflag = 1;
return stdin;
}
else
{
*stdinflag = 0;
return fopen(argv[1], "r");
}
}
int main (int argc, const char **argv)
{
size_t stdf = 0;
FILE *src = input_from_args (argc, argv, &stdf);
if(src == NULL)
{
fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
int c = 0;
int nc = 0;
int strip = 0;
while (1)
{
if (stdf) printf (" input : ");
c = fgetc (src);
if(c == ' ' || c == '\t')
{
printf("\t Blank Space\n");
}
else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
{
if(c == '+')
{
printf("\t %c \t addition operator\n", c);
}
else if(c == '-')
{
printf("\t %c \t subtraction operator\n", c);
}
else if(c == '*')
{
printf("\t %c \t multiplication operator\n", c);
}
if(c == '/')
{
printf("\t %c \t division operator\n", c);
}
else if(c == '^')
{
printf("\t %c \t exponentiation\n", c);
}
}
else if(c == '(' || c == ')')
{
if(c == '(')
{
printf("\t %c \t open parentheses\n", c);
}
else if(c == ')')
{
printf("\t %c \t close parentheses\n", c);
}
}
else if ( c == '=')
{
printf("\t %c \t equals operator\n", c);
}
else if(c == '<' || c == '>')
{
if ((nc = fgetc (src)) == '=')
{
if(c == '<')
{
printf("\t %c= \t comparison equivalence operator\n", c);
}
else if(c == '>')
{
printf("\t %c= \t comparison equivalence operator\n", c);
}
}
else
{
printf("\t %c \t comparison operator\n", c);
ungetc (nc, src);
}
}
else if(isdigit(c))
{
printf("\t %c \t simple numeral \n", c);
}
else if(isalpha(c))
{
if (tolower (c) == 'q')
{
printf ("\t\n 'q' received, quitting.\n\n");
break;
}
printf("\t %c \t invalid token\n", c);
}
while ((strip = fgetc (src)) != '\n' && strip != EOF);
}
if (src && !stdf) fclose (src);
return 0;
}
$ ./bin/getchar_token_calc
input :
Blank Space
input :
Blank Space
input : +
+ addition operator
input : -
- subtraction operator
input : *
* multiplication operator
input : /
/ division operator
input : ^
^ exponentiation
input : (
( open parentheses
input : )
) close parentheses
input : <
< comparison operator
input : >
> comparison operator
input : <=
<= comparison equivalence operator
input : >=
>= comparison equivalence operator
input : 6
6 simple numeral
input : a
a invalid token
input : q
'q' received, quitting.
$ ./bin/getchar_token_calc dat/tokens.txt
Blank Space
Blank Space
+ addition operator
- subtraction operator
* multiplication operator
/ division operator
^ exponentiation
( open parentheses
) close parentheses
= equals operator
< comparison operator
> comparison operator
<= comparison equivalence operator
>= comparison equivalence operator
6 simple numeral
'q' received, quitting.