JavaScript/ECMAScript是常规语言吗? [英] Is JavaScript/ECMAScript a Regular Language?
问题描述
我已经尝试了一些Google搜索,但无法提出解决此问题的任何文章/先前的问题.原因是我与某人之间存在一个小争议,涉及使用输入验证来拒绝可能的XSS.我知道HTML不是一种常规语言,但是我不能对JavaScript提出如此强烈的要求.
I've tried a few google searches, and I cannot come up with any articles/previous questions that address this. The reason is a minor dispute I'm having with someone about using input validation to reject possible XSS. I know for a fact that HTML isn't a regular language, but I can't make that argument quite as strongly for javascript.
我检查了此链接:
http://www.dlsi.ua.es/~ mlf/nnafmc/pbook/node46.html
我想出了这一点:由于html中的标签可以无限嵌套,因此这是为什么HTML不是常规语言的直观概念.通过扩展,由于您可以使用{}
无限嵌套JavaScript代码块,因此javascript也不是常规语言.
And I've come up with this: Since tags in html can be infinitely nested, that's an intuitive notion as to why HTML is NOT a regular language. By extension, since you can infinitely nest blocks of JavaScript code with {}
, then javascript too is NOT a regular language.
我希望看到一个更正式的演讲,以支持或反对这种非正式的主张.甚至可能是关于编程语言中的正则表达式扩展的讨论,这些扩展使也许无需编写解析器就可以执行此类操作.
I'd like to see a more formal presentation either for or against this informal proposition. Or maybe even a discussion about regex extensions in programming languages that perhaps make it possible to do this kind of thing without writing a parser.
推荐答案
实际上,JavaScript不是一种常规语言,事实可以证明,必须根据您的直觉建议平衡括号.
Indeed, JavaScript is not a regular language, which can be proved with the fact that braces must be balanced as your intuition suggested.
抽引引语是证明语言不正常的有用工具.您可以使用它来证明如果JavaScript是常规的,则类似
A useful tool for demonstrating that languages are not regular is the Pumping Lemma. You can use it to demonstrate that if JavaScript was regular, some sequence like
function(){ function() { function(){ ... function () {
(其中{
不匹配)在被某个前缀和后缀包围时可以重复任意次,这显然与必须匹配花括号的事实相矛盾.
(in which {
are not matched) could be repeated any number of times when surrounded by a certain prefix and suffix, which is in obviously in contradiction with the fact that curly braces must be matched.
这篇关于JavaScript/ECMAScript是常规语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!