嵌套循环时间分析(Nested Loops Time Analysis)

好的我正在学习算法课,正在为考试而学习......不幸的是......我无法理解嵌套循环时间分析背后的概念

这段代码中有三个循环

for (i=1->n)
 for (j=1->i)
   for (k=1->i)
     x=x+1;

我无法理解如何找到答案:s任何答案将是一个很好的帮助谢谢人们:)


Ok I am taking an Algorithm Class and am studying for the Exam... Unfortunately.. I can't understand the concept behind the nested loops time analysis

there are three loops in this code

for (i=1->n)
 for (j=1->i)
   for (k=1->i)
     x=x+1;

I can't understand how to figure out the answer :s Any Answer would be a great help Thanks Folks :)


原文:https://stackoverflow.com/questions/15176578
2021-07-02 06:07

满意答案

你写的是想要检索procedure_declaration的解析,但是在你的代码中,你只想检索一个procedure_body,所以我假设这就是你想要的。

用我自己的话说,你想要编写语法而不告诉嵌入语法嵌入了哪种语法。 问题(在你的情况下没问题,因为你幸运地有一个非常友好的语法)这是在LALR(1)中,你需要一个前瞻标记来决定采取哪个规则。 你的语法看起来像这样:

procedure_declaration:
  SUB procedure_name EOS
  procedure_body
  END SUB EOS

您可以将procedure_name和procedure_body组合在一起,因此您的规则和语义操作将如下所示:

procedure_declaration:
  SUB combined = procedure_name EOS /* nothing here */ EOS
  { { procedure_name = fst combined; procedure_body = snd combined; } }

procedure_name:
  id = IDENT {
    let lexbuf = _menhir_env._menhir_lexbuf in
    (id, Parser_pd.main Lexer_pd.token lexbuf)
  }

Parser_pd将包含此规则:

main: procedure_body END SUB { $1 }

您很可能在Parser_pd中想要END SUB,因为procedure_body可能不会自我分隔。

请注意,在解析过程名称标识符之后的第一个EOS之前调用子解析器,因为这是您的前瞻。 如果你 EOS中调用它,则为时已晚,并且解析器已经从正文中提取了一个令牌。 第二个EOS是END SUB之后的EOS。

_menhir_env显然是一个只适用于menhir的黑客。 您可能需要另一个hack来制作menhir --infer work(如果您使用它),因为不希望用户引用它,因此符号不在范围内。 那个黑客将是:

%{
  type menhir_env_hack = { _menhir_lexbuf : Lexing.lexbuf }
  let _menhir_env = { _menhir_lexbuf = Lexing.from_function
    (* Make sure this lexbuf is never actually used. *)
    (fun _ _ -> assert false) }
%}

You write that you want to retrieve the parsing of procedure_declaration, but in your code, you only want to retrieve a procedure_body, so I'm assuming that's what you want.

To put into my own words, you want to have to compose grammars without telling the embedding grammar which grammar is embedded. The problem (no problem in your case, because you luckily have a very friendly grammar) with this is that in LALR(1), you need one token of lookahead to decide which rule to take. Your grammar looks like this:

procedure_declaration:
  SUB procedure_name EOS
  procedure_body
  END SUB EOS

You can combine procedure_name and procedure_body, so your rule and semantic action will look like:

procedure_declaration:
  SUB combined = procedure_name EOS /* nothing here */ EOS
  { { procedure_name = fst combined; procedure_body = snd combined; } }

procedure_name:
  id = IDENT {
    let lexbuf = _menhir_env._menhir_lexbuf in
    (id, Parser_pd.main Lexer_pd.token lexbuf)
  }

Parser_pd will contain this rule:

main: procedure_body END SUB { $1 }

You will very likely want END SUB in Parser_pd, because procedure_body is likely not self-delimiting.

Note that you call the sub-parser before parsing the first EOS after the procedure name identifier, because that is your lookahead. If you call it in EOS, it is too late, and the parser will have pulled a token from the body, already. The second EOS is the one after END SUB.

The _menhir_env thing is obviously a hack that only works with menhir. You may need another hack to make menhir --infer work (if you use that), because that doesn't expect a user to refer to it, so the symbol won't be in scope. That hack would be:

%{
  type menhir_env_hack = { _menhir_lexbuf : Lexing.lexbuf }
  let _menhir_env = { _menhir_lexbuf = Lexing.from_function
    (* Make sure this lexbuf is never actually used. *)
    (fun _ _ -> assert false) }
%}

相关问答

更多

使用ocamllex / ocamlyacc解析语法的一部分(Using ocamllex/ocamlyacc to parse part of a grammar)

