0

所以我们有一个分词器的代码,它确实有点坏了。

现在我们不能修改这个函数是给定的。

不过,它在大多数情况下都给出了正确的输出。除了在某些情况下,它会在末尾添加随机的额外垃圾。

现在程序读入一个字符串并将其转换为一棵树以供执行。最底部的节点包含简单的命令。

这些简单的命令必须被解析才能执行。

现在在解析过程中,最后的额外垃圾阻碍了 execvp() 调用。

我想请教关于如何在最后消除垃圾的建议。

我想过使用正则表达式来检查字符串是否包含有效字符,并且很好奇是否有任何标准库函数可以在这方面提供帮助?

如果它有任何区别 - 垃圾字符就像一个盒子

00
19

作为它的内容。它显示在 gedit 中,但不在此处。junk print 的出处是第一次解析,修改是不可行的。

这是该程序的示例运行。

sgml@SGML3:~/smelly-fish$ ./timetrash exectest
 Word is: expr 3 + 8 + 9 + 8  (Junk - this character won't display)
 Temp is: expr 3 + 8 + 9 + 8  (Junk - this character won't display)
c->u.word[0] is expr
c->u.word[1] is 3
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is  (Junk - this character won't display)
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
 Word is:  expr 7 + 8 + 9 + 8  (Junk - this character won't display)
 Temp is:  expr 7 + 8 + 9 + 8 (Junk - this character won't display)
c->u.word[0] is expr
c->u.word[1] is 7
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is (Junk - this character won't display)
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
sgml@SGML3:~/smelly-fish$ ./timetrash exectest >execresults1.txt
expr: syntax error
expr: syntax error
sgml@SGML3:~/smelly-fish$ ./timetrash exectest
 Word is: expr 3 + 8 + 9 + 8 (Junk - this character won't display)  
 Temp is: expr 3 + 8 + 9 + 8    (Junk - this character won't display) 
c->u.word[0] is expr
c->u.word[1] is 3
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is (Junk - this character won't display)   
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
 Word is:  expr 7 + 8 + 9 + 8 
 Temp is:  expr 7 + 8 + 9 + 8 
c->u.word[0] is expr
c->u.word[1] is 7
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is 
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
sgml@SGML3:~/smelly-fish$ 
4

0 回答 0