第一个广告分钟:你应该考虑使用FrançoisPottier的Menhir ,而不是ocamlyacc ,它就像一个“yacc,升级”,在所有方面都更好(更可读的语法,更强大的构造,更容易调试......),但仍然非常相似。 它当然可以与ocamllex组合使用。 您的expr1规则只允许以expr规则开始和结束。 你应该放大它以允许expr之前或之后的“stuff”。 就像是: junk: | junk LPAREN | junk RPAREN | junk INT | junk IDENT ...

解析XML文件的一部分(Parsing part of XML file)

取自我的一个源代码。 你必须潜入比我更深的一个级别,我有<pricing> <price age_group =“”> <EUR> ... </ EUR> </ pricing> </ price> { while (cur != NULL) { if (xmlStrcmp(cur->name, (const xmlChar *) "pricing") == 0) { cur = cur->xmlChildrenNode; while (cur != NULL) { ...

创建一个包含令牌的表格.mly和.mll(Make a table containing tokens visible for both .mly an .mll)

正如gsg的回答中提到的 , ocamlyacc与解析器的ml实现一起生成mli接口,并且只导出令牌类型和入口点。 根据http://caml.inria.fr/mantis/view.php?id=1703 ,这不太可能改变,所以你基本上有两个解决方案: 之后修改生成的mli (我的Makefile中通常有一条规则,只是简单地说它,但你可能只想添加必要的签名)。 使用上面提到的错误报告中建议的menhir 。 As mentioned in gsg's answer, ocamlyacc gen...

突出显示mll和mly文件(Highlight mll and mly files)

我想使用tuareg for .mll和.mly ,你可以添加这些行: (add-to-list 'auto-mode-alist '("\\.mll\\'" . tuareg-mode)) (add-to-list 'auto-mode-alist '("\\.mly\\'" . tuareg-mode)) 你正在使用tuareg和ocp-indent。 但我认为这是处理缩进的ocp-indent。 I you want to use tuareg for .mll and .mly, you...

用python解析一个url并改变其中的一部分(parsing a url in python with changing part in it)

>>> import urlparse >>> url = 'http://example.com/wps/portal/lYuxDoIwGAYf6f9aqKSjMNQ/?PartNo=74743&IntNumberOf=&is=' >>> split_url = urlparse.urlsplit(url) >>> split_url.path '/wps/portal/lYuxDoIwGAYf6f9aqKSjMNQ/' 您可以使用“/”将路径拆分为字符串列表,对列表进行切片,然后重新加入...

通过单独生成.mly和.mll来检索解析的一部分(Retrieve a part of parsing by making separate .mly and .mll)

你写的是想要检索procedure_declaration的解析,但是在你的代码中,你只想检索一个procedure_body,所以我假设这就是你想要的。 用我自己的话说,你想要编写语法而不告诉嵌入语法嵌入了哪种语法。 问题(在你的情况下没问题,因为你幸运地有一个非常友好的语法)这是在LALR(1)中,你需要一个前瞻标记来决定采取哪个规则。 你的语法看起来像这样: procedure_declaration: SUB procedure_name EOS procedure_body ...

在ocamlbuild中使用.mly文件中的电池(Using Batteries in .mly file with ocamlbuild)

据推测,这可以在最近的OCaml版本中修复(参见http://caml.inria.fr/mantis/view.php?id=5763 )。 你在运行哪个版本? This is supposedly fixed in recent OCaml versions (see http://caml.inria.fr/mantis/view.php?id=5763). Which version are you running?

美元+标识符在mly(Dollar + identifier in mly)

这些是由menhir定义的内部符号。 $startpos start position of the first symbol in the production’s right-hand side, if there is one; end position of the most recently parsed symbol, otherwise $endpos end position of the first symbol in the...

相关文章

更多

Solr参数(Analysis Collection Common CoreAdmin)

一.Analysis 1.analysis.query:Holds the query to be a ...

Solr官方文档系列——Text Analysis

Text fields are typically indexed by breaking the t ...

Real-Time Rendering 笔记 --- 1-4章

里面有些公式和矩阵无法在电脑上书写, 故用纸笔记录了一些笔记, 比如公式的推算, 注意要点等. 由于电 ...

计算机病毒及其防治 Computer Virus Analysis and Antivirus

Episode I 计算机病毒的历史 FUDAN UNIVERSITY 3 ...

zz Data Analysis Process

An interesting article....easy to understand. Summa ...

企业级搜索引擎Solr 第二章 Schema和文本分析(Schema and Text Analysis)[2]

文章转载自网易博客,原文地址:http://quweiprotoss.blog.163.com/blo ...

java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter

2013-6-24 13:28:51 org.apache.solr.common.SolrExcep ...

POJ 3620 Avoid The Lakes【DFS水题练格式Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Sub】

DFS 水题 原题链接:http://poj.org/problem?id=3620 我的链接:htt ...

科技英语翻译480句 (一)时间

1. When a wire is broken by bending it back and for ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本